package com.prolog.eis.controller.shelves;

import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.json.JSONObject;
import com.prolog.eis.common.util.PrologApiJsonHelper;
import com.prolog.eis.dto.shelves.*;
import com.prolog.eis.model.inbound.BizInboundTask;
import com.prolog.eis.service.shelves.InboundTaskService;
import com.prolog.eis.vo.GoodsInfoVO;
import com.prolog.eis.vo.InboundTaskVO;
import com.prolog.framework.common.message.RestMessage;
import com.prolog.framework.core.pojo.Page;
import com.prolog.framework.dao.util.PageUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.springframework.util.Assert;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

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

@Slf4j
@RestController
@Api(tags = "货架入库")
@RequestMapping("/api/v1/inbound")
public class InboundTaskController {

    @Resource
    InboundTaskService inboundTaskService;


    @ApiOperation(value = "验证工作站", notes = "验证工作站")
    @PostMapping("/checkStation")
    public RestMessage<InboundTaskVO> checkStation(@RequestBody StationNoDTO dto) throws Exception {
        if (StringUtils.isEmpty(dto.getStationNo())) {
            return RestMessage.newInstance(false, "202", "请输入站台号", null);
        }
        return inboundTaskService.checkStation(dto.getStationNo());
    }

    @ApiOperation(value = "入库拆零区", notes = "入库拆零区")
    @PostMapping("/callShelves")
    public RestMessage<Boolean> callShelves(@RequestBody BizInboundTask inboundTask) throws Exception {
        try {
            return RestMessage.newInstance(true, "202", null, inboundTaskService.callShelves(inboundTask));
        } catch (Exception e) {
            log.error("拆零上架呼叫货架失败{}", e.getMessage(),e);
            return RestMessage.newInstance(false, "500", e.getMessage(), false);
        }
    }

    @ApiOperation(value = "托盘扫描", notes = "托盘扫描")
    @PostMapping("/scanContainerNo")
    public RestMessage<String> scanContainerNo(@RequestBody ScanContainerNoDto dto) throws Exception {
        try {
            inboundTaskService.scanContainerNo(dto);
        }catch (IllegalArgumentException e){
            return RestMessage.newInstance(false, "201", e.getMessage(), null);
        } catch (Exception e){
            e.printStackTrace();
            return RestMessage.newInstance(false, "202", e.getMessage(), null);
        }
        return RestMessage.newInstance(true, "扫描成功！", null);
    }

    @ApiOperation(value = "根据条码获取入库任务", notes = "根据条码获取入库任务")
    @PostMapping("/getInboundTasks")
    public RestMessage<InboundTaskVO> getAllInboundTasks(@RequestBody InboundRequestDto inbound) throws Exception {
        if (StringUtils.isEmpty(inbound.getStationNo())) {
            return RestMessage.newInstance(false, "201", "请输入站台号", null);
        }
        inbound.setStationNo(inbound.getStationNo());
        return inboundTaskService.getInboundTaskList(inbound);
    }

    @ApiOperation(value = "获取单个入库任务", notes = "获取单个入库任务")
    @PostMapping("/getOneInboundTask")
    public RestMessage<InboundTaskVO> getOneInboundTask(@RequestBody List<GoodsInfoVO> param) throws Exception {
        if (param.size() == 0) {
            return RestMessage.newInstance(false, "202", "请求参数异常", null);
        }
        GoodsInfoVO inbound = param.get(0);
        if (StringUtils.isEmpty(inbound.getStationNo())) {
            return RestMessage.newInstance(false, "202", "请输入站台号", null);
        }
        inbound.setStationNo(inbound.getStationNo().toUpperCase());
        return inboundTaskService.getOneInboundTask(inbound);
    }

    @ApiOperation(value = "入库确认", notes = "入库确认")
    @PostMapping("/inboundConfirm")
    public RestMessage<String> inboundConfirm(@RequestBody GoodsInfoVO inbound) throws Exception {
        try {
            Assert.notNull(inbound,"确认信息为空！");
            return inboundTaskService.agvInboundConfirm(inbound);
        }catch (IllegalArgumentException e){
            return RestMessage.newInstance(false, "201", e.getMessage(), null);
        } catch (Exception e){
            e.printStackTrace();
            return RestMessage.newInstance(false, "202", e.getMessage(), null);
        }
    }


    @ApiOperation(value = "任务挂起", notes = "任务挂起")
    @PostMapping("/taskHangUp")
    public RestMessage<String> taskHangUp(@RequestBody JSONObject req) throws Exception {
        Long taskId = req.getLong("taskId");
        try {
            Assert.notNull(taskId,"任务号不能为空");
            inboundTaskService.taskHangUp(taskId);
        }catch (IllegalArgumentException e){
            return RestMessage.newInstance(false, "201", e.getMessage(), null);
        } catch (Exception e){
            e.printStackTrace();
            return RestMessage.newInstance(false, "202", e.getMessage(), null);
        }
        return RestMessage.newInstance(true,  "挂起成功！", null);
    }

