|
@@ -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);
|
|
|
}
|
|
|
|
|
|
/**
|