package com.wms_main.controller.wms;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.wms_main.dao.impl.TAppLocationServiceImpl;
import com.wms_main.dao.impl.TAppStockServiceImpl;
import com.wms_main.model.po.TAppLocation;
import com.wms_main.model.po.TAppStock;
import com.wms_main.service.controller.IDataControllerService;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 看板数据类
 */
@RestController
@ResponseBody
@CrossOrigin
@RequiredArgsConstructor
@RequestMapping("/wms/board")
public class DataController {
    private final IDataControllerService dataControllerService;
    private final TAppStockServiceImpl tAppStockService;
    private final TAppLocationServiceImpl tAppLocationService;

    @GetMapping("/api/query/queryRecentTask")
    public Map<String, Object> queryRecentTask() {
        try {
            List<Map<String, Object>> result = dataControllerService.queryRecentTask();
            Map<String, Object> response = new HashMap<>();
            response.put("isSuccess", true);
            response.put("responseCode", 0);
            response.put("responseMessage", "查询成功");
            response.put("returnData", result);
            return response;
        } catch (Exception e) {
            Map<String, Object> response = new HashMap<>();
            response.put("isSuccess", false);
            response.put("responseCode", -1);
            response.put("responseMessage", "查询失败: " + e.getMessage());
            return response;
        }
    }

    @PostMapping("/api/query/queryStockDetail")
    public Map<String, Object> queryStockDetail(@RequestBody Map<String, Object> request) {
        try {
            // 提取请求参数
            String requestDate = (String) request.get("requestDate");
            String locationId = (String) request.get("locationId");

            // 验证请求时间参数
            if (requestDate == null || requestDate.isEmpty()) {
                Map<String, Object> response = new HashMap<>();
                response.put("isSuccess", false);
                response.put("responseCode", 500);
                response.put("responseMessage", "请求时间不能为空");
                return response;
            }
            // 调用服务层查询库存详情
            //List<Map<String, Object>> stockDetails = dataControllerService.queryStockDetail(locationId);

            List<Map<String, Object>> stockDetails = new ArrayList<>();

            // 1. 构造查询条件
            LambdaQueryWrapper<TAppStock> queryWrapper = new LambdaQueryWrapper<>();
            queryWrapper.eq(TAppStock::getLocationId, locationId);

            // 2. 查询库存数据
            List<TAppStock> stockList = tAppStockService.list(queryWrapper);

            if (stockList.isEmpty()){
                Map<String, Object> response = new HashMap<>();
                response.put("isSuccess", false);
                response.put("responseCode", 500);
                response.put("responseMessage", "库存数据为空");
                return response;
            }

            // 3. 转换为 Map 格式返回
            for (TAppStock stock : stockList) {
                Map<String, Object> item = new HashMap<>();
                item.put("goodsId", stock.getGoodsId()); // 物料编号
                item.put("goodsName", stock.getGoodsDesc()); // 物料名称
                item.put("goodsNum", stock.getRealNum()); // 物料数量
                item.put("wareDate", stock.getFirstInTime()); // 库上架时间
                item.put("specialStock", stock.getSpecialStock()); // 质量（kg）
                item.put("specialStockNo", stock.getSpecialStockNo()); // 产期
                item.put("specialStockItemNo", stock.getSpecialStockItemNo()); // 到期期
                item.put("batchId", stock.getBatchNo()); // 批次号
                stockDetails.add(item);
            }

            // 构造符合要求的返回数据结构
            Map<String, Object> locationInfo = new HashMap<>();
            locationInfo.put("locationId", locationId); // 使用传入的locationId
            locationInfo.put("vesselNo", stockList.getFirst().getVehicleId()); // 这里需要根据实际业务获取vesselNo
            locationInfo.put("locationData", stockDetails);

            List<Map<String, Object>> result = new ArrayList<>();
            result.add(locationInfo);

            // 构造成功响应
            Map<String, Object> response = new HashMap<>();
            response.put("isSuccess", true);
            response.put("responseCode", 0);
            response.put("responseMessage", "查询成功");
            response.put("returnData", result);
            return response;
        } catch (Exception e) {
            // 构造失败响应
            Map<String, Object> response = new HashMap<>();
            response.put("isSuccess", false);
            response.put("responseCode", 500);
            response.put("responseMessage", "查询失败: " + e.getMessage());
            return response;
        }
    }

