|
|
|
|
package cn.teammodel.test;
|
|
|
|
|
|
|
|
|
|
import cn.teammodel.model.dto.admin.teacher.IdCodePair;
|
|
|
|
|
import cn.teammodel.model.entity.teacher.PtTeacherInfo;
|
|
|
|
|
import cn.teammodel.model.vo.admin.GpTeacherVo;
|
|
|
|
|
import cn.teammodel.repository.PtTeacherRepository;
|
|
|
|
|
import com.azure.cosmos.implementation.guava25.collect.Lists;
|
|
|
|
|
import com.itextpdf.text.log.Logger;
|
|
|
|
|
import com.itextpdf.text.log.LoggerFactory;
|
|
|
|
|
import org.apache.commons.lang3.tuple.Pair;
|
|
|
|
|
import org.springframework.beans.BeanUtils;
|
|
|
|
|
import org.springframework.beans.factory.annotation.Autowired;
|
|
|
|
|
import org.springframework.core.env.Environment;
|
|
|
|
|
import org.springframework.scheduling.annotation.Async;
|
|
|
|
|
import org.springframework.stereotype.Service;
|
|
|
|
|
import org.springframework.transaction.annotation.Transactional;
|
|
|
|
|
|
|
|
|
|
import java.util.Collections;
|
|
|
|
|
import java.util.List;
|
|
|
|
|
import java.util.Map;
|
|
|
|
|
import java.util.Objects;
|
|
|
|
|
import java.util.function.Function;
|
|
|
|
|
import java.util.stream.Collectors;
|
|
|
|
|
|
|
|
|
|
import static cn.teammodel.controller.admin.service.impl.TeacherServiceImpl.PT_TEACHER_CACHE;
|
|
|
|
|
|
|
|
|
|
@Service
|
|
|
|
|
public class AsyncTeacherService {
|
|
|
|
|
private static final Logger log = LoggerFactory.getLogger(AsyncTeacherService.class);
|
|
|
|
|
|
|
|
|
|
@Autowired
|
|
|
|
|
private PtTeacherRepository ptTeacherRepository;
|
|
|
|
|
|
|
|
|
|
@Transactional(rollbackFor = Exception.class)
|
|
|
|
|
public void asyncSavePtTeacherInfos(List<GpTeacherVo> teachersList) {
|
|
|
|
|
try {
|
|
|
|
|
Map<String, PtTeacherInfo> existingTeachers = loadExistingTeachers(teachersList);
|
|
|
|
|
|
|
|
|
|
List<PtTeacherInfo> updates = teachersList.stream()
|
|
|
|
|
.filter(t -> t.getId() != null)
|
|
|
|
|
.map(t -> convertToPtTeacherInfo(t, existingTeachers))
|
|
|
|
|
.filter(Objects::nonNull) // 过滤掉未变更的记录
|
|
|
|
|
.collect(Collectors.toList());
|
|
|
|
|
|
|
|
|
|
if (!updates.isEmpty()) {
|
|
|
|
|
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);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private Map<String, PtTeacherInfo> loadExistingTeachers(List<GpTeacherVo> teachersList) {
|
|
|
|
|
List<IdCodePair> idAndCode = teachersList.stream()
|
|
|
|
|
.filter(t -> t.getId() != null)
|
|
|
|
|
.map(t -> new IdCodePair(t.getId(), t.getCode() + t.getId()))
|
|
|
|
|
.collect(Collectors.toList());
|
|
|
|
|
|
|
|
|
|
List<PtTeacherInfo> existingList = ptTeacherRepository.findByIdCodePairs(idAndCode);
|
|
|
|
|
|
|
|
|
|
return existingList.stream()
|
|
|
|
|
.collect(Collectors.toMap(
|
|
|
|
|
PtTeacherInfo::getCode,
|
|
|
|
|
Function.identity()
|
|
|
|
|
));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private PtTeacherInfo convertToPtTeacherInfo(GpTeacherVo teacher, Map<String, PtTeacherInfo> existingTeachers) {
|
|
|
|
|
|
|
|
|
|
String code = teacher.getCode() +"-"+ teacher.getId();
|
|
|
|
|
PtTeacherInfo existing = existingTeachers.get(code);
|
|
|
|
|
// 如果数据库无记录,直接创建新对象
|
|
|
|
|
if (existing == null) {
|
|
|
|
|
return createNewPtTeacherInfo(teacher, code);
|
|
|
|
|
}
|
|
|
|
|
// 检查字段是否有变化
|
|
|
|
|
boolean isChanged = !Objects.equals(existing.getIdentity(), teacher.getIdentity());
|
|
|
|
|
|
|
|
|
|
// 无变化则返回null,表示无需更新
|
|
|
|
|
if (!isChanged) {
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
PtTeacherInfo updated = new PtTeacherInfo();
|
|
|
|
|
BeanUtils.copyProperties(existing, updated);
|
|
|
|
|
updated.setVersion(getNextVersion(updated.getId(), updated.getCode()));
|
|
|
|
|
return updated;
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private Integer getNextVersion(String id,String code) {
|
|
|
|
|
PtTeacherInfo existingPtTeacherInfo = ptTeacherRepository.findByIdAndCode(id,code);
|
|
|
|
|
if (existingPtTeacherInfo != null) {
|
|
|
|
|
return existingPtTeacherInfo.getVersion() + 1;
|
|
|
|
|
}
|
|
|
|
|
return 1;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private PtTeacherInfo createNewPtTeacherInfo(GpTeacherVo teacher, String code) {
|
|
|
|
|
PtTeacherInfo ptTeacherInfo = new PtTeacherInfo();
|
|
|
|
|
|
|
|
|
|
// 固定值
|
|
|
|
|
ptTeacherInfo.setPk("PtTeacher");
|
|
|
|
|
ptTeacherInfo.setTtl(-1);
|
|
|
|
|
ptTeacherInfo.setSchool(teacher.getCode());
|
|
|
|
|
|
|
|
|
|
// 基础信息映射
|
|
|
|
|
ptTeacherInfo.setId(teacher.getId());
|
|
|
|
|
ptTeacherInfo.setCode(code);
|
|
|
|
|
ptTeacherInfo.setIdentity(teacher.getIdentity());
|
|
|
|
|
|
|
|
|
|
// 版本号初始化
|
|
|
|
|
ptTeacherInfo.setVersion(1);
|
|
|
|
|
|
|
|
|
|
return ptTeacherInfo;
|
|
|
|
|
}
|
|
|
|
|
}
|