src/main/java/com/mindskip/xzs/controller/student/QuestionController.java
@@ -38,11 +38,23 @@ return RestResponse.ok(vm); } /** * 查询题目内容 * * @param id * @return */ @GetMapping("/{id}") public RestResponse getById(@PathVariable("id") Integer id) { public RestResponse getContentById(@PathVariable("id") Integer id) { return questionService.selectContentById(id); } /** * 获取题目答案 * * @param id * @return */ @GetMapping("/answer/{id}") public RestResponse getAnswer(@PathVariable("id") Integer id) { return questionService.getAnswer(id); src/main/java/com/mindskip/xzs/controller/student/SelfPracticeController.java
@@ -58,4 +58,15 @@ return selfPracticeService.startPractice(id); } /** * 随机一道题 * * @param id * @return */ @GetMapping("/random/{id}") public RestResponse randomOneQuestion(@PathVariable("id") Integer id) { return selfPracticeService.randomOneQuestion(id); } } src/main/java/com/mindskip/xzs/domain/enums/QuestionTypeEnum.java
@@ -57,7 +57,7 @@ } } public int getCode() { public Integer getCode() { return code; } src/main/java/com/mindskip/xzs/domain/vo/QuestionContentVO.java
@@ -40,4 +40,6 @@ } } src/main/java/com/mindskip/xzs/domain/vo/QuestionVO.java
@@ -31,4 +31,7 @@ /** 多选题答案 */ private List<String> correctList; /** 多选题答案数 */ private Integer answerNum = 0; } src/main/java/com/mindskip/xzs/repository/QuestionMapper.java
@@ -34,5 +34,5 @@ QuestionVO selectContentById(Integer id); String getAnswer(Integer id); QuestionVO getAnswer(Integer id); } src/main/java/com/mindskip/xzs/service/SelfPracticeService.java
@@ -53,4 +53,12 @@ * @return */ RestResponse subjectQuestionNum(List<Integer> subjectIds); /** * 随机一道题 * * @param id * @return */ RestResponse randomOneQuestion(Integer id); } src/main/java/com/mindskip/xzs/service/impl/QuestionServiceImpl.java
@@ -274,18 +274,29 @@ @Override public RestResponse selectContentById(Integer id) { QuestionVO vo = questionMapper.selectContentById(id); jsonQuestion(vo); return RestResponse.ok(vo); QuestionVO questionVO = questionMapper.selectContentById(id); jsonQuestion(questionVO); if (QuestionTypeEnum.MultipleChoice.getCode().equals(questionVO.getQuestionType())) { // 多选题需要返回答案数量,学员选中对应数量才查询答案 if (StringUtils.hasText(questionVO.getCorrect())) { questionVO.setAnswerNum(questionVO.getCorrect().split(",").length); } } questionVO.setContentJson(""); questionVO.setCorrect(""); questionVO.getContent().setCorrect(""); questionVO.getContent().setAnalyze(""); return RestResponse.ok(questionVO); } @Override public RestResponse getAnswer(Integer id) { String content = questionMapper.getAnswer(id); if (StringUtils.hasText(content)) { QuestionContentVO vo = JSON.parseObject(content, QuestionContentVO.class); QuestionVO questionVO = questionMapper.getAnswer(id); if (Objects.nonNull(questionVO) && StringUtils.hasText(questionVO.getCorrect())) { QuestionContentVO vo = JSON.parseObject(questionVO.getContentJson(), QuestionContentVO.class); vo.setQuestionItemObjects(null); vo.setTitleContent(null); vo.setCorrect(questionVO.getCorrect()); return RestResponse.ok(vo); } return RestResponse.ok(null); src/main/java/com/mindskip/xzs/service/impl/SelfPracticeServiceImpl.java
@@ -129,18 +129,41 @@ return RestResponse.ok(num); } @Override public RestResponse randomOneQuestion(Integer id) { SelfPractice en = selfPracticeMapper.selectById(id); if (Objects.isNull(en)) { throw new RuntimeException("练习不存在"); } List<Integer> subjectIds = JSON.parseArray(en.getSubjects(), Integer.class); List<QuestionVO> one = questionSubjectMapper.getRandomQuestionId(subjectIds, 1); if (one.size() < 1) { throw new RuntimeException("没有找到题目,可能所选课目包含题目不足"); } QuestionVO questionVO = one.get(0); jsonQuestion(questionVO); if (QuestionTypeEnum.MultipleChoice.getCode().equals(questionVO.getQuestionType())) { // 多选题需要返回答案数量,学员选中对应数量才查询答案 if (StringUtils.hasText(questionVO.getCorrect())) { questionVO.setAnswerNum(questionVO.getCorrect().split(",").length); } } questionVO.setContentJson(""); questionVO.setCorrect(""); questionVO.getContent().setCorrect(""); questionVO.getContent().setAnalyze(""); return RestResponse.ok(questionVO); } /** * 处理题目内容JSON * * @param vos * @param vo */ public void jsonQuestion(List<QuestionVO> vos) { vos.stream().forEach(vo -> { if (StringUtils.hasText(vo.getContentJson())) { QuestionContentVO questionContent = JSON.parseObject(vo.getContentJson(), QuestionContentVO.class); vo.setContent(questionContent); } }); public void jsonQuestion(QuestionVO vo) { if (StringUtils.hasText(vo.getContentJson())) { QuestionContentVO questionContent = JSON.parseObject(vo.getContentJson(), QuestionContentVO.class); vo.setContent(questionContent); } } } src/main/resources/mapper/QuestionMapper.xml
@@ -317,15 +317,17 @@ tq.id, tq.question_type as questionType, tq.difficult, ttc.content as contentJson ttc.content as contentJson, tq.correct FROM t_question tq INNER JOIN t_text_content ttc ON tq.info_text_content_id = ttc.id AND tq.id = #{id} </select> <select id="getAnswer" resultType="string"> <select id="getAnswer" resultType="com.mindskip.xzs.domain.vo.QuestionVO"> SELECT ttc.content tq.correct, ttc.content as contentJson FROM t_question tq INNER JOIN t_text_content ttc ON tq.info_text_content_id = ttc.id AND tq.id = #{id} AND tq.deleted = 0 src/main/resources/mapper/QuestionSubjectMapper.xml
@@ -70,7 +70,8 @@ tq.id, tq.question_type as questionType, tq.difficult, ttc.content as contentJson ttc.content as contentJson, tq.correct FROM t_question_subject tqs INNER JOIN t_question tq ON tqs.question_id = tq.id AND tq.deleted = 0 AND tqs.subject_id IN <foreach collection="subjectIds" open="(" separator="," close=")" item="subjectId">#{subjectId}</foreach>