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

bug修复

parent 9b5c1b46
......@@ -5,6 +5,7 @@ import com.platform.business.model.vo.TheoreticalPracticeStudentQuestionBankVO;
import com.platform.business.service.TheoreticalPracticeStudentQuestionBankService;
import com.platform.business.service.AuthenticatedUserService;
import com.platform.business.model.entity.UserEntity;
import com.platform.exception.BusinessException;
import com.platform.controller.BaseController;
import com.platform.model.resp.Result;
import com.platform.page.TableDataInfo;
......@@ -28,13 +29,19 @@ public class TheoreticalPracticeStudentQuestionBankController extends BaseContro
@ApiOperation(value = "一级分页")
@PostMapping("/one/page")
public TableDataInfo onePage(@RequestBody TheoreticalPracticeStudentQuestionBankDTO theoreticalPracticeStudentQuestionBankDTO) {
public TableDataInfo onePage(
@RequestBody TheoreticalPracticeStudentQuestionBankDTO theoreticalPracticeStudentQuestionBankDTO,
HttpServletRequest request) {
bindCurrentStudent(theoreticalPracticeStudentQuestionBankDTO, request);
return getDataTable(theoreticalPracticeStudentQuestionBankService.onePage(theoreticalPracticeStudentQuestionBankDTO));
}
@ApiOperation(value = "二级分页")
@PostMapping("/two/page")
public TableDataInfo twoPage(@RequestBody TheoreticalPracticeStudentQuestionBankDTO theoreticalPracticeStudentQuestionBankDTO) {
public TableDataInfo twoPage(
@RequestBody TheoreticalPracticeStudentQuestionBankDTO theoreticalPracticeStudentQuestionBankDTO,
HttpServletRequest request) {
bindCurrentStudent(theoreticalPracticeStudentQuestionBankDTO, request);
return getDataTable(theoreticalPracticeStudentQuestionBankService.twoPage(theoreticalPracticeStudentQuestionBankDTO));
}
......@@ -51,7 +58,10 @@ public class TheoreticalPracticeStudentQuestionBankController extends BaseContro
@ApiOperation(value = "题库总览")
@PostMapping("/overview")
public Result<List<TheoreticalPracticeStudentQuestionBankVO>> overview(@RequestBody TheoreticalPracticeStudentQuestionBankDTO theoreticalPracticeStudentQuestionBankDTO) {
public Result<List<TheoreticalPracticeStudentQuestionBankVO>> overview(
@RequestBody TheoreticalPracticeStudentQuestionBankDTO theoreticalPracticeStudentQuestionBankDTO,
HttpServletRequest request) {
bindCurrentStudent(theoreticalPracticeStudentQuestionBankDTO, request);
return Result.data(theoreticalPracticeStudentQuestionBankService.overview(theoreticalPracticeStudentQuestionBankDTO));
}
......@@ -63,7 +73,10 @@ public class TheoreticalPracticeStudentQuestionBankController extends BaseContro
@ApiOperation(value = "重新作答")
@PostMapping("/reAnswer")
public Result<Boolean> reAnswer(@RequestBody TheoreticalPracticeStudentQuestionBankDTO theoreticalPracticeStudentQuestionBankDTO) {
public Result<Boolean> reAnswer(
@RequestBody TheoreticalPracticeStudentQuestionBankDTO theoreticalPracticeStudentQuestionBankDTO,
HttpServletRequest request) {
bindCurrentStudent(theoreticalPracticeStudentQuestionBankDTO, request);
return Result.data(theoreticalPracticeStudentQuestionBankService.reAnswer(theoreticalPracticeStudentQuestionBankDTO));
}
......@@ -72,4 +85,16 @@ public class TheoreticalPracticeStudentQuestionBankController extends BaseContro
public Result<Long> countdown() {
return Result.data(theoreticalPracticeStudentQuestionBankService.countdown());
}
/**
* 学生题库查询和重答都可能触发当前等级题库初始化,因此学员ID只能来自JWT。
* 请求体中的studentId仅为旧客户端兼容字段,服务端必须覆盖,不能用于数据权限判断。
*/
private void bindCurrentStudent(TheoreticalPracticeStudentQuestionBankDTO dto,
HttpServletRequest request) {
if (dto == null) {
throw new BusinessException("请求参数不能为空");
}
dto.setStudentId(authenticatedUserService.current(request).getId());
}
}
......@@ -14,8 +14,6 @@ import java.util.List;
@Mapper
public interface TheoreticalPracticeStudentQuestionBankMapper extends BaseMapper<TheoreticalPracticeStudentQuestionBankEntity> {
List<String> groupByStudentId();
List<TheoreticalPracticeStudentQuestionBankVO> onePage(TheoreticalPracticeStudentQuestionBankDTO theoreticalPracticeStudentQuestionBankDTO);
List<TheoreticalPracticeStudentQuestionBankVO> twoPage(TheoreticalPracticeStudentQuestionBankDTO theoreticalPracticeStudentQuestionBankDTO);
......@@ -25,7 +23,8 @@ public interface TheoreticalPracticeStudentQuestionBankMapper extends BaseMapper
List<TheoreticalPracticeStudentQuestionBankVO> paperOverview(@Param("studentId") String studentId,
@Param("firstLevelName") String firstLevelName,
@Param("secondLevelName") String secondLevelName,
@Param("type") String type);
@Param("type") String type,
@Param("scope") String scope);
Boolean reAnswer(TheoreticalPracticeStudentQuestionBankDTO theoreticalPracticeStudentQuestionBankDTO);
......@@ -37,4 +36,7 @@ public interface TheoreticalPracticeStudentQuestionBankMapper extends BaseMapper
*/
Date countdown(@Param("studentId") String studentId, @Param("now") Date now);
/** 返回当前班级唯一开放理论练习的等级,学生端题目读写均以该值限定数据范围。 */
String activePracticeScope(@Param("studentId") String studentId, @Param("now") Date now);
}
......@@ -8,7 +8,13 @@ import com.platform.business.model.vo.TheoreticalPracticeStudentQuestionBankVO;
import java.util.List;
public interface TheoreticalPracticeStudentQuestionBankService extends IService<TheoreticalPracticeStudentQuestionBankEntity> {
List<String> groupByStudentId();
/**
* 为指定学员补齐某一等级的理论练习题库。
*
* <p>题库按“学员+等级”初始化,不能只按学员判断;否则学员换等级后会因为已有旧等级
* 题库而跳过新等级初始化。</p>
*/
void ensureStudentLibrary(String level, List<String> studentIds);
List<TheoreticalPracticeStudentQuestionBankVO> onePage(TheoreticalPracticeStudentQuestionBankDTO theoreticalPracticeStudentQuestionBankDTO);
......
......@@ -42,12 +42,10 @@ import java.math.BigDecimal;
import java.math.RoundingMode;
import java.net.URLEncoder;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Date;
import java.util.List;
import java.util.Locale;
import java.util.Map;
import java.util.stream.Collectors;
/**
......@@ -66,8 +64,6 @@ public class TheoreticalPracticeServiceImpl extends ServiceImpl<TheoreticalPract
@Resource
private JwtTokenUtil jwtTokenUtil;
@Resource
private TheoreticalLibraryService theoreticalLibraryService;
@Resource
private TheoreticalPracticeStudentQuestionBankService theoreticalPracticeStudentQuestionBankService;
@Resource
private UserService userService;
......@@ -119,6 +115,11 @@ public class TheoreticalPracticeServiceImpl extends ServiceImpl<TheoreticalPract
@Override
@Transactional
public Result<Boolean> saveOrUpdateData(TheoreticalPracticeDTO theoreticalPracticeDTO) {
if (theoreticalPracticeDTO == null || StrUtil.isBlank(theoreticalPracticeDTO.getLev())) {
throw new BusinessException("理论练习等级不能为空");
}
// 保存前统一去除等级首尾空格,确保练习等级能与题库scope稳定匹配。
theoreticalPracticeDTO.setLev(theoreticalPracticeDTO.getLev().trim());
if (StrUtil.isEmpty(theoreticalPracticeDTO.getId())) {
//查询当前登陆人信息
//判断是否有未完成的练习
......@@ -141,8 +142,15 @@ public class TheoreticalPracticeServiceImpl extends ServiceImpl<TheoreticalPract
}
List<TheoreticalPracticeStudentVO> studentList = theoreticalPracticeDTO.getStudentList();
if (!ObjectUtils.isEmpty(studentList)) {
//初始化学生理论练习题库
initStudentLibrary(theoreticalPracticeDTO.getLev(), studentList);
/*
* 题库初始化以“学员+本期等级”为维度。学员换班或升级后即使保留旧等级题库,
* 也必须生成新等级题目,不能再用“该学员是否有任意题目”作为跳过条件。
*/
theoreticalPracticeStudentQuestionBankService.ensureStudentLibrary(
theoreticalPracticeDTO.getLev(),
studentList.stream()
.map(TheoreticalPracticeStudentVO::getStudentId)
.collect(Collectors.toList()));
for (TheoreticalPracticeStudentVO theoreticalPracticeStudentVO : studentList) {
theoreticalPracticeStudentVO.setPracticeId(theoreticalPracticeDTO.getId());
}
......@@ -151,62 +159,6 @@ public class TheoreticalPracticeServiceImpl extends ServiceImpl<TheoreticalPract
return Result.data(Boolean.TRUE);
}
private void initStudentLibrary(String level, List<TheoreticalPracticeStudentVO> studentList) {
//获取章节
List<TheoreticalLibraryEntity> chapterList = theoreticalLibraryService.groupByChapter(level);
Map<String, String> chapterMap = chapterList.stream().collect(Collectors.toMap(key -> key.getFirstLevelName() + key.getSecondLevelName(), TheoreticalLibraryEntity::getSequence));
List<String> studentIds = new ArrayList<>();
List<TheoreticalLibraryEntity> theoreticalLibraryList = theoreticalLibraryService
.list(new LambdaQueryWrapper<TheoreticalLibraryEntity>()
.eq(TheoreticalLibraryEntity::getScope, level).eq(TheoreticalLibraryEntity::getIsProblem, true));
List<String> theoreticalPracticeStudentQuestionBankList = theoreticalPracticeStudentQuestionBankService.groupByStudentId();
for (TheoreticalPracticeStudentVO theoreticalPracticeStudentVO : studentList) {
if (!theoreticalPracticeStudentQuestionBankList.contains(theoreticalPracticeStudentVO.getStudentId())) {
studentIds.add(theoreticalPracticeStudentVO.getStudentId());
}
}
if (!studentIds.isEmpty()) {
List<TheoreticalPracticeStudentQuestionBankEntity> result = new ArrayList<>();
for (String studentId : studentIds) {
for (TheoreticalLibraryEntity theoreticalLibrary : theoreticalLibraryList) {
TheoreticalPracticeStudentQuestionBankEntity theoreticalPracticeStudentQuestionBank = new TheoreticalPracticeStudentQuestionBankEntity();
theoreticalPracticeStudentQuestionBank.setStudentId(studentId);
theoreticalPracticeStudentQuestionBank.setScope(theoreticalLibrary.getScope());
theoreticalPracticeStudentQuestionBank.setLev(theoreticalLibrary.getLev());
theoreticalPracticeStudentQuestionBank.setSequence(theoreticalLibrary.getSequence());
theoreticalPracticeStudentQuestionBank.setCode(theoreticalLibrary.getCode());
theoreticalPracticeStudentQuestionBank.setFirstLevelName(theoreticalLibrary.getFirstLevelName());
theoreticalPracticeStudentQuestionBank.setSecondLevelName(theoreticalLibrary.getSecondLevelName());
theoreticalPracticeStudentQuestionBank.setThirdLevelName(theoreticalLibrary.getThirdLevelName());
theoreticalPracticeStudentQuestionBank.setFourthLevelName(theoreticalLibrary.getFourthLevelName());
theoreticalPracticeStudentQuestionBank.setType(theoreticalLibrary.getType());
theoreticalPracticeStudentQuestionBank.setProblem(theoreticalLibrary.getProblem());
theoreticalPracticeStudentQuestionBank.setAnswer(theoreticalLibrary.getAnswer());
theoreticalPracticeStudentQuestionBank.setOptionA(theoreticalLibrary.getOptionA());
theoreticalPracticeStudentQuestionBank.setOptionB(theoreticalLibrary.getOptionB());
theoreticalPracticeStudentQuestionBank.setOptionC(theoreticalLibrary.getOptionC());
theoreticalPracticeStudentQuestionBank.setOptionD(theoreticalLibrary.getOptionD());
theoreticalPracticeStudentQuestionBank.setOptionE(theoreticalLibrary.getOptionE());
//设置章节
if("技能竞赛".equals(level)){
theoreticalPracticeStudentQuestionBank.setChapter("1");
theoreticalPracticeStudentQuestionBank.setSection("1");
} else {
if(chapterMap.containsKey(theoreticalLibrary.getFirstLevelName() + theoreticalLibrary.getSecondLevelName())){
String sequence = chapterMap.get(theoreticalLibrary.getFirstLevelName() + theoreticalLibrary.getSecondLevelName());
if(sequence.getBytes().length == 2){
theoreticalPracticeStudentQuestionBank.setChapter(String.valueOf(Integer.parseInt(String.valueOf(sequence.charAt(0))) + 1));
theoreticalPracticeStudentQuestionBank.setSection(sequence.substring(1));
}
}
}
result.add(theoreticalPracticeStudentQuestionBank);
}
}
theoreticalPracticeStudentQuestionBankService.saveBatch(result);
}
}
@Override
public Boolean close(TheoreticalPracticeDTO theoreticalPracticeDTO) {
theoreticalPracticeDTO.setIsClose(true);
......
......@@ -12,8 +12,10 @@ 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.business.model.entity.TheoreticalLibraryEntity;
import com.platform.business.model.entity.UserEntity;
import com.platform.business.model.vo.TheoreticalPracticeStudentQuestionBankVO;
import com.platform.business.service.TheoreticalLibraryService;
import com.platform.business.service.TheoreticalPracticeStudentQuestionBankService;
import com.platform.business.service.UserService;
import com.platform.business.utils.ChineseNumberUtils;
......@@ -27,10 +29,13 @@ import org.springframework.util.ObjectUtils;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Date;
import java.util.LinkedHashSet;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.Set;
import java.util.stream.Collectors;
@Service
......@@ -47,16 +52,76 @@ public class TheoreticalPracticeStudentQuestionBankServiceImpl extends ServiceIm
private TheoreticalPracticeAnswerRecordMapper answerRecordMapper;
@Resource
private TheoreticalPracticeMapper theoreticalPracticeMapper;
@Resource
private TheoreticalLibraryService theoreticalLibraryService;
@Override
public List<String> groupByStudentId() {
return baseMapper.groupByStudentId();
@Transactional(rollbackFor = Exception.class)
public void ensureStudentLibrary(String level, List<String> studentIds) {
String normalizedLevel = level == null ? null : level.trim();
if (StringUtils.isEmpty(normalizedLevel) || studentIds == null || studentIds.isEmpty()) {
return;
}
// 去掉空ID和重复ID,避免同一批练习名单重复生成完全相同的个人题库。
Set<String> requestedStudentIds = studentIds.stream()
.filter(Objects::nonNull)
.map(String::trim)
.filter(id -> !id.isEmpty())
.collect(Collectors.toCollection(LinkedHashSet::new));
if (requestedStudentIds.isEmpty()) {
return;
}
/*
* 必须按“学员+等级”判断是否已经初始化。旧实现只按学员判断,学员从三级换到
* 四级后会因为仍有三级题库而永远跳过四级初始化,最终所有提交都无法匹配本期等级。
* 这里先限定本批学员再在Java中trim比较,同时兼容历史scope首尾存在空格的数据。
*/
Set<String> initializedStudentIds = list(
new LambdaQueryWrapper<TheoreticalPracticeStudentQuestionBankEntity>()
.select(TheoreticalPracticeStudentQuestionBankEntity::getStudentId,
TheoreticalPracticeStudentQuestionBankEntity::getScope)
.in(TheoreticalPracticeStudentQuestionBankEntity::getStudentId,
requestedStudentIds))
.stream()
.filter(question -> question.getScope() != null
&& normalizedLevel.equals(question.getScope().trim()))
.map(TheoreticalPracticeStudentQuestionBankEntity::getStudentId)
.collect(Collectors.toSet());
requestedStudentIds.removeAll(initializedStudentIds);
if (requestedStudentIds.isEmpty()) {
return;
}
List<TheoreticalLibraryEntity> chapterList = theoreticalLibraryService.groupByChapter(normalizedLevel);
Map<String, String> chapterMap = chapterList.stream().collect(Collectors.toMap(
key -> key.getFirstLevelName() + key.getSecondLevelName(),
TheoreticalLibraryEntity::getSequence));
List<TheoreticalLibraryEntity> questions = theoreticalLibraryService.list(
new LambdaQueryWrapper<TheoreticalLibraryEntity>()
.eq(TheoreticalLibraryEntity::getScope, normalizedLevel)
.eq(TheoreticalLibraryEntity::getIsProblem, true));
if (questions.isEmpty()) {
return;
}
List<TheoreticalPracticeStudentQuestionBankEntity> result = new ArrayList<>();
for (String studentId : requestedStudentIds) {
for (TheoreticalLibraryEntity source : questions) {
result.add(copyQuestionForStudent(source, studentId, normalizedLevel, chapterMap));
}
}
saveBatch(result);
}
@Override
@Transactional(rollbackFor = Exception.class)
public List<TheoreticalPracticeStudentQuestionBankVO> onePage(TheoreticalPracticeStudentQuestionBankDTO theoreticalPracticeStudentQuestionBankDTO) {
String scope = prepareCurrentPractice(theoreticalPracticeStudentQuestionBankDTO);
List<TheoreticalPracticeStudentQuestionBankEntity> all = list(new LambdaQueryWrapper<TheoreticalPracticeStudentQuestionBankEntity>()
.eq(TheoreticalPracticeStudentQuestionBankEntity::getStudentId, theoreticalPracticeStudentQuestionBankDTO.getStudentId()));
.eq(TheoreticalPracticeStudentQuestionBankEntity::getStudentId, theoreticalPracticeStudentQuestionBankDTO.getStudentId())
.apply("TRIM(scope) = {0}", scope));
//转换成以firstLevelName分组的map
Map<String, List<TheoreticalPracticeStudentQuestionBankEntity>> groupedMap = all.stream()
.collect(Collectors.groupingBy(TheoreticalPracticeStudentQuestionBankEntity::getFirstLevelName));
......@@ -141,9 +206,12 @@ public class TheoreticalPracticeStudentQuestionBankServiceImpl extends ServiceIm
}
@Override
@Transactional(rollbackFor = Exception.class)
public List<TheoreticalPracticeStudentQuestionBankVO> twoPage(TheoreticalPracticeStudentQuestionBankDTO theoreticalPracticeStudentQuestionBankDTO) {
String scope = prepareCurrentPractice(theoreticalPracticeStudentQuestionBankDTO);
List<TheoreticalPracticeStudentQuestionBankEntity> all = list(new LambdaQueryWrapper<TheoreticalPracticeStudentQuestionBankEntity>()
.eq(TheoreticalPracticeStudentQuestionBankEntity::getStudentId, theoreticalPracticeStudentQuestionBankDTO.getStudentId())
.apply("TRIM(scope) = {0}", scope)
.eq(TheoreticalPracticeStudentQuestionBankEntity::getFirstLevelName, theoreticalPracticeStudentQuestionBankDTO.getFirstLevelName()));
//转换成以secondLevelName分组的map
Map<String, List<TheoreticalPracticeStudentQuestionBankEntity>> groupedMap = all.stream()
......@@ -163,14 +231,16 @@ public class TheoreticalPracticeStudentQuestionBankServiceImpl extends ServiceIm
}
@Override
@Transactional(rollbackFor = Exception.class)
public List<TheoreticalPracticeStudentQuestionBankVO> overview(TheoreticalPracticeStudentQuestionBankDTO theoreticalPracticeStudentQuestionBankDTO) {
String scope = prepareCurrentPractice(theoreticalPracticeStudentQuestionBankDTO);
List<TheoreticalPracticeStudentQuestionBankVO> result = baseMapper.overview(theoreticalPracticeStudentQuestionBankDTO);
for (TheoreticalPracticeStudentQuestionBankVO theoreticalPracticeStudentQuestionBank : result) {
if (StringUtils.isNotEmpty(theoreticalPracticeStudentQuestionBank.getType())) {
List<TheoreticalPracticeStudentQuestionBankVO> data = baseMapper.paperOverview(theoreticalPracticeStudentQuestionBankDTO.getStudentId(),
theoreticalPracticeStudentQuestionBankDTO.getFirstLevelName(),
theoreticalPracticeStudentQuestionBankDTO.getSecondLevelName(),
theoreticalPracticeStudentQuestionBank.getType());
theoreticalPracticeStudentQuestionBank.getType(), scope);
theoreticalPracticeStudentQuestionBank.setSubset(data);
}
}
......@@ -209,10 +279,67 @@ public class TheoreticalPracticeStudentQuestionBankServiceImpl extends ServiceIm
}
@Override
@Transactional(rollbackFor = Exception.class)
public Boolean reAnswer(TheoreticalPracticeStudentQuestionBankDTO theoreticalPracticeStudentQuestionBankDTO) {
prepareCurrentPractice(theoreticalPracticeStudentQuestionBankDTO);
return baseMapper.reAnswer(theoreticalPracticeStudentQuestionBankDTO);
}
/**
* 由服务端定位当前开放练习等级,并在读取题目之前自动修复该等级缺失的个人题库。
* 请求体传入的scope会被覆盖,避免客户端查看或重置其他历史等级的数据。
*/
private String prepareCurrentPractice(TheoreticalPracticeStudentQuestionBankDTO dto) {
if (dto == null || StringUtils.isEmpty(dto.getStudentId())) {
throw new BusinessException("学员ID不能为空");
}
String scope = baseMapper.activePracticeScope(dto.getStudentId(), new Date());
if (StringUtils.isEmpty(scope)) {
throw new BusinessException("当前没有可作答的理论练习");
}
scope = scope.trim();
dto.setScope(scope);
ensureStudentLibrary(scope, Collections.singletonList(dto.getStudentId()));
return scope;
}
/** 将公共理论题复制为学员个人题目;答案状态保持为空,由实际作答流程写入。 */
private TheoreticalPracticeStudentQuestionBankEntity copyQuestionForStudent(
TheoreticalLibraryEntity source, String studentId, String level,
Map<String, String> chapterMap) {
TheoreticalPracticeStudentQuestionBankEntity target =
new TheoreticalPracticeStudentQuestionBankEntity();
target.setStudentId(studentId);
target.setScope(source.getScope());
target.setLev(source.getLev());
target.setSequence(source.getSequence());
target.setCode(source.getCode());
target.setFirstLevelName(source.getFirstLevelName());
target.setSecondLevelName(source.getSecondLevelName());
target.setThirdLevelName(source.getThirdLevelName());
target.setFourthLevelName(source.getFourthLevelName());
target.setType(source.getType());
target.setProblem(source.getProblem());
target.setAnswer(source.getAnswer());
target.setOptionA(source.getOptionA());
target.setOptionB(source.getOptionB());
target.setOptionC(source.getOptionC());
target.setOptionD(source.getOptionD());
target.setOptionE(source.getOptionE());
if ("技能竞赛".equals(level)) {
target.setChapter("1");
target.setSection("1");
return target;
}
String sequence = chapterMap.get(source.getFirstLevelName() + source.getSecondLevelName());
if (sequence != null && sequence.getBytes().length == 2) {
target.setChapter(String.valueOf(Integer.parseInt(String.valueOf(sequence.charAt(0))) + 1));
target.setSection(sequence.substring(1));
}
return target;
}
@Override
@Transactional(rollbackFor = Exception.class)
public Boolean edit(TheoreticalPracticeStudentQuestionBankDTO dto, String currentStudentId) {
......
......@@ -149,7 +149,7 @@
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
on qb.student_id = ps.student_id and trim(qb.scope) = trim(tp.lev)
left join theoretical_practice_answer_record ar
on ar.practice_id = tp.id
and ar.student_id = ps.student_id
......@@ -186,7 +186,7 @@
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
on qb.student_id = assigned.student_id and trim(qb.scope) = trim(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
......@@ -231,7 +231,7 @@
join theoretical_practice_student_question_bank qb
on qb.id = #{questionId}
and qb.student_id = ps.student_id
and qb.scope = tp.lev
and trim(qb.scope) = trim(tp.lev)
where coalesce(tp.is_close, false) = false
and tp.start_time &lt;= #{now}
and tp.end_time &gt;= #{now}
......
......@@ -33,14 +33,6 @@
</sql>
<select id="groupByStudentId" resultType="java.lang.String">
select
student_id
from
theoretical_practice_student_question_bank
group by
student_id
</select>
<select id="onePage" resultType="com.platform.business.model.vo.TheoreticalPracticeStudentQuestionBankVO">
SELECT first_level_name,
chapter,
......@@ -51,6 +43,7 @@
) AS accuracyRate
FROM theoretical_practice_student_question_bank
WHERE student_id = #{studentId}
AND TRIM(scope) = #{scope}
GROUP BY first_level_name,chapter
ORDER BY chapter
</select>
......@@ -66,6 +59,7 @@
) AS accuracyRate
FROM theoretical_practice_student_question_bank
WHERE student_id = #{studentId}
and TRIM(scope) = #{scope}
and first_level_name = #{firstLevelName}
GROUP BY
first_level_name,second_level_name,section
......@@ -80,6 +74,8 @@
theoretical_practice_student_question_bank
where
student_id = #{studentId}
and
TRIM(scope) = #{scope}
and
first_level_name = #{firstLevelName}
and
......@@ -97,6 +93,8 @@
theoretical_practice_student_question_bank
where
student_id = #{studentId}
and
TRIM(scope) = #{scope}
and
first_level_name = #{firstLevelName}
and
......@@ -117,6 +115,23 @@
order by tp.end_time
limit 1
</select>
<!--
学生端页面不能直接相信请求体中的scope,也不能继续展示该学员所有历史等级题目。
等级必须由当前班级、当前开放练习和练习名单共同确定,与倒计时和提交校验保持一致。
-->
<select id="activePracticeScope" resultType="java.lang.String">
select trim(tp.lev)
from theoretical_practice tp
inner join theoretical_practice_student assigned
on assigned.practice_id = tp.id
and assigned.student_id = #{studentId}
<include refid="currentStudentClassJoin"/>
where coalesce(tp.is_close, false) = false
and tp.end_time &gt;= #{now}
and tp.start_time &lt;= #{now}
order by tp.start_time desc, tp.id desc
limit 1
</select>
<update id="reAnswer">
update
theoretical_practice_student_question_bank
......@@ -125,6 +140,8 @@
result = null
where
student_id = #{studentId}
and
TRIM(scope) = #{scope}
and
first_level_name = #{firstLevelName}
and
......
package com.platform.business.controller;
import com.platform.business.model.dto.TheoreticalPracticeStudentQuestionBankDTO;
import com.platform.business.model.entity.UserEntity;
import com.platform.business.service.AuthenticatedUserService;
import com.platform.business.service.TheoreticalPracticeStudentQuestionBankService;
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.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.Mockito.verify;
import static org.mockito.Mockito.when;
/** 验证学生理论练习入口只能使用JWT中的学员身份。 */
@ExtendWith(MockitoExtension.class)
class TheoreticalPracticeStudentQuestionBankControllerTest {
@Mock
private TheoreticalPracticeStudentQuestionBankService questionBankService;
@Mock
private AuthenticatedUserService authenticatedUserService;
private TheoreticalPracticeStudentQuestionBankController controller;
private MockHttpServletRequest request;
private UserEntity currentStudent;
@BeforeEach
void setUp() {
controller = new TheoreticalPracticeStudentQuestionBankController();
ReflectionTestUtils.setField(controller, "theoreticalPracticeStudentQuestionBankService",
questionBankService);
ReflectionTestUtils.setField(controller, "authenticatedUserService",
authenticatedUserService);
request = new MockHttpServletRequest();
currentStudent = new UserEntity();
currentStudent.setId("current-student");
}
@Test
void overviewShouldOverwriteClientStudentIdWithJwtStudent() {
TheoreticalPracticeStudentQuestionBankDTO query =
new TheoreticalPracticeStudentQuestionBankDTO();
query.setStudentId("another-student");
when(authenticatedUserService.current(request)).thenReturn(currentStudent);
when(questionBankService.overview(query)).thenReturn(Collections.emptyList());
controller.overview(query, request);
// 自动补题属于写操作,绝不能由客户端studentId指定目标学员。
assertEquals("current-student", query.getStudentId());
verify(questionBankService).overview(query);
}
@Test
void studentQuestionEntryShouldRejectEmptyBody() {
assertThrows(BusinessException.class, () -> controller.overview(null, request));
}
}
package com.platform.business.mapper;
import com.platform.business.model.dto.TheoreticalPracticeStudentQuestionBankDTO;
import org.apache.ibatis.builder.xml.XMLMapperBuilder;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.mapping.BoundSql;
......@@ -65,6 +66,49 @@ class TheoreticalPracticeCurrentClassScopeTest {
assertCurrentClassScope(sql);
}
@Test
void questionPagesShouldFilterServerResolvedPracticeScope() throws Exception {
Configuration configuration = parse(
"com/platform/business/mapper/TheoreticalPracticeStudentQuestionBankMapper.xml");
TheoreticalPracticeStudentQuestionBankDTO query =
new TheoreticalPracticeStudentQuestionBankDTO();
query.setStudentId("student-1");
query.setScope("污水四级");
query.setFirstLevelName("基础知识");
query.setSecondLevelName("工艺原理");
/*
* 四个学生题目读写入口都必须携带scope,避免页面展示旧等级题目、重答操作清除
* 历史等级答案。scope由服务层查询当前开放练习后覆盖,客户端传值不参与授权。
*/
assertScopeFilter(configuration, "onePage", query);
assertScopeFilter(configuration, "twoPage", query);
assertScopeFilter(configuration, "overview", query);
assertScopeFilter(configuration, "reAnswer", query);
Map<String, Object> parameters = new HashMap<>();
parameters.put("studentId", "student-1");
parameters.put("firstLevelName", "基础知识");
parameters.put("secondLevelName", "工艺原理");
parameters.put("type", "单选题");
parameters.put("scope", "污水四级");
assertScopeFilter(configuration, "paperOverview", parameters);
}
@Test
void activePracticeScopeShouldFollowCurrentClassAndOpenTime() throws Exception {
Configuration configuration = parse(
"com/platform/business/mapper/TheoreticalPracticeStudentQuestionBankMapper.xml");
String sql = normalize(configuration.getMappedStatement(
"com.platform.business.mapper.TheoreticalPracticeStudentQuestionBankMapper.activePracticeScope")
.getBoundSql(commonParameters()).getSql());
assertCurrentClassScope(sql);
assertTrue(sql.contains("assigned.student_id = ?"));
assertTrue(sql.contains("tp.start_time <= ?"));
assertTrue(sql.contains("tp.end_time >= ?"));
}
/** 验证SQL选择的是唯一当前班级,而不是任意一条历史班级关系。 */
private void assertCurrentClassScope(String sql) {
assertTrue(sql.contains("from class_student current_member"));
......@@ -74,6 +118,16 @@ class TheoreticalPracticeCurrentClassScopeTest {
assertTrue(sql.contains("c.start_date desc, c.id desc limit 1"));
}
/** 验证指定题目查询包含服务端写入DTO的当前等级过滤条件。 */
private void assertScopeFilter(Configuration configuration, String statement,
Object parameters) {
String sql = normalize(configuration.getMappedStatement(
"com.platform.business.mapper.TheoreticalPracticeStudentQuestionBankMapper."
+ statement)
.getBoundSql(parameters).getSql());
assertTrue(sql.contains("trim(scope) = ?"));
}
/** 创建两个Mapper共同使用的命名参数,日期固定以保证BoundSql生成过程可重复。 */
private Map<String, Object> commonParameters() {
Map<String, Object> parameters = new HashMap<>();
......
package com.platform.business.service.impl;
import com.baomidou.mybatisplus.core.MybatisConfiguration;
import com.baomidou.mybatisplus.core.metadata.TableInfoHelper;
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.TheoreticalLibraryEntity;
import com.platform.business.model.entity.TheoreticalPracticeStudentQuestionBankEntity;
import com.platform.business.service.TheoreticalLibraryService;
import com.platform.exception.BusinessException;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.BeforeAll;
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.apache.ibatis.builder.MapperBuilderAssistant;
import org.springframework.test.util.ReflectionTestUtils;
import java.util.Collections;
import java.util.Collection;
import java.util.List;
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.ArgumentMatchers.anyCollection;
import static org.mockito.Mockito.doReturn;
import static org.mockito.Mockito.never;
import static org.mockito.Mockito.spy;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;
......@@ -34,15 +45,28 @@ class TheoreticalPracticeAnswerServiceImplTest {
private TheoreticalPracticeAnswerRecordMapper answerRecordMapper;
@Mock
private TheoreticalPracticeMapper practiceMapper;
@Mock
private TheoreticalLibraryService theoreticalLibraryService;
private TheoreticalPracticeStudentQuestionBankServiceImpl service;
@BeforeAll
static void initializeMybatisPlusMetadata() {
// 单元测试不启动Spring/MyBatis容器,需手动注册两个Lambda查询涉及的实体元数据。
MapperBuilderAssistant assistant =
new MapperBuilderAssistant(new MybatisConfiguration(), "");
TableInfoHelper.initTableInfo(assistant,
TheoreticalPracticeStudentQuestionBankEntity.class);
TableInfoHelper.initTableInfo(assistant, TheoreticalLibraryEntity.class);
}
@BeforeEach
void setUp() {
service = new TheoreticalPracticeStudentQuestionBankServiceImpl();
ReflectionTestUtils.setField(service, "baseMapper", questionMapper);
ReflectionTestUtils.setField(service, "answerRecordMapper", answerRecordMapper);
ReflectionTestUtils.setField(service, "theoreticalPracticeMapper", practiceMapper);
ReflectionTestUtils.setField(service, "theoreticalLibraryService", theoreticalLibraryService);
}
@Test
......@@ -105,6 +129,68 @@ class TheoreticalPracticeAnswerServiceImplTest {
assertThrows(BusinessException.class, () -> service.edit(invalid, "s1"));
}
@Test
void changedLevelShouldInitializeNewScopeEvenWhenStudentHasOldQuestions() {
TheoreticalPracticeStudentQuestionBankEntity oldQuestion =
new TheoreticalPracticeStudentQuestionBankEntity();
oldQuestion.setStudentId("s1");
oldQuestion.setScope("污水三级");
when(questionMapper.selectList(any())).thenReturn(Collections.singletonList(oldQuestion));
when(theoreticalLibraryService.groupByChapter("污水四级"))
.thenReturn(Collections.emptyList());
TheoreticalLibraryEntity source = new TheoreticalLibraryEntity();
source.setScope("污水四级");
source.setCode("S4-1");
source.setProblem("四级题目");
when(theoreticalLibraryService.list(any())).thenReturn(Collections.singletonList(source));
// 拦截MyBatis批量写入,只验证换等级时确实构造了新scope题库,测试不依赖数据库。
TheoreticalPracticeStudentQuestionBankServiceImpl spyService = spy(service);
doReturn(true).when(spyService).saveBatch(anyCollection());
spyService.ensureStudentLibrary("污水四级", Collections.singletonList("s1"));
@SuppressWarnings("unchecked")
ArgumentCaptor<Collection<TheoreticalPracticeStudentQuestionBankEntity>> captor =
ArgumentCaptor.forClass(Collection.class);
verify(spyService).saveBatch(captor.capture());
List<TheoreticalPracticeStudentQuestionBankEntity> generated =
(List<TheoreticalPracticeStudentQuestionBankEntity>) captor.getValue();
assertEquals(1, generated.size());
assertEquals("s1", generated.get(0).getStudentId());
assertEquals("污水四级", generated.get(0).getScope());
assertEquals("S4-1", generated.get(0).getCode());
}
@Test
void studentQuestionPageShouldUseServerResolvedActiveScope() {
TheoreticalPracticeStudentQuestionBankDTO query =
new TheoreticalPracticeStudentQuestionBankDTO();
query.setStudentId("s1");
query.setScope("客户端伪造等级");
query.setPageNum(1);
query.setPageSize(10);
when(questionMapper.activePracticeScope(
org.mockito.ArgumentMatchers.eq("s1"), any()))
.thenReturn("污水四级");
TheoreticalPracticeStudentQuestionBankEntity currentQuestion =
new TheoreticalPracticeStudentQuestionBankEntity();
currentQuestion.setStudentId("s1");
currentQuestion.setScope("污水四级");
currentQuestion.setFirstLevelName("基础知识");
when(questionMapper.selectList(any()))
.thenReturn(Collections.singletonList(currentQuestion));
when(questionMapper.onePage(any(TheoreticalPracticeStudentQuestionBankDTO.class)))
.thenReturn(Collections.emptyList());
service.onePage(query);
assertEquals("污水四级", query.getScope());
verify(questionMapper).onePage(query);
}
private void mockQuestion(String studentId) {
TheoreticalPracticeStudentQuestionBankEntity question =
new TheoreticalPracticeStudentQuestionBankEntity();
......
......@@ -11,6 +11,9 @@ import com.platform.business.model.vo.TheoreticalPracticeStudentStatisticsDetail
import com.platform.business.model.vo.TheoreticalPracticeStudentStatisticsVO;
import com.platform.business.model.vo.TheoreticalPracticeSummaryVO;
import com.platform.business.model.vo.TheoreticalPracticeVO;
import com.platform.business.model.vo.TheoreticalPracticeStudentVO;
import com.platform.business.service.TheoreticalPracticeStudentQuestionBankService;
import com.platform.business.service.TheoreticalPracticeStudentService;
import com.platform.exception.BusinessException;
import org.apache.poi.xwpf.usermodel.XWPFDocument;
import org.junit.jupiter.api.BeforeEach;
......@@ -42,6 +45,10 @@ class TheoreticalPracticeServiceImplTest {
@Mock
private TheoreticalPracticeMapper mapper;
@Mock
private TheoreticalPracticeStudentQuestionBankService questionBankService;
@Mock
private TheoreticalPracticeStudentService practiceStudentService;
private TheoreticalPracticeServiceImpl service;
......@@ -49,6 +56,10 @@ class TheoreticalPracticeServiceImplTest {
void setUp() {
service = new TheoreticalPracticeServiceImpl();
ReflectionTestUtils.setField(service, "baseMapper", mapper);
ReflectionTestUtils.setField(service, "theoreticalPracticeStudentQuestionBankService",
questionBankService);
ReflectionTestUtils.setField(service, "theoreticalPracticeStudentService",
practiceStudentService);
}
@Test
......@@ -127,6 +138,25 @@ class TheoreticalPracticeServiceImplTest {
assertEquals("ENDED", rows.get(3).getStatusCode());
}
@Test
void savingPracticeShouldInitializeStudentsForThisPracticeLevel() {
TheoreticalPracticeDTO request = new TheoreticalPracticeDTO();
request.setId("p1");
request.setLev(" 污水四级 ");
TheoreticalPracticeStudentVO student = new TheoreticalPracticeStudentVO();
student.setStudentId("s1");
request.setStudentList(Collections.singletonList(student));
when(mapper.updateById(request)).thenReturn(1);
service.saveOrUpdateData(request);
// 等级先规范化再传给题库服务,避免首尾空格造成scope无法匹配。
assertEquals("污水四级", request.getLev());
verify(questionBankService).ensureStudentLibrary(
"污水四级", Collections.singletonList("s1"));
assertEquals("p1", student.getPracticeId());
}
@Test
void overviewShouldUseAllAssignedStudentsAndRoundRates() {
TheoreticalPracticeStatisticsOverviewVO raw = new TheoreticalPracticeStatisticsOverviewVO();
......
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