Commit 9c8e2241 authored by 王蔡翾's avatar 王蔡翾
Browse files

考试标记bug修复

parent 04bb4f11
......@@ -53,9 +53,9 @@
user.del_flag,
role.id as roleId,
role.name as roleName,
user.exam_flag,
user.exam_type as examType,
user.exam_oneself_id,
active_exam.exam_student_id is not null as exam_flag,
selected_exam.type as examType,
active_exam.exam_student_id as exam_oneself_id,
user.level,
user.simulation_flag,
user.simulation_id
......@@ -69,6 +69,31 @@
role
on
sys_user_and_role.role_id = role.id
<!--
用户信息接口实时派生当前考试,避免定时同步间隔内返回已经结束的旧标记。
已开考记录优先使用个人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
left join exam_student selected_candidate
on selected_candidate.id = active_exam.exam_student_id
left join exam selected_exam
on selected_exam.id = selected_candidate.exam_id
where user.phone_number = #{userName}
</select>
<update id="closeExam">
......@@ -84,41 +109,43 @@
simulation_id = null
where id = #{studentId}
</update>
<update id="activateDueExams">
<update id="syncExamFlags">
<!--
到达计划开始时间后批量激活学员考试状态。结束时间采用[开始时间, 开始时间+时长)
半开区间,与新增考试的冲突判断完全一致,因此首尾相接的两场考试不会同时生效。
理论与实训考试共用该状态;submit历史空值按未交卷处理。
同一条幂等SQL同时激活当前考试、清理已结束或已交卷的旧标记。未开考时使用考试
计划结束点;已经开考时优先使用个人倒计时end_time。有效区间统一采用
[开始时间, 结束时间),结束时刻立即清理examFlag、应考记录ID和考试类型。
-->
update `user` target_user
inner join (
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;= #{now}
and timestampadd(
and coalesce(
from_unixtime(candidate.end_time / 1000),
timestampadd(
minute,
greatest(coalesce(exam_plan.duration, 0), 0),
exam_plan.exam_time
)
) &gt; #{now}
and coalesce(candidate.submit, 0) = 0
group by candidate.student_id
) active_exam on active_exam.student_id = target_user.id
inner join exam_student selected_candidate
left join exam_student selected_candidate
on selected_candidate.id = active_exam.exam_student_id
inner join exam selected_exam
left join exam selected_exam
on selected_exam.id = selected_candidate.exam_id
<!--
exam_type必须取自min(candidate.id)最终选中的同一条应考记录,不能直接在聚合子查询中
随意选择exam_plan.type,否则同一学员存在多条候选记录时,考试ID和类型可能不对应。
examType必须取自min(candidate.id)最终选中的同一场考试;LEFT JOIN同时保留没有
有效考试的用户,使三个持久化字段能够被原子清空。
-->
set target_user.exam_flag = true,
set target_user.exam_flag = active_exam.exam_student_id is not null,
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, '')
where coalesce(target_user.exam_flag, 0) &lt;&gt; (active_exam.exam_student_id is not null)
or not (target_user.exam_oneself_id &lt;=> active_exam.exam_student_id)
or not (target_user.exam_type &lt;=> selected_exam.type)
</update>
</mapper>
......@@ -19,7 +19,7 @@ import static org.junit.jupiter.api.Assertions.assertTrue;
class UserMapperXmlTest {
@Test
void queryByUserNameShouldReturnCurrentExamType() throws Exception {
void queryByUserNameShouldDeriveCurrentExamStateAndType() throws Exception {
Configuration configuration = parseMapper();
BoundSql boundSql = configuration.getMappedStatement(
......@@ -27,8 +27,15 @@ class UserMapperXmlTest {
.getBoundSql(Collections.singletonMap("userName", "13800000000"));
String sql = boundSql.getSql().replaceAll("\\s+", " ").toLowerCase(Locale.ROOT);
// 使用显式别名保证不依赖不同环境的MyBatis下划线转换配置,接口始终返回examType。
assertTrue(sql.contains("user.exam_type as examtype"));
// 接口不读取可能滞后的持久化标记,而是从同一条当前有效应考记录派生三个字段。
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("selected_exam.type as examtype"));
assertTrue(sql.contains("from_unixtime(candidate.end_time / 1000)"));
assertTrue(sql.contains(") > current_timestamp"));
assertTrue(sql.contains("coalesce(candidate.submit, 0) = 0"));
assertTrue(sql.contains("selected_candidate.id = active_exam.exam_student_id"));
assertTrue(sql.contains("selected_exam.id = selected_candidate.exam_id"));
}
@Test
......@@ -51,27 +58,30 @@ class UserMapperXmlTest {
}
@Test
void activateDueExamsShouldUseActiveHalfOpenTimeRange() throws Exception {
void syncExamFlagsShouldActivateCurrentExamAndClearExpiredState() throws Exception {
Configuration configuration = parseMapper();
BoundSql boundSql = configuration.getMappedStatement(
"com.platform.business.mapper.UserMapper.activateDueExams")
"com.platform.business.mapper.UserMapper.syncExamFlags")
.getBoundSql(Collections.singletonMap(
"now", Timestamp.valueOf("2026-09-18 09:00:00")));
String sql = boundSql.getSql().replaceAll("\\s+", " ").toLowerCase();
// 开始时刻包含在活动区间内,结束时刻不包含,必须与考试冲突校验的边界一致。
// 开始时刻包含、结束时刻不包含;个人end_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(") > ?"));
assertTrue(sql.contains("coalesce(candidate.submit, 0) = 0"));
assertTrue(sql.contains("selected_candidate.id = active_exam.exam_student_id"));
assertTrue(sql.contains("selected_exam.id = selected_candidate.exam_id"));
assertTrue(sql.contains("target_user.exam_flag = true"));
// LEFT JOIN使无有效考试的用户也能进入更新,三个字段必须一起激活或清空。
assertTrue(sql.contains("left join ("));
assertTrue(sql.contains("target_user.exam_flag = active_exam.exam_student_id is not null"));
assertTrue(sql.contains("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, '')"));
assertTrue(sql.contains("not (target_user.exam_oneself_id <=> active_exam.exam_student_id)"));
assertTrue(sql.contains("not (target_user.exam_type <=> selected_exam.type)"));
assertEquals(2, boundSql.getParameterMappings().size());
}
......
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