From c89a3707c556f97c4531f556e6c832cb6b5d2b43 Mon Sep 17 00:00:00 2001 From: xiangpei <xiangpei@timesnew.cn> Date: 星期四, 31 十月 2024 17:53:19 +0800 Subject: [PATCH] 新建/修改考试时,校验试卷是否能成功生成 --- src/main/java/com/ycl/jxkg/service/impl/ExamServiceImpl.java | 163 +++++++++++++++++++++++++++++++++++++++++++++--------- 1 files changed, 135 insertions(+), 28 deletions(-) diff --git a/src/main/java/com/ycl/jxkg/service/impl/ExamServiceImpl.java b/src/main/java/com/ycl/jxkg/service/impl/ExamServiceImpl.java index 4997be2..678767c 100644 --- a/src/main/java/com/ycl/jxkg/service/impl/ExamServiceImpl.java +++ b/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()); - // 璁剧疆涔愯閿佺増鏈� - entity.setUpdateVersion(0); - if (baseMapper.insert(entity) > 0) { - this.sendMQ(entity, 0); + + // 鏌ュ嚭鑰冭瘯璇曞嵎 + 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("娣诲姞鎴愬姛"); } @@ -117,10 +127,19 @@ throw new RuntimeException("鍙兘淇敼杩樻湭寮�濮嬬殑鑰冭瘯"); } BeanUtils.copyProperties(form, entity); - entity.setStatus(ExamStatusEnum.getStatusByTime(form.getStartTime(), form.getEndTime(), null)); - // 濡傛灉淇敼鎴愬姛鍙戦�乵q娑堟伅 - if (baseMapper.updateById(entity) > 0) { - this.sendMQ(entity, entity.getUpdateVersion()); + entity.setStatus(ExamStatusEnum.getStatusByTime(form.getStartTime(), form.getEndTime(), new Date())); + + // 鏌ュ嚭鑰冭瘯璇曞嵎 + ExamPaper examPaper = examPaperMapper.selectById(entity.getExamPaperId()); + try { + this.checkGenExam(examPaper); + // 濡傛灉淇敼鎴愬姛鍙戦�乵q娑堟伅 + if (baseMapper.updateById(entity) > 0) { + this.sendMQ(entity, entity.getUpdateVersion()); + } + } catch (RuntimeException e) { + e.printStackTrace(); + return Result.fail(500, e.getMessage()); } return Result.ok("淇敼鎴愬姛"); } @@ -214,6 +233,7 @@ item.setExamPaperId(null); } }); + // return Result.ok().data(page.getRecords()).total(page.getTotal()); } @@ -225,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) @@ -254,9 +361,9 @@ .one(); if (Objects.nonNull(hasJoin)) { // 鍏佽鎻愪氦鍚庣户缁綔绛� -// if(ExamSubmitTempStatusEnum.finish.equals(hasJoin.getStatus())){ -// throw new RuntimeException("鎮ㄥ凡鎻愪氦璇曞嵎锛岃鍕块噸澶嶄綔绛�"); -// } + if(ExamSubmitTempStatusEnum.finish.equals(hasJoin.getStatus())){ + throw new RuntimeException("鎮ㄥ凡鎻愪氦璇曞嵎锛岃鍕块噸澶嶄綔绛�"); + } StartExamVO startExamVO = new StartExamVO(); startExamVO.setExamName(exam.getExamName()); startExamVO.setId(hasJoin.getExamId()); @@ -299,6 +406,9 @@ 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()); @@ -343,7 +453,9 @@ childQuestionList.addAll(childQuestions); } paperFixQuestionVO.setQuestionList(childQuestionList); - examData.add(paperFixQuestionVO); + if (! CollectionUtils.isEmpty(childQuestionList)) { + examData.add(paperFixQuestionVO); + } } } ExamSubmitTemp examSubmitTemp = new ExamSubmitTemp(); @@ -381,7 +493,6 @@ doQuestionVO.setQuestionType(item.getQuestionType()); //澧炲姞棰樼洰鍒嗘暟 doQuestionVO.setQuestionScore(question.getScore()); - // 棰樼洰鍓湰 QuestionAnswerCopyVO copy = new QuestionAnswerCopyVO(); copy.setId(question.getId()); @@ -514,7 +625,6 @@ ExamSubmitTemp one = new LambdaQueryChainWrapper<>(examSubmitTempMapper) .eq(ExamSubmitTemp::getExamId, submitData.getId()) .eq(ExamSubmitTemp::getUserId, webContext.getCurrentUser().getId()) - .eq(ExamSubmitTemp::getDeleted, 0) .one(); if (Objects.nonNull(one)) { @@ -634,12 +744,14 @@ for (PaperFixQuestionVO titleItem : titleItems) { for (DoQuestionVO doQuestionVO : titleItem.getQuestionList()) { Integer questionId = doQuestionVO.getId(); - Optional<QuestionAnswerCopyVO> first = answerList.stream().filter(answer -> questionId.equals(answer.getId())).findFirst(); - if (first.isPresent()) { - QuestionAnswerCopyVO answerCopyVO = first.get(); - doQuestionVO.setQuestionAnswer(answerCopyVO.getCorrect()); - doQuestionVO.setAnalyze(answerCopyVO.getAnalyze()); - doQuestionVO.setDifficult(answerCopyVO.getDifficult()); + if(questionId!=null) { + Optional<QuestionAnswerCopyVO> first = answerList.stream().filter(answer -> questionId.equals(answer.getId())).findFirst(); + if (first.isPresent()) { + QuestionAnswerCopyVO answerCopyVO = first.get(); + doQuestionVO.setQuestionAnswer(answerCopyVO.getCorrect()); + doQuestionVO.setAnalyze(answerCopyVO.getAnalyze()); + doQuestionVO.setDifficult(answerCopyVO.getDifficult()); + } } } } @@ -954,7 +1066,7 @@ /** * 浣滃簾 * - * @param examId + * @param id * @return {@link Result } * @author */ @@ -977,13 +1089,8 @@ Date currentTime = new Date(); Date startTime = examInfo.getStartTime(); Date endTime = examInfo.getEndTime(); - if (currentTime.before(startTime)) { - examInfo.setStatus(ExamStatusEnum.NOT_START); - } else if (currentTime.after(startTime) && currentTime.before(endTime)) { - examInfo.setStatus(ExamStatusEnum.ING); - } else { - examInfo.setStatus(ExamStatusEnum.FINISHED); - } + ExamStatusEnum statusByTime = ExamStatusEnum.getStatusByTime(startTime, endTime, currentTime); + examInfo.setStatus(statusByTime); // 淇敼褰撳墠鐨勮�冭瘯鐘舵�� new LambdaUpdateChainWrapper<>(examMapper) .eq(Exam::getId, id) -- Gitblit v1.8.0