| | |
| | | import cn.hutool.core.collection.CollUtil; |
| | | import cn.hutool.core.util.StrUtil; |
| | | import cn.hutool.json.JSONUtil; |
| | | import cn.iocoder.yudao.framework.common.exception.ServiceException; |
| | | import cn.iocoder.yudao.framework.common.pojo.PageResult; |
| | | import cn.iocoder.yudao.framework.common.util.object.BeanUtils; |
| | | import cn.iocoder.yudao.module.hrm.controller.admin.employee.vo.HrmEmployeeEducationSaveReqVO; |
| | | import cn.iocoder.yudao.module.hrm.controller.admin.employee.vo.HrmEmployeeEmergencyContactSaveReqVO; |
| | | import cn.iocoder.yudao.module.hrm.controller.admin.employee.vo.HrmEmployeeImportExcelVO; |
| | | import cn.iocoder.yudao.module.hrm.controller.admin.employee.vo.HrmEmployeeImportReqVO; |
| | | import cn.iocoder.yudao.module.hrm.controller.admin.employee.vo.HrmEmployeeImportRespVO; |
| | | import cn.iocoder.yudao.module.hrm.controller.admin.employee.vo.HrmEmployeePageReqVO; |
| | | import cn.iocoder.yudao.module.hrm.controller.admin.employee.vo.HrmEmployeeRespVO; |
| | | import cn.iocoder.yudao.module.hrm.controller.admin.employee.vo.HrmEmployeeSaveReqVO; |
| | |
| | | import cn.iocoder.yudao.module.system.api.permission.dto.RoleRespDTO; |
| | | import cn.iocoder.yudao.module.system.api.user.AdminUserApi; |
| | | import cn.iocoder.yudao.module.system.api.user.dto.AdminUserCreateReqDTO; |
| | | import cn.iocoder.yudao.module.system.api.user.dto.AdminUserUpdateReqDTO; |
| | | import jakarta.annotation.Resource; |
| | | import org.springframework.context.annotation.Lazy; |
| | | import org.springframework.stereotype.Service; |
| | | import org.springframework.transaction.annotation.Transactional; |
| | | import org.springframework.validation.annotation.Validated; |
| | | |
| | | import java.util.ArrayList; |
| | | import java.util.Collection; |
| | | import java.util.Collections; |
| | | import java.util.LinkedHashMap; |
| | | import java.util.LinkedHashSet; |
| | | import java.util.List; |
| | | import java.util.Map; |
| | | import java.util.Objects; |
| | |
| | | import java.util.stream.Collectors; |
| | | |
| | | import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception; |
| | | import static cn.iocoder.yudao.framework.common.util.collection.CollectionUtils.filterList; |
| | | import static cn.iocoder.yudao.module.hrm.enums.ErrorCodeConstants.*; |
| | | |
| | | /** |
| | |
| | | private AdminUserApi adminUserApi; |
| | | @Resource |
| | | private RoleApi roleApi; |
| | | @Resource |
| | | @Lazy // 自注入需延迟加载,否则 bean 被代理包装后注入原始版本导致启动失败 |
| | | private HrmEmployeeService self; |
| | | |
| | | @Override |
| | | @Transactional(rollbackFor = Exception.class) |
| | |
| | | // 1. 校验部门 |
| | | deptApi.validateDeptList(Collections.singleton(createReqVO.getDeptId())); |
| | | // 2. 校验岗位 |
| | | postApi.validPostList(Collections.singleton(createReqVO.getPostId())); |
| | | postApi.validPostList(createReqVO.getPostIds()); |
| | | // 3. 校验手机号是否已存在 |
| | | if (employeeMapper.selectByPhone(createReqVO.getPhone()) != null) { |
| | | throw exception(EMPLOYEE_PHONE_EXISTS); |
| | |
| | | userCreateReqDTO.setUsername(createReqVO.getPhone()); |
| | | userCreateReqDTO.setNickname(createReqVO.getName()); |
| | | userCreateReqDTO.setDeptId(createReqVO.getDeptId()); |
| | | userCreateReqDTO.setPostIds(Collections.singleton(createReqVO.getPostId())); |
| | | userCreateReqDTO.setPostIds(new LinkedHashSet<>(createReqVO.getPostIds())); |
| | | userCreateReqDTO.setMobile(createReqVO.getPhone()); |
| | | userCreateReqDTO.setPassword(createReqVO.getPhone()); |
| | | userCreateReqDTO.setSex(createReqVO.getGender()); |
| | |
| | | // roleIds 存储为 JSON |
| | | if (CollUtil.isNotEmpty(createReqVO.getRoleIds())) { |
| | | employee.setRoleIds(JSONUtil.toJsonStr(createReqVO.getRoleIds())); |
| | | } |
| | | // postIds 存储为 JSON |
| | | if (CollUtil.isNotEmpty(createReqVO.getPostIds())) { |
| | | employee.setPostIds(JSONUtil.toJsonStr(createReqVO.getPostIds())); |
| | | } |
| | | employeeMapper.insert(employee); |
| | | Long employeeId = employee.getId(); |
| | |
| | | // 校验部门 |
| | | deptApi.validateDeptList(Collections.singleton(updateReqVO.getDeptId())); |
| | | // 校验岗位 |
| | | postApi.validPostList(Collections.singleton(updateReqVO.getPostId())); |
| | | postApi.validPostList(updateReqVO.getPostIds()); |
| | | // 校验手机号是否已存在(如果手机号有变更) |
| | | if (!Objects.equals(employee.getPhone(), updateReqVO.getPhone()) |
| | | && employeeMapper.selectByPhone(updateReqVO.getPhone()) != null) { |
| | |
| | | if (CollUtil.isNotEmpty(updateReqVO.getRoleIds())) { |
| | | updateObj.setRoleIds(JSONUtil.toJsonStr(updateReqVO.getRoleIds())); |
| | | } |
| | | // postIds 存储为 JSON |
| | | if (CollUtil.isNotEmpty(updateReqVO.getPostIds())) { |
| | | updateObj.setPostIds(JSONUtil.toJsonStr(updateReqVO.getPostIds())); |
| | | } |
| | | employeeMapper.updateById(updateObj); |
| | | // 同步更新关联系统用户(岗位/部门/昵称/角色/邮箱/备注),保持与用户管理一致 |
| | | if (employee.getUserId() != null) { |
| | | AdminUserUpdateReqDTO userUpdateReqDTO = new AdminUserUpdateReqDTO(); |
| | | userUpdateReqDTO.setId(employee.getUserId()); |
| | | userUpdateReqDTO.setNickname(updateReqVO.getName()); |
| | | userUpdateReqDTO.setDeptId(updateReqVO.getDeptId()); |
| | | userUpdateReqDTO.setPostIds(new LinkedHashSet<>(updateReqVO.getPostIds())); |
| | | userUpdateReqDTO.setRoleIds(CollUtil.isEmpty(updateReqVO.getRoleIds()) ? null |
| | | : new LinkedHashSet<>(updateReqVO.getRoleIds())); |
| | | userUpdateReqDTO.setEmail(updateReqVO.getEmail()); |
| | | userUpdateReqDTO.setRemark(updateReqVO.getRemark()); |
| | | adminUserApi.updateUser(userUpdateReqDTO); |
| | | } |
| | | Long employeeId = updateReqVO.getId(); |
| | | |
| | | // 同步教育经历 |
| | |
| | | respVO.setDeptName(dept.getName()); |
| | | } |
| | | } |
| | | // 填充岗位名称 |
| | | if (employee.getPostId() != null) { |
| | | Map<Long, PostRespDTO> postMap = postApi.getPostMap(Collections.singleton(employee.getPostId())); |
| | | PostRespDTO post = postMap.get(employee.getPostId()); |
| | | if (post != null) { |
| | | respVO.setPostName(post.getName()); |
| | | // 填充岗位信息 |
| | | if (StrUtil.isNotBlank(employee.getPostIds())) { |
| | | List<Long> postIds = JSONUtil.toList(employee.getPostIds(), Long.class); |
| | | respVO.setPostIds(postIds); |
| | | if (CollUtil.isNotEmpty(postIds)) { |
| | | Map<Long, PostRespDTO> postMap = postApi.getPostMap(postIds); |
| | | respVO.setPostNames(postIds.stream() |
| | | .filter(postMap::containsKey) |
| | | .map(postId -> postMap.get(postId).getName()) |
| | | .collect(Collectors.joining(","))); |
| | | } |
| | | } |
| | | // 填充角色名称 |
| | |
| | | return employeeMapper.selectByUserId(userId); |
| | | } |
| | | |
| | | @Override |
| | | public HrmEmployeeDO getEmployeeByName(String name) { |
| | | return employeeMapper.selectByName(name); |
| | | } |
| | | |
| | | @Override |
| | | public List<HrmEmployeeDO> getEmployeeListByName(String name) { |
| | | return employeeMapper.selectListByName(name); |
| | | } |
| | | |
| | | @Override |
| | | public HrmEmployeeImportRespVO importEmployeeList(List<HrmEmployeeImportExcelVO> importEmployees, |
| | | HrmEmployeeImportReqVO importReqVO) { |
| | | // 1. 校验非空 |
| | | importEmployees = filterList(importEmployees, item -> Objects.nonNull(item.getName())); |
| | | if (CollUtil.isEmpty(importEmployees)) { |
| | | throw exception(EMPLOYEE_IMPORT_LIST_IS_EMPTY); |
| | | } |
| | | // 2. 预加载部门/岗位/角色 名称 -> 实体 映射 |
| | | Map<String, DeptRespDTO> deptMap = deptApi.getDeptList().stream() |
| | | .collect(Collectors.toMap(DeptRespDTO::getName, dept -> dept, (v1, v2) -> v1)); |
| | | Map<String, PostRespDTO> postMap = postApi.getPostList().stream() |
| | | .collect(Collectors.toMap(PostRespDTO::getName, post -> post, (v1, v2) -> v1)); |
| | | Map<String, RoleRespDTO> roleMap = roleApi.getRoleList().stream() |
| | | .collect(Collectors.toMap(RoleRespDTO::getName, role -> role, (v1, v2) -> v1)); |
| | | // 3. 逐条处理 |
| | | HrmEmployeeImportRespVO respVO = HrmEmployeeImportRespVO.builder() |
| | | .createNames(new ArrayList<>()) |
| | | .updateNames(new ArrayList<>()) |
| | | .failureNames(new LinkedHashMap<>()) |
| | | .build(); |
| | | for (HrmEmployeeImportExcelVO importEmployee : importEmployees) { |
| | | String name = importEmployee.getName(); |
| | | try { |
| | | // 3.1 名称转 ID |
| | | Long deptId = parseDeptId(importEmployee.getDeptName(), deptMap); |
| | | Set<Long> postIds = parsePostIds(importEmployee.getPostName(), postMap); |
| | | Set<Long> roleIds = parseRoleIds(importEmployee.getRoleNames(), roleMap); |
| | | // 3.2 构建保存请求 |
| | | HrmEmployeeSaveReqVO saveReqVO = buildImportSaveReqVO(importEmployee, deptId, postIds, roleIds); |
| | | // 3.3 判断存在性:按手机号 |
| | | HrmEmployeeDO existEmployee = employeeMapper.selectByPhone(importEmployee.getPhone()); |
| | | if (existEmployee == null) { |
| | | self.createEmployee(saveReqVO); |
| | | respVO.getCreateNames().add(name); |
| | | } else { |
| | | if (!Boolean.TRUE.equals(importReqVO.getUpdateSupport())) { |
| | | respVO.getFailureNames().put(name, EMPLOYEE_PHONE_EXISTS.getMsg()); |
| | | continue; |
| | | } |
| | | saveReqVO.setId(existEmployee.getId()); |
| | | self.updateEmployee(saveReqVO); |
| | | respVO.getUpdateNames().add(name); |
| | | } |
| | | } catch (ServiceException ex) { |
| | | respVO.getFailureNames().put(name, ex.getMessage()); |
| | | } catch (Exception ex) { |
| | | respVO.getFailureNames().put(name, ex.getMessage()); |
| | | } |
| | | } |
| | | return respVO; |
| | | } |
| | | |
| | | private HrmEmployeeSaveReqVO buildImportSaveReqVO(HrmEmployeeImportExcelVO importEmployee, |
| | | Long deptId, Set<Long> postIds, Set<Long> roleIds) { |
| | | if (StrUtil.isBlank(importEmployee.getName())) { |
| | | throw exception(EMPLOYEE_IMPORT_FIELD_REQUIRED, "员工姓名"); |
| | | } |
| | | if (StrUtil.isBlank(importEmployee.getPhone())) { |
| | | throw exception(EMPLOYEE_IMPORT_FIELD_REQUIRED, "手机号"); |
| | | } |
| | | if (importEmployee.getHireDate() == null) { |
| | | throw exception(EMPLOYEE_IMPORT_FIELD_REQUIRED, "入职日期"); |
| | | } |
| | | HrmEmployeeSaveReqVO reqVO = new HrmEmployeeSaveReqVO(); |
| | | reqVO.setName(importEmployee.getName()); |
| | | reqVO.setGender(importEmployee.getGender() != null ? importEmployee.getGender() : 1); // 默认男 |
| | | reqVO.setPhone(importEmployee.getPhone()); |
| | | reqVO.setDeptId(deptId); |
| | | reqVO.setPostIds(CollUtil.isEmpty(postIds) ? null : new ArrayList<>(postIds)); |
| | | reqVO.setRoleIds(CollUtil.isEmpty(roleIds) ? null : new ArrayList<>(roleIds)); |
| | | reqVO.setHireDate(importEmployee.getHireDate()); |
| | | reqVO.setEmployeeStatus(importEmployee.getEmployeeStatus() != null |
| | | ? importEmployee.getEmployeeStatus() : 1); // 默认在职 |
| | | reqVO.setEmail(importEmployee.getEmail()); |
| | | reqVO.setBirthday(importEmployee.getBirthday()); |
| | | reqVO.setIdCard(importEmployee.getIdCard()); |
| | | reqVO.setNation(importEmployee.getNation()); |
| | | reqVO.setMarriageStatus(importEmployee.getMarriageStatus()); |
| | | reqVO.setAge(importEmployee.getAge()); |
| | | reqVO.setEducation(importEmployee.getEducation()); |
| | | reqVO.setPoliticalStatus(importEmployee.getPoliticalStatus()); |
| | | reqVO.setRemark(importEmployee.getRemark()); |
| | | return reqVO; |
| | | } |
| | | |
| | | private Long parseDeptId(String deptName, Map<String, DeptRespDTO> deptMap) { |
| | | if (StrUtil.isBlank(deptName)) { |
| | | throw exception(EMPLOYEE_IMPORT_FIELD_REQUIRED, "部门名称"); |
| | | } |
| | | DeptRespDTO dept = deptMap.get(deptName.trim()); |
| | | if (dept == null) { |
| | | throw exception(EMPLOYEE_DEPT_NAME_NOT_EXISTS, deptName.trim()); |
| | | } |
| | | return dept.getId(); |
| | | } |
| | | |
| | | private Set<Long> parsePostIds(String postNames, Map<String, PostRespDTO> postMap) { |
| | | if (StrUtil.isBlank(postNames)) { |
| | | throw exception(EMPLOYEE_IMPORT_FIELD_REQUIRED, "岗位名称"); |
| | | } |
| | | Set<Long> postIds = new LinkedHashSet<>(); |
| | | for (String postName : postNames.split("[,,]")) { |
| | | String trimmed = postName.trim(); |
| | | if (StrUtil.isBlank(trimmed)) { |
| | | continue; |
| | | } |
| | | PostRespDTO post = postMap.get(trimmed); |
| | | if (post == null) { |
| | | throw exception(EMPLOYEE_POST_NAME_NOT_EXISTS, trimmed); |
| | | } |
| | | postIds.add(post.getId()); |
| | | } |
| | | if (CollUtil.isEmpty(postIds)) { |
| | | throw exception(EMPLOYEE_IMPORT_FIELD_REQUIRED, "岗位名称"); |
| | | } |
| | | return postIds; |
| | | } |
| | | |
| | | private Set<Long> parseRoleIds(String roleNames, Map<String, RoleRespDTO> roleMap) { |
| | | if (StrUtil.isBlank(roleNames)) { |
| | | return Collections.emptySet(); |
| | | } |
| | | Set<Long> roleIds = new LinkedHashSet<>(); |
| | | for (String roleName : roleNames.split("[,,]")) { |
| | | String trimmed = roleName.trim(); |
| | | if (StrUtil.isBlank(trimmed)) { |
| | | continue; |
| | | } |
| | | RoleRespDTO role = roleMap.get(trimmed); |
| | | if (role == null) { |
| | | throw exception(EMPLOYEE_ROLE_NAME_NOT_EXISTS, trimmed); |
| | | } |
| | | roleIds.add(role.getId()); |
| | | } |
| | | return roleIds; |
| | | } |
| | | |
| | | } |