Skip to content
GitLab
Menu
Projects
Groups
Snippets
Loading...
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
Menu
Open sidebar
邬吉朋
practical_train
Commits
e34240ea
"git@gitlab.ruzhoukj.com:wujipeng/ops.git" did not exist on "e4227e8a2be36b8268d1ca6722df84bda3975789"
Commit
e34240ea
authored
Aug 17, 2026
by
wujipeng
Browse files
管理员-教务管理-理论练习
parent
226897b4
Changes
27
Hide whitespace changes
Inline
Side-by-side
practical-business/src/main/resources/com/platform/business/mapper/TheoreticalPracticeMapper.xml
View file @
e34240ea
...
@@ -35,8 +35,26 @@
...
@@ -35,8 +35,26 @@
ON
ON
cs.class_id = tp.class_id
cs.class_id = tp.class_id
<where>
<where>
<if
test=
"lev != null and lev != ''"
>
<if
test=
"query.lev != null and query.lev != ''"
>
and tp.lev = #{lev}
and tp.lev = #{query.lev}
</if>
<if
test=
"query.status == 'PENDING'"
>
and coalesce(tp.is_close, false) = false
and tp.start_time
>
#{now}
</if>
<if
test=
"query.status == 'ONGOING'"
>
and coalesce(tp.is_close, false) = false
and tp.start_time
<
= #{now}
and tp.end_time
>
= #{now}
</if>
<if
test=
"query.status == 'ENDED'"
>
and (coalesce(tp.is_close, false) = true or tp.end_time
<
#{now})
</if>
<if
test=
"query.filterStartDate != null"
>
and tp.start_time
>
= #{query.filterStartDate}
</if>
<if
test=
"query.filterEndDate != null"
>
and tp.end_time
<
date_add(#{query.filterEndDate}, interval 1 day)
</if>
</if>
</where>
</where>
GROUP BY
GROUP BY
...
@@ -58,4 +76,127 @@
...
@@ -58,4 +76,127 @@
where
where
id = #{id}
id = #{id}
</select>
</select>
<!-- 一级页面统计始终基于全量练习,不叠加列表当前筛选条件。 -->
<select
id=
"summary"
resultType=
"com.platform.business.model.vo.TheoreticalPracticeSummaryVO"
>
select count(*) as totalCount,
coalesce(sum(case
when coalesce(is_close, false) = false
and start_time
<
= #{now} and end_time
>
= #{now}
then 1 else 0 end), 0) as ongoingCount,
coalesce(sum(case
when coalesce(is_close, false) = true or end_time
<
#{now}
then 1 else 0 end), 0) as endedCount
from theoretical_practice
</select>
<!-- 使用相关子查询避免学员名单与作答记录直接连接后产生笛卡尔乘积。 -->
<select
id=
"statisticsOverview"
resultType=
"com.platform.business.model.vo.TheoreticalPracticeStatisticsOverviewVO"
>
select tp.id as practiceId,
tp.lev as level,
c.name as className,
(select count(*) from theoretical_practice_student ps
where ps.practice_id = tp.id) as assignedStudentCount,
(select count(*) from theoretical_practice_answer_record ar
where ar.practice_id = tp.id) as answerCount,
(select count(*) from theoretical_practice_answer_record ar
where ar.practice_id = tp.id and ar.is_correct = true) as correctCount,
(select coalesce(sum(ar.duration_seconds), 0)
from theoretical_practice_answer_record ar
where ar.practice_id = tp.id) as totalDurationSeconds,
(select count(distinct ar.student_id)
from theoretical_practice_answer_record ar
where ar.practice_id = tp.id) as activeStudentCount,
(select min(ar.answered_at)
from theoretical_practice_answer_record ar
where ar.practice_id = tp.id) as statisticsStartTime
from theoretical_practice tp
left join class c on c.id = tp.class_id
where tp.id = #{practiceId}
</select>
<!--
先以本期名单与对应等级题库形成“应掌握题目”集合,再左连不可变作答记录。
因此未作答知识点仍会返回,且重复作答只增加答题数,不增加已覆盖题目数。
-->
<select
id=
"knowledgeStatistics"
resultType=
"com.platform.business.model.vo.TheoreticalPracticeKnowledgeStatisticsVO"
>
select qb.first_level_name as knowledgePoint,
count(ar.id) as answerCount,
coalesce(sum(case when ar.is_correct = true then 1 else 0 end), 0) as correctCount,
coalesce(sum(ar.duration_seconds), 0) as totalDurationSeconds,
count(distinct ps.student_id) as assignedStudentCount,
count(distinct qb.id) as questionAssignmentCount,
count(distinct case when ar.id is not null
then concat(ar.student_id, '#', ar.question_id) end) as uniqueAnsweredQuestionCount
from theoretical_practice tp
join theoretical_practice_student ps on ps.practice_id = tp.id
join theoretical_practice_student_question_bank qb
on qb.student_id = ps.student_id and qb.scope = tp.lev
left join theoretical_practice_answer_record ar
on ar.practice_id = tp.id
and ar.student_id = ps.student_id
and ar.question_id = qb.id
where tp.id = #{practiceId}
<if
test=
"studentId != null and studentId != ''"
>
and ps.student_id = #{studentId}
</if>
group by qb.first_level_name
order by min(qb.chapter), qb.first_level_name
</select>
<!-- 学员名单是主表,确保未作答学员仍出现在统计分页中。 -->
<select
id=
"studentStatistics"
resultType=
"com.platform.business.model.vo.TheoreticalPracticeStudentStatisticsVO"
>
select tp.id as practiceId,
ps.student_id as studentId,
coalesce(u.nick_name, ps.name) as studentName,
c.name as className,
count(ar.id) as answerCount,
coalesce(sum(ar.duration_seconds), 0) as studyDurationSeconds,
coalesce(sum(case when ar.is_correct = true then 1 else 0 end), 0) as correctCount,
count(distinct ar.question_id) as uniqueAnsweredQuestionCount,
coalesce(bank.question_count, 0) as questionCount,
max(ar.answered_at) as lastAnswerTime
from theoretical_practice tp
join theoretical_practice_student ps on ps.practice_id = tp.id
left join user u on u.id = ps.student_id
left join class c on c.id = tp.class_id
left join theoretical_practice_answer_record ar
on ar.practice_id = tp.id and ar.student_id = ps.student_id
left join (
select assigned.practice_id, qb.student_id, count(qb.id) as question_count
from theoretical_practice_student assigned
join theoretical_practice p on p.id = assigned.practice_id
join theoretical_practice_student_question_bank qb
on qb.student_id = assigned.student_id and qb.scope = p.lev
where assigned.practice_id = #{practiceId}
group by assigned.practice_id, qb.student_id
) bank on bank.practice_id = tp.id and bank.student_id = ps.student_id
where tp.id = #{practiceId}
<if
test=
"keyword != null and keyword != ''"
>
and coalesce(u.nick_name, ps.name) like concat('%', #{keyword}, '%')
</if>
<if
test=
"studentId != null and studentId != ''"
>
and ps.student_id = #{studentId}
</if>
group by tp.id, ps.student_id, u.nick_name, ps.name, c.name, bank.question_count
order by coalesce(u.nick_name, ps.name), ps.student_id
</select>
<select
id=
"activePracticeIdsForAnswer"
resultType=
"java.lang.String"
>
select tp.id
from theoretical_practice tp
join theoretical_practice_student ps
on ps.practice_id = tp.id and ps.student_id = #{studentId}
join theoretical_practice_student_question_bank qb
on qb.id = #{questionId}
and qb.student_id = ps.student_id
and qb.scope = tp.lev
where coalesce(tp.is_close, false) = false
and tp.start_time
<
= #{now}
and tp.end_time
>
= #{now}
order by tp.start_time desc, tp.id
</select>
</mapper>
</mapper>
practical-business/src/main/resources/sql/theoretical_practice_statistics.sql
0 → 100644
View file @
e34240ea
-- 管理员端“理论练习-查看数据统计”增量数据库脚本(MySQL 5.7+/8.0)。
--
-- 设计说明:
-- 1. 旧题库表只保存最后一次答案,不能可靠回填累计次数和学习时长,因此本脚本不伪造历史数据。
-- 2. 新表从上线后逐次保存不可变作答记录;同一学员的request_id唯一,防止网络重试重复累计。
-- 3. 沿用理论练习旧表的字符串ID约定,不新增外键,避免部署时被历史孤儿数据阻断。
create
table
if
not
exists
theoretical_practice_answer_record
(
id
varchar
(
64
)
not
null
comment
'作答记录ID(雪花算法字符串)'
,
practice_id
varchar
(
64
)
not
null
comment
'理论练习ID'
,
student_id
varchar
(
64
)
not
null
comment
'学员user.id'
,
question_id
varchar
(
64
)
not
null
comment
'学员题库题目ID'
,
knowledge_point
varchar
(
255
)
null
comment
'作答时的一级知识点名称快照'
,
actual_choice
varchar
(
1000
)
not
null
comment
'本次实际答案'
,
is_correct
tinyint
(
1
)
not
null
comment
'本次判题结果:0错误/1正确'
,
duration_seconds
int
unsigned
not
null
default
0
comment
'本题实际停留秒数,接口限制0-3600'
,
request_id
varchar
(
64
)
not
null
comment
'客户端本次提交幂等号'
,
answered_at
datetime
(
3
)
not
null
comment
'服务端作答时间'
,
primary
key
(
id
),
unique
key
uk_tp_answer_student_request
(
student_id
,
request_id
),
key
idx_tp_answer_practice_time
(
practice_id
,
answered_at
),
key
idx_tp_answer_practice_student
(
practice_id
,
student_id
,
answered_at
),
key
idx_tp_answer_practice_knowledge
(
practice_id
,
knowledge_point
),
key
idx_tp_answer_practice_question
(
practice_id
,
student_id
,
question_id
)
)
engine
=
InnoDB
default
charset
=
utf8mb4
collate
=
utf8mb4_unicode_ci
comment
=
'理论练习不可变作答历史及统计数据源'
;
-- 如果目标环境曾提前创建过精简版表,以下过程会补齐统计所需索引;重复执行不会报错。
delimiter
$$
drop
procedure
if
exists
migrate_theoretical_practice_statistics
$$
create
procedure
migrate_theoretical_practice_statistics
()
begin
if
not
exists
(
select
1
from
information_schema
.
statistics
where
table_schema
=
database
()
and
table_name
=
'theoretical_practice_answer_record'
and
index_name
=
'uk_tp_answer_student_request'
)
then
alter
table
theoretical_practice_answer_record
add
unique
index
uk_tp_answer_student_request
(
student_id
,
request_id
);
end
if
;
if
not
exists
(
select
1
from
information_schema
.
statistics
where
table_schema
=
database
()
and
table_name
=
'theoretical_practice_answer_record'
and
index_name
=
'idx_tp_answer_practice_time'
)
then
alter
table
theoretical_practice_answer_record
add
index
idx_tp_answer_practice_time
(
practice_id
,
answered_at
);
end
if
;
if
not
exists
(
select
1
from
information_schema
.
statistics
where
table_schema
=
database
()
and
table_name
=
'theoretical_practice_answer_record'
and
index_name
=
'idx_tp_answer_practice_student'
)
then
alter
table
theoretical_practice_answer_record
add
index
idx_tp_answer_practice_student
(
practice_id
,
student_id
,
answered_at
);
end
if
;
if
not
exists
(
select
1
from
information_schema
.
statistics
where
table_schema
=
database
()
and
table_name
=
'theoretical_practice_answer_record'
and
index_name
=
'idx_tp_answer_practice_knowledge'
)
then
alter
table
theoretical_practice_answer_record
add
index
idx_tp_answer_practice_knowledge
(
practice_id
,
knowledge_point
);
end
if
;
if
not
exists
(
select
1
from
information_schema
.
statistics
where
table_schema
=
database
()
and
table_name
=
'theoretical_practice_answer_record'
and
index_name
=
'idx_tp_answer_practice_question'
)
then
alter
table
theoretical_practice_answer_record
add
index
idx_tp_answer_practice_question
(
practice_id
,
student_id
,
question_id
);
end
if
;
end
$$
call
migrate_theoretical_practice_statistics
()
$$
drop
procedure
if
exists
migrate_theoretical_practice_statistics
$$
delimiter
;
-- 上线后可用该只读语句确认每次练习的数据起算时间和累计记录数。
select
practice_id
,
min
(
answered_at
)
as
statistics_start_time
,
count
(
*
)
as
answer_count
from
theoretical_practice_answer_record
group
by
practice_id
;
practical-business/src/main/resources/theoretical-practice-api.md
0 → 100644
View file @
e34240ea
# 管理员端理论练习接口
本文档对应“管理员 → 教务管理 → 理论练习”一级列表、二级统计页面和个人详情弹框。
所有管理员接口继续使用
`/theoretical/practice`
前缀并要求有效管理员令牌。
## 1. 数据库升级
上线接口前执行:
```
text
practical-business/src/main/resources/sql/theoretical_practice_statistics.sql
```
脚本创建不可变作答记录表及统计索引,可重复执行。旧题库表只保存最后答案,无法可靠还原
历史作答次数和耗时,因此不回填旧数据;统计从新表产生第一条记录后开始。
## 2. 一级页面分页
`POST /theoretical/practice/page`
```
json
{
"pageNum"
:
1
,
"pageSize"
:
10
,
"lev"
:
"污水一级"
,
"status"
:
"ONGOING"
,
"filterStartDate"
:
"2026-07-01"
,
"filterEndDate"
:
"2026-07-31"
}
```
-
`status`
:
`PENDING`
、
`ONGOING`
、
`ENDED`
,忽略大小写。
-
时间采用完整包含口径:练习开始时间不早于起始日,结束时间不晚于结束日当天。
-
返回仍为项目统一
`TableDataInfo`
。原有中文
`status`
保留,并新增
`statusCode`
。
-
`ENDED`
同时包含自然到期和手动关闭;中文状态仍区分“已结束”和“已手动关闭”。
## 3. 一级页面全局统计
`GET /theoretical/practice/summary`
```
json
{
"code"
:
200
,
"data"
:
{
"totalCount"
:
12
,
"ongoingCount"
:
4
,
"endedCount"
:
6
}
}
```
顶部数字始终统计全部练习,不随列表筛选条件变化。未开始练习只计入总数。
## 4. 二级页面总体与知识点统计
`GET /theoretical/practice/{practiceId}/statistics/overview`
核心返回字段:
| 字段 | 说明 |
|---|---|
| assignedStudentCount | 本期分配学员数,人均指标的分母 |
| averageAnswerCount | 人均累计提交次数,保留两位小数 |
| averageStudyDurationSeconds | 人均学习时长(秒) |
| averageAccuracyRate | 正确提交数/全部提交数,百分比 |
| activeStudentCount | 至少提交过一次答案的学员数 |
| statisticsStartTime | 本期第一条新作答记录时间,无记录时为空 |
| knowledgePoints | 知识点答题数、时长、正确率和掌握度 |
## 5. 二级页面学员分页
`POST /theoretical/practice/{practiceId}/statistics/students/page`
```
json
{
"pageNum"
:
1
,
"pageSize"
:
10
,
"keyword"
:
"张"
}
```
名单以
`theoretical_practice_student`
为准,未作答学员也会返回。主要字段包括
`answerCount`
、
`studyDurationSeconds`
、
`accuracyRate`
、
`masteryRate`
、
`statusCode`
、
`statusLabel`
和
`lastAnswerTime`
。
学员状态:
-
无作答:
`NOT_STARTED`
/ 未开始;
-
正确率大于等于80%:
`EXCELLENT`
/ 优秀;
-
正确率大于等于60%:
`GOOD`
/ 良好;
-
其余:
`NEEDS_IMPROVEMENT`
/ 待加强。
## 6. 个人详情与Word报告
-
`GET /theoretical/practice/{practiceId}/statistics/students/{studentId}`
-
`GET /theoretical/practice/{practiceId}/statistics/students/{studentId}/export`
详情返回个人累计指标和知识点能力列表,前端可用
`knowledgePoints[].masteryRate`
绘制雷达图。
导出接口返回
`.docx`
,包含学员、班级、累计指标、知识点明细和数据更新时间。
## 7. 学生答题接口接入
原路径保持不变:
`POST /theoretical/practice/student/question/bank/update`
```
json
{
"id"
:
"题目ID"
,
"actualChoice"
:
"A"
,
"requestId"
:
"7d2630eb-c9d1-4dd1-a939-8d7b43fcf756"
,
"durationSeconds"
:
42
}
```
-
`requestId`
:每次点击提交时新建、网络重试时复用,最长64字符。
-
`durationSeconds`
:当前题实际停留秒数,范围0到3600。
-
服务端从JWT确定学员,验证题目归属和唯一开放练习,并自行判定正确结果。
-
同一学员的相同
`requestId`
只记录一次;题库当前答案与历史记录在同一事务中提交。
## 8. 统计口径
-
答题数:本期所有有效提交,重复作答累计。
-
正确率:正确提交数 ÷ 全部提交数。
-
掌握度:已答过的不同题目数 ÷ 当前学员本等级题库题目数。
-
人均答题数和人均时长:总量 ÷ 本期全部分配学员,未作答学员按0计算。
-
所有比例保留两位小数,零分母返回
`0.00`
。
practical-business/src/test/java/com/platform/business/controller/TheoreticalPracticeControllerTest.java
0 → 100644
View file @
e34240ea
package
com.platform.business.controller
;
import
com.platform.business.model.entity.UserEntity
;
import
com.platform.business.service.AuthenticatedUserService
;
import
com.platform.business.service.TheoreticalPracticeService
;
import
com.platform.exception.BusinessException
;
import
org.junit.jupiter.api.BeforeEach
;
import
org.junit.jupiter.api.Test
;
import
org.junit.jupiter.api.extension.ExtendWith
;
import
org.mockito.Mock
;
import
org.mockito.junit.jupiter.MockitoExtension
;
import
org.springframework.mock.web.MockHttpServletRequest
;
import
org.springframework.mock.web.MockHttpServletResponse
;
import
org.springframework.test.util.ReflectionTestUtils
;
import
static
org
.
junit
.
jupiter
.
api
.
Assertions
.
assertThrows
;
import
static
org
.
mockito
.
Mockito
.
doThrow
;
import
static
org
.
mockito
.
Mockito
.
times
;
import
static
org
.
mockito
.
Mockito
.
verify
;
import
static
org
.
mockito
.
Mockito
.
when
;
/** 理论练习新增统计与导出入口的管理员鉴权和参数转发测试。 */
@ExtendWith
(
MockitoExtension
.
class
)
class
TheoreticalPracticeControllerTest
{
@Mock
private
TheoreticalPracticeService
theoreticalPracticeService
;
@Mock
private
AuthenticatedUserService
authenticatedUserService
;
private
TheoreticalPracticeController
controller
;
@BeforeEach
void
setUp
()
{
controller
=
new
TheoreticalPracticeController
();
ReflectionTestUtils
.
setField
(
controller
,
"theoreticalPracticeService"
,
theoreticalPracticeService
);
ReflectionTestUtils
.
setField
(
controller
,
"authenticatedUserService"
,
authenticatedUserService
);
}
@Test
void
statisticsEndpointsShouldRequireAdminAndForwardIds
()
{
MockHttpServletRequest
request
=
new
MockHttpServletRequest
();
MockHttpServletResponse
response
=
new
MockHttpServletResponse
();
UserEntity
administrator
=
new
UserEntity
();
when
(
authenticatedUserService
.
current
(
request
)).
thenReturn
(
administrator
);
controller
.
summary
(
request
);
controller
.
statisticsOverview
(
"p1"
,
request
);
controller
.
studentStatisticsDetail
(
"p1"
,
"s1"
,
request
);
controller
.
exportStudentStatistics
(
"p1"
,
"s1"
,
request
,
response
);
verify
(
authenticatedUserService
,
times
(
4
)).
requireAdmin
(
administrator
);
verify
(
theoreticalPracticeService
).
summary
();
verify
(
theoreticalPracticeService
).
statisticsOverview
(
"p1"
);
verify
(
theoreticalPracticeService
).
studentStatisticsDetail
(
"p1"
,
"s1"
);
verify
(
theoreticalPracticeService
).
exportStudentStatistics
(
"p1"
,
"s1"
,
response
);
}
@Test
void
statisticsEndpointShouldStopWhenCurrentUserIsNotAdmin
()
{
MockHttpServletRequest
request
=
new
MockHttpServletRequest
();
UserEntity
student
=
new
UserEntity
();
when
(
authenticatedUserService
.
current
(
request
)).
thenReturn
(
student
);
doThrow
(
new
BusinessException
(
"只有管理员可以访问管理端接口"
))
.
when
(
authenticatedUserService
).
requireAdmin
(
student
);
assertThrows
(
BusinessException
.
class
,
()
->
controller
.
statisticsOverview
(
"p1"
,
request
));
}
}
practical-business/src/test/java/com/platform/business/mapper/TheoreticalPracticeMapperXmlTest.java
0 → 100644
View file @
e34240ea
package
com.platform.business.mapper
;
import
org.apache.ibatis.builder.xml.XMLMapperBuilder
;
import
org.apache.ibatis.io.Resources
;
import
org.apache.ibatis.session.Configuration
;
import
org.junit.jupiter.api.Test
;
import
java.io.InputStream
;
import
static
org
.
junit
.
jupiter
.
api
.
Assertions
.
assertTrue
;
/** 在应用启动前解析理论练习动态SQL和幂等写入SQL。 */
class
TheoreticalPracticeMapperXmlTest
{
@Test
void
theoreticalPracticeMapperShouldRegisterStatisticsStatements
()
throws
Exception
{
Configuration
configuration
=
parse
(
"com/platform/business/mapper/TheoreticalPracticeMapper.xml"
);
assertTrue
(
configuration
.
hasStatement
(
"com.platform.business.mapper.TheoreticalPracticeMapper.summary"
));
assertTrue
(
configuration
.
hasStatement
(
"com.platform.business.mapper.TheoreticalPracticeMapper.statisticsOverview"
));
assertTrue
(
configuration
.
hasStatement
(
"com.platform.business.mapper.TheoreticalPracticeMapper.knowledgeStatistics"
));
assertTrue
(
configuration
.
hasStatement
(
"com.platform.business.mapper.TheoreticalPracticeMapper.studentStatistics"
));
assertTrue
(
configuration
.
hasStatement
(
"com.platform.business.mapper.TheoreticalPracticeMapper.activePracticeIdsForAnswer"
));
}
@Test
void
answerRecordMapperShouldRegisterIdempotentStatements
()
throws
Exception
{
Configuration
configuration
=
parse
(
"com/platform/business/mapper/TheoreticalPracticeAnswerRecordMapper.xml"
);
assertTrue
(
configuration
.
hasStatement
(
"com.platform.business.mapper.TheoreticalPracticeAnswerRecordMapper.insertIdempotently"
));
assertTrue
(
configuration
.
hasStatement
(
"com.platform.business.mapper.TheoreticalPracticeAnswerRecordMapper.previousAffectedRows"
));
}
private
Configuration
parse
(
String
resource
)
throws
Exception
{
Configuration
configuration
=
new
Configuration
();
try
(
InputStream
input
=
Resources
.
getResourceAsStream
(
resource
))
{
XMLMapperBuilder
builder
=
new
XMLMapperBuilder
(
input
,
configuration
,
resource
,
configuration
.
getSqlFragments
());
builder
.
parse
();
}
return
configuration
;
}
}
practical-business/src/test/java/com/platform/business/service/impl/TheoreticalPracticeAnswerServiceImplTest.java
0 → 100644
View file @
e34240ea
package
com.platform.business.service.impl
;
import
com.platform.business.mapper.TheoreticalPracticeAnswerRecordMapper
;
import
com.platform.business.mapper.TheoreticalPracticeMapper
;
import
com.platform.business.mapper.TheoreticalPracticeStudentQuestionBankMapper
;
import
com.platform.business.model.dto.TheoreticalPracticeStudentQuestionBankDTO
;
import
com.platform.business.model.entity.TheoreticalPracticeAnswerRecordEntity
;
import
com.platform.business.model.entity.TheoreticalPracticeStudentQuestionBankEntity
;
import
com.platform.exception.BusinessException
;
import
org.junit.jupiter.api.BeforeEach
;
import
org.junit.jupiter.api.Test
;
import
org.junit.jupiter.api.extension.ExtendWith
;
import
org.mockito.ArgumentCaptor
;
import
org.mockito.Mock
;
import
org.mockito.junit.jupiter.MockitoExtension
;
import
org.springframework.test.util.ReflectionTestUtils
;
import
java.util.Collections
;
import
static
org
.
junit
.
jupiter
.
api
.
Assertions
.
assertEquals
;
import
static
org
.
junit
.
jupiter
.
api
.
Assertions
.
assertThrows
;
import
static
org
.
mockito
.
ArgumentMatchers
.
any
;
import
static
org
.
mockito
.
Mockito
.
never
;
import
static
org
.
mockito
.
Mockito
.
verify
;
import
static
org
.
mockito
.
Mockito
.
when
;
/** 学员逐题上报、身份校验和幂等累计测试。 */
@ExtendWith
(
MockitoExtension
.
class
)
class
TheoreticalPracticeAnswerServiceImplTest
{
@Mock
private
TheoreticalPracticeStudentQuestionBankMapper
questionMapper
;
@Mock
private
TheoreticalPracticeAnswerRecordMapper
answerRecordMapper
;
@Mock
private
TheoreticalPracticeMapper
practiceMapper
;
private
TheoreticalPracticeStudentQuestionBankServiceImpl
service
;
@BeforeEach
void
setUp
()
{
service
=
new
TheoreticalPracticeStudentQuestionBankServiceImpl
();
ReflectionTestUtils
.
setField
(
service
,
"baseMapper"
,
questionMapper
);
ReflectionTestUtils
.
setField
(
service
,
"answerRecordMapper"
,
answerRecordMapper
);
ReflectionTestUtils
.
setField
(
service
,
"theoreticalPracticeMapper"
,
practiceMapper
);
}
@Test
void
answerShouldWriteServerJudgementDurationAndCurrentStateInOneFlow
()
{
mockQuestion
(
"s1"
);
when
(
practiceMapper
.
activePracticeIdsForAnswer
(
org
.
mockito
.
ArgumentMatchers
.
eq
(
"s1"
),
org
.
mockito
.
ArgumentMatchers
.
eq
(
"q1"
),
any
()))
.
thenReturn
(
Collections
.
singletonList
(
"p1"
));
when
(
answerRecordMapper
.
previousAffectedRows
()).
thenReturn
(
1
);
when
(
questionMapper
.
updateById
(
any
(
TheoreticalPracticeStudentQuestionBankEntity
.
class
)))
.
thenReturn
(
1
);
service
.
edit
(
validRequest
(),
"s1"
);
ArgumentCaptor
<
TheoreticalPracticeAnswerRecordEntity
>
record
=
ArgumentCaptor
.
forClass
(
TheoreticalPracticeAnswerRecordEntity
.
class
);
verify
(
answerRecordMapper
).
insertIdempotently
(
record
.
capture
());
assertEquals
(
"p1"
,
record
.
getValue
().
getPracticeId
());
assertEquals
(
Boolean
.
TRUE
,
record
.
getValue
().
getCorrect
());
assertEquals
(
42
,
record
.
getValue
().
getDurationSeconds
());
ArgumentCaptor
<
TheoreticalPracticeStudentQuestionBankEntity
>
update
=
ArgumentCaptor
.
forClass
(
TheoreticalPracticeStudentQuestionBankEntity
.
class
);
verify
(
questionMapper
).
updateById
(
update
.
capture
());
assertEquals
(
"q1"
,
update
.
getValue
().
getId
());
assertEquals
(
Boolean
.
TRUE
,
update
.
getValue
().
getResult
());
}
@Test
void
duplicateRequestShouldNotUpdateCurrentStateAgain
()
{
mockQuestion
(
"s1"
);
when
(
practiceMapper
.
activePracticeIdsForAnswer
(
org
.
mockito
.
ArgumentMatchers
.
eq
(
"s1"
),
org
.
mockito
.
ArgumentMatchers
.
eq
(
"q1"
),
any
()))
.
thenReturn
(
Collections
.
singletonList
(
"p1"
));
when
(
answerRecordMapper
.
previousAffectedRows
()).
thenReturn
(
0
);
service
.
edit
(
validRequest
(),
"s1"
);
verify
(
questionMapper
,
never
()).
updateById
(
any
(
TheoreticalPracticeStudentQuestionBankEntity
.
class
));
}
@Test
void
answerShouldRejectAnotherStudentsQuestion
()
{
mockQuestion
(
"s2"
);
assertThrows
(
BusinessException
.
class
,
()
->
service
.
edit
(
validRequest
(),
"s1"
));
verify
(
answerRecordMapper
,
never
()).
insertIdempotently
(
any
());
}
@Test
void
answerShouldRejectMissingActivePracticeAndInvalidDuration
()
{
mockQuestion
(
"s1"
);
when
(
practiceMapper
.
activePracticeIdsForAnswer
(
org
.
mockito
.
ArgumentMatchers
.
eq
(
"s1"
),
org
.
mockito
.
ArgumentMatchers
.
eq
(
"q1"
),
any
()))
.
thenReturn
(
Collections
.
emptyList
());
assertThrows
(
BusinessException
.
class
,
()
->
service
.
edit
(
validRequest
(),
"s1"
));
TheoreticalPracticeStudentQuestionBankDTO
invalid
=
validRequest
();
invalid
.
setDurationSeconds
(
3601
);
assertThrows
(
BusinessException
.
class
,
()
->
service
.
edit
(
invalid
,
"s1"
));
}
private
void
mockQuestion
(
String
studentId
)
{
TheoreticalPracticeStudentQuestionBankEntity
question
=
new
TheoreticalPracticeStudentQuestionBankEntity
();
question
.
setId
(
"q1"
);
question
.
setStudentId
(
studentId
);
question
.
setFirstLevelName
(
"基本要求"
);
question
.
setAnswer
(
"A"
);
when
(
questionMapper
.
selectById
(
"q1"
)).
thenReturn
(
question
);
}
private
TheoreticalPracticeStudentQuestionBankDTO
validRequest
()
{
TheoreticalPracticeStudentQuestionBankDTO
request
=
new
TheoreticalPracticeStudentQuestionBankDTO
();
request
.
setId
(
"q1"
);
request
.
setActualChoice
(
"A"
);
request
.
setRequestId
(
"request-1"
);
request
.
setDurationSeconds
(
42
);
return
request
;
}
}
practical-business/src/test/java/com/platform/business/service/impl/TheoreticalPracticeServiceImplTest.java
0 → 100644
View file @
e34240ea
package
com.platform.business.service.impl
;
import
com.platform.business.mapper.TheoreticalPracticeMapper
;
import
com.platform.business.model.dto.TheoreticalPracticeDTO
;
import
com.platform.business.model.dto.TheoreticalPracticeStatisticsQueryDTO
;
import
com.platform.business.model.entity.TheoreticalPracticeEntity
;
import
com.platform.business.model.vo.TheoreticalPracticeKnowledgeStatisticsVO
;
import
com.platform.business.model.vo.TheoreticalPracticeStatisticsOverviewVO
;
import
com.platform.business.model.vo.TheoreticalPracticeStudentStatisticsDetailVO
;
import
com.platform.business.model.vo.TheoreticalPracticeStudentStatisticsVO
;
import
com.platform.business.model.vo.TheoreticalPracticeSummaryVO
;
import
com.platform.exception.BusinessException
;
import
org.apache.poi.xwpf.usermodel.XWPFDocument
;
import
org.junit.jupiter.api.BeforeEach
;
import
org.junit.jupiter.api.Test
;
import
org.junit.jupiter.api.extension.ExtendWith
;
import
org.mockito.Mock
;
import
org.mockito.junit.jupiter.MockitoExtension
;
import
org.springframework.mock.web.MockHttpServletResponse
;
import
org.springframework.test.util.ReflectionTestUtils
;
import
java.io.ByteArrayInputStream
;
import
java.math.BigDecimal
;
import
java.util.Arrays
;
import
java.util.Collections
;
import
java.util.Date
;
import
java.util.List
;
import
static
org
.
junit
.
jupiter
.
api
.
Assertions
.
assertEquals
;
import
static
org
.
junit
.
jupiter
.
api
.
Assertions
.
assertNotNull
;
import
static
org
.
junit
.
jupiter
.
api
.
Assertions
.
assertThrows
;
import
static
org
.
mockito
.
ArgumentMatchers
.
any
;
import
static
org
.
mockito
.
Mockito
.
verify
;
import
static
org
.
mockito
.
Mockito
.
when
;
/** 理论练习筛选规范化、统计口径和Word报告测试。 */
@ExtendWith
(
MockitoExtension
.
class
)
class
TheoreticalPracticeServiceImplTest
{
@Mock
private
TheoreticalPracticeMapper
mapper
;
private
TheoreticalPracticeServiceImpl
service
;
@BeforeEach
void
setUp
()
{
service
=
new
TheoreticalPracticeServiceImpl
();
ReflectionTestUtils
.
setField
(
service
,
"baseMapper"
,
mapper
);
}
@Test
void
pageShouldNormalizeLowercaseStatusAndRejectInvalidDateRange
()
{
when
(
mapper
.
page
(
any
(
TheoreticalPracticeDTO
.
class
),
any
(
Date
.
class
)))
.
thenReturn
(
Collections
.
emptyList
());
TheoreticalPracticeDTO
query
=
new
TheoreticalPracticeDTO
();
query
.
setStatus
(
" ongoing "
);
service
.
page
(
query
);
assertEquals
(
"ONGOING"
,
query
.
getStatus
());
verify
(
mapper
).
page
(
org
.
mockito
.
ArgumentMatchers
.
eq
(
query
),
org
.
mockito
.
ArgumentMatchers
.
any
(
Date
.
class
));
query
.
setFilterStartDate
(
new
Date
(
2000
));
query
.
setFilterEndDate
(
new
Date
(
1000
));
assertThrows
(
BusinessException
.
class
,
()
->
service
.
page
(
query
));
}
@Test
void
overviewShouldUseAllAssignedStudentsAndRoundRates
()
{
TheoreticalPracticeStatisticsOverviewVO
raw
=
new
TheoreticalPracticeStatisticsOverviewVO
();
raw
.
setPracticeId
(
"p1"
);
raw
.
setAssignedStudentCount
(
2L
);
raw
.
setAnswerCount
(
3L
);
raw
.
setCorrectCount
(
2L
);
raw
.
setTotalDurationSeconds
(
121L
);
raw
.
setActiveStudentCount
(
1L
);
when
(
mapper
.
statisticsOverview
(
"p1"
)).
thenReturn
(
raw
);
TheoreticalPracticeKnowledgeStatisticsVO
point
=
new
TheoreticalPracticeKnowledgeStatisticsVO
();
point
.
setKnowledgePoint
(
"基本要求"
);
point
.
setAnswerCount
(
3L
);
point
.
setCorrectCount
(
2L
);
point
.
setTotalDurationSeconds
(
121L
);
point
.
setQuestionAssignmentCount
(
4L
);
point
.
setUniqueAnsweredQuestionCount
(
2L
);
when
(
mapper
.
knowledgeStatistics
(
"p1"
,
null
)).
thenReturn
(
Collections
.
singletonList
(
point
));
TheoreticalPracticeStatisticsOverviewVO
result
=
service
.
statisticsOverview
(
"p1"
);
assertEquals
(
new
BigDecimal
(
"1.50"
),
result
.
getAverageAnswerCount
());
assertEquals
(
61L
,
result
.
getAverageStudyDurationSeconds
());
assertEquals
(
new
BigDecimal
(
"66.67"
),
result
.
getAverageAccuracyRate
());
assertEquals
(
new
BigDecimal
(
"50.00"
),
result
.
getKnowledgePoints
().
get
(
0
).
getMasteryRate
());
assertEquals
(
61L
,
result
.
getKnowledgePoints
().
get
(
0
).
getAverageStudyDurationSeconds
());
}
@Test
void
studentRowsShouldIncludeNotStartedAndApplyThresholds
()
{
when
(
mapper
.
selectById
(
"p1"
)).
thenReturn
(
new
TheoreticalPracticeEntity
());
TheoreticalPracticeStudentStatisticsVO
notStarted
=
student
(
"s1"
,
0
,
0
,
0
,
10
);
TheoreticalPracticeStudentStatisticsVO
excellent
=
student
(
"s2"
,
10
,
8
,
5
,
10
);
when
(
mapper
.
studentStatistics
(
"p1"
,
null
,
null
))
.
thenReturn
(
Arrays
.
asList
(
notStarted
,
excellent
));
List
<
TheoreticalPracticeStudentStatisticsVO
>
rows
=
service
.
studentStatistics
(
"p1"
,
new
TheoreticalPracticeStatisticsQueryDTO
());
assertEquals
(
"NOT_STARTED"
,
rows
.
get
(
0
).
getStatusCode
());
assertEquals
(
"未开始"
,
rows
.
get
(
0
).
getStatusLabel
());
assertEquals
(
"EXCELLENT"
,
rows
.
get
(
1
).
getStatusCode
());
assertEquals
(
new
BigDecimal
(
"80.00"
),
rows
.
get
(
1
).
getAccuracyRate
());
assertEquals
(
new
BigDecimal
(
"50.00"
),
rows
.
get
(
1
).
getMasteryRate
());
}
@Test
void
detailShouldRejectStudentOutsidePractice
()
{
when
(
mapper
.
selectById
(
"p1"
)).
thenReturn
(
new
TheoreticalPracticeEntity
());
when
(
mapper
.
studentStatistics
(
"p1"
,
null
,
"s1"
)).
thenReturn
(
Collections
.
emptyList
());
assertThrows
(
BusinessException
.
class
,
()
->
service
.
studentStatisticsDetail
(
"p1"
,
"s1"
));
}
@Test
void
exportShouldProduceReadableWordWithSummaryAndKnowledgeTable
()
throws
Exception
{
when
(
mapper
.
selectById
(
"p1"
)).
thenReturn
(
new
TheoreticalPracticeEntity
());
TheoreticalPracticeStudentStatisticsVO
row
=
student
(
"s1"
,
5
,
4
,
3
,
10
);
row
.
setStudentName
(
"张明"
);
row
.
setClassName
(
"污水一级一班"
);
row
.
setStudyDurationSeconds
(
3661L
);
when
(
mapper
.
studentStatistics
(
"p1"
,
null
,
"s1"
))
.
thenReturn
(
Collections
.
singletonList
(
row
));
TheoreticalPracticeKnowledgeStatisticsVO
point
=
new
TheoreticalPracticeKnowledgeStatisticsVO
();
point
.
setKnowledgePoint
(
"基本要求"
);
point
.
setAnswerCount
(
5L
);
point
.
setCorrectCount
(
4L
);
point
.
setTotalDurationSeconds
(
3661L
);
point
.
setQuestionAssignmentCount
(
10L
);
point
.
setUniqueAnsweredQuestionCount
(
3L
);
when
(
mapper
.
knowledgeStatistics
(
"p1"
,
"s1"
))
.
thenReturn
(
Collections
.
singletonList
(
point
));
MockHttpServletResponse
response
=
new
MockHttpServletResponse
();
service
.
exportStudentStatistics
(
"p1"
,
"s1"
,
response
);
assertEquals
(
"application/vnd.openxmlformats-officedocument.wordprocessingml.document"
,
response
.
getContentType
());
assertNotNull
(
response
.
getHeader
(
"Content-Disposition"
));
try
(
XWPFDocument
document
=
new
XWPFDocument
(
new
ByteArrayInputStream
(
response
.
getContentAsByteArray
())))
{
assertEquals
(
"理论练习个人报告"
,
document
.
getParagraphs
().
get
(
0
).
getText
());
assertEquals
(
2
,
document
.
getTables
().
size
());
assertEquals
(
"基本要求"
,
document
.
getTables
().
get
(
1
).
getRow
(
1
).
getCell
(
1
).
getText
());
}
}
private
TheoreticalPracticeStudentStatisticsVO
student
(
String
id
,
long
answers
,
long
correct
,
long
unique
,
long
questionCount
)
{
TheoreticalPracticeStudentStatisticsVO
row
=
new
TheoreticalPracticeStudentStatisticsVO
();
row
.
setStudentId
(
id
);
row
.
setAnswerCount
(
answers
);
row
.
setCorrectCount
(
correct
);
row
.
setUniqueAnsweredQuestionCount
(
unique
);
row
.
setQuestionCount
(
questionCount
);
row
.
setStudyDurationSeconds
(
0L
);
return
row
;
}
}
Prev
1
2
Next
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
.
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment