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

import cn.estsh.i3plus.ext.mes.api.base.ep.IEpPartCheckExtService;
import cn.estsh.i3plus.ext.mes.pojo.util.MesExtConstWords;
import cn.estsh.i3plus.platform.common.exception.ImppExceptionEnum;
import cn.estsh.i3plus.platform.common.util.CommonConstWords;
import cn.estsh.i3plus.pojo.base.bean.ListPager;
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.mes.bean.MesPartCheck;
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.*;

/**
 * @PROJECT_NAME: i3plus-mes-panasonic-dev
 * @DESCRIPTION:
 * @USER: xinwang.yi
 * @DATE: 2021-12-27 13:39
 */
@RestController
@Api(tags = "物料信息")
@RequestMapping(CommonConstWords.BASE_URL_MES)
public class EpPartCheckExtController {

    @Autowired
    private IEpPartCheckExtService partCheckService;

    @GetMapping(value = "/ep-ext/part-check/query")
    @ApiOperation(value = "按条件分页查询物料校验项数据")
    public ResultBean queryMesPartCheckByPager(MesPartCheck partCheck, Pager pager) {
        try {

            ListPager listPager = partCheckService.queryMesPartCheckByPager(partCheck, pager);

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

    @PostMapping(value = "/ep-ext/part-check/insert")
    @ApiOperation(value = "新增物料校验项信息")
    public ResultBean insertMesPartCheck(MesPartCheck partCheck) {
        try {

            //条件验证
            ValidatorBean.beginValid(partCheck)
                    .notNull(MesExtConstWords.OBJECT_CODE, partCheck.getObjectCode())
                    .notNull(MesExtConstWords.DATA_TYPE, partCheck.getDataType())
                    .notNull(MesExtConstWords.WORK_CENTER_CODE, partCheck.getWorkCenterCode());

            partCheck.setOrganizeCode(AuthUtil.getOrganize().getOrganizeCode());
            partCheck.setCreateUser(AuthUtil.getSessionUser().getUserName());
            partCheckService.insertMesPartCheck(partCheck);
            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 = "/ep-ext/part-check/update")
    @ApiOperation(value = "更新物料校验项信息")
    public ResultBean updateMesPartCheck(MesPartCheck partCheck) {
        try {

            //条件验证
            ValidatorBean.beginValid(partCheck)
                    .notNull(MesExtConstWords.ID, partCheck.getId())
                    .notNull(MesExtConstWords.OBJECT_CODE, partCheck.getObjectCode())
                    .notNull(MesExtConstWords.DATA_TYPE, partCheck.getDataType())
                    .notNull(MesExtConstWords.WORK_CENTER_CODE, partCheck.getWorkCenterCode());

            partCheck.setOrganizeCode(AuthUtil.getOrganize().getOrganizeCode());
            partCheck.setModifyUser(AuthUtil.getSessionUser().getUserName());
            partCheckService.updateMesPartCheck(partCheck);
            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 = "/ep-ext/part-check/status-update")
    @ApiOperation(value = "根据id启用或禁用物料校验项信息")
    public ResultBean updateMesPartCheckStatusById(Long id, int status) {
        try {

            if (null == id) {
                throw ImppExceptionBuilder.newInstance()
                        .setSystemID(CommonEnumUtil.SOFT_TYPE.MES.getCode())
                        .setErrorCode(ImppExceptionEnum.VARIFY_EXCEPTION.getCode())
                        .setErrorDetail("请选择需要操作的资源。")
                        .build();
            }

            partCheckService.updateMesPartCheckStatusById(id, status, 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);
        }
    }

}
