fix: 修复计算学期学年的 start bug

11111
winter 10 months ago
parent 9f0cd7d01a
commit 94ac4286c3

@ -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();
}

@ -144,16 +144,16 @@ public class TestWithoutSpring {
@Test
public void testLocalDateTime() {
List<School.Semester> semesters = Arrays.asList(
new School.Semester("1", "上学期", 0, 2, 1), // 2024-3 , 正确: 2023-3
new School.Semester("3", "下学期", 1, 8, 1)
// new School.Semester("4", "下学期", 0, 11, 1),
// new School.Semester("2", "下学期", 0, 5, 1)
new School.Semester("1", "下学期", 1, 2, 1), // 2024-3 , 正确: 2023-3
// new School.Semester("2", "下学期", 0, 5, 1),
new School.Semester("3", "上学期", 0, 8, 1)
// new School.Semester("4", "下学期", 0, 11, 1)
);
// LocalDate currentDate = LocalDate.of(2024, 11, 1);
LocalDate currentDate = LocalDate.of(2024, 1, 1);
String id = SchoolDateUtil.calculateAcademicYearId(semesters, LocalDate.now());
System.out.println(id);
// String id = SchoolDateUtil.calculateAcademicYearId(semesters, currentDate);
System.out.println(SchoolDateUtil.getSemesterByNow(semesters, currentDate));
}
@Test

Loading…
Cancel
Save