xiangpei
2024-06-26 12aa6d1bbe7b386f81731d797942b6a1ac7e3a2f
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;
@@ -145,35 +146,64 @@
        if (Objects.isNull(exam.getExamPaperId())) {
            throw new RuntimeException("考试未绑定试卷");
        }
        if (ExamStatusEnum.NOT_START.equals(exam.getStatus())) {
            throw new RuntimeException("考试还未开始");
        }
        if (ExamStatusEnum.FINISHED.equals(exam.getStatus())) {
            throw new RuntimeException("考试已经结束");
        }
        // 查出考试试卷
        ExamPaper examPaper = examPaperMapper.selectById(exam.getExamPaperId());
        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<PaperFixQuestionVO> examData = new ArrayList<>(8);
            for (PaperQuestionSettingDTO paperSetting : paperSettingList) {
                if (QuestionTypeEnum.SingleChoice.getCode().equals(paperSetting.getQuestionType())) {
//                    questionMapper.getRandomQuestion(examPaper.getSubjectId(), paperSetting.getQuestionType())
                PaperFixQuestionVO paperFixQuestionVO = new PaperFixQuestionVO();
                paperFixQuestionVO.setTitle(paperSetting.getTitle());
                paperFixQuestionVO.setQuestionType(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());
                paperFixQuestionVO.setQuestionList(childQuestions);
                examData.add(paperFixQuestionVO);
            }
            return Result.ok(examData);
        }
        return null;
        return Result.ok();
    }
    /**
@@ -183,6 +213,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();
@@ -234,6 +267,12 @@
        return Result.ok().data(vos);
    }
    /**
     * 主动提交试卷
     *
     * @param submitData 试卷做题提交数据
     * @return
     */
    @Override
    public Result examSubmit(ExamSubmitVO submitData) {
        // 校验
@@ -273,6 +312,12 @@
        return Result.ok();
    }
    /**
     * 临时保存试卷
     *
     * @param submitData
     * @return
     */
    @Override
    public Result timingSubmit(ExamSubmitVO submitData) {
        saveTempExam(submitData, ExamSubmitTempStatusEnum.TEMP);
@@ -280,7 +325,7 @@
    }
    /**
     * 保存临时试卷
     * 保存试卷:如果接口是定时保存那么是临时试卷。如果接口是自主提交那么是完成试卷
     *
     * @param submitData  前端传递的试卷数据
     * @param status  试卷的状态