| | |
| | | import cn.iocoder.yudao.framework.common.pojo.PageResult; |
| | | import cn.iocoder.yudao.framework.common.util.object.BeanUtils; |
| | | import cn.iocoder.yudao.framework.excel.core.util.ExcelUtils; |
| | | 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 org.springframework.web.bind.annotation.*; |
| | | |
| | | import java.io.IOException; |
| | | import java.time.LocalDate; |
| | | import java.util.*; |
| | | import java.util.stream.Collectors; |
| | | |
| | |
| | | Map<Long, DeptRespDTO> deptMap = CollUtil.isEmpty(deptIds) ? Collections.emptyMap() : deptApi.getDeptMap(deptIds); |
| | | |
| | | // 2. 批量获取岗位信息 |
| | | Set<Long> postIds = employeeList.stream() |
| | | .map(HrmEmployeeDO::getPostId) |
| | | .filter(Objects::nonNull) |
| | | .collect(Collectors.toSet()); |
| | | Set<Long> postIds = new HashSet<>(); |
| | | for (HrmEmployeeDO employee : employeeList) { |
| | | if (StrUtil.isNotBlank(employee.getPostIds())) { |
| | | postIds.addAll(JSONUtil.toList(employee.getPostIds(), Long.class)); |
| | | } |
| | | } |
| | | Map<Long, PostRespDTO> postMap = CollUtil.isEmpty(postIds) ? Collections.emptyMap() : postApi.getPostMap(postIds); |
| | | |
| | | // 3. 收集所有角色ID |
| | |
| | | respVO.setDeptName(deptMap.get(employee.getDeptId()).getName()); |
| | | } |
| | | |
| | | // 填充岗位名称 |
| | | if (employee.getPostId() != null && postMap.containsKey(employee.getPostId())) { |
| | | respVO.setPostName(postMap.get(employee.getPostId()).getName()); |
| | | // 填充岗位信息 |
| | | if (StrUtil.isNotBlank(employee.getPostIds())) { |
| | | List<Long> employeePostIds = JSONUtil.toList(employee.getPostIds(), Long.class); |
| | | respVO.setPostIds(employeePostIds); |
| | | if (CollUtil.isNotEmpty(employeePostIds)) { |
| | | respVO.setPostNames(employeePostIds.stream() |
| | | .filter(postMap::containsKey) |
| | | .map(id -> postMap.get(id).getName()) |
| | | .collect(Collectors.joining(","))); |
| | | } |
| | | } |
| | | |
| | | // 解析 roleIds 并填充角色名称 |
| | |
| | | ExcelUtils.write(response, "员工信息.xls", "数据", HrmEmployeeRespVO.class, list); |
| | | } |
| | | |
| | | @GetMapping("/import-template") |
| | | @Operation(summary = "下载员工导入模板") |
| | | @PreAuthorize("@ss.hasPermission('hrm:employee:import')") |
| | | public void importTemplate(HttpServletResponse response) throws IOException { |
| | | // 示例的部门/岗位/角色取当前系统真实数据,避免照抄 demo 里的「研发部」导致匹配失败 |
| | | String sampleDept = "请填写部门名称"; |
| | | try { |
| | | var depts = deptApi.getDeptList(); |
| | | if (depts != null && !depts.isEmpty()) { |
| | | sampleDept = depts.get(0).getName(); |
| | | } |
| | | } catch (Exception ignored) { |
| | | // 取不到就保留占位文案,不影响模板下载 |
| | | } |
| | | // 岗位未提供简易列表 API,示例用占位文案 |
| | | String samplePost = "请填写岗位名称(多个用英文逗号分隔)"; |
| | | |
| | | List<HrmEmployeeImportExcelVO> list = Arrays.asList( |
| | | HrmEmployeeImportExcelVO.builder().name("张三").gender(1).phone("13800138000") |
| | | .deptName(sampleDept).postName(samplePost).roleNames("普通角色") |
| | | .hireDate(LocalDate.of(2020, 3, 1)).employeeStatus(1) |
| | | .idCard("320102199001150011").birthday(LocalDate.of(1990, 1, 15)) |
| | | .nation("汉族").marriageStatus("已婚").age(36).education("本科").politicalStatus("中共党员") |
| | | .email("zhangsan@example.com").remark("示例数据,导入前请替换").build() |
| | | ); |
| | | ExcelUtils.writeTemplate(response, "员工导入模板.xls", "员工列表", HrmEmployeeImportExcelVO.class, list); |
| | | } |
| | | |
| | | @PostMapping("/import") |
| | | @Operation(summary = "导入员工") |
| | | @PreAuthorize("@ss.hasPermission('hrm:employee:import')") |
| | | public CommonResult<HrmEmployeeImportRespVO> importExcel(@Valid HrmEmployeeImportReqVO importReqVO) |
| | | throws Exception { |
| | | List<HrmEmployeeImportExcelVO> list = ExcelUtils.read(importReqVO.getFile(), HrmEmployeeImportExcelVO.class); |
| | | return success(employeeService.importEmployeeList(list, importReqVO)); |
| | | } |
| | | |
| | | } |