update 优化性能

develop
hhb@hotmail.com 3 months ago
parent d03ab5dc90
commit 81a79cdc6a

@ -365,7 +365,7 @@ public class TeacherServiceImpl implements TeacherService {
@Override
public Map<Long, Integer> getTeacherByRecord(TeacherDto teacherDto) {
final int SLICE_SIZE = 100;
final int SLICE_SIZE = 10000;
// 获取学期起止时间
List<School.Semester> semesters = schoolRepository.findSemestersById(teacherDto.getCode(), teacherDto.getPeriodId());
SchoolDateUtil.semesterModel semesterModel = SchoolDateUtil.getSemesterByNow(semesters, LocalDate.now());
@ -385,14 +385,14 @@ public class TeacherServiceImpl implements TeacherService {
CosmosPageRequest pageRequest = new CosmosPageRequest(0, SLICE_SIZE, null);
Slice<LessonRecord> slice;
Map<Long, Integer> countByWeek = SchoolDateUtil.createEmptyWeekMap(totalWeek);
String lessonRecordKey = String.format(PK.PK_LESSON_RECORD, teacherDto.getCode());
do {
if(StringUtils.isNotEmpty(teacherDto.getGrade())) {
slice = lessonRecordRepository.findAllByGrade(String.format(PK.PK_LESSON_RECORD, teacherDto.getCode()), teacherDto.getGrade(),teacherDto.getSubjectId(),teacherDto.getPeriodId(),pageRequest);
slice = lessonRecordRepository.findAllByGrade(lessonRecordKey, teacherDto.getGrade(),teacherDto.getSubjectId(),teacherDto.getPeriodId(),pageRequest);
} else if(StringUtils.isNotEmpty(teacherDto.getTmdId())) {
slice = lessonRecordRepository.findAllByAcademicYearId(String.format(PK.PK_LESSON_RECORD, teacherDto.getCode()), teacherDto.getTmdId(),teacherDto.getSubjectId(),teacherDto.getPeriodId(),pageRequest);
slice = lessonRecordRepository.findAllByAcademicYearId(lessonRecordKey, teacherDto.getTmdId(),teacherDto.getSubjectId(),teacherDto.getPeriodId(),pageRequest);
} else {
slice = lessonRecordRepository.findAll(String.format(PK.PK_LESSON_RECORD, teacherDto.getCode()), teacherDto.getSubjectId(),teacherDto.getPeriodId(), pageRequest);
slice = lessonRecordRepository.findAll(lessonRecordKey, teacherDto.getSubjectId(),teacherDto.getPeriodId(), pageRequest);
}
List<LessonRecord> content = slice.getContent();
if (ObjectUtils.isEmpty(content)) {
@ -401,15 +401,14 @@ public class TeacherServiceImpl implements TeacherService {
// 分批次计算
for (LessonRecord item : content) {
// 处理每周的课程数
long weekNum = calculateWeekNum(startDatetime, endDatetime, item.getStartTime());
countByWeek.put(weekNum, countByWeek.getOrDefault(weekNum, 0) + 1);
countByWeek.merge(weekNum, 1, Integer::sum); // 使用 merge 方法简化代码
}
if (slice.hasNext()) {
pageRequest = (CosmosPageRequest) slice.nextPageable();
}
} while (slice.hasNext());
} while (slice.hasNext() && !Thread.currentThread().isInterrupted());
countByWeek.entrySet().removeIf(entry -> entry.getKey() == -1);
return countByWeek;
}

@ -12,7 +12,7 @@ import java.util.List;
@Repository
public interface LessonRecordRepository extends CosmosRepository<LessonRecord, String> {
@Query("select * from LessonRecord as c where " +
@Query("select c.id from LessonRecord as c where " +
"c.code = @code and " +
"(IS_NULL(@startTime) or c.startTime >= @startTime) and " +
"(IS_NULL(@endTime) or c.startTime <= @endTime) and "+
@ -24,13 +24,13 @@ public interface LessonRecordRepository extends CosmosRepository<LessonRecord, S
List<LessonRecord> getLessonsByConditions(String code, Long startTime, Long endTime,String subjectId,String tmdId,String grade,String periodId);
@Query("select * from LessonRecord as c where c.code = @code and c.expire<=0 and c.status<>404 and c.tmdid = @tmdId and c.periodId = @periodId and" +
@Query("select c.startTime from LessonRecord as c where c.code = @code and c.expire<=0 and c.status<>404 and c.tmdid = @tmdId and c.periodId = @periodId and" +
"(IS_NULL(@subjectId) or c.subjectId = @subjectId) ")
Slice<LessonRecord> findAllByAcademicYearId(String code, String tmdId,String subjectId,String periodId,Pageable pageable);
@Query("select * from LessonRecord as c where c.code = @code and c.expire<=0 and c.status<>404 and array_contains(c.grade,@grade) and c.periodId = @periodId and" +
@Query("select c.startTime from LessonRecord as c where c.code = @code and c.expire<=0 and c.status<>404 and array_contains(c.grade,@grade) and c.periodId = @periodId and" +
"(IS_NULL(@subjectId) or c.subjectId = @subjectId) ")
Slice<LessonRecord> findAllByGrade(String code, String grade,String subjectId,String periodId,Pageable pageable);
@Query("select * from LessonRecord as c where c.code = @code and c.expire<=0 and c.status<>404 and c.periodId = @periodId and" +
@Query("select c.startTime from LessonRecord as c where c.code = @code and c.expire<=0 and c.status<>404 and c.periodId = @periodId and" +
"(IS_NULL(@subjectId) or c.subjectId = @subjectId) ")
Slice<LessonRecord> findAll(String code,String subjectId,String periodId,Pageable pageable);

Loading…
Cancel
Save