    @GetMapping("/api/query/queryLocationDetail")
    public Map<String, Object> queryLocationDetail() {
        try {
            // 查询所有库位数据
            List<TAppLocation> locationList = tAppLocationService.list();

            // 转换为所需格式
            List<Map<String, Object>> result = new ArrayList<>();
            for (TAppLocation location : locationList) {
                Map<String, Object> item = new HashMap<>();
                item.put("locationId", location.getLocationId()); // 库位编号
                item.put("row", location.getLRow());
                item.put("line", location.getLCol());
                item.put("layer", location.getLLayer());
                item.put("deep", 1); // 深度默认返回1
                item.put("locationStatus", location.getIsOccupy());
                // 设置库位状态（根据是否有库存判断）
                result.add(item);
            }

            Map<String, Object> response = new HashMap<>();
            response.put("isSuccess", true);
            response.put("responseCode", 0);
            response.put("responseMessage", "查询成功");
            response.put("returnData", result);
            return response;
        } catch (Exception e) {
            Map<String, Object> response = new HashMap<>();
            response.put("isSuccess", false);
            response.put("responseCode", 500);
            response.put("responseMessage", "查询失败: " + e.getMessage());
            return response;
        }
    }

    @GetMapping("/api/query/queryRunningTask")
    public Map<String, Object> queryRunningTask() {
        try {
            // 调用服务层查询运行中的任务数据
            List<Map<String, Object>> result = dataControllerService.queryRunningTask();

            Map<String, Object> response = new HashMap<>();
            response.put("isSuccess", true);
            response.put("responseCode", 0);
            response.put("responseMessage", "查询成功");
            response.put("returnData", result);
            return response;
        } catch (Exception e) {
            Map<String, Object> response = new HashMap<>();
            response.put("isSuccess", false);
            response.put("responseCode", 500);
            response.put("responseMessage", "查询失败: " + e.getMessage());
            return response;
        }
    }





    /**
     * 获取入库数据
     */
    @GetMapping("/getTaskTypInData")
    public Object getTaskTypInData(@RequestParam(defaultValue = "1") int pageNum,
                                   @RequestParam(defaultValue = "10") int pageSize,
                                   @RequestParam(defaultValue = "1") int standId) {
        try {
            return dataControllerService.getTaskTypInData(pageNum, pageSize, standId);
        } catch (Exception e) {
            return "获取入库数据失败: " + e.getMessage();
        }
    }

    /**
     * 获取出库数据
     */
    @GetMapping("/getOutsData")
    public Object getOutsData(@RequestParam(defaultValue = "1") int pageNum,
                              @RequestParam(defaultValue = "10") int pageSize) {
        try {
            return dataControllerService.getOutsData(pageNum, pageSize);
        } catch (Exception e) {
            return "获取出库数据失败: " + e.getMessage();
        }
    }

    /**
     * 获取拣选任务
     */
    @GetMapping("/getPickTaskDataP1")
    public Object getPickTaskDataP1(@RequestParam(defaultValue = "1") int pageNum,
                                   @RequestParam(defaultValue = "10") int pageSize) {
        try {
            return dataControllerService.getPickTaskDataP1(pageNum, pageSize);
        } catch (Exception e) {
            return "获取拣选任务失败: " + e.getMessage();
        }
    }

    @GetMapping("/getPickTaskDataP2")
    public Object getPickTaskDataP2(@RequestParam(defaultValue = "1") int pageNum,
                                    @RequestParam(defaultValue = "10") int pageSize) {
        try {
            return dataControllerService.getPickTaskDataP2(pageNum, pageSize);
        } catch (Exception e) {
            return "获取拣选任务失败: " + e.getMessage();
        }
    }

    @GetMapping("/getPickTaskDataP3")
    public Object getPickTaskDataP3(@RequestParam(defaultValue = "1") int pageNum,
                                    @RequestParam(defaultValue = "10") int pageSize) {
        try {
            return dataControllerService.getPickTaskDataP3(pageNum, pageSize);
        } catch (Exception e) {
            return "获取拣选任务失败: " + e.getMessage();
        }
    }
}
