|
|
@@ -3,11 +3,10 @@ package cn.reghao.tnb.content.app.exam.service;
|
|
|
import cn.reghao.jutil.jdk.db.Page;
|
|
|
import cn.reghao.jutil.jdk.db.PageList;
|
|
|
import cn.reghao.jutil.jdk.result.Result;
|
|
|
-import cn.reghao.jutil.jdk.serializer.JsonConverter;
|
|
|
import cn.reghao.tnb.common.auth.UserContext;
|
|
|
import cn.reghao.tnb.common.db.KeyValue;
|
|
|
import cn.reghao.tnb.content.app.exam.db.mapper.*;
|
|
|
-import cn.reghao.tnb.content.app.exam.model.constant.PaperStatus;
|
|
|
+import cn.reghao.tnb.content.app.exam.model.constant.PaperViewType;
|
|
|
import cn.reghao.tnb.content.app.exam.model.constant.QuestionType;
|
|
|
import cn.reghao.tnb.content.app.exam.model.dto.PaperAddDto;
|
|
|
import cn.reghao.tnb.content.app.exam.model.dto.UserResult;
|
|
|
@@ -16,6 +15,7 @@ import cn.reghao.tnb.content.app.exam.model.query.ExamQuery;
|
|
|
import cn.reghao.tnb.content.app.exam.model.query.PaperQuery;
|
|
|
import cn.reghao.tnb.content.app.exam.model.vo.*;
|
|
|
import cn.reghao.tnb.content.app.util.redis.ds.RedisHash;
|
|
|
+import lombok.extern.slf4j.Slf4j;
|
|
|
import org.springframework.stereotype.Service;
|
|
|
import org.springframework.transaction.annotation.Transactional;
|
|
|
|
|
|
@@ -26,6 +26,7 @@ import java.util.stream.Collectors;
|
|
|
* @author reghao
|
|
|
* @date 2024-08-27 17:21:01
|
|
|
*/
|
|
|
+@Slf4j
|
|
|
@Service
|
|
|
public class PaperService {
|
|
|
private final PaperMapper paperMapper;
|
|
|
@@ -36,11 +37,12 @@ public class PaperService {
|
|
|
private String keyPrefix = "tnb:exam:result";
|
|
|
private PaperResultMapper paperResultMapper;
|
|
|
private PaperAnswerMapper paperAnswerMapper;
|
|
|
+ private ExamUserMapper examUserMapper;
|
|
|
|
|
|
public PaperService(PaperMapper paperMapper, PaperQuestionMapper paperQuestionMapper,
|
|
|
QuestionMapper questionMapper, QuestionOptionMapper questionOptionMapper,
|
|
|
RedisHash redisHash, PaperResultMapper paperResultMapper,
|
|
|
- PaperAnswerMapper paperAnswerMapper) {
|
|
|
+ PaperAnswerMapper paperAnswerMapper, ExamUserMapper examUserMapper) {
|
|
|
this.paperMapper = paperMapper;
|
|
|
this.paperQuestionMapper = paperQuestionMapper;
|
|
|
this.questionMapper = questionMapper;
|
|
|
@@ -48,11 +50,12 @@ public class PaperService {
|
|
|
this.redisHash = redisHash;
|
|
|
this.paperResultMapper = paperResultMapper;
|
|
|
this.paperAnswerMapper = paperAnswerMapper;
|
|
|
+ this.examUserMapper = examUserMapper;
|
|
|
}
|
|
|
|
|
|
public Result createExamPaper(PaperAddDto paperAddDto) {
|
|
|
Paper paper = new Paper(paperAddDto);
|
|
|
- List<PaperQuestion> paperQuestions = paperAddDto.getQuestions();
|
|
|
+ List<PaperQuestion> paperQuestions = paperAddDto.getPaperQuestions();
|
|
|
save(paper, paperQuestions);
|
|
|
return Result.success();
|
|
|
}
|
|
|
@@ -65,6 +68,7 @@ public class PaperService {
|
|
|
paperQuestion.setPaperId(paperId);
|
|
|
});
|
|
|
|
|
|
+ examUserMapper.saveAll(getExamUsers(paperId));
|
|
|
/*List<PaperQuestion> list1 = list.stream()
|
|
|
.map(paperQuestion -> {
|
|
|
if (paperQuestion.getChildren().isEmpty()) {
|
|
|
@@ -81,13 +85,24 @@ public class PaperService {
|
|
|
.filter(Objects::nonNull)
|
|
|
.flatMap(Collection::stream)
|
|
|
.collect(Collectors.toList());*/
|
|
|
-
|
|
|
paperQuestionMapper.saveAll(list);
|
|
|
/*if (!list1.isEmpty()) {
|
|
|
paperQuestionMapper.saveAll(list1);
|
|
|
}*/
|
|
|
}
|
|
|
|
|
|
+ private List<ExamUser> getExamUsers(int paperId) {
|
|
|
+ List<ExamUser> list = new ArrayList<>();
|
|
|
+ long start = 10001L;
|
|
|
+ long total = 100;
|
|
|
+ while (start < start + total) {
|
|
|
+ ExamUser examUser = new ExamUser(paperId, start, 1);
|
|
|
+ list.add(examUser);
|
|
|
+ start++;
|
|
|
+ }
|
|
|
+ return list;
|
|
|
+ }
|
|
|
+
|
|
|
public Result deleteExamPaper(int paperId) {
|
|
|
paperMapper.deleteById(paperId);
|
|
|
return Result.success();
|
|
|
@@ -101,22 +116,21 @@ public class PaperService {
|
|
|
List<PaperView> list1 = list.stream()
|
|
|
.map(paper -> {
|
|
|
int paperId = paper.getId();
|
|
|
- int paperType = paper.getStatus();
|
|
|
+ int markType = paper.getMarkType();
|
|
|
PaperResult paperResult = getPaperResult(paperId, loginUser);
|
|
|
int status;
|
|
|
// 1. 试卷未完成
|
|
|
- // 2. 试卷已完成, 待批改
|
|
|
- // 3. 试卷已完成, 自己批改
|
|
|
+ // 2. 试卷已完成, 自己批改
|
|
|
+ // 3. 试卷已完成, 他人批改
|
|
|
// 4. 试卷已完成, 已批改
|
|
|
if (paperResult == null) {
|
|
|
status = 1;
|
|
|
} else if (paperResult.getMarked()) {
|
|
|
- //status = paperResult.getMarked() ? 4 : 2;
|
|
|
status = 4;
|
|
|
- } else if (paperType == 2) {
|
|
|
- status = 3;
|
|
|
- } else {
|
|
|
+ } else if (markType == 2) {
|
|
|
status = 2;
|
|
|
+ } else {
|
|
|
+ status = 3;
|
|
|
}
|
|
|
|
|
|
return new PaperView(paper, status);
|
|
|
@@ -173,26 +187,35 @@ public class PaperService {
|
|
|
return questionInfo;
|
|
|
}
|
|
|
|
|
|
- public PaperDetail getPaperDetail(int paperId, int status) {
|
|
|
+ public PaperDetail getPaperDetail(int paperId, int viewType) {
|
|
|
long loginUser = UserContext.getUser();
|
|
|
+ ExamUser examUser = examUserMapper.findExamUser(paperId, loginUser);
|
|
|
+ if (examUser == null) {
|
|
|
+ return null;
|
|
|
+ }
|
|
|
+
|
|
|
+ int paperStatus = 1;
|
|
|
+ Paper paper = paperMapper.findById(paperId);
|
|
|
+ int markType = paper.getMarkType();
|
|
|
+ int role = examUser.getRole();
|
|
|
+
|
|
|
Map<Long, QuestionAnswer> paperAnswers;
|
|
|
- if (status == PaperStatus.PaperPreview.getCode()) {
|
|
|
- paperAnswers = getPaperAnswersFromResult(paperId, loginUser);
|
|
|
- } else if (status == PaperStatus.PaperExam.getCode()) {
|
|
|
+ if (viewType == PaperViewType.PaperPreview.getCode()) {
|
|
|
+ paperAnswers = getPaperAnswersFromCorrect(paperId);
|
|
|
+ } else if (viewType == PaperViewType.PaperExam.getCode()) {
|
|
|
paperAnswers = getPaperAnswersFromCache(paperId, loginUser);
|
|
|
- } else if (status == PaperStatus.PaperMark.getCode()) {
|
|
|
+ } else if (viewType == PaperViewType.PaperMark.getCode()) {
|
|
|
paperAnswers = getPaperAnswersFromResult(paperId, loginUser);
|
|
|
- } else if (status == PaperStatus.PaperResult.getCode()) {
|
|
|
+ } else if (viewType == PaperViewType.PaperResult.getCode()) {
|
|
|
paperAnswers = getPaperAnswersFromResult(paperId, loginUser);
|
|
|
} else {
|
|
|
- paperAnswers = getPaperAnswersFromResult(paperId, loginUser);
|
|
|
+ paperAnswers = getEmptyPaperAnswers(paperId);
|
|
|
}
|
|
|
|
|
|
- Paper paper = paperMapper.findById(paperId);
|
|
|
Map<Integer, List<QuestionInfo>> questionMap = getPaperQuestions(paperId).stream()
|
|
|
.collect(Collectors.groupingBy(QuestionInfo::getQuestionType));
|
|
|
UserResult userResult = new UserResult(paperId, paperAnswers);
|
|
|
- return new PaperDetail(paper, status, questionMap, userResult);
|
|
|
+ return new PaperDetail(paper, paperStatus, questionMap, userResult);
|
|
|
}
|
|
|
|
|
|
public void cacheUserAnswers(UserResult userResult) {
|
|
|
@@ -200,8 +223,8 @@ public class PaperService {
|
|
|
int paperId = userResult.getPaperId();
|
|
|
String key = String.format("%s:%s:%s", keyPrefix, paperId, loginUser);
|
|
|
|
|
|
- List<QuestionAnswer> userAnswers = userResult.getUserAnswers();
|
|
|
- for (QuestionAnswer userAnswer : userAnswers) {
|
|
|
+ List<QuestionAnswer> submitUserAnswers = userResult.getSubmitUserAnswers();
|
|
|
+ for (QuestionAnswer userAnswer : submitUserAnswers) {
|
|
|
long questionId = userAnswer.getQuestionId();
|
|
|
redisHash.hset(key, ""+questionId, userAnswer);
|
|
|
}
|
|
|
@@ -227,6 +250,18 @@ public class PaperService {
|
|
|
return map;
|
|
|
}
|
|
|
|
|
|
+ return getEmptyPaperAnswers(paperId);
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 获取空白的试卷答案
|
|
|
+ *
|
|
|
+ * @param
|
|
|
+ * @return
|
|
|
+ * @date 2025-08-10 18:08:71
|
|
|
+ */
|
|
|
+ private Map<Long, QuestionAnswer> getEmptyPaperAnswers(int paperId) {
|
|
|
+ Map<Long, QuestionAnswer> map = new HashMap<>();
|
|
|
List<QuestionInfo> questions = getPaperQuestions(paperId);
|
|
|
for (QuestionInfo questionInfo : questions) {
|
|
|
QuestionAnswer questionAnswer = new QuestionAnswer(questionInfo);
|
|
|
@@ -258,11 +293,12 @@ public class PaperService {
|
|
|
private Map<Long, QuestionAnswer> getPaperAnswersFromResult(int paperId, long userId) {
|
|
|
PaperResult paperResult = getPaperResult(paperId, userId);
|
|
|
if (paperResult == null) {
|
|
|
- return getPaperAnswersFromCorrect(paperId);
|
|
|
+ log.error("userId:{} 的试卷 paperId:{} 结果不存在", userId, paperId);
|
|
|
+ return getEmptyPaperAnswers(paperId);
|
|
|
}
|
|
|
|
|
|
int resultId = paperResult.getId();
|
|
|
- return getPaperAnswersFromResult(resultId);
|
|
|
+ return getPaperAnswersFromResult0(paperId, resultId);
|
|
|
}
|
|
|
|
|
|
private PaperResult getPaperResult(int paperId, long userId) {
|
|
|
@@ -275,12 +311,15 @@ public class PaperService {
|
|
|
return list.isEmpty() ? null : list.get(0);
|
|
|
}
|
|
|
|
|
|
- private Map<Long, QuestionAnswer> getPaperAnswersFromResult(int resultId) {
|
|
|
+ private Map<Long, QuestionAnswer> getPaperAnswersFromResult0(int paperId, int resultId) {
|
|
|
Map<Long, QuestionAnswer> map = new HashMap<>();
|
|
|
List<PaperAnswer> paperAnswerList = paperAnswerMapper.findByResultId(resultId);
|
|
|
paperAnswerList.forEach(paperAnswer -> {
|
|
|
- QuestionAnswer questionAnswer = new QuestionAnswer(paperAnswer);
|
|
|
long questionId = paperAnswer.getQuestionId();
|
|
|
+ PaperQuestion paperQuestion = paperQuestionMapper.findPaperQuestion(paperId, questionId);
|
|
|
+ int correctScore = paperQuestion.getScore();
|
|
|
+ QuestionAnswer questionAnswer = new QuestionAnswer(paperAnswer, correctScore);
|
|
|
+
|
|
|
setSubmitAnswer(paperAnswer, questionAnswer);
|
|
|
setCorrectAnswer(questionAnswer);
|
|
|
map.put(questionId, questionAnswer);
|
|
|
@@ -314,16 +353,20 @@ public class PaperService {
|
|
|
questionAnswer.setSubmitAnswer(submitAnswerList.toArray(new Object[0]));
|
|
|
}
|
|
|
|
|
|
- @Deprecated
|
|
|
private Map<Long, QuestionAnswer> getPaperAnswersFromCorrect(int paperId) {
|
|
|
Map<Long, QuestionAnswer> map = new HashMap<>();
|
|
|
List<PaperQuestion> paperQuestions = paperQuestionMapper.findByPaperId(paperId);
|
|
|
- List<Long> questionIds = paperQuestions.stream().map(PaperQuestion::getQuestionId).collect(Collectors.toList());
|
|
|
+ Map<Long, PaperQuestion> paperQuestionMap = paperQuestions.stream()
|
|
|
+ .collect(Collectors.toMap(PaperQuestion::getQuestionId, k -> k));
|
|
|
+ //List<Long> questionIds = paperQuestions.stream().map(PaperQuestion::getQuestionId).collect(Collectors.toList());
|
|
|
+ List<Long> questionIds = new ArrayList<>(paperQuestionMap.keySet());
|
|
|
+
|
|
|
List<Question> questions = questionMapper.findByQuestionIds(questionIds);
|
|
|
questions.forEach(question -> {
|
|
|
- QuestionAnswer questionAnswer = new QuestionAnswer(question);
|
|
|
long questionId = question.getQuestionId();
|
|
|
int type = question.getType();
|
|
|
+ int correctScore = paperQuestionMap.get(questionId).getScore();
|
|
|
+ QuestionAnswer questionAnswer = new QuestionAnswer(question, correctScore);
|
|
|
|
|
|
List<QuestionOption> list = questionOptionMapper.findByQuestionId(questionId);
|
|
|
// 正确答案
|