除员工外 取消导入更新 添加导入文件重复校验

This commit is contained in:
wkc
2026-02-09 09:10:35 +08:00
parent 886176ed7e
commit 8efbd43abd
21 changed files with 4231 additions and 32 deletions

View File

@@ -45,8 +45,13 @@ public class CcdiEmployeeImportServiceImpl implements ICcdiEmployeeImportService
List<CcdiEmployee> updateRecords = new ArrayList<>();
List<ImportFailureVO> failures = new ArrayList<>();
// 批量查询已存在的柜员号
// 批量查询已存在的柜员号和身份证号
Set<Long> existingIds = getExistingEmployeeIds(excelList);
Set<String> existingIdCards = getExistingIdCards(excelList);
// 用于跟踪Excel文件内已处理的主键
Set<Long> processedEmployeeIds = new HashSet<>();
Set<String> processedIdCards = new HashSet<>();
// 分类数据
for (int i = 0; i < excelList.size(); i++) {
@@ -58,21 +63,43 @@ public class CcdiEmployeeImportServiceImpl implements ICcdiEmployeeImportService
BeanUtils.copyProperties(excel, addDTO);
// 验证数据(支持更新模式)
validateEmployeeData(addDTO, isUpdateSupport, existingIds);
validateEmployeeData(addDTO, isUpdateSupport, existingIds, existingIdCards);
CcdiEmployee employee = new CcdiEmployee();
BeanUtils.copyProperties(excel, employee);
// 统一检查Excel内重复更新和新增两个分支都需要检查
if (processedEmployeeIds.contains(excel.getEmployeeId())) {
throw new RuntimeException(String.format("柜员号[%d]在导入文件中重复,已跳过此条记录", excel.getEmployeeId()));
}
if (StringUtils.isNotEmpty(excel.getIdCard()) &&
processedIdCards.contains(excel.getIdCard())) {
throw new RuntimeException(String.format("身份证号[%s]在导入文件中重复,已跳过此条记录", excel.getIdCard()));
}
// 检查柜员号是否在数据库中已存在
if (existingIds.contains(excel.getEmployeeId())) {
if (isUpdateSupport) {
updateRecords.add(employee);
} else {
// 柜员号已存在于数据库
if (!isUpdateSupport) {
throw new RuntimeException("柜员号已存在且未启用更新支持");
}
// 通过检查,添加到更新列表
updateRecords.add(employee);
} else {
// 柜员号不存在,添加到新增列表
newRecords.add(employee);
}
// 统一标记为已处理(只有成功添加到列表后才会执行到这里)
if (excel.getEmployeeId() != null) {
processedEmployeeIds.add(excel.getEmployeeId());
}
if (StringUtils.isNotEmpty(excel.getIdCard())) {
processedIdCards.add(excel.getIdCard());
}
} catch (Exception e) {
ImportFailureVO failure = new ImportFailureVO();
@@ -197,6 +224,29 @@ public class CcdiEmployeeImportServiceImpl implements ICcdiEmployeeImportService
.collect(Collectors.toSet());
}
/**
* 批量查询数据库中已存在的身份证号
* @param excelList Excel数据列表
* @return 已存在的身份证号集合
*/
private Set<String> getExistingIdCards(List<CcdiEmployeeExcel> excelList) {
List<String> idCards = excelList.stream()
.map(CcdiEmployeeExcel::getIdCard)
.filter(StringUtils::isNotEmpty)
.collect(Collectors.toList());
if (idCards.isEmpty()) {
return Collections.emptySet();
}
LambdaQueryWrapper<CcdiEmployee> wrapper = new LambdaQueryWrapper<>();
wrapper.in(CcdiEmployee::getIdCard, idCards);
List<CcdiEmployee> existingEmployees = employeeMapper.selectList(wrapper);
return existingEmployees.stream()
.map(CcdiEmployee::getIdCard)
.collect(Collectors.toSet());
}
/**
* 批量保存
@@ -216,8 +266,9 @@ public class CcdiEmployeeImportServiceImpl implements ICcdiEmployeeImportService
* @param addDTO 新增DTO
* @param isUpdateSupport 是否支持更新
* @param existingIds 已存在的员工ID集合(导入场景使用,传null表示单条新增)
* @param existingIdCards 已存在的身份证号集合(导入场景使用,传null表示单条新增)
*/
public void validateEmployeeData(CcdiEmployeeAddDTO addDTO, Boolean isUpdateSupport, Set<Long> existingIds) {
public void validateEmployeeData(CcdiEmployeeAddDTO addDTO, Boolean isUpdateSupport, Set<Long> existingIds, Set<String> existingIdCards) {
// 验证必填字段
if (StringUtils.isEmpty(addDTO.getName())) {
throw new RuntimeException("姓名不能为空");
@@ -260,10 +311,8 @@ public class CcdiEmployeeImportServiceImpl implements ICcdiEmployeeImportService
} else {
// 导入场景:如果柜员号不存在,才检查身份证号唯一性
if (!existingIds.contains(addDTO.getEmployeeId())) {
// 检查身份证号唯一性
LambdaQueryWrapper<CcdiEmployee> wrapper = new LambdaQueryWrapper<>();
wrapper.eq(CcdiEmployee::getIdCard, addDTO.getIdCard());
if (employeeMapper.selectCount(wrapper) > 0) {
// 使用批量查询的结果检查身份证号唯一性
if (existingIdCards != null && existingIdCards.contains(addDTO.getIdCard())) {
throw new RuntimeException("该身份证号已存在");
}
}

View File

@@ -1,13 +1,12 @@
package com.ruoyi.ccdi.service.impl;
import com.alibaba.fastjson2.JSON;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.ruoyi.ccdi.domain.CcdiPurchaseTransaction;
import com.ruoyi.ccdi.domain.dto.CcdiPurchaseTransactionAddDTO;
import com.ruoyi.ccdi.domain.excel.CcdiPurchaseTransactionExcel;
import com.ruoyi.ccdi.domain.vo.PurchaseTransactionImportFailureVO;
import com.ruoyi.ccdi.domain.vo.ImportResult;
import com.ruoyi.ccdi.domain.vo.ImportStatusVO;
import com.ruoyi.ccdi.domain.vo.PurchaseTransactionImportFailureVO;
import com.ruoyi.ccdi.mapper.CcdiPurchaseTransactionMapper;
import com.ruoyi.ccdi.service.ICcdiPurchaseTransactionImportService;
import com.ruoyi.common.utils.StringUtils;
@@ -50,6 +49,9 @@ public class CcdiPurchaseTransactionImportServiceImpl implements ICcdiPurchaseTr
// 批量查询已存在的采购事项ID
Set<String> existingIds = getExistingPurchaseIds(excelList);
// 用于跟踪Excel文件内已处理的采购事项ID
Set<String> processedIds = new HashSet<>();
// 分类数据
for (int i = 0; i < excelList.size(); i++) {
CcdiPurchaseTransactionExcel excel = excelList.get(i);
@@ -68,10 +70,14 @@ public class CcdiPurchaseTransactionImportServiceImpl implements ICcdiPurchaseTr
if (existingIds.contains(excel.getPurchaseId())) {
// 采购事项ID已存在直接报错
throw new RuntimeException(String.format("采购事项ID[%s]已存在,请勿重复导入", excel.getPurchaseId()));
} else if (processedIds.contains(excel.getPurchaseId())) {
// Excel文件内部重复
throw new RuntimeException(String.format("采购事项ID[%s]在导入文件中重复,已跳过此条记录", excel.getPurchaseId()));
} else {
transaction.setCreatedBy(userName);
transaction.setUpdatedBy(userName);
newRecords.add(transaction);
processedIds.add(excel.getPurchaseId()); // 标记为已处理
}
} catch (Exception e) {