xiangpei
2024-05-14 87c1bdac0c88d208cd2786913742d818e4d2debb
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);
        }
    }
}