package com.wms_main.controller.wms;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.wms_main.constant.enums.wms.WmsPickTaskStatusEnum;
import com.wms_main.dao.ITAppPickTaskService;
import com.wms_main.dao.ITAppStockCompareService;
import com.wms_main.dao.ITempEwmInboundDataService;
import com.wms_main.model.bo.wcs.WcsStackerTask;
import com.wms_main.model.dto.request.ewm.*;
import com.wms_main.model.dto.request.wcs.WcsBoxArriveRequest;
import com.wms_main.model.dto.request.wcs.WcsTaskResultRequest;
import com.wms_main.model.dto.request.wcs.WcsVehicleInRequest;
import com.wms_main.model.dto.request.wms.*;
import com.wms_main.model.dto.response.ewm.EwmApiBackResponse;
import com.wms_main.model.dto.response.ewm.EwmApiLocalResponse;
import com.wms_main.model.dto.response.ewm.EwmApiStockResponse;
import com.wms_main.model.dto.response.wcs.BaseWcsApiResponse;
import com.wms_main.model.dto.response.wcs.WcsApiResponse;
import com.wms_main.model.dto.response.wms.BaseWmsApiResponse;
import com.wms_main.model.dto.response.wms.WmsApiResponse;
import com.wms_main.model.po.TAppPickTask;
import com.wms_main.model.po.TAppStockCompare;
import com.wms_main.model.vo.wms.InventoryConfirmVo;
import com.wms_main.model.vo.wms.TaskConfirmVo;
import com.wms_main.service.api.IEwmApiService;
import com.wms_main.service.controller.ITaskControllerService;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 任务控制类
 */
@RestController
@ResponseBody
@CrossOrigin
@RequiredArgsConstructor
@RequestMapping("/wms/task")
public class TaskController {
    private final ITaskControllerService taskControllerService;
    private final IEwmApiService ewmApiService;
    private final ITAppPickTaskService appPickTaskService;// 拣选任务服务


    /**
     * 请求入库
     * @param stockInRequest 请求信息
     * @return 处理结果
     */
    //@PostMapping("/requireStockIn")
    public BaseWmsApiResponse requireStockIn(@RequestBody StockInRequest stockInRequest) {
        return taskControllerService.requireStockIn(stockInRequest);
    }

    /**
     * 请求出库
     * @param stockOutRequest 请求信息
     * @return 处理结果
     */
    //@PostMapping("/requireStockOut")
    public BaseWmsApiResponse requireStockOut(@RequestBody StockOutRequest stockOutRequest) {
        return taskControllerService.requireStockOut(stockOutRequest);
    }

    /**
     * 拉取EWM系统的库存信息进行比对
     */
    @PostMapping("/queryStocksCompare")
    public BaseWmsApiResponse getEwmStock(SendEwmGetStockInformationRequest request) {
        return taskControllerService.getEwmStock(request);
    }

    /**
     * EWM请求取消入库任务
     */
    @PostMapping("/cancelOrderInTasks")
    public EwmApiBackResponse cancelOrderInTasks(@RequestBody SendWarehouseInCompletedRequest request) {
        return taskControllerService.cancelOrderInTasks(request);
    }


    /**
     * 查询比对数据库的数据
     */
    @PostMapping("/getCompareData")
    public WmsApiResponse<List<TAppStockCompare>> getCompareData() {
        return taskControllerService.getCompareData();
    }

    /**
     * 接收EWM系统下发的出库任务
     * @param EwmOutTaskRequest 请求请求
     * @return 处理结果
     */
    @PostMapping("/ewmRequestOutTask")
    public EwmApiBackResponse ewmRequestOutTask(@RequestBody EwmOutTaskRequest EwmOutTaskRequest) {
        return taskControllerService.ewmRequestOutTask(EwmOutTaskRequest);
    }

    /**
     * 入库用出库
     * @param voiRequest 请求信息
     * @return 处理结果
     */
//    @PostMapping("/requireVehiclesForIn")
//    public BaseWmsApiResponse requireVehiclesForIn(@RequestBody VehicleOutForInRequest voiRequest) {
//        return taskControllerService.requireVehiclesForIn(voiRequest);
//    }

    /**
     * 接收wcs反馈任务状态
     * @param wcsTaskResultRequest 请求信息
     * @return 处理结果
     */
    @PostMapping("/sendTaskResult")
    public BaseWcsApiResponse sendTaskResult(@RequestBody WcsTaskResultRequest wcsTaskResultRequest) {
        return taskControllerService.sendTaskResult(wcsTaskResultRequest);
    }


    /**
     * test
     */
    @PostMapping("/status")
    public EwmApiBackResponse test(@RequestBody SendWarehouseOutCompletedRequest request) {
       //return taskControllerService.ewmInformationBack(wcsVehicleInRequest);
       // return ewmApiService.getEwmStock(request);
        return ewmApiService.sendWarehouseOutCompleted(request);
        //return ewmApiService.sendWarehouseInCompleted(new SendWarehouseInCompletedRequest());
    }

