| | |
| | | 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; |
| | |
| | | // 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(","))); |
| | | } |
| | | } |
| | | // 填充角色名称 |
| | |
| | | try { |
| | | // 3.1 名称转 ID |
| | | Long deptId = parseDeptId(importEmployee.getDeptName(), deptMap); |
| | | Long postId = parsePostId(importEmployee.getPostName(), postMap); |
| | | Set<Long> postIds = parsePostIds(importEmployee.getPostName(), postMap); |
| | | Set<Long> roleIds = parseRoleIds(importEmployee.getRoleNames(), roleMap); |
| | | // 3.2 构建保存请求 |
| | | HrmEmployeeSaveReqVO saveReqVO = buildImportSaveReqVO(importEmployee, deptId, postId, roleIds); |
| | | HrmEmployeeSaveReqVO saveReqVO = buildImportSaveReqVO(importEmployee, deptId, postIds, roleIds); |
| | | // 3.3 判断存在性:按手机号 |
| | | HrmEmployeeDO existEmployee = employeeMapper.selectByPhone(importEmployee.getPhone()); |
| | | if (existEmployee == null) { |
| | |
| | | } |
| | | |
| | | private HrmEmployeeSaveReqVO buildImportSaveReqVO(HrmEmployeeImportExcelVO importEmployee, |
| | | Long deptId, Long postId, Set<Long> roleIds) { |
| | | Long deptId, Set<Long> postIds, Set<Long> roleIds) { |
| | | if (StrUtil.isBlank(importEmployee.getName())) { |
| | | throw exception(EMPLOYEE_IMPORT_FIELD_REQUIRED, "员工姓名"); |
| | | } |
| | |
| | | reqVO.setGender(importEmployee.getGender() != null ? importEmployee.getGender() : 1); // 默认男 |
| | | reqVO.setPhone(importEmployee.getPhone()); |
| | | reqVO.setDeptId(deptId); |
| | | reqVO.setPostId(postId); |
| | | 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 |
| | |
| | | return dept.getId(); |
| | | } |
| | | |
| | | private Long parsePostId(String postName, Map<String, PostRespDTO> postMap) { |
| | | if (StrUtil.isBlank(postName)) { |
| | | private Set<Long> parsePostIds(String postNames, Map<String, PostRespDTO> postMap) { |
| | | if (StrUtil.isBlank(postNames)) { |
| | | throw exception(EMPLOYEE_IMPORT_FIELD_REQUIRED, "岗位名称"); |
| | | } |
| | | PostRespDTO post = postMap.get(postName.trim()); |
| | | if (post == null) { |
| | | throw exception(EMPLOYEE_POST_NAME_NOT_EXISTS, postName.trim()); |
| | | 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()); |
| | | } |
| | | return post.getId(); |
| | | if (CollUtil.isEmpty(postIds)) { |
| | | throw exception(EMPLOYEE_IMPORT_FIELD_REQUIRED, "岗位名称"); |
| | | } |
| | | return postIds; |
| | | } |
| | | |
| | | private Set<Long> parseRoleIds(String roleNames, Map<String, RoleRespDTO> roleMap) { |