|
@@ -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("同步人员信息成功");
|
|
|
}
|
|
|
|
|
|
/**
|