package com.prolog.cs.biz.outbound.controller;

import cn.hutool.core.util.StrUtil;
import com.prolog.common.RestMsgHelper;
import com.prolog.cs.biz.outbound.model.dto.check.CheckConfirmDTO;
import com.prolog.cs.biz.outbound.model.dto.check.CheckInfoDTO;
import com.prolog.cs.biz.outbound.model.dto.check.CheckMakeRedDTO;
import com.prolog.cs.biz.outbound.model.dto.check.LclAdjustDTO;
import com.prolog.cs.biz.outbound.model.vo.CheckSplitVo;
import com.prolog.cs.biz.outbound.model.vo.check.BarCodeDVO;
import com.prolog.cs.biz.outbound.model.vo.check.InnerCheckBillV2VO;
import com.prolog.cs.biz.outbound.model.vo.check.InnerCheckConfirmCheckVO;
import com.prolog.cs.biz.outbound.model.vo.check.LclVO;
import com.prolog.cs.biz.outbound.service.InnerCheckV2Service;
import com.prolog.entity.vo.ApplyForPickSlideVO;
import com.prolog.framework.bz.common.print.PrintData;
import com.prolog.framework.common.message.RestMessage;
import com.prolog.framework.core.exception.ParameterException;
import com.prolog.framework.lock.annotation.RepeatOperationLock;
import com.prolog.framework.log.aop.annotation.UserLog;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.math.BigDecimal;
import java.util.List;
import java.util.Map;

@Api(tags = "复核任务")
@RestController
@RequestMapping("/inner/check/v2")
public class InnerCheckV2Controller {

    @Autowired
    private InnerCheckV2Service innerCheckService;