    @ApiOperation(value = "任务挂起释放", notes = "任务挂起释放")
    @PostMapping("/taskHangUpRelease")
    public RestMessage<String> taskHangUpRelease(@RequestBody JSONObject req) throws Exception {
        Long taskId = req.getLong("taskId");
        try {
            Assert.notNull(taskId,"任务号不能为空");
            inboundTaskService.taskHangUpRelease(taskId);
        }catch (IllegalArgumentException e){
            return RestMessage.newInstance(false, "201", e.getMessage(), null);
        } catch (Exception e){
            e.printStackTrace();
            return RestMessage.newInstance(false, "202", e.getMessage(), null);
        }
        return RestMessage.newInstance(true,  "挂起成功！", null);
    }

    @ApiOperation(value = "任务挂起", notes = "任务挂起")
    @PostMapping("/queryHangUp")
    public RestMessage<Page<GoodsInfoVO>> queryHangUp(@RequestBody String json) throws Exception {
        PrologApiJsonHelper helper = PrologApiJsonHelper.createHelper(json);
        String itemCode = helper.getString("itemCode");
        String cargoOwnerName = helper.getString("cargoOwnerName");
        String itemName = helper.getString("itemName");
        String billNo = helper.getString("billNo");
        String hangUpUser = helper.getString("hangUpUser");
        String startTime = helper.getString("startTime");
        String endTime = helper.getString("endTime");
        int pageSize = helper.getInt("pageSize");
        int pageNum = helper.getInt("pageNum");
        PageUtils.startPage(pageNum,pageSize);
        List<GoodsInfoVO> goodsInfoVOS = null;
        try {
            goodsInfoVOS = inboundTaskService.queryHangUp(itemCode,cargoOwnerName,itemName,billNo,hangUpUser,startTime,endTime);
        }catch (Exception e){
            return RestMessage.newInstance(false,e.getMessage());
        }
        if (CollectionUtil.isEmpty(goodsInfoVOS)){
            return RestMessage.newInstance(true,  "查询成功！", null);
        }
        Page<GoodsInfoVO> page = PageUtils.getPage(goodsInfoVOS);
        page.setPageNum(pageNum);
        page.setPageSize(pageSize);
        return RestMessage.newInstance(true,  "查询成功！", page);
    }
    @ApiOperation(value = "货位修改查询可用货位", notes = "货位修改查询可用货位")
    @PostMapping("/findNowPalletNo")
    public RestMessage<Page<GoodsInfoVO>> findNowPalletNo(@RequestBody String json) throws Exception {
        PrologApiJsonHelper helper = PrologApiJsonHelper.createHelper(json);
        String palletNo = helper.getString("palletNo");
        String itemId = helper.getString("itemId");
        String lotId = helper.getString("lotId");
        String packId = helper.getString("packId");;
        String ownerId = helper.getString("ownerId");;
        int pageSize = helper.getInt("pageSize");
        int pageNum = helper.getInt("pageNum");
        Page<GoodsInfoVO> page = null;
        try {
            Assert.isTrue(pageSize != 0,"分页行数不能为空！");
            Assert.isTrue(pageNum != 0,"当前页不能为空！");
            page = inboundTaskService.findNowPalletNo(palletNo,itemId,lotId,packId,ownerId,pageSize,pageNum);
        }catch (IllegalArgumentException e){
            return RestMessage.newInstance(false, "201", e.getMessage(), null);
        } catch (Exception e){
            e.printStackTrace();
            return RestMessage.newInstance(false, "202", e.getMessage(), null);
        }
        return RestMessage.newInstance(true, "请求成功！", page);
    }

    @ApiOperation(value = "重新分配查询可用货位", notes = "重新分配查询可用货位")
    @PostMapping("/findNewPalletNo")
    public RestMessage<Page<GoodsInfoVO>> findNewPalletNo(@RequestBody QueryLocationDto dto) throws Exception {
        Page<GoodsInfoVO> page = null;
        try {
            Assert.notNull(dto.getItemId(),"物料id不能为空！");
            Assert.notNull(dto.getItemHealthCategory(),"商品大类不能为空！");
            Assert.notNull(dto.getStationNo(),"站台号不能为空！");
            Assert.notNull(dto.getAbcType(),"abc属性不能为空！");
            Assert.notNull(dto.getLotId(),"批次id不能为空！");
            Assert.isTrue(dto.getPageSize() != 0,"分页行数不能为空！");
            Assert.isTrue(dto.getPageNum() != 0,"当前页不能为空！");
            page = inboundTaskService.findNewPalletNo(dto);
        }catch (IllegalArgumentException e){
            return RestMessage.newInstance(false, "201", e.getMessage(), null);
        } catch (Exception e){
            e.printStackTrace();
            return RestMessage.newInstance(false, "202", e.getMessage(), null);
        }
        return RestMessage.newInstance(true, "201", "请求成功！", page);
    }

