From 94ac4286c326144163a60b7bbe2c9b75b00508db Mon Sep 17 00:00:00 2001 From: winter <2436197699@qq.com> Date: Tue, 27 Feb 2024 16:49:15 +0800 Subject: [PATCH] =?UTF-8?q?fix:=20=E4=BF=AE=E5=A4=8D=E8=AE=A1=E7=AE=97?= =?UTF-8?q?=E5=AD=A6=E6=9C=9F=E5=AD=A6=E5=B9=B4=E7=9A=84=20start=20bug?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../cn/teammodel/utils/SchoolDateUtil.java | 21 ++++++++++++++++++- .../java/cn/teammodel/TestWithoutSpring.java | 14 ++++++------- 2 files changed, 27 insertions(+), 8 deletions(-) diff --git a/src/main/java/cn/teammodel/utils/SchoolDateUtil.java b/src/main/java/cn/teammodel/utils/SchoolDateUtil.java index 82cd322..cb05eb7 100644 --- a/src/main/java/cn/teammodel/utils/SchoolDateUtil.java +++ b/src/main/java/cn/teammodel/utils/SchoolDateUtil.java @@ -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(); } diff --git a/src/test/java/cn/teammodel/TestWithoutSpring.java b/src/test/java/cn/teammodel/TestWithoutSpring.java index 2d14183..21568bc 100644 --- a/src/test/java/cn/teammodel/TestWithoutSpring.java +++ b/src/test/java/cn/teammodel/TestWithoutSpring.java @@ -144,16 +144,16 @@ public class TestWithoutSpring { @Test public void testLocalDateTime() { List 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