From 51b4d1996a14eae3a10d5605a036fe7c961822f7 Mon Sep 17 00:00:00 2001
From: xiangpei <xiangpei@timesnew.cn>
Date: 星期一, 24 六月 2024 11:53:27 +0800
Subject: [PATCH] 开始考试接口随机试卷生成
---
src/main/java/com/ycl/jxkg/mapper/QuestionMapper.java | 10 ++++++++++
src/main/resources/mapper/QuestionMapper.xml | 11 +++++++++++
src/main/java/com/ycl/jxkg/service/impl/ExamServiceImpl.java | 36 +++++++++++++++++++++++++++---------
3 files changed, 48 insertions(+), 9 deletions(-)
diff --git a/src/main/java/com/ycl/jxkg/mapper/QuestionMapper.java b/src/main/java/com/ycl/jxkg/mapper/QuestionMapper.java
index 3042302..c418e3d 100644
--- a/src/main/java/com/ycl/jxkg/mapper/QuestionMapper.java
+++ b/src/main/java/com/ycl/jxkg/mapper/QuestionMapper.java
@@ -31,4 +31,14 @@
List<Question> getAnswerInfo(@Param("questionIds") List<Integer> questionIds);
List<RandomQuestionDTO> selectBySubject(@Param("subjectId") Integer subjectId, @Param("types") List<Integer> types);
+
+ /**
+ * 鑾峰彇闅忔満棰�
+ *
+ * @param subjectId 璇剧洰
+ * @param questionType 棰樺瀷
+ * @param num 鏁伴噺
+ * @return
+ */
+ List<Question> getRandomQuestion(@Param("subjectId") Integer subjectId, @Param("questionType") Integer questionType, @Param("num") Integer num);
}
diff --git a/src/main/java/com/ycl/jxkg/service/impl/ExamServiceImpl.java b/src/main/java/com/ycl/jxkg/service/impl/ExamServiceImpl.java
index f7ffe3a..674129f 100644
--- a/src/main/java/com/ycl/jxkg/service/impl/ExamServiceImpl.java
+++ b/src/main/java/com/ycl/jxkg/service/impl/ExamServiceImpl.java
@@ -7,6 +7,7 @@
import com.ycl.jxkg.domain.entity.*;
import com.ycl.jxkg.domain.exam.PaperFixQuestionDTO;
import com.ycl.jxkg.domain.exam.PaperQuestionSettingDTO;
+import com.ycl.jxkg.domain.question.QuestionObject;
import com.ycl.jxkg.domain.vo.*;
import com.ycl.jxkg.domain.vo.admin.exam.ExamPaperEditRequestVO;
import com.ycl.jxkg.enums.ExamPaperTypeEnum;
@@ -150,30 +151,47 @@
if (Objects.isNull(examPaper)) {
throw new RuntimeException("璇曞嵎涓嶅瓨鍦�");
}
- if (StringUtils.hasText(examPaper.getContent())) {
- throw new RuntimeException("璇曞嵎棰樼洰涓虹┖");
- }
- // 灏嗛鐩浆鎹负鍙复鏃朵繚瀛樼殑棰樼洰缁撴瀯
+ // 灏嗛鐩浆鎹负鍙复鏃朵繚瀛樼殑棰樼洰缁撴瀯銆傚浐瀹氳瘯鍗峰拰闅忓簭璇曞嵎鐨勯鐩槸鐩存帴淇濆瓨鍒癱ontent瀛楁鐨�
if (ExamPaperTypeEnum.Fixed.getCode().equals(examPaper.getPaperType())
|| ExamPaperTypeEnum.RandomOrder.getCode().equals(examPaper.getPaperType())) {
+ if (StringUtils.hasText(examPaper.getContent())) {
+ throw new RuntimeException("璇曞嵎棰樼洰涓虹┖");
+ }
// 杞崲
List<PaperFixQuestionVO> data = this.coverTo(examPaper);
+
return Result.ok().data(data);
} else if (ExamPaperTypeEnum.Random.getCode().equals(examPaper.getPaperType())) {
- // todo 闅忔満棰樼洰鐢熸垚
// 鏍规嵁闅忔満璇曞嵎鐨勯厤缃紝闅忔満鐢熸垚瀵瑰簲棰樼洰
if (! StringUtils.hasText(examPaper.getContent())) {
throw new RuntimeException("璇曞嵎閰嶇疆寮傚父锛岃鑱旂郴鑰佸笀");
}
List<PaperQuestionSettingDTO> paperSettingList = JSON.parseArray(examPaper.getContent(), PaperQuestionSettingDTO.class);
- List<Question> questionList = new ArrayList<>(24);
+ List<DoQuestionVO> questionList = new ArrayList<>(24);
for (PaperQuestionSettingDTO paperSetting : paperSettingList) {
- if (QuestionTypeEnum.SingleChoice.getCode().equals(paperSetting.getQuestionType())) {
-// questionMapper.getRandomQuestion(examPaper.getSubjectId(), paperSetting.getQuestionType())
+ // 鎷垮埌璇剧洰涓嬫煇绫婚鍨嬬殑x閬撻殢鏈洪
+ List<Question> questions = questionMapper.getRandomQuestion(examPaper.getSubjectId(), paperSetting.getQuestionType(), paperSetting.getNum());
+ if (paperSetting.getNum() > questions.size()) {
+ throw new RuntimeException("閰嶇疆鐨勯鐩暟涓嶈冻浠ョ敓鎴愯瘯鍗�");
}
+ // 鎷垮埌棰樼洰鍚庣粍瑁呬负鍙复鏃朵繚瀛樼殑棰樼洰缁撴瀯
+ List<DoQuestionVO> childQuestions = questions.stream().map(item -> {
+ DoQuestionVO doQuestionVO = new DoQuestionVO();
+ doQuestionVO.setTitle(item.getTitle());
+ doQuestionVO.setQuestionType(item.getQuestionType());
+ if (StringUtils.hasText(item.getContent())) {
+ QuestionObject questionObject = JSON.parseObject(item.getContent(), QuestionObject.class);
+ doQuestionVO.setQuestionItemList(questionObject.getQuestionItemObjects());
+ }
+ doQuestionVO.setId(item.getId());
+ doQuestionVO.setOriginalFile(item.getOriginalFile());
+ doQuestionVO.setAudioFile(item.getAudioFile());
+ return doQuestionVO;
+ }).collect(Collectors.toList());
+ questionList.addAll(childQuestions);
}
}
- return null;
+ return Result.ok();
}
/**
diff --git a/src/main/resources/mapper/QuestionMapper.xml b/src/main/resources/mapper/QuestionMapper.xml
index 2a14509..074a926 100644
--- a/src/main/resources/mapper/QuestionMapper.xml
+++ b/src/main/resources/mapper/QuestionMapper.xml
@@ -106,4 +106,15 @@
and deleted = 0 and status = 1
</select>
+ <select id="getRandomQuestion" resultMap="BaseResultMap">
+ SELECT
+ *
+ FROM
+ t_question tq
+ WHERE
+ tq.subject_id = #{subjectId}
+ ORDER BY
+ RAND()
+ LIMIT #{num}
+ </select>
</mapper>
--
Gitblit v1.8.0