diff --git a/src/main/java/cn/teammodel/model/entity/school/Semester.java b/src/main/java/cn/teammodel/model/entity/school/Semester.java index 647cfd9..6c528e7 100644 --- a/src/main/java/cn/teammodel/model/entity/school/Semester.java +++ b/src/main/java/cn/teammodel/model/entity/school/Semester.java @@ -1,6 +1,8 @@ package cn.teammodel.model.entity.school; +import lombok.AllArgsConstructor; import lombok.Data; +import lombok.NoArgsConstructor; /** * 学期(标记了学期的开始与结束) @@ -8,6 +10,8 @@ import lombok.Data; * @create 2023-11-28 10:39 */ @Data +@AllArgsConstructor +@NoArgsConstructor public class Semester { private String id; private String name; diff --git a/src/main/java/cn/teammodel/utils/SchoolDateUtil.java b/src/main/java/cn/teammodel/utils/SchoolDateUtil.java index b2ac09e..467b104 100644 --- a/src/main/java/cn/teammodel/utils/SchoolDateUtil.java +++ b/src/main/java/cn/teammodel/utils/SchoolDateUtil.java @@ -3,6 +3,7 @@ package cn.teammodel.utils; import cn.teammodel.model.entity.school.Semester; import java.time.LocalDate; +import java.util.Comparator; import java.util.List; /** @@ -32,9 +33,35 @@ public class SchoolDateUtil { * "id": "1" * } *
+     * 

+ * 示例: 传参 2023.11.28 返回 2023学年上学期 + * 注意: 处理跨年问题 -> 2024.1.1, 应该也是 2023学年上学期,同时你需要注意,semesters 可能不止两个学期 + *

*/ public static String getSemesterByNow(List semesters, LocalDate date) { - return ""; - } + if (semesters == null || semesters.isEmpty() || date == null) { + throw new IllegalArgumentException("Invalid input: semesters and date must not be null or empty."); + } + + // 将 semester 根据大小排序 + semesters.sort(Comparator.comparing(s -> LocalDate.of(date.getYear(), s.getMonth(), s.getDay()))); + // i = 0 的日期是开学最早, i = size - 1 是开学最晚 + int size = semesters.size(); + // 拿到最晚开学的日期,将年份 - 1后,与头节点组成可跨年区间 + Semester lastSemester = semesters.get(size - 1); + LocalDate tmpSemester = LocalDate.of(date.getYear() - 1, lastSemester.getMonth(), lastSemester.getDay()); + for (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 curSemester = LocalDate.of(date.getYear(), semester.getMonth(), semester.getDay()); + if (date.isEqual(tmpSemester) || date.isAfter(tmpSemester) && date.isBefore(curSemester)) { + return tmpSemester.toString(); + } + tmpSemester = curSemester; + } + + // 剩下的时间段, 学年应该就是最后一个学年 + return LocalDate.of(date.getYear(), lastSemester.getMonth(), lastSemester.getDay()).toString(); + } } diff --git a/src/test/java/cn/teammodel/TestWithoutSpring.java b/src/test/java/cn/teammodel/TestWithoutSpring.java index afb9955..6a456d7 100644 --- a/src/test/java/cn/teammodel/TestWithoutSpring.java +++ b/src/test/java/cn/teammodel/TestWithoutSpring.java @@ -2,7 +2,9 @@ package cn.teammodel; import cn.teammodel.model.entity.appraise.Appraise; import cn.teammodel.model.entity.appraise.AppraiseTreeNode; +import cn.teammodel.model.entity.school.Semester; import cn.teammodel.service.impl.EvaluationServiceImpl; +import cn.teammodel.utils.SchoolDateUtil; import com.dingtalk.api.DefaultDingTalkClient; import com.dingtalk.api.DingTalkClient; import com.dingtalk.api.request.OapiRobotSendRequest; @@ -11,6 +13,7 @@ import com.taobao.api.ApiException; import lombok.extern.slf4j.Slf4j; import org.junit.jupiter.api.Test; +import java.time.LocalDate; import java.util.ArrayList; import java.util.Arrays; import java.util.List; @@ -136,6 +139,16 @@ public class TestWithoutSpring { @Test public void testLocalDateTime() { + List semesters = Arrays.asList( + new Semester("1", "上学期", 1, 2, 1), + new Semester("3", "下学期", 0, 8, 1), + new Semester("4", "下学期", 0, 11, 1), + new Semester("2", "下学期", 0, 5, 1) + ); + LocalDate currentDate = LocalDate.of(2024, 1, 1); + + String currentSemester = SchoolDateUtil.getSemesterByNow(semesters, currentDate); + System.out.println(currentSemester); } }