package com.ruoyi.web.controller.app;

import java.util.ArrayList;
import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.ruoyi.app.domain.AppLedConfig;
import com.ruoyi.app.domain.AppLocation;
import com.ruoyi.app.service.IAppLocationService;
import com.ruoyi.common.annotation.Anonymous;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.web.controller.section.EnhanceDataList;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 【请填写功能名称】Controller
 *
 * @author ruoyi
 * @date 2025-01-15
 */
@Api("库位管理")
@RestController
@RequestMapping("/app/location")
public class AppLocationController extends BaseController {
    @Autowired
    private IAppLocationService appLocationService;

    /**
     * 查询【请填写功能名称】列表
     */
    @PreAuthorize("@ss.hasPermi('system:location:list')")
    @GetMapping("/list")
    @EnhanceDataList(entityType = AppLocation.class)
    public TableDataInfo list(AppLocation appLocation) {
        startPage();
        List<AppLocation> list = appLocationService.selectAppLocationList(appLocation);
        return getDataTable(list);
    }

    /**
     * 导出【请填写功能名称】列表
     */
    @PreAuthorize("@ss.hasPermi('system:location:export')")
    @Log(title = "【请填写功能名称】", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, AppLocation appLocation) {
        List<AppLocation> list = appLocationService.selectAppLocationList(appLocation);
        ExcelUtil<AppLocation> util = new ExcelUtil<AppLocation>(AppLocation.class);
        util.exportExcel(response, list, "【请填写功能名称】数据");
    }

    /**
     * 获取【请填写功能名称】详细信息
     */
    @PreAuthorize("@ss.hasPermi('system:location:query')")
    @GetMapping(value = "/{locationId}")
    public AjaxResult getInfo(@PathVariable("locationId") String locationId) {
        return success(appLocationService.selectAppLocationByLocationId(locationId));
    }

    /**
     * 新增【请填写功能名称】
     */
    @PreAuthorize("@ss.hasPermi('system:location:add')")
    @Log(title = "【请填写功能名称】", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody AppLocation appLocation) {
        return toAjax(appLocationService.insertAppLocation(appLocation));
    }

    /**
     * 修改【请填写功能名称】
     */
    @PreAuthorize("@ss.hasPermi('system:location:edit')")
    @Log(title = "【请填写功能名称】", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody AppLocation appLocation) {
        return toAjax(appLocationService.updateAppLocation(appLocation));
    }

    /**
     * 删除【请填写功能名称】
     */
    @PreAuthorize("@ss.hasPermi('system:location:remove')")
    @Log(title = "【请填写功能名称】", businessType = BusinessType.DELETE)
    @DeleteMapping("/{locationIds}")
    public AjaxResult remove(@PathVariable String[] locationIds) {
        return toAjax(appLocationService.deleteAppLocationByLocationIds(locationIds));
    }

    /**
     * 获取【请填写功能名称】详细信息
     */
    @ApiOperation("生成库位")
//    @PreAuthorize("@ss.hasPermi('system:location:query')")
    @GetMapping(value = "/genLocations/{areaId}")
    @Anonymous
    public AjaxResult genLocations(@PathVariable("areaId") Integer areaId) {
        if (areaId == null) {
            return error("库区必填。");
        }
        if (areaId == 1) {// 料箱库
            List<AppLocation> newLocationListA = new ArrayList<>();
            // 2排90列14层2深，1排的79列到90列没有2深度，排前缀A
            for (int row = 1; row <= 2; row++) {
                // 排str
                String rowStr = "A" + row;
                for (int col = 1; col <= 90; col++) {
                    // 列str
                    String colStr = StringUtils.leftPad(String.valueOf(col), 2, "0");
                    for (int layer = 1; layer <= 14; layer++) {
                        // 层str
                        String layerStr = StringUtils.leftPad(String.valueOf(layer), 2, "0");
                        for (int depth = 1; depth <= 2; depth++) {
                            if (row == 1 && col >= 79) {
                                continue;
                            }
                            // 深度str
                            String depthStr = StringUtils.leftPad(String.valueOf(depth), 2, "0");
                            // 创建库位
                            AppLocation appLocation = new AppLocation();
                            appLocation.setLocationId(rowStr + "-" + colStr + "-" + layerStr + "-" + depthStr);
                            appLocation.setAreaId(areaId);
                            appLocation.setTunnelId(1);
                            appLocation.setEquipmentId(1);
                            appLocation.setwRow(row);
                            appLocation.setwCol(col);
                            appLocation.setwLayer(layer);
                            appLocation.setwDepth(depth);
                            appLocation.setIsLock(0);
                            appLocation.setLocationStatus(0);
                            appLocation.setLocationType(1);
//                            appLocation.setOuterId("");
//                            appLocation.setVehicleId("");
                            appLocation.setIsWorking(0);
                            newLocationListA.add(appLocation);
                        }
                    }
                }
            }
            appLocationService.insertAppLocationBatch(newLocationListA);
        }
        if (areaId == 2) {// 托盘库
            List<AppLocation> newLocationListB = new ArrayList<>();
            // 2排28列7层2深，1列1、2层没有，28列1、2层没有，排前缀B
            for (int row = 1; row <= 2; row++) {
                // 排str
                String rowStr = "B" + row;
                for (int col = 1; col <= 28; col++) {
                    // 列str
                    String colStr = StringUtils.leftPad(String.valueOf(col), 2, "0");
                    for (int layer = 1; layer <= 7; layer++) {
                        if (col == 1 || col == 28) {
                            if (layer == 1 || layer == 2) {
                                continue;
                            }
                        }
                        // 层str
                        String layerStr = StringUtils.leftPad(String.valueOf(layer), 2, "0");
                        for (int depth = 1; depth <= 2; depth++) {
                            // 深度str
                            String depthStr = StringUtils.leftPad(String.valueOf(depth), 2, "0");
                            // 创建库位
                            AppLocation appLocation = new AppLocation();
                            appLocation.setLocationId(rowStr + "-" + colStr + "-" + layerStr + "-" + depthStr);
                            appLocation.setAreaId(areaId);
                            appLocation.setTunnelId(1);
                            appLocation.setEquipmentId(1);
                            appLocation.setwRow(row);
                            appLocation.setwCol(col);
                            appLocation.setwLayer(layer);
                            appLocation.setwDepth(depth);
                            appLocation.setIsLock(0);
                            appLocation.setLocationStatus(0);
                            appLocation.setLocationType(1);
                            appLocation.setIsWorking(0);
                            newLocationListB.add(appLocation);
                        }
                    }
                }
            }
            appLocationService.insertAppLocationBatch(newLocationListB);
        }
        return success("创建成功。");
    }

    @Anonymous
    @GetMapping("/count")
    public AjaxResult count() {
        return success(appLocationService.countAvailable());
    }

    /**
     * 禁用启用状态
     */
    @PreAuthorize("@ss.hasPermi('system:location:edit')")
    @Log(title = "禁用启用状态", businessType = BusinessType.UPDATE)
    @PostMapping("/changeIsEnableStatus/{locationIds}")
    public AjaxResult changeIsEnableStatus(@PathVariable String[] locationIds) {
        appLocationService.changeIsEnableStatus(locationIds);
        return success("修改状态成功");
    }
}
