package com.prolog.cs.biz.inbound.controller.external;

import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.IdUtil;
import com.alibaba.fastjson.JSONObject;
import com.prolog.common.util.ToolKits;
import com.prolog.cs.biz.inbound.dto.rcv.CompleteInstockTaskDTO;
import com.prolog.cs.biz.inbound.model.EisToWmsMiddle;
import com.prolog.cs.biz.inbound.service.MqInBoundProducer;
import com.prolog.cs.biz.inbound.service.rcv.RcvInstockLinesService;
import com.prolog.cs.biz.inbound.vo.EisWmsRequestParamVO;
import com.prolog.cs.biz.inbound.vo.task.InstockTaskQueryVO;
import com.prolog.entity.EisLog;
import com.prolog.entity.vo.ContainerTailgatingRequestVO;
import com.prolog.entity.vo.InBoundRequestVO;
import com.prolog.framework.common.message.RestMessage;
import com.prolog.framework.utils.AssertUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;

@Api(tags = "提供外部接口调用API")
@RestController
@RequestMapping("/wms/v1.0")
public class ExternalController {

    @Resource
    private RcvInstockLinesService rcvInstockLinesService;

    @Resource
    private MqInBoundProducer logProducer;

    /**
     * 索取上架任务
     */
    @ApiOperation("索取上架任务数据")
    @GetMapping("/pickInstockTask")
    public RestMessage<Object> pickInstockTask(InstockTaskQueryVO queryVO) {
        AssertUtils.notNull(queryVO, "查询条件为空!");
        return RestMessage.newInstance(true, "pickInstockTask", rcvInstockLinesService.pickInstockTask(queryVO));
    }

    /**
     * 回调任务结果
     */
    @ApiOperation("任务确认上架")
    @PostMapping("/completeInstockTask")
    public RestMessage<Object> completeInstockTask(
            @RequestBody(required = false) List<CompleteInstockTaskDTO> completeInstockTaskList) {
        AssertUtils.isNotEmpty(completeInstockTaskList, "确认的数据不可为空!");
        return RestMessage.newInstance(true, "completeInstockTask",
                rcvInstockLinesService.completeInstockTask(completeInstockTaskList));
    }

    /**
     * 模拟通过接口平台下传数据，实际入口为代码主动推EIS
     */
    @ApiOperation("agv模拟通过接口平台下传数据")
    @PostMapping("/tansfor2AGV")
    public RestMessage<Object> tansfor2AGV(InstockTaskQueryVO queryVO) {
        return RestMessage.newInstance(true, "tansfor2AGV", rcvInstockLinesService.tansfor2AGV(queryVO));
    }

    /**
     * 模拟轮询索取agv已完成上架任务
     */
    /**
     * 回调任务结果， 调用方式： 1、EIS调用，需要传入参 2、接口平台定时任务，不需要传入参
     */
    @ApiOperation("agv任务确认上架")
    @PostMapping("/receiveAgv")
    public RestMessage<Object> receiveAgv(@RequestBody Map<String, Object> map) {
        return RestMessage.newInstance(true, "receiveAgv", rcvInstockLinesService.receiveAgv(map));
    }

    /**
     * eis立库入库任务到位后 上架确认回告（EIS->WMS）
     *
     * @return
     */
    @ApiOperation(" eis立库入库任务到位后  上架确认回告（EIS->WMS）")
    @PostMapping("/acceptEisToWmsRequestMessage")
    public RestMessage<List<EisToWmsMiddle>> acceptEisToWmsRequestMessage(@RequestBody EisWmsRequestParamVO vo) {
        if (ToolKits.isEmpty(vo)) {
            return RestMessage.newInstance(false, "无请求数据", null);
        }
        EisLog log = new EisLog();
        log.setId(IdUtil.simpleUUID());
        log.setUrl("/api/wms/shelfConfirmation");
        log.setRequest(JSONObject.toJSONString(vo));
        log.setInitiator("EIS");
        log.setGmtCreate(DateUtil.date());
        log.setStatus(1);
        List<EisToWmsMiddle> tasks;
        try {
            tasks = rcvInstockLinesService.acceptEisToWmsRequestMessage(vo);
        } catch (Exception e) {
            log.setResponse(e.getMessage());
            log.setStatus(2);
            log.setGmtModified(DateUtil.date());
            logProducer.saveEisLog(log);
            return RestMessage.newInstance(false, e.getMessage(), null);
        }
        log.setResponse("WMS接收上架确认任务成功！");
        log.setGmtModified(DateUtil.date());
        logProducer.saveEisLog(log);
        return RestMessage.newInstance(true, "WMS接收上架确认任务成功！", tasks);
    }

