feat: 实现招聘信息异步导入功能

- 添加异步导入服务接口和实现
- 创建导入失败记录VO类
- 添加导入设计文档和测试数据生成脚本
- 支持大批量招聘数据的异步处理

Co-Authored-By: Claude Sonnet 4.5 <noreply@anthropic.com>
This commit is contained in:
wkc
2026-02-06 15:12:40 +08:00
parent d83732f07c
commit 9232a9f10f
8 changed files with 1463 additions and 0 deletions

View File

@@ -0,0 +1,33 @@
package com.ruoyi.ccdi.domain.vo;
import io.swagger.v3.oas.annotations.media.Schema;
import lombok.Data;
/**
* 招聘信息导入失败记录VO
*
* @author ruoyi
* @date 2026-02-06
*/
@Data
@Schema(description = "招聘信息导入失败记录")
public class RecruitmentImportFailureVO {
@Schema(description = "招聘项目编号")
private String recruitId;
@Schema(description = "招聘项目名称")
private String recruitName;
@Schema(description = "应聘人员姓名")
private String candName;
@Schema(description = "证件号码")
private String candId;
@Schema(description = "录用情况")
private String admitStatus;
@Schema(description = "错误信息")
private String errorMessage;
}

View File

@@ -0,0 +1,44 @@
package com.ruoyi.ccdi.service;
import com.ruoyi.ccdi.domain.excel.CcdiStaffRecruitmentExcel;
import com.ruoyi.ccdi.domain.vo.ImportStatusVO;
import com.ruoyi.ccdi.domain.vo.RecruitmentImportFailureVO;
import java.util.List;
/**
* 招聘信息异步导入Service
*
* @author ruoyi
* @date 2026-02-06
*/
public interface ICcdiStaffRecruitmentImportService {
/**
* 异步导入招聘信息数据
*
* @param excelList Excel数据列表
* @param isUpdateSupport 是否更新已存在的数据
* @param taskId 任务ID
*/
void importRecruitmentAsync(List<CcdiStaffRecruitmentExcel> excelList,
Boolean isUpdateSupport,
String taskId,
String userName);
/**
* 查询导入状态
*
* @param taskId 任务ID
* @return 导入状态信息
*/
ImportStatusVO getImportStatus(String taskId);
/**
* 获取导入失败记录
*
* @param taskId 任务ID
* @return 失败记录列表
*/
List<RecruitmentImportFailureVO> getImportFailures(String taskId);
}

View File