    @UserLog
    @RepeatOperationLock(timeOut = 1000)
    @ApiOperation(value = "根据容器id获取订单信息")
    @GetMapping("/container")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "containerCode", value = "容器id", required = true),
            @ApiImplicitParam(name = "stationCode", value = "复核台编码"),
            @ApiImplicitParam(name = "lcNo", value = "拼箱号"),
            @ApiImplicitParam(name = "isDoubleCheck", value = "是否双人复核"),
            @ApiImplicitParam(name = "warehouseId", value = "仓库Id")
    })
    public RestMessage<InnerCheckBillV2VO> getCheckInfoFromContainerId(
            String containerCode,
            String stationCode,
            String billGroupId,
            Integer lclNo,
            Integer isDoubleCheck,
            String warehouseId
    ) {
        return innerCheckService.getCurrentContainer(containerCode, stationCode, billGroupId, lclNo, isDoubleCheck, warehouseId);
    }

    @UserLog
    @ApiOperation(value = "复核结果冲红2")
    @PostMapping("/red")
    public RestMessage<InnerCheckBillV2VO> makeCheckRed(@RequestBody CheckMakeRedDTO redDTO) {
        return innerCheckService.makeCheckRed(redDTO);
    }

    @UserLog
    @ApiOperation(value = "扫码复核商品")
    @PostMapping("/item")
    @RepeatOperationLock(timeOut = 1000)
    public RestMessage<InnerCheckBillV2VO> checkItem(@RequestBody CheckInfoDTO checkInfoDTO) {
        return innerCheckService.checkItem(checkInfoDTO);
    }

    @UserLog
    @ApiOperation(value = "数量复核商品")
    @PostMapping("/item/number")
    @RepeatOperationLock(timeOut = 1000)
    public RestMessage<InnerCheckBillV2VO> checkItemByNum(@RequestBody CheckInfoDTO checkInfoDTO) throws Exception {
        return innerCheckService.checkItemByNum(checkInfoDTO);
    }

    @UserLog
    @ApiOperation(value = "根据barcodeId查询拼箱信息")
    @GetMapping("queryLclByBarcodeId")
    @RepeatOperationLock(timeOut = 500)
    @ApiImplicitParams({@ApiImplicitParam(name = "barcodeId", value = "拼箱Id")})
    public RestMessage<List<BarCodeDVO>> queryLclByBarcodeId(String barcodeId) {
        return RestMessage.doSuccess(innerCheckService.queryLclByBarcodeId(barcodeId));
    }

    @UserLog
    @ApiOperation(value = "拼箱调整V2")
    @PostMapping("/lcl/adjustV2")
    public RestMessage<Boolean> lclAdjust(@RequestBody List<LclAdjustDTO> lclInfos) {
        return innerCheckService.lclAdjust(lclInfos);
    }

    @UserLog
    @ApiOperation(value = "封箱打印")
    @GetMapping("/lcl/print")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "containerCode", value = "复核台编码", required = false),
            @ApiImplicitParam(name = "billGroupId", value = "订单id", required = true),
            @ApiImplicitParam(name = "lclNo", value = "拼箱号"),
            @ApiImplicitParam(name = "printFlag", value = "是否打印拼箱 0封箱 1封箱自动打印")
    })
    public RestMessage<PrintData> printLcl(String containerCode, String billGroupId, String lclNo, String printFlag) {
        if (StrUtil.isNotBlank(printFlag) && printFlag.equals("1")) {
            return RestMessage.doSuccess(innerCheckService.printLclDetail(containerCode, billGroupId, lclNo));
        }
        return RestMessage.doSuccess(innerCheckService.printLcl(containerCode, billGroupId, lclNo));
    }

    @UserLog
    @ApiOperation(value = "拼箱查询")
    @GetMapping("/lcl/query")
    @RepeatOperationLock(timeOut = 500)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "stationCode", value = "复核台编码", required = true),
            @ApiImplicitParam(name = "billGroupId", value = "订单id", required = true),
            @ApiImplicitParam(name = "containerCode", value = "容器编码"),
            @ApiImplicitParam(name = "lclNo", value = "拼箱号"),
            @ApiImplicitParam(name = "isDoubleCheck", value = "是否双人复核")

    })
    public RestMessage<LclVO> queryLcl(
            String stationCode,
            String billGroupId,
            Integer lclNo,
            String containerCode,
            Integer isDoubleCheck
    ) {
        return RestMessage.doSuccess(innerCheckService.queryLcl(stationCode, billGroupId, lclNo, containerCode, isDoubleCheck, null));
    }

    @UserLog
    @ApiOperation(value = "容器复核确认-当前周转箱内所有明细全部内复核完成")
    @PostMapping("/container/confirm/check")
    public RestMessage<InnerCheckConfirmCheckVO> containerConfirmCheck(@RequestBody CheckConfirmDTO checkConfirmDTO) {
        return innerCheckService.confirmCheck(checkConfirmDTO);
    }

    @UserLog
    @ApiOperation(value = "拣货单拆分")
    @PostMapping("/task/split")
    public RestMessage<InnerCheckBillV2VO> splitPickTaskD(@RequestBody CheckSplitVo param) {
        String pickTaskDId = param.getPickTaskDId();
        if (StrUtil.isEmpty(pickTaskDId) || null == param.getQty()) {
            throw new ParameterException("被拆分的明细行和拆分出来的数量都不能为空");
        }
        BigDecimal qty = param.getQty();
        if (qty.doubleValue() <= 0) {
            throw new ParameterException("拆分数量必须大于0！");
        }
        String itemCode = param.getItemCode();
        if (StrUtil.isEmpty(itemCode)) {
            throw new ParameterException("商品code不能为空！");
        }
        int lclNo = param.getLclNo();
        String workCode = param.getWorkCode();
        if (StrUtil.isEmpty(workCode)) {
            throw new ParameterException("工号不能为空！");
        }
        return RestMessage.success("操作成功", innerCheckService.splitPickTaskD(pickTaskDId, qty, itemCode, lclNo, workCode));
    }

    @ApiOperation(value = "内复核获取配置参数:" +
            "pickWay拣货方式：0-整散分开，拼箱保存按钮显示 1-整单拣货,拼箱保存按钮隐藏" +
            "scanSupervisionCodeStage不变")
    @ApiImplicitParam(name = "warehouseId", value = "warehouseId", required = true, dataType = "String")
    @GetMapping("innerCheckConfig")
    public RestMessage<Map> innerCheckConfig(String warehouseId) throws Exception {
        return RestMessage.newInstance(true, "查询成功", innerCheckService.innerCheckConfig(warehouseId));
    }

    @ApiOperation(value = "根据包裹号获取滑道信息(分拣滑道请求分配)")
    @PostMapping("/applyForPickSlide")
    public RestMessage<Map<String, Object>> applyForPickSlide(@RequestBody ApplyForPickSlideVO vo) {
        return RestMsgHelper.runAndGet(() -> innerCheckService.applyForPickSlide(vo));
    }
}
