"...platform/git@gitlab.ruzhoukj.com:wujipeng/relation.git" did not exist on "db2763e47f82d5543752d87c7faea059a84a1b39"
Commit 04bb4f11 authored by 王蔡翾's avatar 王蔡翾
Browse files

Merge remote-tracking branch 'origin/dev-v1.0' into dev-v1.0

# Conflicts:
#	practical-business/src/main/resources/com/platform/business/mapper/UserMapper.xml
#	practical-business/src/test/java/com/platform/business/mapper/UserMapperXmlTest.java
parents 9b9782a3 a530d209
...@@ -81,6 +81,10 @@ public class User { ...@@ -81,6 +81,10 @@ public class User {
@TableField("exam_flag") @TableField("exam_flag")
private Boolean examFlag; private Boolean examFlag;
@ApiModelProperty("考试类型")
@TableField("exam_type")
private String examType;
@ApiModelProperty("考生个人考试id") @ApiModelProperty("考生个人考试id")
@TableField("exam_oneself_id") @TableField("exam_oneself_id")
private Integer examOneselfId; private Integer examOneselfId;
......
...@@ -205,6 +205,10 @@ public class UserEntity { ...@@ -205,6 +205,10 @@ public class UserEntity {
@TableField("exam_flag") @TableField("exam_flag")
private Boolean examFlag; private Boolean examFlag;
@ApiModelProperty("考试类型")
@TableField("exam_type")
private String examType;
@ApiModelProperty("考生个人考试id") @ApiModelProperty("考生个人考试id")
@TableField("exam_oneself_id") @TableField("exam_oneself_id")
private Integer examOneselfId; private Integer examOneselfId;
......
...@@ -137,7 +137,7 @@ public class StudentExamCenterServiceImpl implements StudentExamCenterService { ...@@ -137,7 +137,7 @@ public class StudentExamCenterServiceImpl implements StudentExamCenterService {
attempt.getExamId(), studentId); attempt.getExamId(), studentId);
if (attempt.getStartTime() != null && generated > 0) { if (attempt.getStartTime() != null && generated > 0) {
// 已经开考时返回原应考记录ID,前端可直接恢复,不再把重复点击当作异常。 // 已经开考时返回原应考记录ID,前端可直接恢复,不再把重复点击当作异常。
markUserExamState(studentId, attempt.getId()); markUserExamState(studentId, attempt);
return attempt.getId(); return attempt.getId();
} }
...@@ -152,7 +152,7 @@ public class StudentExamCenterServiceImpl implements StudentExamCenterService { ...@@ -152,7 +152,7 @@ public class StudentExamCenterServiceImpl implements StudentExamCenterService {
throw new BusinessException("考试状态已变化,请刷新后重试"); throw new BusinessException("考试状态已变化,请刷新后重试");
} }
} }
markUserExamState(studentId, attempt.getId()); markUserExamState(studentId, attempt);
return attempt.getId(); return attempt.getId();
} }
...@@ -429,11 +429,16 @@ public class StudentExamCenterServiceImpl implements StudentExamCenterService { ...@@ -429,11 +429,16 @@ public class StudentExamCenterServiceImpl implements StudentExamCenterService {
} }
} }
private void markUserExamState(String studentId, Integer examStudentId) { private void markUserExamState(String studentId, StudentExamCenterVO.Attempt attempt) {
UserEntity update = new UserEntity(); UserEntity update = new UserEntity();
update.setId(studentId); update.setId(studentId);
update.setExamFlag(true); update.setExamFlag(true);
update.setExamOneselfId(examStudentId); update.setExamOneselfId(attempt.getId());
/*
* examFlag、examOneselfId和examType共同描述同一场进行中的考试,必须从同一个
* Attempt一次性写入,避免重复开考恢复时只修复考试ID而遗留空或错误的考试类型。
*/
update.setExamType(attempt.getExamType());
if (!userService.updateById(update)) { if (!userService.updateById(update)) {
throw new BusinessException("学员考试状态更新失败"); throw new BusinessException("学员考试状态更新失败");
} }
......
...@@ -53,8 +53,9 @@ ...@@ -53,8 +53,9 @@
user.del_flag, user.del_flag,
role.id as roleId, role.id as roleId,
role.name as roleName, role.name as roleName,
active_exam.exam_student_id is not null as exam_flag, user.exam_flag,
active_exam.exam_student_id as exam_oneself_id, user.exam_type as examType,
user.exam_oneself_id,
user.level, user.level,
user.simulation_flag, user.simulation_flag,
user.simulation_id user.simulation_id
...@@ -68,32 +69,12 @@ ...@@ -68,32 +69,12 @@
role role
on on
sys_user_and_role.role_id = role.id sys_user_and_role.role_id = role.id
<!--
用户信息接口直接按数据库当前时刻派生考试标记,避免在5秒定时同步窗口内返回旧值。
已开考记录优先使用个人end_time,未开考记录回退到统一考试计划结束点。
-->
left join (
select candidate.student_id,
min(candidate.id) as exam_student_id
from exam_student candidate
inner join exam exam_plan on exam_plan.id = candidate.exam_id
where exam_plan.exam_time &lt;= current_timestamp
and coalesce(
from_unixtime(candidate.end_time / 1000),
timestampadd(
minute,
greatest(coalesce(exam_plan.duration, 0), 0),
exam_plan.exam_time
)
) &gt; current_timestamp
and coalesce(candidate.submit, 0) = 0
group by candidate.student_id
) active_exam on active_exam.student_id = user.id
where user.phone_number = #{userName} where user.phone_number = #{userName}
</select> </select>
<update id="closeExam"> <update id="closeExam">
update user update user
set exam_flag = false, set exam_flag = false,
exam_type = null,
exam_oneself_id = null exam_oneself_id = null
where id = #{id} where id = #{id}
</update> </update>
...@@ -103,33 +84,41 @@ ...@@ -103,33 +84,41 @@
simulation_id = null simulation_id = null
where id = #{studentId} where id = #{studentId}
</update> </update>
<update id="syncExamFlags"> <update id="activateDueExams">
<!-- <!--
同一条幂等SQL同时激活当前考试、清理已结束或已交卷的旧标记。未开考时使用考试 到达计划开始时间后批量激活学员考试状态。结束时间采用[开始时间, 开始时间+时长)
计划结束点;已经开考时优先使用个人倒计时end_time,避免晚开考后被计划时间提前 半开区间,与新增考试的冲突判断完全一致,因此首尾相接的两场考试不会同时生效。
清理。有效区间统一采用[开始时间, 结束时间),结束时刻立即返回false。 理论与实训考试共用该状态;submit历史空值按未交卷处理。
--> -->
update `user` target_user update `user` target_user
left join ( inner join (
select candidate.student_id, select candidate.student_id,
min(candidate.id) as exam_student_id min(candidate.id) as exam_student_id
from exam_student candidate from exam_student candidate
inner join exam exam_plan on exam_plan.id = candidate.exam_id inner join exam exam_plan on exam_plan.id = candidate.exam_id
where exam_plan.exam_time &lt;= #{now} where exam_plan.exam_time &lt;= #{now}
and coalesce( and timestampadd(
from_unixtime(candidate.end_time / 1000), minute,
timestampadd( greatest(coalesce(exam_plan.duration, 0), 0),
minute, exam_plan.exam_time
greatest(coalesce(exam_plan.duration, 0), 0),
exam_plan.exam_time
)
) &gt; #{now} ) &gt; #{now}
and coalesce(candidate.submit, 0) = 0 and coalesce(candidate.submit, 0) = 0
group by candidate.student_id group by candidate.student_id
) active_exam on active_exam.student_id = target_user.id ) active_exam on active_exam.student_id = target_user.id
set target_user.exam_flag = active_exam.exam_student_id is not null, inner join exam_student selected_candidate
target_user.exam_oneself_id = active_exam.exam_student_id on selected_candidate.id = active_exam.exam_student_id
where coalesce(target_user.exam_flag, 0) &lt;&gt; (active_exam.exam_student_id is not null) inner join exam selected_exam
or not (target_user.exam_oneself_id &lt;=> active_exam.exam_student_id) on selected_exam.id = selected_candidate.exam_id
<!--
exam_type必须取自min(candidate.id)最终选中的同一条应考记录,不能直接在聚合子查询中
随意选择exam_plan.type,否则同一学员存在多条候选记录时,考试ID和类型可能不对应。
-->
set target_user.exam_flag = true,
target_user.exam_oneself_id = active_exam.exam_student_id,
target_user.exam_type = selected_exam.type
where coalesce(target_user.exam_flag, 0) = 0
or target_user.exam_oneself_id is null
or target_user.exam_oneself_id &lt;&gt; active_exam.exam_student_id
or coalesce(target_user.exam_type, '') &lt;&gt; coalesce(selected_exam.type, '')
</update> </update>
</mapper> </mapper>
...@@ -15,9 +15,22 @@ import static org.junit.jupiter.api.Assertions.assertEquals; ...@@ -15,9 +15,22 @@ import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNotNull; import static org.junit.jupiter.api.Assertions.assertNotNull;
import static org.junit.jupiter.api.Assertions.assertTrue; import static org.junit.jupiter.api.Assertions.assertTrue;
/** 验证用户查询条件和考试标记批量更新SQL。 */ /** 验证用户查询条件以及考试标记、考试类型的查询和批量更新SQL。 */
class UserMapperXmlTest { class UserMapperXmlTest {
@Test
void queryByUserNameShouldReturnCurrentExamType() throws Exception {
Configuration configuration = parseMapper();
BoundSql boundSql = configuration.getMappedStatement(
"com.platform.business.mapper.UserMapper.queryByUserName")
.getBoundSql(Collections.singletonMap("userName", "13800000000"));
String sql = boundSql.getSql().replaceAll("\\s+", " ").toLowerCase(Locale.ROOT);
// 使用显式别名保证不依赖不同环境的MyBatis下划线转换配置,接口始终返回examType。
assertTrue(sql.contains("user.exam_type as examtype"));
}
@Test @Test
void keywordAndRoleShouldBeCombinedAsTwoCompleteConditions() throws Exception { void keywordAndRoleShouldBeCombinedAsTwoCompleteConditions() throws Exception {
Configuration configuration = parseMapper(); Configuration configuration = parseMapper();
...@@ -38,45 +51,44 @@ class UserMapperXmlTest { ...@@ -38,45 +51,44 @@ class UserMapperXmlTest {
} }
@Test @Test
void userMessageShouldDeriveExamFlagFromCurrentActiveAttempt() throws Exception { void activateDueExamsShouldUseActiveHalfOpenTimeRange() throws Exception {
Configuration configuration = parseMapper();
BoundSql boundSql = configuration.getMappedStatement(
"com.platform.business.mapper.UserMapper.queryByUserName")
.getBoundSql(Collections.singletonMap("userName", "13800000000"));
String sql = boundSql.getSql().replaceAll("\\s+", " ").toLowerCase();
// 接口查询不直接返回可能滞后的user.exam_flag,而是按当前有效考试实时派生两个字段。
assertTrue(sql.contains("active_exam.exam_student_id is not null as exam_flag"));
assertTrue(sql.contains("active_exam.exam_student_id as exam_oneself_id"));
assertTrue(sql.contains("from_unixtime(candidate.end_time / 1000)"));
assertTrue(sql.contains(") > current_timestamp"));
assertTrue(sql.contains("coalesce(candidate.submit, 0) = 0"));
}
@Test
void syncExamFlagsShouldActivateCurrentExamAndClearExpiredFlag() throws Exception {
Configuration configuration = parseMapper(); Configuration configuration = parseMapper();
BoundSql boundSql = configuration.getMappedStatement( BoundSql boundSql = configuration.getMappedStatement(
"com.platform.business.mapper.UserMapper.syncExamFlags") "com.platform.business.mapper.UserMapper.activateDueExams")
.getBoundSql(Collections.singletonMap( .getBoundSql(Collections.singletonMap(
"now", Timestamp.valueOf("2026-09-18 09:00:00"))); "now", Timestamp.valueOf("2026-09-18 09:00:00")));
String sql = boundSql.getSql().replaceAll("\\s+", " ").toLowerCase(); String sql = boundSql.getSql().replaceAll("\\s+", " ").toLowerCase();
// 开始时刻包含、结束时刻不包含;个人end_time优先,未开考记录回退到计划结束点。 // 开始时刻包含在活动区间内,结束时刻不包含,必须与考试冲突校验的边界一致。
assertTrue(sql.contains("exam_plan.exam_time <= ?")); assertTrue(sql.contains("exam_plan.exam_time <= ?"));
assertTrue(sql.contains("from_unixtime(candidate.end_time / 1000)"));
assertTrue(sql.contains("greatest(coalesce(exam_plan.duration, 0), 0)")); assertTrue(sql.contains("greatest(coalesce(exam_plan.duration, 0), 0)"));
assertTrue(sql.contains(") > ?")); assertTrue(sql.contains(") > ?"));
assertTrue(sql.contains("coalesce(candidate.submit, 0) = 0")); assertTrue(sql.contains("coalesce(candidate.submit, 0) = 0"));
// LEFT JOIN使无有效考试的用户也进入更新范围,从而同时清理flag和个人考试ID。 assertTrue(sql.contains("selected_candidate.id = active_exam.exam_student_id"));
assertTrue(sql.contains("left join (")); assertTrue(sql.contains("selected_exam.id = selected_candidate.exam_id"));
assertTrue(sql.contains("target_user.exam_flag = active_exam.exam_student_id is not null")); assertTrue(sql.contains("target_user.exam_flag = true"));
assertTrue(sql.contains("target_user.exam_oneself_id = active_exam.exam_student_id")); assertTrue(sql.contains("target_user.exam_oneself_id = active_exam.exam_student_id"));
assertTrue(sql.contains("not (target_user.exam_oneself_id <=> active_exam.exam_student_id)")); assertTrue(sql.contains("target_user.exam_type = selected_exam.type"));
assertTrue(sql.contains("coalesce(target_user.exam_type, '') "
+ "<> coalesce(selected_exam.type, '')"));
assertEquals(2, boundSql.getParameterMappings().size()); assertEquals(2, boundSql.getParameterMappings().size());
} }
@Test
void closeExamShouldClearFlagAttemptIdAndExamTypeTogether() throws Exception {
Configuration configuration = parseMapper();
BoundSql boundSql = configuration.getMappedStatement(
"com.platform.business.mapper.UserMapper.closeExam")
.getBoundSql(Collections.singletonMap("id", "student-1"));
String sql = boundSql.getSql().replaceAll("\\s+", " ").toLowerCase(Locale.ROOT);
// 三个字段描述同一场当前考试,关闭时必须原子清理,避免接口返回已结束考试的类型。
assertTrue(sql.contains("set exam_flag = false, exam_type = null, "
+ "exam_oneself_id = null"));
}
/** 解析真实Mapper XML,使测试覆盖MyBatis动态标签最终生成的SQL。 */ /** 解析真实Mapper XML,使测试覆盖MyBatis动态标签最终生成的SQL。 */
private Configuration parseMapper() throws Exception { private Configuration parseMapper() throws Exception {
String resource = "com/platform/business/mapper/UserMapper.xml"; String resource = "com/platform/business/mapper/UserMapper.xml";
......
...@@ -7,6 +7,7 @@ import com.platform.business.model.dto.StudentExamCenterQueryDTO; ...@@ -7,6 +7,7 @@ import com.platform.business.model.dto.StudentExamCenterQueryDTO;
import com.platform.business.model.entity.TheoreticalPaperDetailEntity; import com.platform.business.model.entity.TheoreticalPaperDetailEntity;
import com.platform.business.model.entity.TheoreticalPaperEntity; import com.platform.business.model.entity.TheoreticalPaperEntity;
import com.platform.business.model.entity.TheoreticalPaperStudentDetailEntity; import com.platform.business.model.entity.TheoreticalPaperStudentDetailEntity;
import com.platform.business.model.entity.UserEntity;
import com.platform.business.model.vo.StudentExamCenterVO; import com.platform.business.model.vo.StudentExamCenterVO;
import com.platform.business.service.TheoreticalPaperDetailService; import com.platform.business.service.TheoreticalPaperDetailService;
import com.platform.business.service.TheoreticalPaperService; import com.platform.business.service.TheoreticalPaperService;
...@@ -143,6 +144,15 @@ class StudentExamCenterServiceImplTest { ...@@ -143,6 +144,15 @@ class StudentExamCenterServiceImplTest {
assertEquals(1, service.start(1, "student-1")); assertEquals(1, service.start(1, "student-1"));
verify(studentDetailService, never()).saveBatch(anyCollection()); verify(studentDetailService, never()).saveBatch(anyCollection());
verify(mapper, never()).updateAttemptStarted(any(), any(), any(), any()); verify(mapper, never()).updateAttemptStarted(any(), any(), any(), any());
ArgumentCaptor<UserEntity> userCaptor = ArgumentCaptor.forClass(UserEntity.class);
verify(userService).updateById(userCaptor.capture());
UserEntity updatedUser = userCaptor.getValue();
// 重复开考同样要修复完整的当前考试状态,不能只恢复examFlag和应考记录ID。
assertEquals("student-1", updatedUser.getId());
assertEquals(Boolean.TRUE, updatedUser.getExamFlag());
assertEquals(Integer.valueOf(1), updatedUser.getExamOneselfId());
assertEquals(ExamAdminConstants.TYPE_THEORY, updatedUser.getExamType());
} }
@Test @Test
......
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