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

import cn.estsh.i3plus.ext.mes.api.base.bu.IMesTravelMenuFieldConfigService;
import cn.estsh.i3plus.ext.mes.pojo.bean.MesTravelMenuFieldConfig;
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: xinwang.yi
 * @CreateDate: 2022/10/28 11:45
 * @Description:
 **/
@RestController
    @RequestMapping(CommonConstWords.BASE_URL_MES + "/ext/travel-menu-field-config")
@Api(tags = "履历菜单字段配置控制层")
public class MesTravelMenuFieldConfigController extends MesBaseController {

    @Autowired
    private IMesTravelMenuFieldConfigService travelMenuFieldConfigService;

    @GetMapping(value = "/query")
    @ApiOperation(value = "按条件分页查询履历菜单字段配置信息")
    public ResultBean queryTravelMenuFieldConfigByPager(MesTravelMenuFieldConfig travelMenuFieldConfig, Pager pager) {
        try {
            travelMenuFieldConfig.setOrganizeCode(AuthUtil.getOrganize().getOrganizeCode());
            return ResultBean.success("操作成功").setCode(ResourceEnumUtil.MESSAGE.SUCCESS.getCode())
                    .setListPager(travelMenuFieldConfigService.queryTravelMenuFieldConfigByPager(travelMenuFieldConfig, pager));
        } catch (ImppBusiException busExcep) {
            return ResultBean.fail(busExcep);
        } catch (Exception e) {
            return ImppExceptionBuilder.newInstance().buildExceptionResult(e);
        }
    }

    @PostMapping(value = "/insert")
    @ApiOperation(value = "新增履历菜单字段配置信息")
    public ResultBean insertTravelMenuFieldConfig(MesTravelMenuFieldConfig travelMenuFieldConfig) {
        try {
            ValidatorBean.beginValid(travelMenuFieldConfig)
                    .notNull(MesExtConstWords.MENU_CODE, travelMenuFieldConfig.getMenuCode())
                    .notNull(MesExtConstWords.FIELD_CODE, travelMenuFieldConfig.getFieldCode())
                    .notNull(MesExtConstWords.FIELD_NAME, travelMenuFieldConfig.getFieldName());

            travelMenuFieldConfig.setOrganizeCode(AuthUtil.getOrganize().getOrganizeCode());
            travelMenuFieldConfig.setCreateUser(AuthUtil.getSessionUser().getUserName());
            travelMenuFieldConfigService.insertTravelMenuFieldConfig(travelMenuFieldConfig);

            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")
    @ApiOperation(value = "更新履历菜单字段配置信息")
    public ResultBean updateTravelMenuFieldConfig(MesTravelMenuFieldConfig travelMenuFieldConfig) {
        try {
            ValidatorBean.beginValid(travelMenuFieldConfig)
                    .notNull(MesExtConstWords.ID, travelMenuFieldConfig.getId())
                    .notNull(MesExtConstWords.MENU_CODE, travelMenuFieldConfig.getMenuCode())
                    .notNull(MesExtConstWords.FIELD_CODE, travelMenuFieldConfig.getFieldCode())
                    .notNull(MesExtConstWords.FIELD_NAME, travelMenuFieldConfig.getFieldName());

            travelMenuFieldConfig.setOrganizeCode(AuthUtil.getOrganize().getOrganizeCode());
            travelMenuFieldConfig.setModifyUser(AuthUtil.getSessionUser().getUserName());
            travelMenuFieldConfigService.updateTravelMenuFieldConfig(travelMenuFieldConfig);
            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 = "/status-update")
    @ApiOperation(value = "根据id启用或禁用履历菜单字段配置信息")
    public ResultBean updateTravelMenuFieldConfigValidById(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();
            }
            travelMenuFieldConfigService.updateTravelMenuFieldConfigValidById(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);
        }
    }

}
