| | |
| | | import com.mindskip.xzs.base.RestResponse; |
| | | import com.mindskip.xzs.context.WebContext; |
| | | import com.mindskip.xzs.domain.ExamPaperAnswer; |
| | | import com.mindskip.xzs.domain.Question; |
| | | import com.mindskip.xzs.domain.SelfPractice; |
| | | 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.repository.QuestionMapper; |
| | | import com.mindskip.xzs.repository.QuestionSubjectMapper; |
| | | import com.mindskip.xzs.repository.SelfPracticeMapper; |
| | | import com.mindskip.xzs.repository.SubjectMapper; |
| | | import com.mindskip.xzs.service.QuestionSubjectService; |
| | | import com.mindskip.xzs.service.SelfPracticeService; |
| | | import lombok.RequiredArgsConstructor; |
| | | import org.springframework.beans.BeanUtils; |
| | | import org.springframework.stereotype.Service; |
| | | import org.springframework.util.CollectionUtils; |
| | | import org.springframework.util.StringUtils; |
| | | |
| | | import java.util.Date; |
| | | import java.util.List; |
| | |
| | | private final WebContext webContext; |
| | | private final QuestionSubjectMapper questionSubjectMapper; |
| | | private final SubjectMapper subjectMapper; |
| | | private final QuestionMapper questionMapper; |
| | | |
| | | @Override |
| | | public RestResponse add(SelfPracticeVO vo) { |
| | |
| | | selfPracticeMapper.remove(ids); |
| | | return RestResponse.ok("删除成功"); |
| | | } |
| | | |
| | | @Override |
| | | public RestResponse startPractice(Integer id) { |
| | | SelfPractice en = selfPracticeMapper.selectById(id); |
| | | 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<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); |
| | | } |
| | | } |
| | | |
| | | @Override |
| | | public RestResponse subjectQuestionNum(List<Integer> subjectIds) { |
| | | Integer num = questionSubjectMapper.countQuestionNum(subjectIds); |
| | | return RestResponse.ok(num); |
| | | } |
| | | |
| | | |
| | | /** |
| | | * 处理题目内容JSON |
| | | * |
| | | * @param vos |
| | | */ |
| | | 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); |
| | | } |
| | | }); |
| | | } |
| | | } |