From 526745df87b382366960df1c765b83a84d493019 Mon Sep 17 00:00:00 2001
From: fuliqi <fuliqi@qq.com>
Date: 星期三, 03 七月 2024 16:23:23 +0800
Subject: [PATCH] 随机试卷增加维度

---
 src/main/java/com/ycl/jxkg/service/impl/ExamPaperServiceImpl.java |  129 ++++++++++++++++++------------------------
 1 files changed, 56 insertions(+), 73 deletions(-)

diff --git a/src/main/java/com/ycl/jxkg/service/impl/ExamPaperServiceImpl.java b/src/main/java/com/ycl/jxkg/service/impl/ExamPaperServiceImpl.java
index 3b72bc8..40b3d69 100644
--- a/src/main/java/com/ycl/jxkg/service/impl/ExamPaperServiceImpl.java
+++ b/src/main/java/com/ycl/jxkg/service/impl/ExamPaperServiceImpl.java
@@ -16,6 +16,7 @@
 import com.ycl.jxkg.domain.exam.PaperFixQuestionDTO;
 import com.ycl.jxkg.domain.exam.PaperQuestion;
 import com.ycl.jxkg.domain.exam.PaperQuestionSettingDTO;
+import com.ycl.jxkg.domain.exam.PaperSettingItem;
 import com.ycl.jxkg.domain.form.ExamPaperForm;
 import com.ycl.jxkg.domain.other.KeyValue;
 import com.ycl.jxkg.domain.question.QuestionItemObject;
@@ -45,6 +46,7 @@
 import org.springframework.stereotype.Service;
 import org.springframework.transaction.annotation.Transactional;
 import org.springframework.util.CollectionUtils;
+import org.springframework.util.StringUtils;
 
 import java.math.BigDecimal;
 import java.util.*;
