package cn.estsh.i3plus.ext.mes.pcn.apiservice.controller.busi;

import cn.estsh.i3plus.ext.mes.pcn.api.base.IMesTemplateService;
import cn.estsh.i3plus.ext.mes.pcn.api.busi.IMesProduceSnPrintService;
import cn.estsh.i3plus.ext.mes.pcn.apiservice.util.MesPcnException;
import cn.estsh.i3plus.ext.mes.pcn.pojo.constant.MesCommonConstant;
import cn.estsh.i3plus.ext.mes.pcn.pojo.model.MesProduceSnPrintModel;
import cn.estsh.i3plus.ext.mes.pcn.pojo.util.MesPcnExtConstWords;
import cn.estsh.i3plus.pojo.mes.bean.MesPrintQueue;
import cn.estsh.i3plus.pojo.mes.bean.MesProductionAssembly;
import cn.estsh.i3plus.pojo.mes.bean.MesWorkOrderCut;
import cn.estsh.i3plus.pojo.mes.util.MesExtEnumUtil;
import cn.estsh.impp.framework.boot.auth.AuthUtil;
import cn.estsh.impp.framework.boot.exception.ImppBusiException;
import cn.estsh.impp.framework.boot.exception.ImppExceptionBuilder;
import cn.estsh.impp.framework.boot.util.ResultBean;
import cn.estsh.impp.framework.boot.util.ValidatorBean;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Objects;
import java.util.StringJoiner;

/**
 * @Description : 条码补打
 * @Reference :
 * @Author : junsheng.li
 * @CreateDate 2024/6/13 15:13
 * @Modify:
 **/
@RestController
@Api(tags = "条码补打")
@RequestMapping(MesCommonConstant.MES_YANFEN + "/mes-produce-sn-print")
public class MesProduceSnPrintController {

    @Autowired
    private IMesProduceSnPrintService mesProduceSnPrintService;

    @Autowired
    private IMesTemplateService mesTemplateService;

    @GetMapping("/mes-template/find")
    @ApiOperation(value = "查询打印模板信息")
    public ResultBean findMesLabelTemplate(MesProduceSnPrintModel mesProduceSnPrintModel) {
        try {
            ValidatorBean.checkNotNull(mesProduceSnPrintModel.getPrintTemplate(), "打印模板不能为空");
            ValidatorBean.checkNotNull(mesProduceSnPrintModel.getOrganizeCode(), "工厂代码不能为空");
            return ResultBean.success("查询成功").setResultObject(mesTemplateService.getMesLabelTemplate(mesProduceSnPrintModel.getPrintTemplate(), mesProduceSnPrintModel.getOrganizeCode()));
        } catch (ImppBusiException imppException) {
            return ResultBean.fail(imppException);
        } catch (Exception e) {
            return ImppExceptionBuilder.newInstance().buildExceptionResult(e);
        }
    }

    @GetMapping("/mes-packing-define/find")
    @ApiOperation(value = "查询信息")
    public ResultBean findMesPackingDefine(MesProduceSnPrintModel mesProduceSnPrintModel) {
        try {
            ValidatorBean.checkNotNull(mesProduceSnPrintModel.getPartNo(), "ERP零件号不能为空");
            ValidatorBean.checkNotNull(mesProduceSnPrintModel.getOrganizeCode(), "工厂代码不能为空");
            return ResultBean.success("查询成功").setResultObject(mesProduceSnPrintService.findMesPackingDefine(mesProduceSnPrintModel));
        } catch (ImppBusiException imppException) {
            return ResultBean.fail(imppException);
        } catch (Exception e) {
            return ImppExceptionBuilder.newInstance().buildExceptionResult(e);
        }
    }

    @PostMapping("/mes-produce-sn/find")
    @ApiOperation(value = "查询信息")
    public ResultBean findMesProduceSn(MesProduceSnPrintModel mesProduceSnPrintModel) {
        try {
            ValidatorBean.checkNotNull(mesProduceSnPrintModel.getProductSn(), "条码信息不能为空");
            ValidatorBean.checkNotNull(mesProduceSnPrintModel.getOrganizeCode(), "工厂代码不能为空");
            return ResultBean.success("查询成功").setResultObject(mesProduceSnPrintService.findMesProduceSn(mesProduceSnPrintModel));
        } catch (ImppBusiException imppException) {
            return ResultBean.fail(imppException);
        } catch (Exception e) {
            return ImppExceptionBuilder.newInstance().buildExceptionResult(e);
        }
    }

