| | |
| | | import com.mindskip.xzs.domain.ExamPaperAnswer; |
| | | import com.mindskip.xzs.domain.Question; |
| | | import com.mindskip.xzs.domain.SelfPractice; |
| | | import com.mindskip.xzs.domain.enums.PracticeQuestionTypeEnum; |
| | | import com.mindskip.xzs.domain.enums.PracticeTypeEnum; |
| | | import com.mindskip.xzs.domain.enums.QuestionTypeEnum; |
| | | import com.mindskip.xzs.domain.vo.QuestionContentVO; |
| | | import com.mindskip.xzs.domain.vo.QuestionVO; |
| | | import com.mindskip.xzs.domain.vo.SelfPracticeVO; |
| | | import com.mindskip.xzs.domain.vo.SubjectQuestionVO; |
| | | import com.mindskip.xzs.repository.QuestionMapper; |
| | | import com.mindskip.xzs.repository.QuestionSubjectMapper; |
| | | import com.mindskip.xzs.repository.SelfPracticeMapper; |
| | |
| | | import org.springframework.util.CollectionUtils; |
| | | import org.springframework.util.StringUtils; |
| | | |
| | | import java.util.ArrayList; |
| | | import java.util.Date; |
| | | import java.util.List; |
| | | import java.util.Objects; |
| | |
| | | if (Objects.isNull(en)) { |
| | | throw new RuntimeException("练习不存在"); |
| | | } |
| | | if (StringUtils.hasText(en.getQuestionIds())) { |
| | | // 生成了题目就直接查 |
| | | List<Integer> questionIdList = JSON.parseArray(en.getQuestionIds(), Integer.class); |
| | | List<QuestionVO> vos = questionMapper.getVoByIds(questionIdList); |
| | | jsonQuestion(vos); |
| | | return RestResponse.ok(vos); |
| | | } else { |
| | | // 没生成过就随机生成题目 |
| | | List<Integer> subjectIds = JSON.parseArray(en.getSubjects(), Integer.class); |
| | | Integer totalQuestionNum = questionSubjectMapper.countQuestionNum(subjectIds); |
| | | if (totalQuestionNum < en.getQuestionNum()) { |
| | | throw new RuntimeException("你所选的课目题目数量不足"); |
| | | List<Integer> subjectIds = JSON.parseArray(en.getSubjects(), Integer.class); |
| | | if (PracticeTypeEnum.ORDERED.getValue().equals(en.getPracticeType())) { |
| | | List<SubjectQuestionVO> list = new ArrayList<>(2); |
| | | // 顺序做题,把选择的题库的题(id)全部查出来,前端有个序号面板,点击哪道题做哪道 |
| | | for (Integer subjectId : subjectIds) { |
| | | String subjectName = subjectMapper.selectSubjectNameById(subjectId); |
| | | // todo根据做题记录查询做没做过 |
| | | List<Integer> questionIds = new ArrayList<>(); |
| | | if (PracticeQuestionTypeEnum.ALL.getValue().equals(en.getQuestionType())) { |
| | | questionIds = questionSubjectMapper.questionsBySubjectId(subjectId); |
| | | } else { |
| | | questionIds = questionSubjectMapper.questionsBySubjectIdAndQuestionType(subjectId, PracticeQuestionTypeEnum.getDataBaseValueByValue(en.getQuestionType())); |
| | | } |
| | | SubjectQuestionVO subjectQuestionVO = new SubjectQuestionVO(); |
| | | subjectQuestionVO.setSubjectId(subjectId); |
| | | subjectQuestionVO.setSubjectName(subjectName); |
| | | subjectQuestionVO.setQuestionIds(questionIds); |
| | | list.add(subjectQuestionVO); |
| | | } |
| | | // 查询出课目下随机的设定数量题目 |
| | | List<QuestionVO> questionVOList = questionSubjectMapper.getRandomQuestionId(subjectIds, en.getQuestionNum()); |
| | | List<Integer> ids = questionVOList.stream().map(QuestionVO::getId).collect(Collectors.toList()); |
| | | selfPracticeMapper.setQuestionIds(en.getId(), JSON.toJSONString(ids)); |
| | | jsonQuestion(questionVOList); |
| | | return RestResponse.ok(questionVOList); |
| | | return RestResponse.ok(list); |
| | | } else if (PracticeTypeEnum.RANDOM.getValue().equals(en.getPracticeType())) { |
| | | // 随机练习,是一道题一道题练习 |
| | | List<QuestionVO> one = questionSubjectMapper.getRandomQuestionId(subjectIds, PracticeQuestionTypeEnum.getDataBaseValueByValue(en.getQuestionType()), 1); |
| | | if (one.size() < 1) { |
| | | throw new RuntimeException("没有找到题目,可能所选课目包含题目不足"); |
| | | } |
| | | QuestionVO questionVO = one.get(0); |
| | | jsonQuestion(questionVO); |
| | | return RestResponse.ok(questionVO); |
| | | } |
| | | return RestResponse.ok(); |
| | | } |
| | | |
| | | @Override |
| | |
| | | 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, PracticeQuestionTypeEnum.getDataBaseValueByValue(en.getQuestionType()), 1); |
| | | if (one.size() < 1) { |
| | | throw new RuntimeException("没有找到题目,可能所选课目包含题目不足"); |
| | | } |
| | | QuestionVO questionVO = one.get(0); |
| | | jsonQuestion(questionVO); |
| | | |
| | | return RestResponse.ok(questionVO); |
| | | } |
| | | |
| | | /** |
| | | * 处理题目内容JSON |
| | | * |
| | | * @param vos |
| | | * @param questionVO |
| | | */ |
| | | 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 questionVO) { |
| | | if (StringUtils.hasText(questionVO.getContentJson())) { |
| | | QuestionContentVO questionContent = JSON.parseObject(questionVO.getContentJson(), QuestionContentVO.class); |
| | | questionVO.setContent(questionContent); |
| | | } |
| | | 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(""); |
| | | } |
| | | } |