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


import cn.estsh.i3plus.ext.mes.pcn.api.busi.IMesInputDefectRecordService;
import cn.estsh.i3plus.ext.mes.pcn.pojo.constant.MesCommonConstant;
import cn.estsh.i3plus.ext.mes.pcn.pojo.model.MesPartInspectionInputModel;
import cn.estsh.i3plus.ext.mes.pcn.pojo.model.MesPartInspectionViewModel;
import cn.estsh.i3plus.platform.common.tool.MathOperation;
import cn.estsh.i3plus.pojo.mes.bean.MesProduceSn;
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 lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.util.Objects;

@RestController
@Api(tags = "缺陷记录页面")
@RequestMapping(MesCommonConstant.MES_YANFEN + "/defect_record")
@Slf4j
public class MesInputDefectRecordController {

    @Autowired
    private IMesInputDefectRecordService inputDefectRecordService;

    @PostMapping("/query-part-inspection")
    @ApiOperation(value = "查询页面信息")
    public ResultBean queryPartInspection(@RequestBody MesPartInspectionInputModel model) {

        try {
            ValidatorBean.checkNotNull(model.getOrganizeCode(), "工厂不能为空");
            MesPartInspectionViewModel result = inputDefectRecordService.queryPartInspection(model);
            String msg = "查询成功";
            if (!StringUtils.isEmpty(model.getFrontBack()) && model.getFrontBack().compareTo(10) == 0 && (Objects.isNull(result.getPartTypePicture()) || StringUtils.isEmpty(result.getPartTypePicture().getFrontPictureName()))) msg = "A面图片未维护";
            if (!StringUtils.isEmpty(model.getFrontBack()) && model.getFrontBack().compareTo(20) == 0 && (Objects.isNull(result.getPartTypePicture()) || StringUtils.isEmpty(result.getPartTypePicture().getBackPictureName()))) msg = "B面图片未维护";
            return ResultBean.success(msg).setResultObject(result);
        } catch (ImppBusiException imppException) {
            return ResultBean.fail(imppException);
        } catch (Exception e) {
            return ImppExceptionBuilder.newInstance().buildExceptionResult(e);
        }
    }

    @GetMapping("/query-part-type-defect")
    @ApiOperation(value = "查询零件类型缺陷类型对应关系")
    public ResultBean queryPartTypeDefect(String partNo, String defectLocation, boolean flg, Integer frontBack, String organizeCode) {

        try {
            ValidatorBean.checkNotNull(partNo, "物料号不能为空");
            ValidatorBean.checkNotNull(defectLocation, "位置不能为空");
            ValidatorBean.checkNotNull(frontBack, "正反面不能为空");
            ValidatorBean.checkNotNull(organizeCode, "工厂不能为空");
            return ResultBean.success("查询成功").setResultList(inputDefectRecordService.queryPartTypeDefect(partNo, defectLocation, flg, frontBack, organizeCode));
        } catch (ImppBusiException imppException) {
            return ResultBean.fail(imppException);
        } catch (Exception e) {
            return ImppExceptionBuilder.newInstance().buildExceptionResult(e);
        }
    }

    @PostMapping("/save-defect-record")
    @ApiOperation(value = "完成质检")
    public ResultBean savePartInspection(@RequestBody MesPartInspectionInputModel model) {

        try {
            ValidatorBean.checkNotNull(model.getOrganizeCode(), "工厂不能为空");
            ValidatorBean.checkNotNull(model.getUserInfo(), "操作人不能为空");
            if (StringUtils.isEmpty(model.getSn()) &&
                    (StringUtils.isEmpty(model.getPartNo()) || StringUtils.isEmpty(model.getLotNo()) || StringUtils.isEmpty(model.getQty()) || MathOperation.compareTo(model.getQty(), new Double(0)) == 0)) {
                throw new ImppBusiException("条码为空或零件号+批次+数量为空");
            }

            inputDefectRecordService.savePartInspection(model);
            return ResultBean.success("质检已完成，请继续");
        } catch (ImppBusiException imppException) {
            return ResultBean.fail(imppException);
        } catch (Exception e) {
            return ImppExceptionBuilder.newInstance().buildExceptionResult(e);
        }
    }

    @PostMapping("/customerBack")
    @ApiOperation(value = "客退品")
    public ResultBean customerBack(@RequestBody MesPartInspectionInputModel model) {

        try {
            MesProduceSn mesProduceSn = inputDefectRecordService.customerBack(model.getOrganizeCode(), model.getSn(), model.getUserInfo());
            return ResultBean.success("客退品已完成，请继续").setResultObject(mesProduceSn);
        } catch (ImppBusiException imppException) {
            return ResultBean.fail(imppException);
        } catch (Exception e) {
            return ImppExceptionBuilder.newInstance().buildExceptionResult(e);
        }
    }

    @PostMapping("/customerBackCommit")
    @ApiOperation(value = "客退品提交")
    public ResultBean customerBackCommit(@RequestBody MesPartInspectionInputModel model) {

        try {
            inputDefectRecordService.customerBackCommit(model.getOrganizeCode(), model.getSn(), model.getUserInfo());
            return ResultBean.success("客退品提交已完成，请继续");
        } catch (ImppBusiException imppException) {
            return ResultBean.fail(imppException);
        } catch (Exception e) {
            return ImppExceptionBuilder.newInstance().buildExceptionResult(e);
        }
    }
}
