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

import cn.estsh.i3plus.ext.mes.api.base.jx.IJxRepairJudgeTypeService;
import cn.estsh.i3plus.ext.mes.pojo.bean.MesRepairJudgeType;
import cn.estsh.i3plus.ext.mes.pojo.model.BatchIdModel;
import cn.estsh.i3plus.ext.mes.pojo.util.MesExtConstWords;
import cn.estsh.i3plus.platform.common.convert.ConvertBean;
import cn.estsh.i3plus.platform.common.exception.ImppExceptionEnum;
import cn.estsh.i3plus.pojo.base.common.Pager;
import cn.estsh.i3plus.pojo.base.enumutil.CommonEnumUtil;
import cn.estsh.i3plus.pojo.base.enumutil.ResourceEnumUtil;
import cn.estsh.i3plus.pojo.platform.bean.SessionUser;
import cn.estsh.impp.framework.base.controller.MesBaseController;
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.web.bind.annotation.*;

/**
 * @Author: xinwang.yi
 * @CreateDate: 2021/01/18 13:33 下午
 * @Description:
 **/
@RestController
@Api(tags = "维修判定类型 控制层")
@RequestMapping(MesExtConstWords.BASE_URL_MES_JX + "/repair-judge-type")
public class RepairJudgeTypeController extends MesBaseController {

    @Autowired
    private IJxRepairJudgeTypeService repairJudgeTypeService;

    @GetMapping(value = "/query")
    @ApiOperation(value = "按条件分页查询MES_维修判定类型信息", notes = "按条件分页查询MES_维修判定类型信息")
    public ResultBean queryMesRepairJudgeTypeByPager(MesRepairJudgeType model, Pager pager) {
        try {
            model.setOrganizeCode(AuthUtil.getOrganize().getOrganizeCode());
            return ResultBean.success().setListPager(repairJudgeTypeService.queryRepairJudgeTypePager(model, pager))
                    .setCode(ResourceEnumUtil.MESSAGE.SUCCESS.getCode());
        } catch (ImppBusiException busExcep) {
            return ResultBean.fail(busExcep);
        } catch (Exception e) {
            return ImppExceptionBuilder.newInstance().buildExceptionResult(e);
        }
    }

    @PostMapping(value = "/insert")
    @ApiOperation(value = "添加MES_维修判定类型信息")
    public ResultBean insertMesRepairJudgeType(MesRepairJudgeType model) {
        try {
            //条件验证
            ValidatorBean.beginValid(model)
                    .notNull(MesExtConstWords.REPAIR_JUDGE_CODE, model.getRepairJudgeCode())
                    .notNull(MesExtConstWords.REPAIR_JUDGE_NAME, model.getRepairJudgeName())
                    .notNull(MesExtConstWords.SEQ, model.getSeq());
            model.setOrganizeCode(AuthUtil.getOrganize().getOrganizeCode());
            repairJudgeTypeService.insertRepairJudgeType(model);
            return ResultBean.success("添加成功").setCode(ResourceEnumUtil.MESSAGE.SUCCESS.getCode());
        } catch (ImppBusiException imppException) {
            LOGGER.error(imppException.getErrorMsg() + "：{}", imppException.getErrorDetail(), imppException);
            return ResultBean.fail(imppException);
        } catch (Exception e) {
            return ImppExceptionBuilder.newInstance().buildExceptionResult(e);
        }
    }

    @PostMapping(value = "/update")
    @ApiOperation(value = "更新MES_维修判定类型项目")
    public ResultBean updateMesRepairJudgeType(MesRepairJudgeType model) {
        try {
            //条件验证
            ValidatorBean.beginValid(model)
                    .notNull(MesExtConstWords.REPAIR_JUDGE_CODE, model.getRepairJudgeCode())
                    .notNull(MesExtConstWords.REPAIR_JUDGE_NAME, model.getRepairJudgeName())
                    .notNull(MesExtConstWords.SEQ, model.getSeq());

            if (model.getId() == null || model.getId().longValue() < 1) {
                throw ImppExceptionBuilder.newInstance()
                        .setSystemID(CommonEnumUtil.SOFT_TYPE.MES.getCode())
                        .setErrorCode(cn.estsh.i3plus.platform.common.exception.ImppExceptionEnum.VARIFY_EXCEPTION.getCode())
                        .setErrorDetail("请选择需要操作的资源。")
                        .build();
            }
            model.setModifyUser(AuthUtil.getSessionUser().getUserName());
            repairJudgeTypeService.updateRepairJudgeType(model);

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

    @PutMapping(value = "/update-status-batch")
    @ApiOperation(value = "批量禁用/启用详情", notes = "批量禁用/启用详情")
    public ResultBean updateMesRepairJudgeTypeStatus(@RequestBody BatchIdModel model) {
        try {

            ValidatorBean.checkNotNull(model.getStatus(), "未有启用禁用标志");
            ValidatorBean.checkNotNull(model.getIds(), "未选择启用/禁用资源");

            ConvertBean.modelSafeArrayNumber(model.getIds(), true);
            //登陆用户
            SessionUser user = AuthUtil.getSessionUser();

            repairJudgeTypeService.updateRepairJudgeTypeValidById(model, user.getUserName());
            return ResultBean.success("修改成功")
                    .setCode(ResourceEnumUtil.MESSAGE.SUCCESS.getCode());
        } catch (ImppBusiException busExcep) {
            return ResultBean.fail(busExcep);
        } catch (Exception e) {
            return ImppExceptionBuilder.newInstance().buildExceptionResult(e);
        }
    }

    @DeleteMapping(value = "/delete-status-batch")
    @ApiOperation(value = "根据id删除产线班次固定作业内容信息")
    public ResultBean deleteShiftProdCenterById(@RequestBody BatchIdModel model) {
        try {
            if (null == model.getIds()) {
                throw ImppExceptionBuilder.newInstance()
                        .setSystemID(CommonEnumUtil.SOFT_TYPE.MES.getCode())
                        .setErrorCode(ImppExceptionEnum.VARIFY_EXCEPTION.getCode())
                        .setErrorDetail("请选择需要操作的资源。")
                        .build();
            }

            repairJudgeTypeService.deleteRepairJudgeTypeValidById(model, AuthUtil.getSessionUser().getUserName());
            return ResultBean.success("删除成功").setCode(ResourceEnumUtil.MESSAGE.SUCCESS.getCode());
        } catch (ImppBusiException busExcep) {
            return ResultBean.fail(busExcep);
        } catch (Exception e) {
            return ImppExceptionBuilder.newInstance().buildExceptionResult(e);
        }
    }

    @GetMapping(value = "/query-all")
    @ApiOperation(value = "查询所有MES_维修判定类型信息", notes = "查询所有MES_维修判定类型信息")
    public ResultBean queryMesRepairJudgeTypeList() {
        try {
            return ResultBean.success().setResultList(repairJudgeTypeService.queryRepairJudgeList(AuthUtil.getOrganize().getOrganizeCode()))
                    .setCode(ResourceEnumUtil.MESSAGE.SUCCESS.getCode());
        } catch (ImppBusiException busExcep) {
            return ResultBean.fail(busExcep);
        } catch (Exception e) {
            return ImppExceptionBuilder.newInstance().buildExceptionResult(e);
        }
    }
}