    @ApiOperation(value = "货位修改调整确认", notes = "货位修改调整确认")
    @PostMapping("/nowPalletNoConfirm")
    public RestMessage<String> nowPalletNoConfirm(@RequestBody String json) throws Exception {
        PrologApiJsonHelper helper = PrologApiJsonHelper.createHelper(json);
        String stationNo = helper.getString("stationNo");
        String palletNo = helper.getString("palletNo");
        String planes = helper.getString("planes");
        String palletLocation = helper.getString("palletLocation");
        String oldPalletLocation = helper.getString("oldPalletLocation");
        Long taskId = helper.getLong("taskId");
        try {
            Assert.notNull(stationNo,"站台号不允许为空！");
            Assert.notNull(palletNo,"货架号不允许为空！");
            Assert.notNull(planes,"货架面不允许为空！");
            Assert.notNull(palletLocation,"货位不允许为空！");
            Assert.notNull(taskId,"任务号不允许为空！");
            Assert.notNull(oldPalletLocation,"旧货位不允许为空！");
            inboundTaskService.nowPalletNoConfirm(stationNo.trim().toUpperCase(),palletNo,planes,taskId,palletLocation.trim().toUpperCase(),oldPalletLocation);
        }catch (IllegalArgumentException e){
            return RestMessage.newInstance(false, "201", e.getMessage(), null);
        }catch (Exception e){
            e.printStackTrace();
            return RestMessage.newInstance(false, "202", e.getMessage(), null);
        }
        return RestMessage.newInstance(true, "调整成功！");
    }


    @ApiOperation(value = "重新分配确认调整", notes = "重新分配确认调整")
    @PostMapping("/newPalletNoConfirm")
    public RestMessage<String> newPalletNoConfirm(@RequestBody NewPalletNoConfirmDto dto) throws Exception {
        try {
            Assert.notNull(dto.getStationNo(),"站台号不允许为空！");
            Assert.notNull(dto.getOldPalletNo(),"原货架号不允许为空！");
            Assert.notNull(dto.getNewPalletNo(),"新货架号不允许为空！");
            Assert.notNull(dto.getPlanes(),"新货架面不允许为空！");
            Assert.notNull(dto.getTaskId(),"任务号不允许为空！");
            Assert.notNull(dto.getPalletLocation(),"新货架货位号不允许为空！");
            Assert.notNull(dto.getItemHealthCategory(),"商品大类不允许为空！");
            Assert.notNull(dto.getOldLocationType(),"旧容器类型不能为空！");
            Assert.notNull(dto.getNewLocationType(),"新容器类型不能为空！");
            inboundTaskService.newPalletNoConfirm(dto);
        }catch (IllegalArgumentException e){
            return RestMessage.newInstance(false, "201", e.getMessage(), null);
        }catch (Exception e){
            e.printStackTrace();
            return RestMessage.newInstance(false, "202", e.getMessage(), null);
        }
        return RestMessage.newInstance(true, "201", "调整成功！");
    }

    @ApiOperation(value = "货架归还", notes = "货架归还")
    @PostMapping("/shelfReturn")
    public RestMessage<?> shelfReturn(@RequestBody ShelfReturnDto dto) throws Exception {
        try {
            return inboundTaskService.shelfReturn(dto);
        }catch (IllegalArgumentException e){
            return RestMessage.newInstance(false, "201", e.getMessage(), null);
        }catch (Exception e){
            e.printStackTrace();
            return RestMessage.newInstance(false, "202", e.getMessage(), null);
        }
    }

    @ApiOperation(value = "获取货架", notes = "获取货架")
    @PostMapping("/getPallet")
    public RestMessage<String> getPallet(@RequestBody ScanContainerNoDto dto) throws Exception{
        try {
            Assert.notNull(dto.getStationNo(),"站台号不允许为空！");
            Assert.notNull(dto.getContainerNo(),"容器号不允许为空！");
            return inboundTaskService.getPallet(dto);
        }catch (IllegalArgumentException e){
            return RestMessage.newInstance(false, "201", e.getMessage(), null);
        }catch (Exception e){
            e.printStackTrace();
            return RestMessage.newInstance(false, "202", e.getMessage(), null);
        }
    }

    @ApiOperation(value = "点击确认判断", notes = "点击确认判断")
    @PostMapping("/queryConfirmABC")
    public RestMessage<Integer> queryConfirmABC(@RequestBody String json) throws Exception {
        PrologApiJsonHelper helper = PrologApiJsonHelper.createHelper(json);
        String abcType = helper.getString("abcType");
        Long bizPropertyId = helper.getLong("bizPropertyId");
        String palletNo = helper.getString("palletNo");
        String palletLocation = helper.getString("palletLocation");
        Integer num = 0;
        try {
            Assert.notNull(abcType,"商品ABC属性不允许为空！");
            Assert.notNull(palletNo,"货架号不允许为空！");
            Assert.notNull(palletLocation,"货位号不允许为空！");
            num = inboundTaskService.queryConfirmABC(abcType, bizPropertyId, palletNo, palletLocation.trim().toUpperCase());
        }catch (IllegalArgumentException e){
            return RestMessage.newInstance(false, "201", e.getMessage(), null);
        }catch (Exception e){
            e.printStackTrace();
            return RestMessage.newInstance(false, "202", e.getMessage(), null);
        }
        return RestMessage.newInstance(true,  "200",num);
    }

}
