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

import cn.estsh.i3plus.ext.mes.api.base.bu3.ISxWorkLicenseService;
import cn.estsh.i3plus.ext.mes.pojo.bean.MesWorkLicense;
import cn.estsh.i3plus.ext.mes.pojo.bean.MesWorkLicenseTravel;
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 SxWorkLicenseController extends MesBaseController {

    @Autowired
    private ISxWorkLicenseService MesWorkLicenseService;

    @GetMapping(value = "/work-license/query")
    @ApiOperation(value = "按条件分页查询上岗证配置信息")
    public ResultBean queryMesWorkLicenseByPager(MesWorkLicense workLicense, Pager pager) {
        try {
            workLicense.setOrganizeCode(AuthUtil.getOrganize().getOrganizeCode());
            return ResultBean.success("查询成功").setCode(ResourceEnumUtil.MESSAGE.SUCCESS.getCode())
                    .setListPager(MesWorkLicenseService.queryWorkLicenseByPager(workLicense, pager));
        } catch (ImppBusiException busExcep) {
            return ResultBean.fail(busExcep);
        } catch (Exception e) {
            return ImppExceptionBuilder.newInstance().buildExceptionResult(e);
        }
    }

    @GetMapping(value = "/work-license-travel/query")
    @ApiOperation(value = "按条件分页查询上岗证配置履历信息")
    public ResultBean queryMesWorkLicenseTravelByPager(MesWorkLicenseTravel workLicenseTravel, Pager pager) {
        try {
            workLicenseTravel.setOrganizeCode(AuthUtil.getOrganize().getOrganizeCode());
            return ResultBean.success("查询成功").setCode(ResourceEnumUtil.MESSAGE.SUCCESS.getCode())
                    .setListPager(MesWorkLicenseService.queryWorkLicenseTravelByPager(workLicenseTravel, pager));
        } catch (ImppBusiException busExcep) {
            return ResultBean.fail(busExcep);
        } catch (Exception e) {
            return ImppExceptionBuilder.newInstance().buildExceptionResult(e);
        }
    }

    @PostMapping(value = "/work-license/insert")
    @ApiOperation(value = "新增上岗证配置信息")
    public ResultBean insertMesWorkLicense(MesWorkLicense workLicense) {
        try {
            ValidatorBean.beginValid(workLicense)
                    .notNull(MesExtConstWords.WORK_LICENSE_CODE, workLicense.getWorkLicenseCode())
                    .notNull(MesExtConstWords.USER_NAME, workLicense.getUserName())
                    .notNull(MesExtConstWords.VALIDITY_DATE, workLicense.getValidityDate());

            workLicense.setOrganizeCode(AuthUtil.getOrganize().getOrganizeCode());
            workLicense.setCreateUser(AuthUtil.getSessionUser().getUserName());
            MesWorkLicenseService.insertWorkLicense(workLicense);

            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 = "/work-license/update")
    @ApiOperation(value = "更新上岗证配置信息")
    public ResultBean updateMesWorkLicense(MesWorkLicense workLicense) {
        try {
            ValidatorBean.beginValid(workLicense)
                    .notNull(MesExtConstWords.ID, workLicense.getId())
                    .notNull(MesExtConstWords.WORK_LICENSE_CODE, workLicense.getWorkLicenseCode())
                    .notNull(MesExtConstWords.USER_NAME, workLicense.getUserName())
                    .notNull(MesExtConstWords.VALIDITY_DATE, workLicense.getValidityDate());

            workLicense.setOrganizeCode(AuthUtil.getOrganize().getOrganizeCode());
            workLicense.setModifyUser(AuthUtil.getSessionUser().getUserName());
            MesWorkLicenseService.updateWorkLicense(workLicense);
            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 = "/work-license/status-update")
    @ApiOperation(value = "根据id启用或禁用上岗证配置信息")
    public ResultBean updateMesWorkLicenseValidById(String idStr, Integer status) {
        try {
            if (StringUtils.isEmpty(idStr)) {
                throw ImppExceptionBuilder.newInstance()
                        .setSystemID(CommonEnumUtil.SOFT_TYPE.MES.getCode())
                        .setErrorCode(ImppExceptionEnum.VARIFY_EXCEPTION.getCode())
                        .setErrorDetail("请选择需要操作的资源。")
                        .build();
            }
            MesWorkLicenseService.updateWorkLicenseValidById(idStr, 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);
        }
    }

    @DeleteMapping(value = "/work-license/delete")
    @ApiOperation(value = "根据id删除上岗证配置信息")
    public ResultBean deleteMesWorkLicenseById(String idStr) {
        try {
            if (StringUtils.isEmpty(idStr)) {
                throw ImppExceptionBuilder.newInstance()
                        .setSystemID(CommonEnumUtil.SOFT_TYPE.MES.getCode())
                        .setErrorCode(ImppExceptionEnum.VARIFY_EXCEPTION.getCode())
                        .setErrorDetail("请选择需要操作的资源。")
                        .build();
            }
            MesWorkLicenseService.deleteWorkLicenseById(idStr, 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);
        }
    }

}