@@ -71,18 +73,24 @@
         if (ExamPaperTypeEnum.Random.getCode().equals(form.getPaperType())) {
             //鏍¢獙棰樼洰鏁伴噺
             List<PaperQuestionSettingDTO> questionSetting = form.getQuestionSetting();
-            //棰樼洰閰嶇疆閲岄厤鐨勮瘯鍗风被鍨�
-            List<Integer> types = questionSetting.stream().map(PaperQuestionSettingDTO::getQuestionType).collect(Collectors.toList());
-            Map<Integer, List<Integer>> map = questionMapper.selectBySubject(form.getSubjectId(), types)
-                    .stream().collect(Collectors.groupingBy(RandomQuestionDTO::getQuestionType, Collectors.mapping(RandomQuestionDTO::getQuestionId, Collectors.toList())));
-
             for (PaperQuestionSettingDTO settingDTO : questionSetting) {
-                Integer num = settingDTO.getNum();
                 Integer questionType = settingDTO.getQuestionType();
-                //闇�瑕侀厤缃殑棰樼洰鏁伴噺涓�0鍒欒烦杩�
-                if(num ==null || num ==0 )continue;
-                List<Integer> questions = map.get(questionType);
-                if(CollectionUtils.isEmpty(questions) || num > questions.size())return Result.fail(SystemCode.InnerError.getCode(), QuestionTypeEnum.fromCode(questionType).getName()+"棰樼洰鏁伴噺涓嶈冻");
+                for (PaperSettingItem item : settingDTO.getSettingList()) {
+                    Integer num = item.getNum();
+                    Integer difficult = item.getDifficult();
+                    //闇�瑕侀厤缃殑棰樼洰鏁伴噺涓�0鍒欒烦杩�
+                    if(num ==null || num ==0 )continue;
+                    //鍓嶇榛樿鏁版嵁涓�0锛岃浆鎹负null
+                    if (0 == item.getDifficult()){
+                        difficult= null;
+                    }
+                    RandomQuestionDTO randomQuestionDTO = new RandomQuestionDTO();
+                    randomQuestionDTO.setQuestionType(questionType);
+                    randomQuestionDTO.setSubjectId(item.getSubjectId());
+                    randomQuestionDTO.setDifficult(difficult);
+                    Integer numInData = questionMapper.selectByDifAndSub(randomQuestionDTO);
+                    if(num > numInData)return Result.fail(SystemCode.InnerError.getCode(), QuestionTypeEnum.fromCode(questionType).getName()+"闅惧害涓�"+item.getDifficult()+"鐨勯鐩暟閲忎笉瓒�");
+                }
             }
             examPaper.setContent(JSON.toJSONString(form.getQuestionSetting()));
             baseMapper.insert(examPaper);
@@ -94,86 +102,61 @@
             return Result.ok();
         } else {
             //闅忓簭璇曞嵎
-            Integer subjectId = form.getSubjectId();
-            //棰樼洰閰嶇疆
             List<PaperQuestionSettingDTO> questionSetting = form.getQuestionSetting();
             //棰樼洰閰嶇疆閲岄厤鐨勮瘯鍗风被鍨�
             List<Integer> types = questionSetting.stream().map(PaperQuestionSettingDTO::getQuestionType).collect(Collectors.toList());
             if (CollectionUtils.isEmpty(types)) {
                 return Result.fail(SystemCode.InnerError.getCode(), "璇曞嵎棰樼洰绫诲瀷涓嶈兘涓虹┖");
             }
-            Map<Integer, List<Integer>> map = questionMapper.selectBySubject(subjectId, types)
-                    .stream().collect(Collectors.groupingBy(RandomQuestionDTO::getQuestionType, Collectors.mapping(RandomQuestionDTO::getQuestionId, Collectors.toList())));
             List<PaperFixQuestionDTO> questionTitleList = new ArrayList<>();
-            // 閬嶅巻map
-            for (Integer questionType : map.keySet()) {
-                //鏁版嵁搴撻噷鐨勮繖涓被鍨嬬殑棰樼洰
-                List<Integer> questionIdList = map.get(questionType);
-                Result result = createQuestion(questionSetting, questionTitleList, questionType, questionIdList,QuestionTypeEnum.fromCode(questionType).getCode());
-                if (result != null) return result;
+            for (PaperQuestionSettingDTO settingDTO : questionSetting) {
+                List<PaperSettingItem> settingList = settingDTO.getSettingList();
+                List<PaperQuestion> questionList = new ArrayList<>();
+                for (PaperSettingItem item : settingList) {
+                    Integer num = item.getNum();
+                    Integer difficult = item.getDifficult();
+                    //闇�瑕侀厤缃殑棰樼洰鏁伴噺涓�0鍒欒烦杩�
+                    if(num ==null || num ==0 )continue;
+                    //鍓嶇榛樿鏁版嵁涓�0锛岃浆鎹负null
+                    if (0 == difficult){
+                        difficult = null;
+                    }
+                    List<Question> questions = questionMapper.getRandomQuestion(item.getSubjectId(), settingDTO.getQuestionType(), difficult, item.getNum());
+                    if (CollectionUtils.isEmpty(questions) || item.getNum() > questions.size()) {
+                        return Result.fail(SystemCode.InnerError.getCode(), QuestionTypeEnum.fromCode(settingDTO.getQuestionType()).getName()+"闅惧害涓猴細"+item.getDifficult()+"鐨勯鐩暟閲忎笉瓒�");
+                    }
+                    //杞崲鏁版嵁
+                    convert(questionList, item, questions);
+                }
+                    PaperFixQuestionDTO dto = new PaperFixQuestionDTO();
+                    dto.setTitle(settingDTO.getTitle());
+                    dto.setQuestionType(settingDTO.getQuestionType());
+                    dto.setQuestionList(questionList);
+                    questionTitleList.add(dto);
             }
             examPaper.setContent(JSON.toJSONString(questionTitleList));
             baseMapper.insert(examPaper);
             return Result.ok();
         }
     }
