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

import cn.estsh.i3plus.ext.mes.pcn.api.busi.IMesCutRawErrorProofingService;
import cn.estsh.i3plus.ext.mes.pcn.pojo.constant.MesCommonConstant;
import cn.estsh.i3plus.ext.mes.pcn.pojo.model.MesCutRawErrorProofingModel;
import cn.estsh.i3plus.ext.mes.pcn.pojo.model.MesCutUpdateRawLengthModel;
import cn.estsh.i3plus.pojo.base.common.Pager;
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.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

/**
 * @Description: 裁片原材料防错
 * @Author: gsz
 * @Date: 2024/5/25 18:16
 * @Modify:
 */
@RestController
@RequestMapping(MesCommonConstant.MES_YANFEN + "/cut-raw-error-proofing")
public class MesCutRawErrorProofingController {

    @Autowired
    private IMesCutRawErrorProofingService cutRawErrorProofingService;

    @PostMapping("/scan-sn")
    @ApiOperation(value = "扫描裁片工单/原材料条码")
    public ResultBean doScanSn(@RequestBody MesCutRawErrorProofingModel model) {

        try {
            MesCutRawErrorProofingModel result = cutRawErrorProofingService.doScanSn(model);
            return ResultBean.success(StringUtils.isEmpty(result.getShowInfo()) ? "扫描成功" : result.getShowInfo()).setResultObject(result);
        } catch (ImppBusiException imppException) {
            return ResultBean.fail(imppException);
        } catch (Exception e) {
            return ImppExceptionBuilder.newInstance().buildExceptionResult(e);
        }
    }

    @GetMapping("/query-work-order-cut-material-by-pager")
    @ApiOperation(value = "展示原材料信息")
    public ResultBean queryWorkOrderMaterialByPager(String cutWorkOrderNo, String organizeCode, Pager pager) {
        try {
            return ResultBean.success("查询成功")
                    .setListPager(cutRawErrorProofingService.queryWorkOrderMaterialByPager(cutWorkOrderNo, organizeCode, pager));
        } catch (ImppBusiException imppException) {
            return ResultBean.fail(imppException);
        } catch (Exception e) {
            return ImppExceptionBuilder.newInstance().buildExceptionResult(e);
        }
    }

    @GetMapping("/query-length-by-sn")
    @ApiOperation(value = "查询条码长度")
    public ResultBean queryLengthByScanSn(MesCutRawErrorProofingModel model) {
        try {

            ValidatorBean.checkNotNull(model.getSn(), "查询条码不能为空");
            ValidatorBean.checkNotNull(model.getOrganizeCode(), "工厂代码不能为空");

            return ResultBean.success("查询成功").setResultObject(cutRawErrorProofingService.queryLengthBySn(model));
        } catch (ImppBusiException imppException) {
            return ResultBean.fail(imppException);
        } catch (Exception e) {
            return ImppExceptionBuilder.newInstance().buildExceptionResult(e);
        }
    }

    @PostMapping("/update-raw-length")
    @ApiOperation(value = "修改原材料条码长度")
    public ResultBean doUpdateLengthByScanSn(@RequestBody MesCutUpdateRawLengthModel model) {
        try {

            cutRawErrorProofingService.doUpdateLengthByScanSn(model);

            return ResultBean.success("保存成功");
        } catch (ImppBusiException imppException) {
            return ResultBean.fail(imppException);
        } catch (Exception e) {
            return ImppExceptionBuilder.newInstance().buildExceptionResult(e);
        }
    }
}
