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

import cn.estsh.i3plus.ext.mes.api.busi.bu.IMesEmployeeSkillsManagementService;
import cn.estsh.i3plus.ext.mes.pojo.bean.MesEmployeeSkillsManagement;
import cn.estsh.i3plus.ext.mes.pojo.util.MesExtConstWords;
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.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-skills-management")
@Api(tags = "员工技能管理")
public class MesEmployeeSkillsManagementController extends MesBaseController {

    @Autowired
    private IMesEmployeeSkillsManagementService employeeSkillsManagementService;

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

            return ResultBean.success("员工技能管理分页查询")
                    .setListPager(employeeSkillsManagementService.getEmployeeSkillsManagementPage(employeeSkillsManagement, 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 insertEmployeeSkillsManagement(MesEmployeeSkillsManagement employeeSkillsManagement) {
        String userName = AuthUtil.getSessionUser().getUserName();
        String organizeCode = AuthUtil.getOrganize().getOrganizeCode();
        try {
            // 属性验证
            ValidatorBean.beginValid(employeeSkillsManagement)
                    .notNull(MesExtConstWords.EMPLOYEE_CODE, employeeSkillsManagement.getEmployeeCode())
                    .notNull(MesExtConstWords.EMPLOYEE_NAME, employeeSkillsManagement.getEmployeeName())
                    .notNull(MesExtConstWords.WORK_CENTER_CODE, employeeSkillsManagement.getWorkCenterCode())
                    .notNull(MesExtConstWords.SEQ, employeeSkillsManagement.getSeq());
            employeeSkillsManagementService.insertEmployeeSkillsManagement(employeeSkillsManagement, 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 updateEmployeeSkillsManagement(MesEmployeeSkillsManagement employeeSkillsManagement) {
        String userName = AuthUtil.getSessionUser().getUserName();
        String organizeCode = AuthUtil.getOrganize().getOrganizeCode();
        try {
            // 属性验证
            ValidatorBean.beginValid(employeeSkillsManagement)
                    .notNull(MesExtConstWords.EMPLOYEE_CODE, employeeSkillsManagement.getEmployeeCode())
                    .notNull(MesExtConstWords.EMPLOYEE_NAME, employeeSkillsManagement.getEmployeeName())
                    .notNull(MesExtConstWords.WORK_CENTER_CODE, employeeSkillsManagement.getWorkCenterCode())
                    .notNull(MesExtConstWords.SEQ, employeeSkillsManagement.getSeq())
                    .notNull(MesExtConstWords.ID, employeeSkillsManagement.getId());
            employeeSkillsManagementService.updateEmployeeSkillsManagement(employeeSkillsManagement, organizeCode, userName);
            return new ResultBean(true).setMsg("更新成功");
        } catch (ImppBusiException imppException) {

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

}
