package com.prolog.eis.controller.wms;

import com.prolog.eis.core.model.biz.route.ContainerLocation;
import com.prolog.eis.dto.inbound.InBoundDto;
import com.prolog.eis.dto.inbound.WmsCarryTaskDto;
import com.prolog.eis.dto.outbound.*;
import com.prolog.eis.dto.wms.ItemContainerDto;
import com.prolog.eis.dto.wms.StockContainerReqDto;
import com.prolog.eis.dto.wms.StockContainerRespDto;
import com.prolog.eis.service.carry.WmsCarryTaskService;
import com.prolog.eis.service.inbound.BizInboundTaskService;
import com.prolog.eis.service.outbound.BizOutboundTaskService;
import com.prolog.eis.service.wms.WmsService;
import com.prolog.framework.common.message.RestMessage;
import com.prolog.framework.core.pojo.Page;
import com.prolog.framework.dao.util.PageUtils;
import com.prolog.fx.inf.core.InterfaceType;
import com.prolog.fx.inf.core.annotation.InterfaceMethod;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;

@Slf4j
@RequiredArgsConstructor
@RestController
@Validated
@Api(tags = "WMS接口服务")
@RequestMapping(value = "api/v1/wms")
public class WmsController {
    private final BizInboundTaskService inboundTaskService;
    private final BizOutboundTaskService outboundTaskService;
    private final WmsCarryTaskService wmsCarryTaskService;
    private final WmsService wmsService;

    @ApiOperation(value = "WMS入库任务下发", notes = "WMS入库任务下发")
    @PostMapping(value = "/inBoundReq")
    @InterfaceMethod(key = "WMS入库任务下发", name = "WMS->EIS入库任务下发", type = InterfaceType.Receive)
    public RestMessage wmsInBound(@Valid @RequestBody List<InBoundDto> inBoundDtos) {
        try {
            inboundTaskService.saveInboundTask(inBoundDtos);
            return RestMessage.success("请求成功", null);
        } catch (Exception e) {
            log.error("WMS入库任务下发异常:" + e.getMessage(), e);
            return RestMessage.error("请求异常" + e.getMessage());
        }
    }


    @ApiOperation(value = "WMS出库任务下发", notes = "WMS出库任务下发")
    @PostMapping(value = "/outBound")
    @InterfaceMethod(key = "WMS出库任务下发", name = "WMS->EIS出库任务下发", type = InterfaceType.Receive)
    public RestMessage wmsOutBound(@Valid @RequestBody List<OutBoundDto> outBoundDtos) {
        try {
            outboundTaskService.wmsOutBound(outBoundDtos);
            return RestMessage.success("请求成功", null);
        } catch (Exception e) {
            log.error("WMS请求出库异常:" + e.getMessage(), e);
            return RestMessage.error("请求异常" + e.getMessage());
        }
    }

    @ApiOperation(value = "wms作业完成确认", notes = "wms作业完成确认")
    @RequestMapping(method = RequestMethod.POST, value = "/task/finish")
    @InterfaceMethod(key = "wms作业完成确认", name = "WMS->EIS作业完成确认", type = InterfaceType.Receive)
    public RestMessage taskFinishConfirm(@Valid @RequestBody List<ConfirmDto> confirmDtos) {
        try {
            outboundTaskService.taskFinishConfirm(confirmDtos);
            return RestMessage.success("请求成功", null);
        } catch (Exception e) {
            log.error("wms作业完成确认异常:" + e.getMessage(), e);
            return RestMessage.error("请求异常" + e.getMessage());
        }

    }


    /**
     * @param cancelList
     * @return
     */
    @ApiOperation(value = "wms作业取消接口", notes = "wms作业取消接口")
    @RequestMapping(method = RequestMethod.POST, value = "/taskCancel")
    @InterfaceMethod(key = "wms作业取消接口", name = "WMS->EIS作业取消接口", type = InterfaceType.Receive)
    public RestMessage taskCancel(@Valid @RequestBody List<CancelDto> cancelList) {
        try {
            return outboundTaskService.taskCancel(cancelList);
        } catch (Exception e) {
            log.error("wms作业取消接口异常:{}", e);
            return RestMessage.newInstance(false, "请求异常" + e.getMessage());
        }
    }


    /**
     * @param priorityList
     * @return
     */
    @ApiOperation(value = "WMS更新优先级", notes = "WMS更新优先级")
    @RequestMapping(method = RequestMethod.POST, value = "/update/priority")
    @InterfaceMethod(key = "WMS更新优先级", name = "WMS->EIS更新优先级", type = InterfaceType.Receive)
    public RestMessage updatePriority(@Valid @RequestBody List<PriorityDto> priorityList) {
        try {
            return outboundTaskService.updatePriority(priorityList);
        } catch (Exception e) {
            log.error("WMS更新优先级异常:{}", e);
            return RestMessage.newInstance(false, "请求异常" + e.getMessage());
        }
    }