-    //鐢熸垚棰樼洰
-    private Result createQuestion(List<PaperQuestionSettingDTO> questionSetting, List<PaperFixQuestionDTO> questionTitleList, Integer questionType, List<Integer> questionIdList , Integer questionEnumCode) {
-        if (questionType.equals(questionEnumCode)) {
-            //寰幆鎵惧埌瀵瑰簲棰樼洰
-            PaperQuestionSettingDTO settingDTO = new PaperQuestionSettingDTO();
-            for (PaperQuestionSettingDTO dto : questionSetting) {
-                if (dto.getQuestionType().equals(questionType)) {
-                    settingDTO = dto;
-                }
-            }// 闇�瑕佺敓鎴愮殑棰樼洰鏁伴噺
-            Integer num = settingDTO.getNum();
-            if(num ==null || num ==0 ){
-                //棰樼洰閰嶇疆姝ょ被鍨嬫暟閲忎负0锛岃烦杩囷紝涓嶇敓鎴愰鐩��
-                return null;
+    //杞崲鏁版嵁
+    private void convert(List<PaperQuestion> questionList, PaperSettingItem item, List<Question> questions) {
+        for (Question question : questions) {
+            PaperQuestion paperQuestion = new PaperQuestion();
+            BeanUtils.copyProperties(question,paperQuestion);
+            paperQuestion.setScore(item.getScore());
+            //杞崲
+            QuestionObject questionObject = JSONObject.parseObject(question.getContent(), QuestionObject.class);
+            if(questionObject != null){
+                paperQuestion.setItems(questionObject.getQuestionItemObjects());
+                paperQuestion.setAnalyze(questionObject.getAnalyze());
+                paperQuestion.setTitle(questionObject.getTitleContent());
             }
-            if (CollectionUtils.isEmpty(questionIdList) || num > questionIdList.size()) {
-                return Result.fail(SystemCode.InnerError.getCode(), QuestionTypeEnum.fromCode(questionType).getName()+"棰樼洰鏁伴噺涓嶈冻");
-            }
-
-            // 浣跨敤Random绫荤敓鎴愪笉閲嶅鐨勯殢鏈虹储寮�
-            Set<Integer> indexes = new HashSet<>();
-            Random random = new Random();
-            while (indexes.size() < num) {
-                int index = random.nextInt(questionIdList.size());
-                indexes.add(index);
-            }
-            // 鏍规嵁绱㈠紩鑾峰彇棰樼洰
-            List<Integer> questionIds = new ArrayList<>();
-            for (int index : indexes) {
-                questionIds.add(questionIdList.get(index));
-            }
-            QueryWrapper<Question> wrapper = new QueryWrapper<>();
-            wrapper.in("id",questionIds);
-            List<Question> questions = questionMapper.selectList(wrapper);
-            List<PaperQuestion> questionList = new ArrayList<>();
-            for (Question question : questions) {
-                PaperQuestion paperQuestion = new PaperQuestion();
-                BeanUtils.copyProperties(question,paperQuestion);
-                paperQuestion.setScore(settingDTO.getScore());
-                //杞崲
-                QuestionObject questionObject = JSONObject.parseObject(question.getContent(), QuestionObject.class);
-                if(questionObject != null){
-                    paperQuestion.setItems(questionObject.getQuestionItemObjects());
-                    paperQuestion.setAnalyze(questionObject.getAnalyze());
-                    paperQuestion.setTitle(questionObject.getTitleContent());
-                }
-                questionList.add(paperQuestion);
-            }
-            PaperFixQuestionDTO dto = new PaperFixQuestionDTO();
-            dto.setTitle(settingDTO.getTitle());
-            dto.setQuestionType(questionType);
-            dto.setQuestionList(questionList);
-            questionTitleList.add(dto);
+            questionList.add(paperQuestion);
         }
-        return null;
     }
 
+
     @Override
     public Result updateExamPaper(ExamPaperForm form) {
         //TODO锛氶獙璇佹槸鍚︽槸璇曞嵎鍒涘缓浜�

--
Gitblit v1.8.0