feat: 新增根据指定日期获取学年的工具

11111
winter 1 year ago
parent aa582361f8
commit 8301cfd96d

@ -1,6 +1,8 @@
package cn.teammodel.model.entity.school; package cn.teammodel.model.entity.school;
import lombok.AllArgsConstructor;
import lombok.Data; import lombok.Data;
import lombok.NoArgsConstructor;
/** /**
* () * ()
@ -8,6 +10,8 @@ import lombok.Data;
* @create 2023-11-28 10:39 * @create 2023-11-28 10:39
*/ */
@Data @Data
@AllArgsConstructor
@NoArgsConstructor
public class Semester { public class Semester {
private String id; private String id;
private String name; private String name;

@ -3,6 +3,7 @@ package cn.teammodel.utils;
import cn.teammodel.model.entity.school.Semester; import cn.teammodel.model.entity.school.Semester;
import java.time.LocalDate; import java.time.LocalDate;
import java.util.Comparator;
import java.util.List; import java.util.List;
/** /**
@ -32,9 +33,35 @@ public class SchoolDateUtil {
* "id": "1" * "id": "1"
* } * }
* <pre/> * <pre/>
* <p>
* : 2023.11.28 2023
* : -> 2024.1.1, 2023,,semesters
* </p>
*/ */
public static String getSemesterByNow(List<Semester> semesters, LocalDate date) { public static String getSemesterByNow(List<Semester> 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();
}
} }

@ -2,7 +2,9 @@ package cn.teammodel;
import cn.teammodel.model.entity.appraise.Appraise; import cn.teammodel.model.entity.appraise.Appraise;
import cn.teammodel.model.entity.appraise.AppraiseTreeNode; import cn.teammodel.model.entity.appraise.AppraiseTreeNode;
import cn.teammodel.model.entity.school.Semester;
import cn.teammodel.service.impl.EvaluationServiceImpl; import cn.teammodel.service.impl.EvaluationServiceImpl;
import cn.teammodel.utils.SchoolDateUtil;
import com.dingtalk.api.DefaultDingTalkClient; import com.dingtalk.api.DefaultDingTalkClient;
import com.dingtalk.api.DingTalkClient; import com.dingtalk.api.DingTalkClient;
import com.dingtalk.api.request.OapiRobotSendRequest; import com.dingtalk.api.request.OapiRobotSendRequest;
@ -11,6 +13,7 @@ import com.taobao.api.ApiException;
import lombok.extern.slf4j.Slf4j; import lombok.extern.slf4j.Slf4j;
import org.junit.jupiter.api.Test; import org.junit.jupiter.api.Test;
import java.time.LocalDate;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Arrays; import java.util.Arrays;
import java.util.List; import java.util.List;
@ -136,6 +139,16 @@ public class TestWithoutSpring {
@Test @Test
public void testLocalDateTime() { public void testLocalDateTime() {
List<Semester> 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);
} }
} }

Loading…
Cancel
Save