fuliqi
2024-06-25 0fb6563de530e5278356f7557480764cb59942c5
src/main/java/com/ycl/jxkg/service/impl/ExamServiceImpl.java
@@ -6,6 +6,8 @@
import com.ycl.jxkg.context.WebContext;
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;
@@ -149,20 +151,56 @@
        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<DoQuestionVO> questionList = new ArrayList<>(24);
            for (PaperQuestionSettingDTO paperSetting : paperSettingList) {
                // 拿到课目下某类题型的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);
            }
            // 保持随机试卷和固定试卷的格式一致
            List<PaperFixQuestionVO> examData = new ArrayList<>(1);
            PaperFixQuestionVO paperFixQuestionVO = new PaperFixQuestionVO();
            paperFixQuestionVO.setQuestionList(questionList);
            paperFixQuestionVO.setTitle("");
            paperFixQuestionVO.setQuestionType(null);
            examData.add(paperFixQuestionVO);
            return Result.ok(examData);
        }
        return null;
        return Result.ok();
    }
    /**
@@ -172,6 +210,9 @@
     * @return
     */
    private List<PaperFixQuestionVO> coverTo(ExamPaper examPaper) {
        if (! StringUtils.hasText(examPaper.getContent())) {
            throw new RuntimeException("试卷未配置题目,请联系老师");
        }
        List<PaperFixQuestionDTO> questionWarpList = JSON.parseArray(examPaper.getContent(), PaperFixQuestionDTO.class);
        return questionWarpList.stream().map(item -> {
            PaperFixQuestionVO vo = new PaperFixQuestionVO();
@@ -301,7 +342,7 @@
    }
    @Override
    public Result getMarkPaperInfo(Integer id) {
    public Result getMarkResultInfo(Integer id) {
        Exam exam = baseMapper.selectById(id);
        if (Objects.isNull(exam)) {
            throw new RuntimeException("该考试不存在");
@@ -341,4 +382,23 @@
        return Result.ok().data(markPaperVO);
    }
    @Override
    public Result getMarkPaperInfo(Integer examId, Integer userId) {
        ExamSubmitTemp userExam = new LambdaQueryChainWrapper<>(examSubmitTempMapper)
                .eq(ExamSubmitTemp::getExamId, examId)
                .eq(ExamSubmitTemp::getUserId, userId)
                .one();
        if (Objects.isNull(userExam)) {
            throw new RuntimeException("该学员考试记录不存在");
        }
        ExamSubmitVO vo = new ExamSubmitVO();
        vo.setExamId(userExam.getExamId());
        vo.setDoTime(userExam.getDoTime());
        vo.setUpdateTime(userExam.getUpdateTime());
        if (StringUtils.hasText(userExam.getExamSubmit())) {
            vo.setPaperQuestionList(JSON.parseArray(userExam.getExamSubmit(), PaperFixQuestionVO.class));
        }
        return Result.ok(vo);
    }
}