考试生成试卷题目跟上一次考试不一样

This commit is contained in:
wuchunlei
2025-06-10 14:53:15 +08:00
parent 0331e2b58d
commit 15b3732d36

View File

@@ -1,6 +1,8 @@
package com.peanut.modules.common.service.impl;
import cn.hutool.core.date.DateUtil;
import cn.hutool.json.JSONArray;
import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
@@ -149,6 +151,7 @@ public class ClassExamServiceImpl extends ServiceImpl<ClassExamDao, ClassExam> i
return R.error("存在正在考试,请结束再试");
}
List<ClassCourse> courseList = new ArrayList<>();
List<String> historySubjectList = new ArrayList<>();
int examTime = 45;//考试持续时间
int stotal = 60;//单选题总数
int mtotal = 40;//多选题总数
@@ -173,6 +176,17 @@ public class ClassExamServiceImpl extends ServiceImpl<ClassExamDao, ClassExam> i
classCourse.setCourseId((Integer) params.get("relationId"));
courseList.add(classCourse);
}
//查询上次考试得问题,生成问题时,去重
List<ClassExamUser> historyList = classExamUserDao.selectList(new LambdaQueryWrapper<ClassExamUser>()
.eq(ClassExamUser::getUserId,ShiroUtils.getUId()).eq(ClassExamUser::getScoreSuccess,1)
.eq(ClassExamUser::getRelationId,params.get("relationId").toString()).orderByDesc(ClassExamUser::getStartTime));
if (historyList.size() > 0) {
ClassExamUser lastExamUser = historyList.get(0);
JSONArray subjectList = JSONUtil.parseArray(lastExamUser.getSubject());
for (Object o : subjectList){
historySubjectList.add(((Map)o).get("id").toString());
}
}
List<Map<String,Object>> resultList = new ArrayList<>();
List<String> answerList = new ArrayList<>();
List<Map<String,Object>> sList = new ArrayList<>();
@@ -183,12 +197,11 @@ public class ClassExamServiceImpl extends ServiceImpl<ClassExamDao, ClassExam> i
for (ClassCourse classCourse:courseList){
CourseEntity course = courseDao.selectById(classCourse.getCourseId());
//单选
List<Map<String,Object>> singleList = classExamSubjectDao.selectMaps(new MPJLambdaWrapper<ClassExamSubject>()
.selectAs(ClassExamSubject::getId,"id")
.selectAs(ClassExamSubject::getType,"type")
.selectAs(ClassExamSubject::getContent,"content")
List<Map<String,Object>> singleList = classExamSubjectDao.selectMaps(new LambdaQueryWrapper<ClassExamSubject>()
.select(ClassExamSubject::getId,ClassExamSubject::getType,ClassExamSubject::getContent)
.eq(ClassExamSubject::getCourseId,classCourse.getCourseId())
.eq(ClassExamSubject::getType,0));
.eq(ClassExamSubject::getType,0)
.notIn(ClassExamSubject::getId,historySubjectList));
Collections.shuffle(singleList);//打乱顺序
try {
if (sList.size()+snum>stotal){
@@ -200,12 +213,11 @@ public class ClassExamServiceImpl extends ServiceImpl<ClassExamDao, ClassExam> i
return R.error(course.getTitle()+"-单选题数量不足");
}
//多选
List<Map<String,Object>> mulList = classExamSubjectDao.selectMaps(new MPJLambdaWrapper<ClassExamSubject>()
.selectAs(ClassExamSubject::getId,"id")
.selectAs(ClassExamSubject::getType,"type")
.selectAs(ClassExamSubject::getContent,"content")
List<Map<String,Object>> mulList = classExamSubjectDao.selectMaps(new LambdaQueryWrapper<ClassExamSubject>()
.select(ClassExamSubject::getId,ClassExamSubject::getType,ClassExamSubject::getContent)
.eq(ClassExamSubject::getCourseId,classCourse.getCourseId())
.eq(ClassExamSubject::getType,1));
.eq(ClassExamSubject::getType,1)
.notIn(ClassExamSubject::getId,historySubjectList));
Collections.shuffle(mulList);//打乱顺序
try {
if (mList.size()+mnum>mtotal){
@@ -224,10 +236,8 @@ public class ClassExamServiceImpl extends ServiceImpl<ClassExamDao, ClassExam> i
if (resultList.size()>0){
for (Map<String,Object> subject:resultList) {
answerList.add("");//预留答案位置
List<Map<String,Object>> options = classExamOptionDao.selectMaps(new MPJLambdaWrapper<ClassExamOption>()
.selectAs(ClassExamOption::getId,"id")
.selectAs(ClassExamOption::getContent,"content")
.selectAs(ClassExamOption::getRightWrong,"rightWrong")
List<Map<String,Object>> options = classExamOptionDao.selectMaps(new LambdaQueryWrapper<ClassExamOption>()
.select(ClassExamOption::getId,ClassExamOption::getContent,ClassExamOption::getRightWrong)
.eq(ClassExamOption::getSubjectId,subject.get("id")));
Collections.shuffle(options);//打乱顺序
subject.put("options",options);