|
|
|
@ -26,51 +26,49 @@ public class AsyncTeacherService {
|
|
|
|
|
@Transactional(rollbackFor = Exception.class)
|
|
|
|
|
public void asyncSavePtTeacherInfos(List<GpTeacherVo> teachersList) {
|
|
|
|
|
try {
|
|
|
|
|
if (teachersList.isEmpty()) {
|
|
|
|
|
return; // 处理空输入,避免后续NPE
|
|
|
|
|
}
|
|
|
|
|
String schoolCode = teachersList.get(0).getCode();
|
|
|
|
|
|
|
|
|
|
// 加载现有教师数据(假设已按学校码过滤)
|
|
|
|
|
Map<String, PtTeacherInfo> existingTeachers = loadExistingTeachers(teachersList);
|
|
|
|
|
// 查询该学校的所有现有教师
|
|
|
|
|
String code = String.format(PK.PTTEACHER, schoolCode);
|
|
|
|
|
List<PtTeacherInfo> allExistingInSchool = ptTeacherRepository.findByCode(code);
|
|
|
|
|
|
|
|
|
|
// 收集传入列表中的教师ID
|
|
|
|
|
// 收集传入教师ID集合
|
|
|
|
|
Set<String> incomingIds = teachersList.stream()
|
|
|
|
|
.map(GpTeacherVo::getId)
|
|
|
|
|
.filter(Objects::nonNull)
|
|
|
|
|
.collect(Collectors.toSet());
|
|
|
|
|
|
|
|
|
|
// 找出需要停用的教师(存在于数据库但不在传入列表中)
|
|
|
|
|
List<PtTeacherInfo> toDeactivate = allExistingInSchool.stream()
|
|
|
|
|
.filter(pt -> pt.getId() != null && !incomingIds.contains(pt.getId()))
|
|
|
|
|
// 直接使用existingTeachers获取待停用教师
|
|
|
|
|
List<PtTeacherInfo> toDeactivate = existingTeachers.values().stream()
|
|
|
|
|
.filter(pt -> !incomingIds.contains(pt.getId()) && pt.getId() != null)
|
|
|
|
|
.filter(pt -> !"pt".equals(pt.getSource())) // 提前过滤无需处理的记录
|
|
|
|
|
.peek(pt -> {
|
|
|
|
|
pt.setState(2);
|
|
|
|
|
pt.setVersion(pt.getVersion() + 1);
|
|
|
|
|
})
|
|
|
|
|
.collect(Collectors.toList());
|
|
|
|
|
|
|
|
|
|
// 更新停用教师的状态和版本 根据属性source等于pt 无需改变状态和版本
|
|
|
|
|
toDeactivate.removeIf(pt -> pt.getSource().equals("pt"));
|
|
|
|
|
toDeactivate.forEach(pt -> {
|
|
|
|
|
{
|
|
|
|
|
pt.setState(2);
|
|
|
|
|
pt.setVersion(pt.getVersion() + 1);
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
// 保存停用教师到数据库并更新缓存
|
|
|
|
|
// 批量停用
|
|
|
|
|
if (!toDeactivate.isEmpty()) {
|
|
|
|
|
ptTeacherRepository.saveAll(toDeactivate);
|
|
|
|
|
//toDeactivate.forEach(pt -> PT_TEACHER_CACHE.put(pt.getCode(), pt));
|
|
|
|
|
// toDeactivate.forEach(pt -> PT_TEACHER_CACHE.put(pt.getCode(), pt));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 处理更新(合并过滤与转换操作)
|
|
|
|
|
List<PtTeacherInfo> updates = teachersList.stream()
|
|
|
|
|
.filter(t -> t.getId() != null)
|
|
|
|
|
.map(t -> convertToPtTeacherInfo(t, existingTeachers))
|
|
|
|
|
.filter(Objects::nonNull) // 过滤掉未变更的记录
|
|
|
|
|
.filter(Objects::nonNull)
|
|
|
|
|
.collect(Collectors.toList());
|
|
|
|
|
|
|
|
|
|
if (!updates.isEmpty()) {
|
|
|
|
|
//ptTeacherRepository.saveAll(updates);
|
|
|
|
|
//updates.forEach(pt -> PT_TEACHER_CACHE.put(pt.getCode(), pt));
|
|
|
|
|
ptTeacherRepository.saveAll(updates);
|
|
|
|
|
// updates.forEach(pt -> PT_TEACHER_CACHE.put(pt.getCode(), pt));
|
|
|
|
|
}
|
|
|
|
|
} catch (Exception e) {
|
|
|
|
|
log.error("Failed to save teacher info asynchronously", e);
|
|
|
|
|
log.error("Async save teacher info failed", e);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|