|
|
|
@ -54,23 +54,42 @@ public class SchoolDateUtil {
|
|
|
|
|
|
|
|
|
|
// 将 semester 根据大小排序
|
|
|
|
|
semesters.sort(Comparator.comparing(s -> LocalDate.of(date.getYear(), s.getMonth(), s.getDay())));
|
|
|
|
|
School.Semester start = semesters.stream().filter(s -> s.getStart() == 1).findFirst().orElseThrow(() -> new ServiceException("学期异常, 未找到新学年开学日期"));
|
|
|
|
|
LocalDate startDate = LocalDate.of(date.getYear(), start.getMonth(), start.getDay());
|
|
|
|
|
|
|
|
|
|
// i = 0 的日期是开学最早, i = size - 1 是开学最晚
|
|
|
|
|
int size = semesters.size();
|
|
|
|
|
// 拿到最晚开学的日期,将年份 - 1后,与头节点组成可跨年区间
|
|
|
|
|
School.Semester lastSemester = semesters.get(size - 1);
|
|
|
|
|
School.Semester firstSemester = semesters.get(0);
|
|
|
|
|
LocalDate firstSemesterDate = LocalDate.of(date.getYear(), firstSemester.getMonth(), firstSemester.getDay());
|
|
|
|
|
|
|
|
|
|
// 动态替换
|
|
|
|
|
LocalDate lastSemesterStart = LocalDate.of(date.getYear() - 1, lastSemester.getMonth(), lastSemester.getDay());
|
|
|
|
|
String lastSemesterId = lastSemester.getId();
|
|
|
|
|
|
|
|
|
|
for (School.Semester semester : semesters) {
|
|
|
|
|
// 特判头尾的提起,分别将学期区间提前和延后一年(不用延后),例如 2023.3.1 与 2023.9.1 我们分别向添加一个节点,划分其为 3 个区间:
|
|
|
|
|
// 2022.9.1 - 2023.3.1, 2023.3.1 - 2023.9.1, 2023.9.1 - 2024.3.1
|
|
|
|
|
LocalDate curSemesterStart = LocalDate.of(date.getYear(), semester.getMonth(), semester.getDay());
|
|
|
|
|
if (date.isEqual(lastSemesterStart) || date.isAfter(lastSemesterStart) && date.isBefore(curSemesterStart)) {
|
|
|
|
|
|
|
|
|
|
// 跨年的情况
|
|
|
|
|
if(date.isBefore(firstSemesterDate)) {
|
|
|
|
|
// 这里是去年的最后一个开学日期(last 还未开始迭代)
|
|
|
|
|
String academicYearId = generateCurAcademicId(lastSemesterStart.getYear(), lastSemesterId);
|
|
|
|
|
return new semesterModel(academicYearId, lastSemesterId, lastSemesterStart.atStartOfDay(), semester.getId(), curSemesterStart.atStartOfDay());
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (date.isEqual(lastSemesterStart) || date.isAfter(lastSemesterStart) && date.isBefore(curSemesterStart)) {
|
|
|
|
|
// 如果当前时间在开学时期之前, 学年则是去年
|
|
|
|
|
if (date.isBefore(startDate)) {
|
|
|
|
|
String academicYearId = generateCurAcademicId(date.getYear() - 1, lastSemesterId);
|
|
|
|
|
return new semesterModel(academicYearId, lastSemesterId, lastSemesterStart.atStartOfDay(), semester.getId(), curSemesterStart.atStartOfDay());
|
|
|
|
|
}
|
|
|
|
|
String academicYearId = generateCurAcademicId(date.getYear(), lastSemesterId);
|
|
|
|
|
return new semesterModel(academicYearId, lastSemesterId, lastSemesterStart.atStartOfDay(), semester.getId(), curSemesterStart.atStartOfDay());
|
|
|
|
|
}
|
|
|
|
|
// 迭代
|
|
|
|
|
lastSemesterStart = curSemesterStart;
|
|
|
|
|
lastSemesterId = semester.getId();
|
|
|
|
|
}
|
|
|
|
|