Commit b5d4d905 authored by 王蔡翾's avatar 王蔡翾
Browse files

短信消息接入其他模块

parent c1404eed
...@@ -346,13 +346,15 @@ public class LeaveManagementServiceImpl implements LeaveManagementService { ...@@ -346,13 +346,15 @@ public class LeaveManagementServiceImpl implements LeaveManagementService {
.le(ClassSubjectEntity::getDate, dayEnd)); .le(ClassSubjectEntity::getDate, dayEnd));
Date now = new Date(); Date now = new Date();
List<ClassSubjectEntity> matched = candidates.stream() List<ClassSubjectEntity> matched = candidates.stream()
.filter(subject -> overlaps(ClassSubjectTimeUtils.resolve(subject), request.getStartTime(), request.getEndTime())) .filter(subject -> overlapsAnyFrame(
subject, request.getStartTime(), request.getEndTime()))
.sorted(Comparator.comparing(subject -> ClassSubjectTimeUtils.resolve(subject).getStart())) .sorted(Comparator.comparing(subject -> ClassSubjectTimeUtils.resolve(subject).getStart()))
.collect(Collectors.toList()); .collect(Collectors.toList());
if (matched.isEmpty()) { if (matched.isEmpty()) {
throw new BusinessException("请假时间内没有需要出勤的课程"); throw new BusinessException("请假时间内没有需要出勤的课程");
} }
if (matched.stream().anyMatch(subject -> !ClassSubjectTimeUtils.resolve(subject).getStart().after(now))) { if (matched.stream().anyMatch(subject -> overlappingFrameHasStarted(
subject, request.getStartTime(), request.getEndTime(), now))) {
throw new BusinessException("不能为已经开始或结束的课程补交请假"); throw new BusinessException("不能为已经开始或结束的课程补交请假");
} }
return matched; return matched;
...@@ -508,6 +510,20 @@ public class LeaveManagementServiceImpl implements LeaveManagementService { ...@@ -508,6 +510,20 @@ public class LeaveManagementServiceImpl implements LeaveManagementService {
return window.getStart().before(end) && window.getEnd().after(start); return window.getStart().before(end) && window.getEnd().after(start);
} }
/** 多时段课程逐段判断,请假落在课间空档时不能关联该课程及其授课教师。 */
static boolean overlapsAnyFrame(ClassSubjectEntity subject, Date start, Date end) {
return ClassSubjectTimeUtils.resolveAll(subject).stream()
.anyMatch(window -> overlaps(window, start, end));
}
/** 只检查与本次请假真正重叠的时段,不能因同一课程较早的其他时段已开始而误判。 */
private static boolean overlappingFrameHasStarted(ClassSubjectEntity subject, Date start,
Date end, Date now) {
return ClassSubjectTimeUtils.resolveAll(subject).stream()
.filter(window -> overlaps(window, start, end))
.anyMatch(window -> !window.getStart().after(now));
}
/** 返回指定日期的当天起点,用于缩小候选课程查询范围。 */ /** 返回指定日期的当天起点,用于缩小候选课程查询范围。 */
private static Date startOfDay(Date date) { private static Date startOfDay(Date date) {
Calendar calendar = Calendar.getInstance(); Calendar calendar = Calendar.getInstance();
......
...@@ -10,7 +10,10 @@ import java.time.LocalTime; ...@@ -10,7 +10,10 @@ import java.time.LocalTime;
import java.time.ZoneId; import java.time.ZoneId;
import java.time.format.DateTimeFormatter; import java.time.format.DateTimeFormatter;
import java.time.format.DateTimeParseException; import java.time.format.DateTimeParseException;
import java.util.ArrayList;
import java.util.Comparator;
import java.util.Date; import java.util.Date;
import java.util.List;
/** 将课程日期和timeFrame文本解析成可比较的计划开始、结束时间。 */ /** 将课程日期和timeFrame文本解析成可比较的计划开始、结束时间。 */
public final class ClassSubjectTimeUtils { public final class ClassSubjectTimeUtils {
...@@ -25,14 +28,31 @@ public final class ClassSubjectTimeUtils { ...@@ -25,14 +28,31 @@ public final class ClassSubjectTimeUtils {
* 多段时段取最早开始和最晚结束,例如09:00-10:00,10:30-12:00解析为09:00-12:00。 * 多段时段取最早开始和最晚结束,例如09:00-10:00,10:30-12:00解析为09:00-12:00。
*/ */
public static TimeWindow resolve(ClassSubjectEntity subject) { public static TimeWindow resolve(ClassSubjectEntity subject) {
List<TimeWindow> windows = resolveAll(subject);
Date latestEnd = windows.get(0).getEnd();
for (TimeWindow window : windows) {
if (window.getEnd().after(latestEnd)) {
latestEnd = window.getEnd();
}
}
return new TimeWindow(windows.get(0).getStart(), latestEnd);
}
/**
* 将课程的每个实际授课时段分别解析为时间窗口。
*
* <p>请假等需要精确判断时间重叠的业务必须使用本方法,不能把多个时段之间的休息时间
* 也视为授课时间;只关心课程整体起止边界的签到、评价等业务可以继续使用{@link #resolve}。</p>
*/
public static List<TimeWindow> resolveAll(ClassSubjectEntity subject) {
if (subject == null || subject.getDate() == null || subject.getTimeFrame() == null if (subject == null || subject.getDate() == null || subject.getTimeFrame() == null
|| subject.getTimeFrame().trim().isEmpty()) { || subject.getTimeFrame().trim().isEmpty()) {
throw new BusinessException("课程日期或上课时段未配置"); throw new BusinessException("课程日期或上课时段未配置");
} }
LocalDate date = Instant.ofEpochMilli(subject.getDate().getTime()) LocalDate date = Instant.ofEpochMilli(subject.getDate().getTime())
.atZone(ZoneId.systemDefault()).toLocalDate(); .atZone(ZoneId.systemDefault()).toLocalDate();
LocalTime earliest = null; ZoneId zone = ZoneId.systemDefault();
LocalTime latest = null; List<TimeWindow> windows = new ArrayList<>();
String normalized = subject.getTimeFrame().replace('—', '-').replace('–', '-'); String normalized = subject.getTimeFrame().replace('—', '-').replace('–', '-');
for (String frame : normalized.split(",")) { for (String frame : normalized.split(",")) {
String[] times = frame.trim().split("-", -1); String[] times = frame.trim().split("-", -1);
...@@ -45,23 +65,18 @@ public final class ClassSubjectTimeUtils { ...@@ -45,23 +65,18 @@ public final class ClassSubjectTimeUtils {
if (!end.isAfter(start)) { if (!end.isAfter(start)) {
throw new BusinessException("课程结束时间必须晚于开始时间"); throw new BusinessException("课程结束时间必须晚于开始时间");
} }
if (earliest == null || start.isBefore(earliest)) { windows.add(new TimeWindow(
earliest = start; Date.from(LocalDateTime.of(date, start).atZone(zone).toInstant()),
} Date.from(LocalDateTime.of(date, end).atZone(zone).toInstant())));
if (latest == null || end.isAfter(latest)) {
latest = end;
}
} catch (DateTimeParseException e) { } catch (DateTimeParseException e) {
throw new BusinessException("课程时段格式不正确,应为 HH:mm-HH:mm"); throw new BusinessException("课程时段格式不正确,应为 HH:mm-HH:mm");
} }
} }
if (earliest == null || latest == null) { if (windows.isEmpty()) {
throw new BusinessException("课程时段格式不正确,应为 HH:mm-HH:mm"); throw new BusinessException("课程时段格式不正确,应为 HH:mm-HH:mm");
} }
ZoneId zone = ZoneId.systemDefault(); windows.sort(Comparator.comparing(TimeWindow::getStart));
return new TimeWindow( return windows;
Date.from(LocalDateTime.of(date, earliest).atZone(zone).toInstant()),
Date.from(LocalDateTime.of(date, latest).atZone(zone).toInstant()));
} }
/** 不可变的课程计划时间窗口。 */ /** 不可变的课程计划时间窗口。 */
......
...@@ -12,11 +12,14 @@ import org.mockito.InjectMocks; ...@@ -12,11 +12,14 @@ import org.mockito.InjectMocks;
import org.mockito.Mock; import org.mockito.Mock;
import org.mockito.junit.jupiter.MockitoExtension; import org.mockito.junit.jupiter.MockitoExtension;
import java.sql.Timestamp;
import java.util.Arrays; import java.util.Arrays;
import java.util.Collections; import java.util.Collections;
import static org.junit.jupiter.api.Assertions.assertEquals; import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertNull; import static org.junit.jupiter.api.Assertions.assertNull;
import static org.junit.jupiter.api.Assertions.assertTrue;
import static org.mockito.ArgumentMatchers.eq; import static org.mockito.ArgumentMatchers.eq;
import static org.mockito.Mockito.times; import static org.mockito.Mockito.times;
import static org.mockito.Mockito.verify; import static org.mockito.Mockito.verify;
...@@ -49,6 +52,20 @@ class LeaveManagementNotificationTest { ...@@ -49,6 +52,20 @@ class LeaveManagementNotificationTest {
assertNull(messageCaptor.getValue().getRecipientRoleNames()); assertNull(messageCaptor.getValue().getRecipientRoleNames());
} }
@Test
void leavePeriodShouldNotMatchGapBetweenCourseFrames() {
ClassSubjectEntity subject = subject("teacher-1");
subject.setDate(Timestamp.valueOf("2099-09-18 00:00:00"));
subject.setTimeFrame("09:00-10:00,14:00-15:00");
assertFalse(LeaveManagementServiceImpl.overlapsAnyFrame(subject,
Timestamp.valueOf("2099-09-18 11:00:00"),
Timestamp.valueOf("2099-09-18 12:00:00")));
assertTrue(LeaveManagementServiceImpl.overlapsAnyFrame(subject,
Timestamp.valueOf("2099-09-18 14:30:00"),
Timestamp.valueOf("2099-09-18 14:45:00")));
}
@Test @Test
void reviewShouldSendSeparateAdminOpinionOnlyWhenApproved() { void reviewShouldSendSeparateAdminOpinionOnlyWhenApproved() {
LeaveApplicationVO leave = leave(); LeaveApplicationVO leave = leave();
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment