xiangpei
2024-10-31 c89a3707c556f97c4531f556e6c832cb6b5d2b43
新建/修改考试时,校验试卷是否能成功生成
1个文件已修改
108 ■■■■■ 已修改文件
src/main/java/com/ycl/jxkg/service/impl/ExamServiceImpl.java 108 ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史
src/main/java/com/ycl/jxkg/service/impl/ExamServiceImpl.java
@@ -93,10 +93,20 @@
        Exam entity = ExamForm.getEntityByForm(form, null);
        entity.setStatus(ExamStatusEnum.getStatusByTime(form.getStartTime(), form.getEndTime(), null));
        entity.setTeacherId(webContext.getCurrentUser().getId());
        // 查出考试试卷
        ExamPaper examPaper = examPaperMapper.selectById(entity.getExamPaperId());
        // 校验能否生成一张试卷
        try {
            this.checkGenExam(examPaper);
        // 设置乐观锁版本
        entity.setUpdateVersion(0);
        if (baseMapper.insert(entity) > 0) {
            this.sendMQ(entity, 0);
            }
        } catch (RuntimeException e) {
            e.printStackTrace();
            return Result.fail(500, e.getMessage());
        }
        return Result.ok("添加成功");
    }
@@ -118,9 +128,18 @@
        }
        BeanUtils.copyProperties(form, entity);
        entity.setStatus(ExamStatusEnum.getStatusByTime(form.getStartTime(), form.getEndTime(), new Date()));
        // 查出考试试卷
        ExamPaper examPaper = examPaperMapper.selectById(entity.getExamPaperId());
        try {
            this.checkGenExam(examPaper);
        // 如果修改成功发送mq消息
        if (baseMapper.updateById(entity) > 0) {
            this.sendMQ(entity, entity.getUpdateVersion());
            }
        } catch (RuntimeException e) {
            e.printStackTrace();
            return Result.fail(500, e.getMessage());
        }
        return Result.ok("修改成功");
    }
@@ -226,6 +245,93 @@
        return Result.ok().data(page.getRecords()).total(page.getTotal());
    }
    /**
     * 测试试卷配置能否生成试卷
     *
     * @param examPaper
     */
    public void checkGenExam(ExamPaper examPaper) throws RuntimeException {
        // 试卷内容
        List<PaperFixQuestionVO> examData = new ArrayList<>();
        // 拿到题目副本数据
        List<QuestionAnswerCopyVO> questionAnswerCopyVOList = new ArrayList<>(32);
        if (ExamPaperTypeEnum.Fixed.getCode().equals(examPaper.getPaperType())
                || ExamPaperTypeEnum.RandomOrder.getCode().equals(examPaper.getPaperType())) {
            if (!StringUtils.hasText(examPaper.getContent())) {
                throw new RuntimeException("试卷题目为空");
            }
            // 转换
            examData = this.coverTo(examPaper, questionAnswerCopyVOList);
        } else if (ExamPaperTypeEnum.Random.getCode().equals(examPaper.getPaperType())) {
            // 根据随机试卷的配置,随机生成对应题目
            if (!StringUtils.hasText(examPaper.getContent())) {
                throw new RuntimeException("试卷配置异常,请联系老师");
            }
            List<PaperQuestionSettingDTO> paperSettingList = JSON.parseArray(examPaper.getContent(), PaperQuestionSettingDTO.class);
            examData = new ArrayList<>(8);
            for (PaperQuestionSettingDTO paperSetting : paperSettingList) {
                PaperFixQuestionVO paperFixQuestionVO = new PaperFixQuestionVO();
                paperFixQuestionVO.setTitle(paperSetting.getTitle());
                paperFixQuestionVO.setQuestionType(paperSetting.getQuestionType());
                //一个类型的题目list
                List<DoQuestionVO> childQuestionList = new ArrayList<>();
                List<PaperSettingItem> settingList = paperSetting.getSettingList();
                for (PaperSettingItem settingItem : settingList) {
                    Integer num = settingItem.getNum();
                    Integer difficult = settingItem.getDifficult();
                    if(0 == difficult) {
                        difficult = null;
                    }
                    //需要配置的题目数量为0则跳过
                    if (num == null || num == 0) continue;
                    List<Question> questions = questionMapper.getRandomQuestion(settingItem.getSubjectId(), paperSetting.getQuestionType(), difficult, settingItem.getNum());
                    if (org.springframework.util.CollectionUtils.isEmpty(questions) || settingItem.getNum() > questions.size()) {
                        throw new RuntimeException("试卷配置的题目数不足以生成试卷");
                    }
                    // 拿到题目后组装为可临时保存的题目结构
                    List<DoQuestionVO> childQuestions = questions.stream().map(item -> {
                        DoQuestionVO doQuestionVO = new DoQuestionVO();
                        doQuestionVO.setQuestionType(item.getQuestionType());
                        //从配置里拿题目分数
                        doQuestionVO.setQuestionScore(settingItem.getScore());
                        if (StringUtils.hasText(item.getContent())) {
                            QuestionObject questionObject = JSON.parseObject(item.getContent(), QuestionObject.class);
                            doQuestionVO.setQuestionItemList(questionObject.getQuestionItemObjects());
                            doQuestionVO.setTitle(questionObject.getTitleContent());
                        }
                        doQuestionVO.setId(item.getId());
                        doQuestionVO.setOriginalFile(item.getOriginalFile());
                        doQuestionVO.setAudioFile(item.getAudioFile());
                        // 题目副本
                        QuestionAnswerCopyVO copy = new QuestionAnswerCopyVO();
                        copy.setId(item.getId());
                        copy.setDifficult(item.getDifficult());
                        copy.setAnalyze(JSON.parseObject(item.getContent(), PaperQuestion.class).getAnalyze());
                        //填空的答案在Json里
                        if (QuestionTypeEnum.GapFilling.getCode().equals(item.getQuestionType())) {
                            List<String> gapAnswer = new ArrayList<>();
                            for (QuestionItemObject questionItemObject : doQuestionVO.getQuestionItemList()) {
                                gapAnswer.add(questionItemObject.getContent());
                            }
                            copy.setCorrect(String.join(ANSWER_SPLIT, gapAnswer));
                        } else {
                            copy.setCorrect(item.getCorrect());
                        }
                        questionAnswerCopyVOList.add(copy);
                        return doQuestionVO;
                    }).collect(Collectors.toList());
                    //添加到这个类型的list中
                    childQuestionList.addAll(childQuestions);
                }
                paperFixQuestionVO.setQuestionList(childQuestionList);
                if (! CollectionUtils.isEmpty(childQuestionList)) {
                    examData.add(paperFixQuestionVO);
                }
            }
        }
    }
    @Override
    @Transactional(rollbackFor = Exception.class)
@@ -300,7 +406,7 @@
                for (PaperSettingItem settingItem : settingList) {
                    Integer num = settingItem.getNum();
                    Integer difficult = settingItem.getDifficult();
                    if(difficult == 0){
                    if(0 == difficult){
                        difficult = null;
                    }
                    //需要配置的题目数量为0则跳过