浏览代码

修改套题情况下的unit修改逻辑
修改question模型子题的ID规则
修改quesService保存试题的逻辑

luoshi@qmth.com.cn 8 年之前
父节点
当前提交
cccb4fd78b

+ 0 - 9
cqb-paper/src/main/java/com/qmth/cqb/paper/dto/PaperDetailUnitExp.java

@@ -18,8 +18,6 @@ public class PaperDetailUnitExp implements Serializable {
 
     private Double score;// 小题分数
 
-    private String subScores;//套题情况下所有子题分数,用英文逗号分割
-
     private QuesStructType questionType;// 小题类型
 
     private Question question;// 关联试题
@@ -78,11 +76,4 @@ public class PaperDetailUnitExp implements Serializable {
         this.quesId = quesId;
     }
 
-    public String getSubScores() {
-        return subScores;
-    }
-
-    public void setSubScores(String subScores) {
-        this.subScores = subScores;
-    }
 }

+ 25 - 28
cqb-paper/src/main/java/com/qmth/cqb/paper/service/PaperDetailUnitService.java

@@ -55,41 +55,38 @@ public class PaperDetailUnitService {
     public PaperDetailUnit savePaperDetailUnit(PaperDetailUnitExp pduExp) {
         PaperDetailUnit oldPdu = paperDetailUnitRepo.findOne(pduExp.getId());
         if (oldPdu.getQuestionType() == QuesStructType.NESTED_ANSWER_QUESTION) {
-            //套题情况下单独处理
-            Question oldQuestion = null;
             if (pduExp.getQuestion().getId().equals(oldPdu.getQuestion().getId())) {
-                oldQuestion = oldPdu.getQuestion();
-            }
-            List<Double> subScoreList = oldPdu.getSubScoreList();
-            //套题需要判断更新的对象是套题本身还是子题
-            int index = 0;
-            for (Question sub : oldPdu.getQuestion().getSubQuestions()) {
-                index++;
-                //检查子题分数,确保没有错误数据
-                if (subScoreList.size() < index) {
-                    subScoreList.add(0d);
+                //更新对象为套题本身
+                oldPdu.setQuestion(quesService.saveQues(pduExp.getQuestion()));
+            } else {
+                List<Double> subScoreList = oldPdu.getSubScoreList();
+                int size = oldPdu.getQuestion().getSubQuestions().size();
+                boolean match = false;
+                //判断更新的对象是哪个子题
+                for (int index = 1; index <= size; index++) {
+                    //检查子题分数,确保没有错误数据
+                    if (subScoreList.size() < index) {
+                        subScoreList.add(0d);
+                    }
+                    Question sub = oldPdu.getQuestion().getSubQuestions().get(index - 1);
+                    if (pduExp.getQuestion().getId().equals(sub.getId())) {
+                        //匹配到子题
+                        subScoreList.set(index - 1, pduExp.getScore());
+                        oldPdu.getQuestion().getSubQuestions().set(index - 1, pduExp.getQuestion());
+                        match = true;
+                    }
                 }
-                if (sub.getId().equals(pduExp.getQuestion().getId())) {
-                    //匹配到子题
-                    oldQuestion = sub;
-                    subScoreList.set(index - 1, pduExp.getScore());
+                //更新套题unit当前总分
+                oldPdu.updateSubScore(subScoreList);
+                if (match) {
+                    oldPdu.setQuestion(quesService.saveQues(oldPdu.getQuestion()));
                 }
             }
-            if (oldQuestion == null) {
-                //匹配试题失败,参数错误直接返回
-                return oldPdu;
-            }
-            //更新套题unit当前总分
-            oldPdu.updateSubScore(subScoreList);
-            if (oldQuestion.getId().equals(oldPdu.getQuestion().getId())) {
-                //更新的是套题大题,需要沿用之前的子题
-                pduExp.getQuestion().setSubQuestions(oldQuestion.getSubQuestions());
-            }
         } else {
             oldPdu.setScore(pduExp.getScore());
+            oldPdu.setQuestion(quesService.saveQues(pduExp.getQuestion()));
         }
-        oldPdu.setQuestion(pduExp.getQuestion());
-        quesService.saveQues(pduExp.getQuestion());// 同时要跟新小题里面的Qustion对象
+        // 同时要跟新小题里面的Qustion对象
         paperService.formatPaper(oldPdu.getPaper());
         return paperDetailUnitRepo.save(oldPdu);
     }

+ 77 - 33
cqb-question-resource/src/main/java/com/qmth/cqb/question/service/QuesService.java

@@ -14,6 +14,7 @@ import org.springframework.data.domain.PageRequest;
 import org.springframework.data.domain.Pageable;
 import org.springframework.stereotype.Service;
 
+import java.util.ArrayList;
 import java.util.HashMap;
 import java.util.List;
 import java.util.Set;
@@ -28,61 +29,104 @@ public class QuesService {
     QuesRepo quesRepo;
 
     /**
-     * 批量保存试题
+     * 套题子题按序号自动生成ID
      *
-     * @param list
+     * @param question
      */
-    public void saveQuesList(List<Question> list) {
-        if (list != null) {
-            for (Question question : list) {
-                saveQues(question);
+    private void updateSubId(Question question) {
+        if (question.getSubQuestions() != null) {
+            int index = 0;
+            for (Question sub : question.getSubQuestions()) {
+                index++;
+                sub.setId(String.valueOf(index));
             }
         }
     }
 
     /**
-     * 保存试题
+     * 更新已存在的试题信息
      *
-     * @param question
+     * @param previous
+     * @param update
      */
-    public Question saveQues(Question question) {
+    private void updateMainInfo(Question previous, Question update) {
         String now = CommonUtils.getCurDateTime();
-        Question saveQues = quesRepo.findOne(question.getId());
-        if (saveQues == null) {
-            question.setCreateTime(now);
-            question.setUpdateTime(now);
-            return quesRepo.save(question);
-        }
-        if (question.getScore() != null) {
-            saveQues.setScore(question.getScore());
+        if (update.getScore() != null) {
+            previous.setScore(update.getScore());
         }
-        saveQues.setQuesAnswer(question.getQuesAnswer());
-        saveQues.setQuesAnswerAnalysis(question.getQuesAnswerAnalysis());
-        saveQues.setQuesBody(question.getQuesBody());
-        if (question.getQuesOptions() != null && question.getQuesOptions().size() > 0) {
-            List<QuesOption> quesOptions = question.getQuesOptions();
-            List<QuesOption> saveQuesOptions = saveQues.getQuesOptions();
+        previous.setQuesAnswer(update.getQuesAnswer());
+        previous.setQuesAnswerAnalysis(update.getQuesAnswerAnalysis());
+        previous.setQuesBody(update.getQuesBody());
+        if (update.getQuesOptions() != null && update.getQuesOptions().size() > 0) {
+            List<QuesOption> quesOptions = update.getQuesOptions();
+            List<QuesOption> saveQuesOptions = previous.getQuesOptions();
             if (saveQuesOptions.size() != quesOptions.size()) {
-                saveQues.setQuesOptions(quesOptions);
+                previous.setQuesOptions(quesOptions);
             } else {
                 for (int i = 0; i < quesOptions.size(); i++) {
                     saveQuesOptions.get(i).setOptionBody(quesOptions.get(i).getOptionBody());
                 }
             }
         }
-        if (question.getQuesParams() != null) {
-            if (saveQues.getQuesParams() == null) {
-                saveQues.setQuesParams(new HashMap<>());
+        if (update.getQuesParams() != null) {
+            if (previous.getQuesParams() == null) {
+                previous.setQuesParams(new HashMap<>());
+            }
+            for (String key : update.getQuesParams().keySet()) {
+                previous.getQuesParams().put(key, update.getQuesParams().get(key));
+            }
+        }
+        if (previous.getCreateTime() == null) {
+            previous.setCreateTime(now);
+        }
+        if (update.getSubQuestions() != null) {
+            if (previous.getSubQuestions() == null) {
+                previous.setSubQuestions(new ArrayList<>());
             }
-            for (String key : question.getQuesParams().keySet()) {
-                saveQues.getQuesParams().put(key, question.getQuesParams().get(key));
+            int previousSize = previous.getSubQuestions().size();
+            int updateSize = update.getSubQuestions().size();
+            for (int i = 0; i < updateSize; i++) {
+                if (previousSize <= i) {
+                    previous.getSubQuestions().add(update.getSubQuestions().get(i));
+                } else {
+                    updateMainInfo(previous.getSubQuestions().get(i), update.getSubQuestions().get(i));
+                }
             }
         }
-        if (saveQues.getCreateTime() == null) {
-            saveQues.setCreateTime(now);
+        previous.setUpdateTime(now);
+        updateSubId(previous);
+    }
+
+    /**
+     * 保存试题
+     *
+     * @param question
+     */
+    public Question saveQues(Question question) {
+        String now = CommonUtils.getCurDateTime();
+        Question saveQues = quesRepo.findOne(question.getId());
+        if (saveQues == null) {
+            question.setCreateTime(now);
+            question.setUpdateTime(now);
+            updateSubId(question);
+            return quesRepo.save(question);
+        } else {
+            updateMainInfo(saveQues, question);
+            return quesRepo.save(saveQues);
+        }
+    }
+
+    /**
+     * 批量保存试题
+     *
+     * @param list
+     */
+    public void saveQuesList(List<Question> list) {
+        if (list != null) {
+            for (Question question : list) {
+                saveQues(question);
+            }
         }
-        saveQues.setUpdateTime(now);
-        return quesRepo.save(saveQues);
     }
 
     /**