xiangpei
2024-06-24 51b4d1996a14eae3a10d5605a036fe7c961822f7
开始考试接口随机试卷生成
3个文件已修改
57 ■■■■ 已修改文件
src/main/java/com/ycl/jxkg/mapper/QuestionMapper.java 10 ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史
src/main/java/com/ycl/jxkg/service/impl/ExamServiceImpl.java 36 ●●●● 补丁 | 查看 | 原始文档 | blame | 历史
src/main/resources/mapper/QuestionMapper.xml 11 ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史
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);
}
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("试卷题目为空");
        }
        // 将题目转换为可临时保存的题目结构
        // 将题目转换为可临时保存的题目结构。固定试卷和随序试卷的题目是直接保存到content字段的
        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();
    }
    /**
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>