    @PostMapping("/do-print")
    @ApiOperation(value = "打印")
    public ResultBean doPrint(MesProduceSnPrintModel mesProduceSnPrintModel) {
        try {
            ValidatorBean.checkNotNull(mesProduceSnPrintModel.getPartNo(), "ERP零件号不能为空");
            ValidatorBean.checkNotNull(mesProduceSnPrintModel.getOrganizeCode(), "工厂代码不能为空");
            if (Objects.isNull(mesProduceSnPrintModel.getPrintQty()) || mesProduceSnPrintModel.getPrintQty() <= 0) {
                MesPcnException.throwMesBusiException("打印数量不能小于等于零");
            }
            mesProduceSnPrintModel.setUserName(!StringUtils.isEmpty(mesProduceSnPrintModel.getUserName())?mesProduceSnPrintModel.getUserName():AuthUtil.getSessionUser().getUserName());
            return ResultBean.success("查询成功").setResultObject(mesProduceSnPrintService.doPrint(mesProduceSnPrintModel));
        } catch (ImppBusiException imppException) {
            return ResultBean.fail(imppException);
        } catch (Exception e) {
            return ImppExceptionBuilder.newInstance().buildExceptionResult(e);
        }
    }
    @GetMapping("/mes-production-assembly/find")
    @ApiOperation(value = "装配件查询")
    public ResultBean findMesProductionAssembly(MesProductionAssembly mesProductionAssembly) {
        try {
            ValidatorBean.checkNotNull(mesProductionAssembly.getAssemblySn(), "装配件条码条码不能为空");
            ValidatorBean.checkNotNull(mesProductionAssembly.getOrganizeCode(), "工厂代码不能为空");
            return ResultBean.success("查询成功")
                    .setMsg(String.format("装配件【%s】条码查询成功!!!", mesProductionAssembly.getAssemblySn()))
                    .setResultList(mesProduceSnPrintService.findMesProductionAssembly(mesProductionAssembly));
        } catch (ImppBusiException imppException) {
            return ResultBean.fail(imppException);
        } catch (Exception e) {
            return ImppExceptionBuilder.newInstance().buildExceptionResult(e);
        }
    }

    @PostMapping("/mes-work-order-cut/find")
    @ApiOperation(value = "裁片工单打印查询")
    public ResultBean findMesWorkOrderCut(MesWorkOrderCut workOrderCut) {
        try {
            ValidatorBean.checkNotNull(workOrderCut.getWorkCenterCodeQuery(), "产线代码不能为空");
            ValidatorBean.checkNotNull(workOrderCut.getOrganizeCode(), "工厂代码不能为空");
            ValidatorBean.checkNotNull(workOrderCut.getModifyUser(), "操作人不能为空");

            return ResultBean.success("查询成功")
                    .setMsg("裁片工单查询成功!!!")
                    .setResultList(mesProduceSnPrintService.findMesWorkOrderCut(workOrderCut));
        } catch (ImppBusiException imppException) {
            return ResultBean.fail(imppException);
        } catch (Exception e) {
            return ImppExceptionBuilder.newInstance().buildExceptionResult(e);
        }
    }

    @PostMapping("/mes-print-queue/find")
    @ApiOperation(value = "打印队列查询")
    public ResultBean findMesWorkOrderPrintQueue(MesPrintQueue printQueue) {

        try {

            ValidatorBean.checkNotNull(printQueue.getPrintQueueType(), "打印类型不能为空");
            ValidatorBean.checkNotNull(printQueue.getPrintQueueType(), "打印队列类型不能为空");
            ValidatorBean.checkNotNull(printQueue.getOrganizeCode(), "工厂代码不能为空");
            ValidatorBean.checkNotNull(printQueue.getModifyUser(), "操作人不能为空");
            if (printQueue.getPrintQueueType() == MesExtEnumUtil.PRINT_QUEUE_TYPE.PRINT_WORK_ORDER_ASSEMBLY.getValue()) {
                ValidatorBean.checkNotNull(printQueue.getWorkCenterCodeQuery(), "产线代码不能为空");
                ValidatorBean.checkNotNull(printQueue.getPartProdGroupCodeQuery(), "零件生产组代码不能为空");
            }

            String moduleKey = new StringJoiner(MesPcnExtConstWords.COLON)
                    .add(printQueue.getOrganizeCode())
                    .add(MesExtEnumUtil.PRINT_QUEUE_TYPE.valueOfType(printQueue.getPrintQueueType()).name())
                    .add(printQueue.getWorkCenterCodeQuery()).add(printQueue.getPartProdGroupCodeQuery()).toString();

            synchronized (moduleKey.intern()) {

                List<MesPrintQueue> printQueueList = mesProduceSnPrintService.findMesPrintQueueList(printQueue);

                return ResultBean.success(CollectionUtils.isEmpty(printQueueList) ? "查询暂无可打印数据!!!" : "打印队列查询成功!!!").setResultList(printQueueList);

            }

        } catch (ImppBusiException imppException) {
            return ResultBean.fail(imppException);
        } catch (Exception e) {
            return ImppExceptionBuilder.newInstance().buildExceptionResult(e);
        }
    }

    @PostMapping("/by-packageno/reprint-pack")
    @ApiOperation(value = "根据包装条码补打印包装条码标签")
    public ResultBean doRePrintPackByPackageNo(@RequestBody MesProduceSnPrintModel mesProduceSnPrintModel) {
        try {
            ValidatorBean.checkNotNull(mesProduceSnPrintModel.getPackageNo(), "包装条码不能为空");
            ValidatorBean.checkNotNull(mesProduceSnPrintModel.getOrganizeCode(), "工厂代码不能为空");
            ValidatorBean.checkNotNull(mesProduceSnPrintModel.getUserName(), "操作人不能为空");
            return ResultBean.success("查询成功").setResultObject(mesProduceSnPrintService.doRePrintPackByPackageNo(mesProduceSnPrintModel));
        } catch (ImppBusiException imppException) {
            return ResultBean.fail(imppException);
        } catch (Exception e) {
            return ImppExceptionBuilder.newInstance().buildExceptionResult(e);
        }
    }


}