    @ApiOperation(value = "盘点审核信息推送（WMS -> EIS）", notes = "盘点审核信息推送（WMS -> EIS）")
    @RequestMapping(method = RequestMethod.POST, value = "/pdsh")
    @InterfaceMethod(key = "盘点审核信息推送（WMS -> EIS）", name = "WMS->EIS盘点审核信息推送", type = InterfaceType.Receive)
    public RestMessage inventoryAudit(@Valid @RequestBody List<InventoryAuditDto> inventoryAuditDtos) {
        try {
            return outboundTaskService.inventoryAudit(inventoryAuditDtos);
        } catch (Exception e) {
            log.error("盘点审核信息处理异常:{}", e);
            return RestMessage.newInstance(false, "请求异常" + e.getMessage());
        }
    }

    @ApiOperation(value = "库存锁定（WMS -> EIS）", notes = "库存锁定（WMS -> EIS）")
    @RequestMapping(method = RequestMethod.POST, value = "/lock")
    @InterfaceMethod(key = "库存锁定（WMS -> EIS）", name = "WMS->库存锁定", type = InterfaceType.Receive)
    public RestMessage inventoryLock(@Valid @RequestBody List<InventoryLockDto> inventoryLockDtos) {
        try {
            return outboundTaskService.inventoryLock(inventoryLockDtos);
        } catch (Exception e) {
            log.error("库存锁定处理异常:{}", e);
            return RestMessage.newInstance(false, "请求异常" + e.getMessage());
        }
    }

    @ApiOperation(value = "区域空闲托盘查询（WMS -> EIS）", notes = "区域空闲托盘查询（WMS -> EIS）")
    @RequestMapping(method = RequestMethod.POST, value = "/freeContainer")
    @InterfaceMethod(key = "区域空闲托盘查询（WMS -> EIS）", name = "WMS->区域空闲托盘查询", type = InterfaceType.Receive)
    public RestMessage<List<ContainerLocation>> inventoryLock(@Valid @RequestBody FreeContainerDto freeContainerDto) {
        try {
            List<ContainerLocation> locationList = outboundTaskService.freeContainer(freeContainerDto);
            return RestMessage.newInstance(true, "请求成功", locationList);
        } catch (Exception e) {
            log.error("库存锁定处理异常:{}", e);
            return RestMessage.newInstance(false, "请求异常" + e.getMessage());
        }
    }

    @ApiOperation(value = "托盘库存点位查询（WMS -> EIS）", notes = "托盘库存点位查询（WMS -> EIS）")
    @RequestMapping(method = RequestMethod.POST, value = "/itemContainer")
    @InterfaceMethod(key = "托盘库存点位查询（WMS -> EIS）", name = "WMS->托盘库存点位查询", type = InterfaceType.Receive)
    public RestMessage<Page<ItemContainerDto>> itemContainer(@Valid @RequestBody ItemContainerDto req) {
        try {
            PageUtils.startPage(req.getPageNum(),req.getPageSize());
            List<ItemContainerDto> list = outboundTaskService.itemContainer(req);
            return RestMessage.newInstance(true, "请求成功", PageUtils.getPage(list));
        } catch (Exception e) {
            log.error("库存锁定处理异常:{}", e);
            return RestMessage.newInstance(false, "请求异常" + e.getMessage());
        }
    }

    @ApiOperation(value = "WMS借道任务下发", notes = "WMS借道任务下发")
    @PostMapping(value = "/wmsCarryTask")
    @InterfaceMethod(key = "WMS借道任务下发", name = "WMS->EIS借道任务下发", type = InterfaceType.Receive)
    public RestMessage wmsCarryTask(@Valid @RequestBody List<WmsCarryTaskDto> req) {
        try {
            wmsCarryTaskService.saveCarryTask(req);
            return RestMessage.success("请求成功", null);
        } catch (Exception e) {
            log.error("WMS借道任务下发异常:" + e.getMessage(), e);
            return RestMessage.error("请求异常" + e.getMessage());
        }
    }

    @ApiOperation(value = "EIS容器库存查询", notes = "EIS容器库存查询")
    @PostMapping(value = "/getStockContainer")
    @InterfaceMethod(key = "容器库存查询", name = "WMS->EIS容器库存查询", type = InterfaceType.Receive)
    public RestMessage<List<StockContainerRespDto>> getStockContainer(@RequestBody StockContainerReqDto reqDto) {
        try {
            List<StockContainerRespDto> stockContainer = wmsService.getStockContainer(reqDto);
            return RestMessage.success("请求成功", stockContainer);
        } catch (Exception e) {
            log.error("WMS->EIS容器库存查询:" + e.getMessage(), e);
            return RestMessage.error("请求异常" + e.getMessage());
        }
    }

}
