package cn.estsh.i3plus.ext.mes.apiservice.controller.base.ep;

import cn.estsh.i3plus.ext.mes.api.base.ep.IEpPartDivulgeCheckCfgService;
import cn.estsh.i3plus.ext.mes.pojo.bean.MesPartDivulgeCheckCfg;
import cn.estsh.i3plus.ext.mes.pojo.util.MesExtConstWords;
import cn.estsh.i3plus.platform.common.util.CommonConstWords;
import cn.estsh.i3plus.pojo.base.common.Pager;
import cn.estsh.i3plus.pojo.base.enumutil.ResourceEnumUtil;
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.web.bind.annotation.*;

/**
 * @PROJECT_NAME: i3plus-mes-panasonic-dev
 * @DESCRIPTION:
 * @USER: xinwang.yi
 * @DATE: 2021-12-20 14:11
 */
@RestController
@Api(tags = "24小时泄露检测物料")
@RequestMapping(CommonConstWords.BASE_URL_MES + MesExtConstWords.EP_EXT+"/part-divulge-check-cfg")
public class EpPartDivulgeCheckCfgController {

    @Autowired
    private IEpPartDivulgeCheckCfgService partDivulgeCheckCfgService;

    @PostMapping("/insert")
    public ResultBean insertHappenStation(MesPartDivulgeCheckCfg partDivulgeCheckCfg){
        try{
            ValidatorBean.beginValid(partDivulgeCheckCfg)
                    .notNull(MesExtConstWords.PART_NO, partDivulgeCheckCfg.getPartNo())
                    .notNull(MesExtConstWords.PART_NAME, partDivulgeCheckCfg.getPartName());
            partDivulgeCheckCfgService.insert(partDivulgeCheckCfg);
            return ResultBean.success("添加成功").setCode(ResourceEnumUtil.MESSAGE.SUCCESS.getCode());
        } catch (ImppBusiException busExcep) {
            return ResultBean.fail(busExcep);
        } catch (Exception e) {
            return ImppExceptionBuilder.newInstance().buildExceptionResult(e);
        }
    }

    @GetMapping("/query")
    @ApiOperation(value = "查询")
    public ResultBean queryEquipmentByPager(MesPartDivulgeCheckCfg partDivulgeCheckCfg, Pager pager) {
        try {
            return ResultBean.success("查询成功").setListPager(partDivulgeCheckCfgService.queryByPager(partDivulgeCheckCfg, pager))
                    .setCode(ResourceEnumUtil.MESSAGE.SUCCESS.getCode());
        } catch (ImppBusiException busExcep) {
            return ResultBean.fail(busExcep);
        } catch (Exception e) {
            return ImppExceptionBuilder.newInstance().buildExceptionResult(e);
        }
    }

    @PostMapping("/update")
    @ApiOperation(value = "修改")
    public ResultBean updateEquipment(MesPartDivulgeCheckCfg partDivulgeCheckCfg){
        try {
            //条件验证
            ValidatorBean.beginValid(partDivulgeCheckCfg)
                    .notNull(MesExtConstWords.PART_NO, partDivulgeCheckCfg.getPartNo())
                    .notNull(MesExtConstWords.PART_NAME, partDivulgeCheckCfg.getPartName());

            return ResultBean.success("修改成功").setResultObject(partDivulgeCheckCfgService.update(partDivulgeCheckCfg))
                    .setCode(ResourceEnumUtil.MESSAGE.SUCCESS.getCode());
        } catch (ImppBusiException busExcep) {
            return ResultBean.fail(busExcep);
        } catch (Exception e) {
            return ImppExceptionBuilder.newInstance().buildExceptionResult(e);
        }
    }

    /**
     * 根据ID启用或禁用单据规则
     * @param id
     * @param status
     * @return
     */
    @PutMapping(value = "/status-update")
    @ApiOperation(value = "根据ID启用或禁用")
    public ResultBean updateEquipmentCheckIsValidById(long id, Integer status) {
        try {
            return ResultBean.success("根据ID启用或禁用").setResultObject(partDivulgeCheckCfgService.updateIsValidById(id,status))
                    .setCode(ResourceEnumUtil.MESSAGE.SUCCESS.getCode());
        } catch (ImppBusiException busExcep) {
            return ResultBean.fail(busExcep);
        } catch (Exception e) {
            return ImppExceptionBuilder.newInstance().buildExceptionResult(e);
        }
    }

    @GetMapping(value = "/exist-record/query")
    @ApiOperation(value = "查询24小时检测结果")
    public ResultBean getExistRecord(String organizeCode, String partNo, String packageNo ){
        try {
            return ResultBean.success("查询成功").setResultObject(partDivulgeCheckCfgService.queryPartDivulgeCheckCfg(organizeCode, partNo, packageNo))
                    .setCode(ResourceEnumUtil.MESSAGE.SUCCESS.getCode());
        } catch (ImppBusiException busExcep) {
            return ResultBean.fail(busExcep);
        } catch (Exception e) {
            return ImppExceptionBuilder.newInstance().buildExceptionResult(e);
        }
    }
}
