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

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

/**
 * @Description :加料防错
 * @Reference :
 * @Author : junsheng.li
 * @CreateDate 2024/6/30 14:38
 * @Modify:
 **/
@RestController
@RequestMapping(MesCommonConstant.MES_YANFEN + "/mesRawPartCharging")
public class MesRawPartChargingController {

    @Autowired
    private IMesRawPartChargingService mesRawPartChargingService;

    @GetMapping("/findMesRawPackagePart")
    @ApiOperation(value = "原材料容器零件关系")
    public ResultBean findMesRawPackagePart(MesRawPartChargingModel mesRawPartChargingModel) {
        try {
            ValidatorBean.checkNotNull(mesRawPartChargingModel.getPackageSn(),"料筒条码不能为空");
            mesRawPartChargingModel.setUserName(!StringUtils.isEmpty(mesRawPartChargingModel.getUserName())?mesRawPartChargingModel.getUserName():AuthUtil.getSessionUser().getUserName());
            mesRawPartChargingModel.setOrganizeCode(!StringUtils.isEmpty(mesRawPartChargingModel.getOrganizeCode())?mesRawPartChargingModel.getOrganizeCode():AuthUtil.getOrganizeCode());
            return ResultBean.success("执行成功").setResultObject(mesRawPartChargingService.findMesRawPackagePart(mesRawPartChargingModel));
        } catch (ImppBusiException imppException) {
            return ResultBean.fail(imppException);
        } catch (Exception e) {
            return ImppExceptionBuilder.newInstance().buildExceptionResult(e);
        }
    }

    @PostMapping("/doAction")
    @ApiOperation(value = "操作")
    public ResultBean doAction(@RequestBody MesRawPartChargingModel mesRawPartChargingModel) {
        try {
            ValidatorBean.checkNotNull(mesRawPartChargingModel.getType(), "操作类型不能为空");
            mesRawPartChargingModel.setUserName(!StringUtils.isEmpty(mesRawPartChargingModel.getUserName())?mesRawPartChargingModel.getUserName():AuthUtil.getSessionUser().getUserName());
            mesRawPartChargingModel.setOrganizeCode(!StringUtils.isEmpty(mesRawPartChargingModel.getOrganizeCode())?mesRawPartChargingModel.getOrganizeCode():AuthUtil.getOrganizeCode());
            return ResultBean.success("执行成功").setResultObject(mesRawPartChargingService.doAction(mesRawPartChargingModel));
        } catch (ImppBusiException imppException) {
            return ResultBean.fail(imppException);
        } catch (Exception e) {
            return ImppExceptionBuilder.newInstance().buildExceptionResult(e);
        }
    }
}
