Commit 3a853711 authored by wujipeng's avatar wujipeng
Browse files

优化项

parent 169d9bfe
...@@ -49,6 +49,7 @@ import java.nio.charset.StandardCharsets; ...@@ -49,6 +49,7 @@ import java.nio.charset.StandardCharsets;
import java.time.DayOfWeek; import java.time.DayOfWeek;
import java.time.Instant; import java.time.Instant;
import java.time.LocalDate; import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime; import java.time.LocalTime;
import java.time.format.DateTimeFormatter; import java.time.format.DateTimeFormatter;
import java.time.format.DateTimeParseException; import java.time.format.DateTimeParseException;
...@@ -556,6 +557,8 @@ public class CourseAdminServiceImpl implements CourseAdminService { ...@@ -556,6 +557,8 @@ public class CourseAdminServiceImpl implements CourseAdminService {
prepared.entity.setId(courseId); prepared.entity.setId(courseId);
prepared.entity.setScheduleGroupId(resolveStoredGroupId(existing)); prepared.entity.setScheduleGroupId(resolveStoredGroupId(existing));
copyAdjustmentMetadata(existing, prepared.entity); copyAdjustmentMetadata(existing, prepared.entity);
// 前端会在课程进行中禁用时段控件,但接口仍需独立校验,防止直接调用接口绕过页面限制。
rejectIfInProgressTimeFrameChanged(existing, prepared.entity);
prepared.entity.setUpdateTime(new Date()); prepared.entity.setUpdateTime(new Date());
ensureNoConflict(prepared.entity, prepared.timeRanges, ensureNoConflict(prepared.entity, prepared.timeRanges,
Collections.singletonList(courseId)); Collections.singletonList(courseId));
...@@ -677,6 +680,8 @@ public class CourseAdminServiceImpl implements CourseAdminService { ...@@ -677,6 +680,8 @@ public class CourseAdminServiceImpl implements CourseAdminService {
prepared.entity.setId(item.getId()); prepared.entity.setId(item.getId());
// 课程编辑不能清除既有调课标记;该元数据只由“课次对调”入口维护。 // 课程编辑不能清除既有调课标记;该元数据只由“课次对调”入口维护。
copyAdjustmentMetadata(existing, prepared.entity); copyAdjustmentMetadata(existing, prepared.entity);
// 仅锁定正在上课课程的时段;课程名称、教师、地点等其他字段仍按原规则编辑。
rejectIfInProgressTimeFrameChanged(existing, prepared.entity);
if (!sameCourseContent(existing, prepared.entity) if (!sameCourseContent(existing, prepared.entity)
&& hasCourseDependencies(item.getId())) { && hasCourseDependencies(item.getId())) {
throw new BusinessException("课程已产生签到、请假或评价数据,不能修改"); throw new BusinessException("课程已产生签到、请假或评价数据,不能修改");
...@@ -775,6 +780,15 @@ public class CourseAdminServiceImpl implements CourseAdminService { ...@@ -775,6 +780,15 @@ public class CourseAdminServiceImpl implements CourseAdminService {
source, sourcePeriod, "本节次"); source, sourcePeriod, "本节次");
PeriodSwapSelection targetSelection = selectSwapPeriod( PeriodSwapSelection targetSelection = selectSwapPeriod(
target, targetPeriod, "对调课次"); target, targetPeriod, "对调课次");
/*
* 必须在班级锁内、按重新读取后的真实课程数据判断是否开课,不能只依赖弹窗隐藏:
* 否则管理员可直接调用接口,或在弹窗打开后等待至开课再提交,从而改写历史排课。
* 任意一方所选半天已经到达最早上课时间,都不允许执行对调。
*/
rejectIfSwapPeriodStarted(sourceSelection.selectedPayload, "本节次");
rejectIfSwapPeriodStarted(targetSelection.selectedPayload, "对调课次");
Date now = new Date(); Date now = new Date();
ClassSubjectEntity sourceRemainder = createPeriodRemainder(sourceSelection, now); ClassSubjectEntity sourceRemainder = createPeriodRemainder(sourceSelection, now);
ClassSubjectEntity targetRemainder = createPeriodRemainder(targetSelection, now); ClassSubjectEntity targetRemainder = createPeriodRemainder(targetSelection, now);
...@@ -1366,6 +1380,61 @@ public class CourseAdminServiceImpl implements CourseAdminService { ...@@ -1366,6 +1380,61 @@ public class CourseAdminServiceImpl implements CourseAdminService {
course.getSubjectTimes(), course.getSubjectHour()); course.getSubjectTimes(), course.getSubjectHour());
} }
/**
* 拒绝调整已经开始的半天课程。
*
* <p>一个上午或下午可能包含多个连续时段,业务上的“已开始”以其中最早的开始时间为准;
* 当前时刻等于开始时间也属于已经开始。日期和当前时刻统一按Asia/Shanghai计算,避免部署
* 服务器默认时区不同导致同一请求出现不一致结果。</p>
*/
private void rejectIfSwapPeriodStarted(TemporalPayload payload, String label) {
LocalTime earliestStart = payload.frames.stream()
.map(this::parseTimeRange)
.map(range -> range.start)
.min(LocalTime::compareTo)
.orElseThrow(() -> new BusinessException(label + "课程时段未配置"));
LocalDateTime startsAt = LocalDateTime.of(toLocalDate(payload.date), earliestStart);
if (!currentBusinessDateTime().isBefore(startsAt)) {
throw new BusinessException(label + "课程已开始,不能调课");
}
}
/**
* 统一提供业务时区当前时间;保留包级可见性,便于单元测试精确覆盖开课边界。
*/
LocalDateTime currentBusinessDateTime() {
return LocalDateTime.now(ClassAdminConstants.BUSINESS_ZONE);
}
/**
* 正在上课的课程不允许修改上课时段。
*
* <p>判断基于数据库中的原日期和原时段,而不是客户端提交的新值,避免管理员先修改日期
* 再绕过“上课中”限制。只有当前时刻落在任一原时段的[start, end)区间内才算正在上课;
* 已下课课程不会被本规则额外限制。原时段与提交时段都先规范化后比较,分隔符或补零方式
* 不同但语义一致时不会被误判为修改。</p>
*/
private void rejectIfInProgressTimeFrameChanged(
ClassSubjectEntity existing, ClassSubjectEntity submitted) {
List<TimeRange> existingRanges = normalizeStoredTimeFrames(existing.getTimeFrame());
String existingFrames = existingRanges.stream()
.map(TimeRange::asText).collect(Collectors.joining(","));
if (existingFrames.equals(submitted.getTimeFrame())) {
return;
}
LocalDateTime now = currentBusinessDateTime();
if (!toLocalDate(existing.getDate()).equals(now.toLocalDate())) {
return;
}
boolean inProgress = existingRanges.stream().anyMatch(range ->
!now.toLocalTime().isBefore(range.start)
&& now.toLocalTime().isBefore(range.end));
if (inProgress) {
throw new BusinessException("课程正在上课,不能修改上课时段");
}
}
private List<String> toFrameTexts(List<TimeRange> ranges) { private List<String> toFrameTexts(List<TimeRange> ranges) {
return ranges.stream().map(TimeRange::asText).collect(Collectors.toList()); return ranges.stream().map(TimeRange::asText).collect(Collectors.toList());
} }
......
...@@ -34,6 +34,7 @@ import java.nio.file.Paths; ...@@ -34,6 +34,7 @@ import java.nio.file.Paths;
import java.sql.Date; import java.sql.Date;
import java.time.DayOfWeek; import java.time.DayOfWeek;
import java.time.LocalDate; import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.temporal.TemporalAdjusters; import java.time.temporal.TemporalAdjusters;
import java.util.Arrays; import java.util.Arrays;
import java.util.Collections; import java.util.Collections;
...@@ -48,6 +49,8 @@ import static org.mockito.ArgumentMatchers.any; ...@@ -48,6 +49,8 @@ import static org.mockito.ArgumentMatchers.any;
import static org.mockito.ArgumentMatchers.anyList; import static org.mockito.ArgumentMatchers.anyList;
import static org.mockito.ArgumentMatchers.eq; import static org.mockito.ArgumentMatchers.eq;
import static org.mockito.Mockito.never; import static org.mockito.Mockito.never;
import static org.mockito.Mockito.doReturn;
import static org.mockito.Mockito.spy;
import static org.mockito.Mockito.verify; import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when; import static org.mockito.Mockito.when;
...@@ -395,6 +398,72 @@ class CourseAdminServiceImplTest { ...@@ -395,6 +398,72 @@ class CourseAdminServiceImplTest {
verify(classSubjectMapper, never()).updateAdminCourse(any(ClassSubjectEntity.class)); verify(classSubjectMapper, never()).updateAdminCourse(any(ClassSubjectEntity.class));
} }
@Test
void updateScheduleShouldRejectChangingTimeFrameWhileCourseIsInProgress() {
LocalDate businessDate = LocalDate.of(2026, 11, 19);
ClassSubjectEntity existing = existingCourse(11, "group-1", "10:45-11:45");
existing.setDate(Date.valueOf(businessDate));
when(classSubjectMapper.selectAdminCourseGroupEntities("group-1"))
.thenReturn(Collections.singletonList(existing));
mockWritableClassFor(businessDate);
when(classSubjectMapper.countAdminActiveTeacher("teacher-1")).thenReturn(1);
CourseAdminScheduleItemSaveDTO changed =
scheduleItem(11, "teacher-1", "10:45-12:00");
CourseAdminScheduleGroupSaveDTO request = new CourseAdminScheduleGroupSaveDTO();
request.setDate(Date.valueOf(businessDate));
request.setCourses(Collections.singletonList(changed));
// 固定在原课程时段内部,验证即使请求通过页面以外的方式发起,后端也会拒绝修改。
CourseAdminServiceImpl fixedTimeService = spy(service);
doReturn(LocalDateTime.of(2026, 11, 19, 11, 0))
.when(fixedTimeService).currentBusinessDateTime();
BusinessException error = assertThrows(BusinessException.class,
() -> fixedTimeService.updateSchedule("group-1", request));
assertEquals("第1节课程:课程正在上课,不能修改上课时段", error.getMessage());
verify(classSubjectMapper, never()).updateAdminCourse(any(ClassSubjectEntity.class));
}
@Test
void updateScheduleShouldAllowOtherFieldsWhenTimeFrameIsUnchanged() {
LocalDate businessDate = LocalDate.of(2026, 11, 19);
ClassSubjectEntity existing = existingCourse(11, "group-1", "10:45-11:45");
existing.setDate(Date.valueOf(businessDate));
when(classSubjectMapper.selectAdminCourseGroupEntities("group-1"))
.thenReturn(Collections.singletonList(existing));
mockWritableClassFor(businessDate);
when(classSubjectMapper.countAdminActiveTeacher("teacher-1")).thenReturn(1);
when(classSubjectMapper.selectAdminConflictCandidates(any(), eq(1),
eq("teacher-1"), anyList())).thenReturn(Collections.emptyList());
when(classSubjectMapper.updateAdminCourse(any(ClassSubjectEntity.class))).thenReturn(1);
CourseAdminScheduleItemSaveDTO changed =
scheduleItem(11, "teacher-1", "10:45-11:45");
changed.setSubjectName("上课中允许修改的课程名称");
CourseAdminScheduleGroupSaveDTO request = new CourseAdminScheduleGroupSaveDTO();
request.setDate(Date.valueOf(businessDate));
request.setCourses(Collections.singletonList(changed));
CourseAdminDetailVO detail = new CourseAdminDetailVO();
detail.setClassId(1);
when(classSubjectMapper.selectAdminCourseClass(1)).thenReturn(detail);
CourseAdminScheduleVO refreshed = exportCourse(
11, "group-1", "10:45-11:45", changed.getSubjectName(), "测试教师");
refreshed.setDate(Date.valueOf(businessDate));
when(classSubjectMapper.selectAdminCourseList(1, null, null))
.thenReturn(Collections.singletonList(refreshed));
// 时段未变化时保护逻辑应立即放行,其他字段继续走原有保存流程。
service.updateSchedule("group-1", request);
ArgumentCaptor<ClassSubjectEntity> updated =
ArgumentCaptor.forClass(ClassSubjectEntity.class);
verify(classSubjectMapper).updateAdminCourse(updated.capture());
assertEquals("上课中允许修改的课程名称", updated.getValue().getSubjectName());
}
@Test @Test
void swapScheduleShouldExchangeOnlyTemporalFieldsAndMarkBothGroups() { void swapScheduleShouldExchangeOnlyTemporalFieldsAndMarkBothGroups() {
ClassSubjectEntity source = existingCourse(11, "group-1", "09:00-10:20"); ClassSubjectEntity source = existingCourse(11, "group-1", "09:00-10:20");
...@@ -442,6 +511,65 @@ class CourseAdminServiceImplTest { ...@@ -442,6 +511,65 @@ class CourseAdminServiceImplTest {
.updateAdminCourse(any(ClassSubjectEntity.class)); .updateAdminCourse(any(ClassSubjectEntity.class));
} }
@Test
void swapScheduleShouldRejectWhenSelectedSourcePeriodHasStarted() {
LocalDate businessDate = LocalDate.of(2026, 11, 19);
ClassSubjectEntity source = existingCourse(11, "group-1", "10:45-11:45");
source.setDate(Date.valueOf(businessDate));
ClassSubjectEntity target = existingCourse(22, "group-2", "14:00-15:00");
target.setDate(Date.valueOf(businessDate.plusDays(4)));
when(classSubjectMapper.selectAdminCourseGroupEntities("group-1"))
.thenReturn(Collections.singletonList(source));
when(classSubjectMapper.selectAdminCourseGroupEntities("group-2"))
.thenReturn(Collections.singletonList(target));
mockWritableClass();
// 固定为截图中的业务时间,验证开始一分钟后即使目标课次尚未开始也必须拒绝。
CourseAdminServiceImpl fixedTimeService = spy(service);
doReturn(LocalDateTime.of(2026, 11, 19, 10, 46))
.when(fixedTimeService).currentBusinessDateTime();
CourseAdminScheduleSwapDTO request = new CourseAdminScheduleSwapDTO();
request.setTargetScheduleGroupId("group-2");
request.setSourcePeriod("AM");
request.setTargetPeriod("PM");
BusinessException error = assertThrows(BusinessException.class,
() -> fixedTimeService.swapSchedule("group-1", request));
assertEquals("本节次课程已开始,不能调课", error.getMessage());
verify(classSubjectMapper, never()).updateAdminCourse(any(ClassSubjectEntity.class));
verify(classSubjectMapper, never()).insert(any(ClassSubjectEntity.class));
}
@Test
void swapScheduleShouldRejectWhenSelectedTargetPeriodHasStarted() {
LocalDate businessDate = LocalDate.of(2026, 11, 19);
ClassSubjectEntity source = existingCourse(11, "group-1", "09:00-10:00");
source.setDate(Date.valueOf(businessDate.plusDays(4)));
ClassSubjectEntity target = existingCourse(22, "group-2", "10:45-11:45");
target.setDate(Date.valueOf(businessDate));
when(classSubjectMapper.selectAdminCourseGroupEntities("group-1"))
.thenReturn(Collections.singletonList(source));
when(classSubjectMapper.selectAdminCourseGroupEntities("group-2"))
.thenReturn(Collections.singletonList(target));
mockWritableClass();
CourseAdminServiceImpl fixedTimeService = spy(service);
doReturn(LocalDateTime.of(2026, 11, 19, 10, 46))
.when(fixedTimeService).currentBusinessDateTime();
CourseAdminScheduleSwapDTO request = new CourseAdminScheduleSwapDTO();
request.setTargetScheduleGroupId("group-2");
request.setSourcePeriod("AM");
request.setTargetPeriod("AM");
BusinessException error = assertThrows(BusinessException.class,
() -> fixedTimeService.swapSchedule("group-1", request));
assertEquals("对调课次课程已开始,不能调课", error.getMessage());
verify(classSubjectMapper, never()).updateAdminCourse(any(ClassSubjectEntity.class));
verify(classSubjectMapper, never()).insert(any(ClassSubjectEntity.class));
}
@Test @Test
void swapScheduleShouldSplitAllDayCoursesAndExchangeOnlySelectedPeriod() { void swapScheduleShouldSplitAllDayCoursesAndExchangeOnlySelectedPeriod() {
ClassSubjectEntity source = existingCourse(11, "group-1", ClassSubjectEntity source = existingCourse(11, "group-1",
...@@ -620,6 +748,16 @@ class CourseAdminServiceImplTest { ...@@ -620,6 +748,16 @@ class CourseAdminServiceImplTest {
when(classMapper.selectById(1)).thenReturn(managedClass); when(classMapper.selectById(1)).thenReturn(managedClass);
} }
/** 为固定业务日期的边界测试创建覆盖该日期的可编辑班级。 */
private void mockWritableClassFor(LocalDate courseDate) {
when(classSubjectMapper.lockAdminCourseClass(1)).thenReturn(1);
ClassEntity managedClass = new ClassEntity();
managedClass.setId(1);
managedClass.setStartDate(Date.valueOf(courseDate.minusDays(1)));
managedClass.setEndDate(Date.valueOf(courseDate.plusMonths(1)));
when(classMapper.selectById(1)).thenReturn(managedClass);
}
private void mockEmptyDetail() { private void mockEmptyDetail() {
CourseAdminDetailVO detail = new CourseAdminDetailVO(); CourseAdminDetailVO detail = new CourseAdminDetailVO();
detail.setClassId(1); detail.setClassId(1);
......
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