Przeglądaj źródła

refactor(qhse): 优化学习文件人员同步逻辑

- 修改 StudyFileController 中 syncPerson 方法返回类型为 R
- 更新 StudyFileService 接口,将 syncPerson 方法返回类型改为 R
-重构 StudyFileServiceImpl 中的 syncPerson 方法:
  - 增加日志记录
  - 优化历史人员信息的处理逻辑 - 实现删除多余历史记录和新增缺失记录
  - 提高查询效率使用 Set 集合
  - 返回统一的 R 结果对象
lg 2 dni temu
rodzic
commit
3f53cf7e19

+ 1 - 1
qhse-common/src/main/java/com/rongwei/bscommon/system/service/StudyFileService.java

@@ -20,7 +20,7 @@ public interface StudyFileService {
      * @param dto 同步参数
      * @return 同步结果
      */
-    boolean syncPerson(StudyFileProduceHistoryDto dto);
+    R syncPerson(StudyFileProduceHistoryDto dto);
 
     /**
      * 取消流程,恢复历史数据

+ 41 - 6
qhse-common/src/main/java/com/rongwei/bscommon/system/service/impl/StudyFileServiceImpl.java

@@ -24,6 +24,7 @@ import org.springframework.transaction.annotation.Transactional;
 import java.util.Date;
 import java.util.List;
 import java.util.Objects;
+import java.util.Set;
 import java.util.stream.Collectors;
 
 @Service
@@ -83,23 +84,57 @@ public class StudyFileServiceImpl implements StudyFileService {
      */
     @Override
     @Transactional
-    public boolean syncPerson(StudyFileProduceHistoryDto dto) {
+    public R syncPerson(StudyFileProduceHistoryDto dto) {
+        logger.info("开始同步人员信息,文件ID: {}", dto.getId());
+
+        // 获取当前用户信息
         SysUserVo currentUser = QHSEUtils.getCurrentUser();
         // 查询当前文件的所有人员信息
         List<StudyFilePersonDo> currentPersons = studyFilePersonService.selectListByStudyFileId(dto.getId());
         // 获取最新历史文件
         StudyFileRecordDo historyFile = studyFileRecordService.selectLatestRecordByMainId(dto.getId());
-        // 删除人员历史记录
-        studyFilePersonRecordService.deleteByHistoryId(historyFile.getId());
-        // 转换为历史记录并收集需要删除的ID
+
+        if (historyFile == null) {
+            logger.info("文件:{},没有找到对应历史文件", dto.getId());
+            return R.ok("无需同步历史人员");
+        }
+
+        // 历史人员信息
+        List<StudyFilePersonRecordDo> oldHistoryPersons = studyFilePersonRecordService
+                .selectListByHistoryId(historyFile.getId());
+
+        // Set 加速查询
+        Set<String> currentPersonIds = currentPersons.stream()
+                .map(StudyFilePersonDo::getId)
+                .collect(Collectors.toSet());
+        Set<String> historyPersonIds = oldHistoryPersons.stream()
+                .map(StudyFilePersonRecordDo::getMainId)
+                .collect(Collectors.toSet());
+
+        // 删除多余的历史人员信息
+        List<String> deletePersonIds = oldHistoryPersons.stream()
+                .filter(person -> !currentPersonIds.contains(person.getMainId()))
+                .map(StudyFilePersonRecordDo::getId)
+                .collect(Collectors.toList());
+
+        if (!deletePersonIds.isEmpty()) {
+            studyFilePersonRecordService.removeByIds(deletePersonIds);
+        }
+        logger.info("删除{}条多余历史记录", deletePersonIds.size());
+
+        // 新增人员历史记录
         List<StudyFilePersonRecordDo> historyPersons = currentPersons.stream()
+                .filter(person -> !historyPersonIds.contains(person.getId()))
                 .map(person -> convertToHistoryPersonRecord(person, historyFile.getId(), currentUser))
                 .collect(Collectors.toList());
 
         // 批量保存历史记录
-        studyFilePersonRecordService.saveBatch(historyPersons);
+        if (!historyPersons.isEmpty()) {
+            studyFilePersonRecordService.saveBatch(historyPersons);
+        }
+        logger.info("新增{}条历史记录", historyPersons.size());
 
-        return true;
+        return R.ok("同步人员信息成功");
     }
 
     /**

+ 1 - 1
qhse-server/src/main/java/com/rongwei/controller/StudyFileController.java

@@ -42,7 +42,7 @@ public class StudyFileController {
         if (StringUtils.isBlank(dto.getId())) {
             return R.error("请选择要同步的学习文件!");
         }
-        return studyFileService.syncPerson(dto) ? R.ok() : R.error("同步人员失败!");
+        return studyFileService.syncPerson(dto);
     }
 
     /**