SentenceService.java 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  1. package com.qxgmat.service.extend;
  2. import com.nuliji.tools.Transform;
  3. import com.nuliji.tools.exception.ParameterException;
  4. import com.qxgmat.data.constants.enums.QuestionType;
  5. import com.qxgmat.data.constants.enums.logic.SentenceLogic;
  6. import com.qxgmat.data.dao.entity.Question;
  7. import com.qxgmat.data.dao.entity.SentencePaper;
  8. import com.qxgmat.data.dao.entity.SentenceQuestion;
  9. import com.qxgmat.data.relation.entity.SentenceQuestionRelation;
  10. import com.qxgmat.service.inline.SentencePaperService;
  11. import com.qxgmat.service.inline.QuestionService;
  12. import com.qxgmat.service.inline.SentenceQuestionService;
  13. import org.springframework.beans.factory.annotation.Value;
  14. import org.springframework.stereotype.Service;
  15. import org.springframework.transaction.annotation.Transactional;
  16. import javax.annotation.Resource;
  17. import java.util.ArrayList;
  18. import java.util.Collection;
  19. import java.util.List;
  20. @Service
  21. public class SentenceService {
  22. @Value("${paper.sentenceLength}")
  23. private Integer paperLength;
  24. @Resource
  25. private QuestionService questionService;
  26. @Resource
  27. private SentenceQuestionService sentenceQuestionService;
  28. @Resource
  29. private SentencePaperService sentencePaperService;
  30. /**
  31. * 添加长难句题目:加入题库,关联题目,并关联长难句paper
  32. * @param relation
  33. * @return
  34. */
  35. @Transactional
  36. public SentenceQuestion addQuestion(SentenceQuestionRelation relation){
  37. SentenceQuestion in = sentenceQuestionService.getByNo(relation.getNo());
  38. if (in != null){
  39. throw new ParameterException("序号已经存在");
  40. }
  41. Question question = relation.getQuestion();
  42. question.setQuestionType(QuestionType.SENTENCE.key);
  43. question = questionService.add(question);
  44. // 绑定关系
  45. relation.setQuestionId(question.getId());
  46. relation.setSubject(String.format("CNG%d", relation.getNo()));
  47. SentenceQuestion sentenceQuestion = sentenceQuestionService.add(relation);
  48. // 绑定关系:统一组卷
  49. // addQuestionToPaperWithNo(sentenceQuestion);
  50. //
  51. // if (sentenceQuestion.getIsTrail() > 0){
  52. // addQuestionToPaperWithTrail(sentenceQuestion);
  53. // }
  54. return sentenceQuestion;
  55. }
  56. /**
  57. * 更新长难句题目:更新题库,,变更长难句paper
  58. * @param relation
  59. * @return
  60. */
  61. @Transactional
  62. public SentenceQuestion editQuestion(SentenceQuestionRelation relation){
  63. SentenceQuestion in = sentenceQuestionService.getByNo(relation.getNo());
  64. if (in != null && !in.getId().equals(relation.getId())){
  65. throw new ParameterException("序号已经存在");
  66. }
  67. Question question = relation.getQuestion();
  68. question.setQuestionType(QuestionType.SENTENCE.key);
  69. question = questionService.edit(question);
  70. if(in == null){
  71. in = sentenceQuestionService.get(relation.getId());
  72. }
  73. relation.setSubject(String.format("CNG%d", relation.getNo()));
  74. // 根据序号调整分组:移出原有关系 - 绑定关系
  75. // if (!in.getNo().equals(relation.getNo())){
  76. // removeQuestionFromPaper(in, SentenceLogic.NO);
  77. // addQuestionToPaperWithNo(relation);
  78. // }
  79. // 根据试用调整
  80. // if (!in.getIsTrail().equals(relation.getIsTrail())){
  81. // if (relation.getIsTrail() > 0){
  82. // addQuestionToPaperWithTrail(relation);
  83. // }else{
  84. // removeQuestionFromPaper(in, SentenceLogic.TRAIL);
  85. // }
  86. // }
  87. SentenceQuestion sentenceQuestion = sentenceQuestionService.edit(relation);
  88. return sentenceQuestion;
  89. }
  90. /**
  91. * 根据题目创建组卷序列
  92. * @param logic
  93. * @param questionList
  94. * @return
  95. */
  96. @Transactional
  97. public List<SentencePaper> createPaper(String prefixTitle, SentenceLogic logic, List<SentenceQuestion> questionList){
  98. Integer no = 0;
  99. List<SentencePaper> list = new ArrayList<>();
  100. List<Integer> tmp = new ArrayList<>(paperLength);
  101. for(SentenceQuestion question : questionList){
  102. tmp.add(question.getId());
  103. if (tmp.size() == paperLength){
  104. no += 1;
  105. SentencePaper paper = sentencePaperService.add(SentencePaper.builder()
  106. .logic(logic.key)
  107. .no(no)
  108. .questionNumber(tmp.size())
  109. .status(0)
  110. .questionNoIds(tmp.toArray(new Integer[0])).build()
  111. );
  112. paper.setTitle(sentencePaperService.generateTitle(prefixTitle, paperLength, paper.getNo(), paper.getQuestionNumber()));
  113. list.add(paper);
  114. tmp.clear();
  115. }
  116. }
  117. if (tmp.size() > 0){
  118. no += 1;
  119. SentencePaper paper = sentencePaperService.add(SentencePaper.builder()
  120. .logic(logic.key)
  121. .no(no)
  122. .questionNumber(tmp.size())
  123. .status(0)
  124. .questionNoIds(tmp.toArray(new Integer[0])).build()
  125. );
  126. paper.setTitle(sentencePaperService.generateTitle(prefixTitle, paperLength, paper.getNo(), paper.getQuestionNumber()));
  127. list.add(paper);
  128. }
  129. return list;
  130. }
  131. /**
  132. * 切换新组卷
  133. * @param logic
  134. * @param ids
  135. */
  136. @Transactional
  137. public void switchPaper(SentenceLogic logic, Collection ids){
  138. // 先将可用卷删除
  139. List<SentencePaper> list = sentencePaperService.listByLogic(logic);
  140. Collection oldIds = Transform.getIds(list, SentencePaper.class, "id");
  141. sentencePaperService.updatePaper(oldIds, 0);
  142. // 将新组卷启用
  143. sentencePaperService.updatePaper(ids, 1);
  144. }
  145. }