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

import cn.estsh.i3plus.ext.mes.api.base.bu.IBusiConfigService;
import cn.estsh.i3plus.ext.mes.pojo.bean.MesBusiConfig;
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.common.Pager;
import cn.estsh.i3plus.pojo.base.enumutil.CommonEnumUtil;
import cn.estsh.i3plus.pojo.base.enumutil.ResourceEnumUtil;
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.util.StringUtils;
import org.springframework.web.bind.annotation.*;

/**
 * @Author: wangjie
 * @CreateDate: 2021/01/19 15:37
 * @Description:
 **/
@RestController
@RequestMapping(CommonConstWords.BASE_URL_MES)
@Api(tags = "业务配置控制层")
public class BusiCfgController extends MesBaseController {

    @Autowired
    private IBusiConfigService busiConfigService;

    @GetMapping(value = "/busi-config/query")
    @ApiOperation(value = "按条件分页查询业务配置信息")
    public ResultBean queryBusiConfigByPager(MesBusiConfig busiConfig, Pager pager) {
        try {
            busiConfig.setOrganizeCode(AuthUtil.getOrganize().getOrganizeCode());
            return ResultBean.success("操作成功").setCode(ResourceEnumUtil.MESSAGE.SUCCESS.getCode())
                    .setListPager(busiConfigService.queryBusiConfigByPager(busiConfig, pager));
        } catch (ImppBusiException busExcep) {
            return ResultBean.fail(busExcep);
        } catch (Exception e) {
            return ImppExceptionBuilder.newInstance().buildExceptionResult(e);
        }
    }

    @PostMapping(value = "/busi-config/insert")
    @ApiOperation(value = "新增业务配置信息")
    public ResultBean insertBusiConfig(MesBusiConfig busiConfig) {
        try {
            ValidatorBean.beginValid(busiConfig)
                    .notNull(MesExtConstWords.CFG_CODE, busiConfig.getCfgCode())
                    .notNull(MesExtConstWords.CFG_NAME, busiConfig.getCfgName())
                    .notNull(MesExtConstWords.CFG_VALUE, busiConfig.getCfgValue())
                    .notNull(MesExtConstWords.CFG_VALUE_SEQ, busiConfig.getCfgValueSeq());

            busiConfig.setOrganizeCode(AuthUtil.getOrganize().getOrganizeCode());
            busiConfig.setCreateUser(AuthUtil.getSessionUser().getUserName());
            busiConfigService.insertBusiConfig(busiConfig);

            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 = "/busi-config/update")
    @ApiOperation(value = "更新业务配置信息")
    public ResultBean updateBusiConfig(MesBusiConfig busiConfig) {
        try {
            ValidatorBean.beginValid(busiConfig)
                    .notNull(MesExtConstWords.ID, busiConfig.getId())
                    .notNull(MesExtConstWords.CFG_CODE, busiConfig.getCfgCode())
                    .notNull(MesExtConstWords.CFG_NAME, busiConfig.getCfgName())
                    .notNull(MesExtConstWords.CFG_VALUE, busiConfig.getCfgValue())
                    .notNull(MesExtConstWords.CFG_VALUE_SEQ, busiConfig.getCfgValueSeq());

            busiConfig.setOrganizeCode(AuthUtil.getOrganize().getOrganizeCode());
            busiConfig.setModifyUser(AuthUtil.getSessionUser().getUserName());
            busiConfigService.updateBusiConfig(busiConfig);
            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 = "/busi-config/status-update")
    @ApiOperation(value = "根据id启用或禁用业务配置信息")
    public ResultBean updateBusiConfigValidById(Long id, Integer status) {
        try {
            if (StringUtils.isEmpty(id)) {
                throw ImppExceptionBuilder.newInstance()
                        .setSystemID(CommonEnumUtil.SOFT_TYPE.MES.getCode())
                        .setErrorCode(ImppExceptionEnum.VARIFY_EXCEPTION.getCode())
                        .setErrorDetail("请选择需要操作的资源。")
                        .build();
            }
            busiConfigService.updateBusiConfigValidById(id, status, AuthUtil.getOrganize().getOrganizeCode(), 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 = "/busi-config/get-cfg")
    @ApiOperation(value = "按条件分页查询业务配置信息")
    public ResultBean getBusiConfigMap(String cfgCode) {
        try {
            if (StringUtils.isEmpty(cfgCode)) {
                throw ImppExceptionBuilder.newInstance()
                        .setSystemID(CommonEnumUtil.SOFT_TYPE.MES.getCode())
                        .setErrorCode(ImppExceptionEnum.VARIFY_EXCEPTION.getCode())
                        .setErrorDetail("入参缺失必要参数配置代码[cfgCode]")
                        .build();
            }
            return ResultBean.success("查询成功").setCode(ResourceEnumUtil.MESSAGE.SUCCESS.getCode())
                    .setResultMap(busiConfigService.getConfigMap(AuthUtil.getOrganize().getOrganizeCode(), cfgCode));
        } catch (ImppBusiException busExcep) {
            return ResultBean.fail(busExcep);
        } catch (Exception e) {
            return ImppExceptionBuilder.newInstance().buildExceptionResult(e);
        }
    }

}