@@ -0,0 +1,269 @@
package com.ruoyi.ccdi.service.impl;
import com.alibaba.fastjson2.JSON;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.ruoyi.ccdi.domain.CcdiStaffRecruitment;
import com.ruoyi.ccdi.domain.excel.CcdiStaffRecruitmentExcel;
import com.ruoyi.ccdi.domain.vo.ImportResult;
import com.ruoyi.ccdi.domain.vo.ImportStatusVO;
import com.ruoyi.ccdi.domain.vo.RecruitmentImportFailureVO;
import com.ruoyi.ccdi.enums.AdmitStatus;
import com.ruoyi.ccdi.mapper.CcdiStaffRecruitmentMapper;
import com.ruoyi.ccdi.service.ICcdiStaffRecruitmentImportService;
import com.ruoyi.common.utils.IdCardUtil;
import com.ruoyi.common.utils.StringUtils;
import jakarta.annotation.Resource;
import org.springframework.beans.BeanUtils;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.scheduling.annotation.Async;
import org.springframework.scheduling.annotation.EnableAsync;
import org.springframework.stereotype.Service;
import java.util.*;
import java.util.concurrent.TimeUnit;
import java.util.stream.Collectors;
/**
* 招聘信息异步导入Service实现
*
* @author ruoyi
* @date 2026-02-06
*/
@Service
@EnableAsync
public class CcdiStaffRecruitmentImportServiceImpl implements ICcdiStaffRecruitmentImportService {
@Resource
private CcdiStaffRecruitmentMapper recruitmentMapper;
@Resource
private RedisTemplate<String, Object> redisTemplate;
@Override
@Async
public void importRecruitmentAsync(List<CcdiStaffRecruitmentExcel> excelList,
Boolean isUpdateSupport,
String taskId,
String userName) {
long startTime = System.currentTimeMillis();
// 初始化Redis状态
String statusKey = "import:recruitment:" + taskId;
Map<String, Object> statusData = new HashMap<>();
statusData.put("taskId", taskId);
statusData.put("status", "PROCESSING");
statusData.put("totalCount", excelList.size());
statusData.put("successCount", 0);
statusData.put("failureCount", 0);
statusData.put("progress", 0);
statusData.put("startTime", startTime);
statusData.put("message", "正在处理...");
redisTemplate.opsForHash().putAll(statusKey, statusData);
redisTemplate.expire(statusKey, 7, TimeUnit.DAYS);
List<CcdiStaffRecruitment> newRecords = new ArrayList<>();
List<CcdiStaffRecruitment> updateRecords = new ArrayList<>();
List<RecruitmentImportFailureVO> failures = new ArrayList<>();
// 批量查询已存在的招聘项目编号
Set<String> existingRecruitIds = getExistingRecruitIds(excelList);
// 分类数据
for (CcdiStaffRecruitmentExcel excel : excelList) {
try {
// 验证数据
validateRecruitmentData(excel, isUpdateSupport, existingRecruitIds);
CcdiStaffRecruitment recruitment = new CcdiStaffRecruitment();
BeanUtils.copyProperties(excel, recruitment);
if (existingRecruitIds.contains(excel.getRecruitId())) {
if (isUpdateSupport) {
recruitment.setUpdatedBy(userName);
updateRecords.add(recruitment);
} else {
throw new RuntimeException("该招聘项目编号已存在");
}
} else {
recruitment.setCreatedBy(userName);
recruitment.setUpdatedBy(userName);
newRecords.add(recruitment);
}
} catch (Exception e) {
RecruitmentImportFailureVO failure = new RecruitmentImportFailureVO();
BeanUtils.copyProperties(excel, failure);
failure.setErrorMessage(e.getMessage());
failures.add(failure);
}
}
// 批量插入新数据
if (!newRecords.isEmpty()) {
recruitmentMapper.insertBatch(newRecords);
}
// 批量更新已有数据
if (!updateRecords.isEmpty() && isUpdateSupport) {
recruitmentMapper.updateBatch(updateRecords);
}
// 保存失败记录到Redis
if (!failures.isEmpty()) {
String failuresKey = "import:recruitment:" + taskId + ":failures";
redisTemplate.opsForValue().set(failuresKey, failures, 7, TimeUnit.DAYS);
}
// 更新最终状态
ImportResult result = new ImportResult();
result.setTotalCount(excelList.size());
result.setSuccessCount(newRecords.size() + updateRecords.size());
result.setFailureCount(failures.size());
String finalStatus = result.getFailureCount() == 0 ? "SUCCESS" : "PARTIAL_SUCCESS";
updateImportStatus(taskId, finalStatus, result);
}
@Override
public ImportStatusVO getImportStatus(String taskId) {
String key = "import:recruitment:" + taskId;
Boolean hasKey = redisTemplate.hasKey(key);
if (Boolean.FALSE.equals(hasKey)) {
throw new RuntimeException("任务不存在或已过期");
}
Map<Object, Object> statusMap = redisTemplate.opsForHash().entries(key);
ImportStatusVO statusVO = new ImportStatusVO();
statusVO.setTaskId((String) statusMap.get("taskId"));
statusVO.setStatus((String) statusMap.get("status"));
statusVO.setTotalCount((Integer) statusMap.get("totalCount"));
statusVO.setSuccessCount((Integer) statusMap.get("successCount"));
statusVO.setFailureCount((Integer) statusMap.get("failureCount"));
statusVO.setProgress((Integer) statusMap.get("progress"));
statusVO.setStartTime((Long) statusMap.get("startTime"));
statusVO.setEndTime((Long) statusMap.get("endTime"));
statusVO.setMessage((String) statusMap.get("message"));
return statusVO;
}
@Override
public List<RecruitmentImportFailureVO> getImportFailures(String taskId) {
String key = "import:recruitment:" + taskId + ":failures";
Object failuresObj = redisTemplate.opsForValue().get(key);
if (failuresObj == null) {
return Collections.emptyList();
}
return JSON.parseArray(JSON.toJSONString(failuresObj), RecruitmentImportFailureVO.class);
}
/**
* 批量查询已存在的招聘项目编号
*/
private Set<String> getExistingRecruitIds(List<CcdiStaffRecruitmentExcel> excelList) {
List<String> recruitIds = excelList.stream()
.map(CcdiStaffRecruitmentExcel::getRecruitId)
.filter(StringUtils::isNotEmpty)
.collect(Collectors.toList());
if (recruitIds.isEmpty()) {
return Collections.emptySet();
}
LambdaQueryWrapper<CcdiStaffRecruitment> wrapper = new LambdaQueryWrapper<>();
wrapper.in(CcdiStaffRecruitment::getRecruitId, recruitIds);
List<CcdiStaffRecruitment> existingRecruitments = recruitmentMapper.selectList(wrapper);
return existingRecruitments.stream()
.map(CcdiStaffRecruitment::getRecruitId)
.collect(Collectors.toSet());
}
/**
* 验证招聘信息数据
*/
private void validateRecruitmentData(CcdiStaffRecruitmentExcel excel,
Boolean isUpdateSupport,
Set<String> existingRecruitIds) {
// 验证必填字段
if (StringUtils.isEmpty(excel.getRecruitId())) {
throw new RuntimeException("招聘项目编号不能为空");
}
if (StringUtils.isEmpty(excel.getRecruitName())) {
throw new RuntimeException("招聘项目名称不能为空");
}
if (StringUtils.isEmpty(excel.getPosName())) {
throw new RuntimeException("职位名称不能为空");
}
if (StringUtils.isEmpty(excel.getPosCategory())) {
throw new RuntimeException("职位类别不能为空");
}
if (StringUtils.isEmpty(excel.getPosDesc())) {
throw new RuntimeException("职位描述不能为空");
}
if (StringUtils.isEmpty(excel.getCandName())) {
throw new RuntimeException("应聘人员姓名不能为空");
}
if (StringUtils.isEmpty(excel.getCandEdu())) {
throw new RuntimeException("应聘人员学历不能为空");
}
if (StringUtils.isEmpty(excel.getCandId())) {
throw new RuntimeException("证件号码不能为空");
}
if (StringUtils.isEmpty(excel.getCandSchool())) {
throw new RuntimeException("应聘人员毕业院校不能为空");
}
if (StringUtils.isEmpty(excel.getCandMajor())) {
throw new RuntimeException("应聘人员专业不能为空");
}
if (StringUtils.isEmpty(excel.getCandGrad())) {
throw new RuntimeException("应聘人员毕业年月不能为空");
}
if (StringUtils.isEmpty(excel.getAdmitStatus())) {
throw new RuntimeException("录用情况不能为空");
}
// 验证证件号码格式
String idCardError = IdCardUtil.getErrorMessage(excel.getCandId());
if (idCardError != null) {
throw new RuntimeException("证件号码" + idCardError);
}
// 验证毕业年月格式(YYYYMM)
if (!excel.getCandGrad().matches("^((19|20)\\d{2})(0[1-9]|1[0-2])$")) {
throw new RuntimeException("毕业年月格式不正确,应为YYYYMM");
}
// 验证录用状态
if (AdmitStatus.getDescByCode(excel.getAdmitStatus()) == null) {
throw new RuntimeException("录用情况只能填写'录用'、'未录用'或'放弃'");
}
}
/**
* 更新导入状态
*/
private void updateImportStatus(String taskId, String status, ImportResult result) {
String key = "import:recruitment:" + taskId;
Map<String, Object> statusData = new HashMap<>();
statusData.put("status", status);
statusData.put("totalCount", result.getTotalCount());
statusData.put("successCount", result.getSuccessCount());
statusData.put("failureCount", result.getFailureCount());
statusData.put("progress", 100);
statusData.put("endTime", System.currentTimeMillis());
if ("SUCCESS".equals(status)) {
statusData.put("message", "全部成功!共导入" + result.getTotalCount() + "条数据");
} else {
statusData.put("message", "成功" + result.getSuccessCount() + "条,失败" + result.getFailureCount() + "");
}
redisTemplate.opsForHash().putAll(key, statusData);
}
}