    /**
     * wcs请求载具入库
     * @param wcsVehicleInRequest 载具请求
     * @return 处理结果
     */
    @PostMapping("/wcsRequestVehicleIn")
    public WcsApiResponse<WcsStackerTask> wcsRequestVehicleIn(@RequestBody WcsVehicleInRequest wcsVehicleInRequest) {
        return taskControllerService.wcsRequestVehicleIn(wcsVehicleInRequest);
    }

    /**
     * Wcs上报载具到达站台
     * @param boxArriveRequest 请求信息
     * @return 处理结果
     */
    @PostMapping("/boxArrive")
    public BaseWcsApiResponse boxArrive(@RequestBody WcsBoxArriveRequest boxArriveRequest) {
        return taskControllerService.boxArrive(boxArriveRequest);
    }

    /**
     * 取消拣选任务
     * @param pickTaskRequest 请求信息
     * @return 处理结果
     */
    @PostMapping("/cancelPickTasks")
    public BaseWmsApiResponse cancelPickTasks(@RequestBody PickTaskRequest pickTaskRequest) {
        return taskControllerService.cancelPickTasks(pickTaskRequest);
    }

    /**
     * PDA获取数据
     */
    @PostMapping("/getPdaData")
    public WmsApiResponse<List<TaskConfirmVo>> getPdaData(@RequestBody SendEwmVehicleForInRequest request) {
        TAppPickTask thisPickTask = appPickTaskService.getOne(new LambdaQueryWrapper<TAppPickTask>()
                .eq(TAppPickTask::getVehicleId, request.getContainerNo())
                .eq(TAppPickTask::getPickStatus, WmsPickTaskStatusEnum.ARRIVE.getCode())
        );
        if (thisPickTask == null){
            return WmsApiResponse.error("该容器无对应的拣选任务，请检查一体机页面", null);
        }
        // 创建 BaseWmsRequest 并设置站台ID
        BaseWmsRequest wmsRequest = new BaseWmsRequest();
        wmsRequest.setStandId(thisPickTask.getPickStand());
        return getCurrentTask(wmsRequest);
    }

    /**
     * 查询站台拣货任务---拣配工作之外
     * @param wmsRequest 请求信息
     * @return 处理结果
     */
    @PostMapping("/getCurrentTask")
    public WmsApiResponse<List<TaskConfirmVo>> getCurrentTask(@RequestBody BaseWmsRequest wmsRequest) {
        return taskControllerService.getCurrentTask(wmsRequest);
    }

    /**
     * 确认当前站台拣货任务---拣配工作之外
     * @param confirmTaskRequest 请求信息
     * @return 处理结果
     */
    @PostMapping("/confirmCurrentTask")
    public BaseWmsApiResponse confirmCurrentTask(@RequestBody ConfirmTaskRequest confirmTaskRequest) {
        return taskControllerService.confirmCurrentTask(confirmTaskRequest);
    }

    /**
     * PDA确认当前站台拣货任务---拣配工作之外
     * @param confirmTaskRequest 请求信息
     * @return 处理结果
     */
    @PostMapping("/confirmCurrentTaskByPDA")
    public BaseWmsApiResponse confirmCurrentTaskByPDA(@RequestBody ConfirmTaskRequest confirmTaskRequest) {
        return taskControllerService.confirmCurrentTask(confirmTaskRequest);
    }



    /**
     * 下发盘点请求
     * @param inventoryRequest 盘点请求
     * @return 请求结果
     */
    @PostMapping("/requestInventory")
    public BaseWmsApiResponse requestInventory(@RequestBody InventoryRequest inventoryRequest) {
        return taskControllerService.requestInventory(inventoryRequest);
    }

    /**
     * 查询盘点任务确认信息
     * @param inventoryConfirmRequest 请求
     * @return 请求结果
     */
    @PostMapping("/getInventoryConfirm")
    public WmsApiResponse<InventoryConfirmVo> getInventoryConfirm(@RequestBody InventoryConfirmRequest inventoryConfirmRequest) {
        return taskControllerService.getInventoryConfirm(inventoryConfirmRequest);
    }

    /**
     * 确认盘点信息
     * @param inventoryConfirmRequest 确认请求
     * @return 处理结果
     */
    @PostMapping("/confirmInventory")
    public BaseWmsApiResponse confirmInventory(@RequestBody InventoryConfirmRequest inventoryConfirmRequest) {
        return taskControllerService.confirmInventory(inventoryConfirmRequest);
    }

    /**
     * 获取缺料数量，当存在库存时，返回库存与需求的较小值
     * @param goodsId 料号
     * @param workOrder 工单号
     * @return 缺料数量
     */
    @GetMapping("/getGoodsLackQty")
    public int getLackQty(@RequestParam("goodsId") String goodsId, @RequestParam("workOrder") String workOrder) {
        return taskControllerService.getLackQty(goodsId, workOrder);
    }
}
