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

import cn.estsh.i3plus.ext.mes.api.busi.bu.IMesEmployeeNatureService;
import cn.estsh.i3plus.ext.mes.pojo.bean.MesEmployeeNature;
import cn.estsh.i3plus.platform.common.util.CommonConstWords;
import cn.estsh.i3plus.pojo.base.common.Pager;
import cn.estsh.i3plus.pojo.base.enumutil.ResourceEnumUtil;
import cn.estsh.i3plus.pojo.platform.bean.SessionUser;
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.*;

@RestController
@RequestMapping(CommonConstWords.BASE_URL_MES+"/employee-nature")
@Api(tags = "员工性质")
public class MesEmployeeNatureController extends MesBaseController {

    @Autowired
    private IMesEmployeeNatureService employeeNatureService;

    @GetMapping(value = "/query-list-page")
    @ApiOperation(value = "员工性质分页查询")
    public ResultBean getEmployeeNaturePage(MesEmployeeNature employeeNature, Pager pager) {
        String organizeCode = AuthUtil.getOrganize().getOrganizeCode();
        try {
            //请求参数非空
            if (null == employeeNature || null == pager) {
                return ResultBean.fail("请求参数不完整");
            }

            ResultBean resultBean = new ResultBean(true);
            return ResultBean.success("员工性质分页查询")
                    .setListPager(employeeNatureService.getEmployeeNaturePage(employeeNature, pager, organizeCode))
                    .setCode(ResourceEnumUtil.MESSAGE.SUCCESS.getCode());
        } catch (ImppBusiException imppException) {

            return ResultBean.fail(imppException);
        } catch (Exception e) {
            return ImppExceptionBuilder.newInstance().buildExceptionResult(e);
        }
    }

    @PostMapping(value = "/insert")
    @ApiOperation(value = "新增员工性质")
    public ResultBean insertEmployeeNature(MesEmployeeNature employeeNature) {
        String userName = AuthUtil.getSessionUser().getUserName();
        SessionUser sessionUser = AuthUtil.getSessionUser();
        String organizeCode = AuthUtil.getOrganize().getOrganizeCode();
        try {
            // 属性验证
            employeeNatureService.insertEmployeeNature(employeeNature, organizeCode, userName);
            return new ResultBean(true).setMsg("新增成功");
        } catch (ImppBusiException imppException) {

            return ResultBean.fail(imppException);
        } catch (Exception e) {
            return ImppExceptionBuilder.newInstance().buildExceptionResult(e);
        }
    }

    @PutMapping(value = "/update")
    @ApiOperation(value = "更新员工性质")
    public ResultBean updateEmployeeNature(MesEmployeeNature employeeNature) {
        String userName = AuthUtil.getSessionUser().getUserName();
        String organizeCode = AuthUtil.getOrganize().getOrganizeCode();
        try {
            // 属性验证
            ValidatorBean.beginValid(employeeNature)
                    .notNull("id", employeeNature.getId());
            employeeNatureService.updateEmployeeNature(employeeNature, organizeCode, userName);
            return new ResultBean(true).setMsg("更新成功");
        } catch (ImppBusiException imppException) {

            return ResultBean.fail(imppException);
        } catch (Exception e) {
            return ImppExceptionBuilder.newInstance().buildExceptionResult(e);
        }
    }

}