    /**
     * 定时任务自动执行EIS回告数据的上架动作
     *
     * @return
     */
    @ApiOperation(" 定时任务自动执行EIS回告数据的上架动作")
    @PostMapping("/autoEisWmsMessageToUpTask")
    public RestMessage<String> autoEisWmsMessageToUpTask() {
        try {
            rcvInstockLinesService.autoEisWmsMessageToUpTask();
        } catch (Exception e) {
            return RestMessage.newInstance(false, e.getMessage(), null);
        }
        return RestMessage.newInstance(true, "执行成功", null);
    }

    /**
     * 入库任务推送（WMS->EIS）
     * 如果库别是四向库（密集库）、AGV托盘库（AGV整件库）和AGV货架库（AGV拆零库）的任务，推送入库任务给EIS
     */
    @ApiOperation("入库任务推送（WMS->EIS）")
    @PostMapping("/receiveInStockWms")
    public RestMessage<List<InBoundRequestVO>> receiveInStockWms() {
        List<InBoundRequestVO> resultDtos = null;
        try {
            resultDtos = rcvInstockLinesService.receiveInStockWms(null);
        } catch (Exception e) {
            return RestMessage.newInstance(false, e.getMessage(), null);
        }
        return RestMessage.newInstance(true, "请求入库任务成功,共" + resultDtos.size() + "条入库任务！", resultDtos);
    }

    /**
     * 容器借道下发(WMS->EIS)
     * 如果是一楼整件地推库、中药饮片、物料库和不合格库的根据托盘生成搬运任务（容器借道下发）推送给EIS
     */
    @ApiOperation("容器借道下发(WMS->EIS)")
    @PostMapping("/receiveInContainerWms")
    public RestMessage<List<ContainerTailgatingRequestVO>> containerTailgating() {
        List<ContainerTailgatingRequestVO> resultDtos = null;
        try {
            resultDtos = rcvInstockLinesService.containerTailgating();
        } catch (Exception e) {
            return RestMessage.newInstance(false, e.getMessage(), null);
        }
        return RestMessage.newInstance(true, "容器借道下发成功,共" + resultDtos.size() + "条任务！", resultDtos);
    }

    /**
     * 容器借道任务反馈（EIS->WMS)
     *
     * @return
     */
    @ApiOperation(" 容器借道下发后  eis进行反馈")
    @PostMapping("/acceptEisContainerMessage")
    public RestMessage<String> acceptEisContainerMessage(@RequestBody EisWmsRequestParamVO vo) {
        if (ToolKits.isEmpty(vo)) {
            return RestMessage.newInstance(false, "无请求数据", null);
        }
        try {
            rcvInstockLinesService.acceptEisContainerMessage(vo);
        } catch (Exception e) {
            return RestMessage.newInstance(false, e.getMessage(), null);
        }
        return RestMessage.newInstance(true, "WMS接收容器借道任务反馈任务成功！", null);
    }

    /**
     * 测试token
     *
     * @return
     */
    @ApiOperation(" 测试token")
    @PostMapping("/checkTokenExp")
    public RestMessage<Map<String, String>> checkTokenExp() {

        return rcvInstockLinesService.checkTokenExp();

    }


}
