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

import cn.estsh.i3plus.ext.mes.api.base.ep.IEpObjectCfgExtService;
import cn.estsh.i3plus.ext.mes.pojo.util.MesExtConstWords;
import cn.estsh.i3plus.platform.common.util.CommonConstWords;
import cn.estsh.i3plus.pojo.base.enumutil.ResourceEnumUtil;
import cn.estsh.i3plus.pojo.mes.bean.MesObjectCfg;
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.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * @Author: wangjie
 * @CreateDate: 2021/01/19 15:37
 * @Description:
 **/
@RestController
@RequestMapping(CommonConstWords.BASE_URL_MES)
@Api(tags = "对象结构信息控制层")
public class EpObjectCfgController extends MesBaseController {

    @Autowired
    private IEpObjectCfgExtService objectCfgExtService;

    @PostMapping(value = "/ep/object-cfg-ext/insert")
    @ApiOperation(value = "新增对象结构信息信息")
    public ResultBean insertObjectCfg(MesObjectCfg objectCfg) {
        try {
            ValidatorBean.beginValid(objectCfg)
                    .notNull(MesExtConstWords.OBJECT_CODE, objectCfg.getObjectCode())
                    .notNull(MesExtConstWords.FIELD_CODE, objectCfg.getFieldCode())
                    .notNull(MesExtConstWords.FIELD_NAME, objectCfg.getFieldName());

            objectCfg.setOrganizeCode(AuthUtil.getOrganize().getOrganizeCode());
            objectCfg.setCreateUser(AuthUtil.getSessionUser().getUserName());
            objectCfgExtService.insertObjectCfg(objectCfg);

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

    @PostMapping(value = "/ep/object-cfg-ext/update")
    @ApiOperation(value = "更新对象结构信息信息")
    public ResultBean updateObjectCfg(MesObjectCfg objectCfg) {
        try {
            ValidatorBean.beginValid(objectCfg)
                    .notNull(MesExtConstWords.ID, objectCfg.getId())
                    .notNull(MesExtConstWords.OBJECT_CODE, objectCfg.getObjectCode())
                    .notNull(MesExtConstWords.FIELD_CODE, objectCfg.getFieldCode())
                    .notNull(MesExtConstWords.FIELD_NAME, objectCfg.getFieldName());

            objectCfg.setOrganizeCode(AuthUtil.getOrganize().getOrganizeCode());
            objectCfg.setModifyUser(AuthUtil.getSessionUser().getUserName());
            objectCfgExtService.updateObjectCfg(objectCfg);
            return ResultBean.success("修改成功").setCode(ResourceEnumUtil.MESSAGE.SUCCESS.getCode());
        } catch (ImppBusiException busExcep) {
            return ResultBean.fail(busExcep);
        } catch (Exception e) {
            return ImppExceptionBuilder.newInstance().buildExceptionResult(e);
        }
    }

}
