AsyncTask.java 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239
  1. package com.qxgmat.task;
  2. import com.alibaba.fastjson.JSONObject;
  3. import com.nuliji.tools.Transform;
  4. import com.qxgmat.data.constants.enums.QuestionDifficult;
  5. import com.qxgmat.data.constants.enums.QuestionType;
  6. import com.qxgmat.data.constants.enums.SettingKey;
  7. import com.qxgmat.data.constants.enums.logic.ExerciseLogic;
  8. import com.qxgmat.data.constants.enums.logic.SentenceLogic;
  9. import com.qxgmat.data.constants.enums.module.StructModule;
  10. import com.qxgmat.data.dao.entity.*;
  11. import com.qxgmat.data.relation.entity.QuestionNoRelation;
  12. import com.qxgmat.service.extend.ExerciseService;
  13. import com.qxgmat.service.extend.SentenceService;
  14. import com.qxgmat.service.inline.ExerciseStructService;
  15. import com.qxgmat.service.inline.QuestionNoService;
  16. import com.qxgmat.service.inline.SentenceQuestionService;
  17. import com.qxgmat.service.inline.SettingService;
  18. import org.slf4j.Logger;
  19. import org.slf4j.LoggerFactory;
  20. import org.springframework.beans.factory.annotation.Autowired;
  21. import org.springframework.scheduling.annotation.Async;
  22. import org.springframework.stereotype.Component;
  23. import java.util.*;
  24. import java.util.stream.Collectors;
  25. @Component
  26. public class AsyncTask {
  27. private static final Logger logger = LoggerFactory.getLogger(AsyncTask.class);
  28. @Autowired
  29. private SentenceQuestionService sentenceQuestionService;
  30. @Autowired
  31. private SentenceService sentenceService;
  32. @Autowired
  33. private ExerciseService exerciseService;
  34. @Autowired
  35. private QuestionNoService questionNoService;
  36. @Autowired
  37. private ExerciseStructService exerciseStructService;
  38. @Autowired
  39. private SettingService settingService;
  40. @Async
  41. public void autoExercisePaper() {
  42. logger.info("自动练习组卷:顺序,考点,难易度");
  43. long start = System.currentTimeMillis();
  44. Setting setting = settingService.getByKey(SettingKey.EXERCISE_PAPER_STATUS);
  45. JSONObject status = setting.getValue();
  46. if (status == null) {
  47. status = new JSONObject();
  48. }
  49. int progress = 0;
  50. status.put("progress", 10);
  51. settingService.edit(setting);
  52. // 按所有4级结构
  53. List<ExerciseStruct> p = exerciseStructService.all();
  54. for(ExerciseStruct struct : p){
  55. if (struct.getLevel()!=4){
  56. continue;
  57. }
  58. progress += 70 / p.size();
  59. status.put("progress", progress);
  60. settingService.edit(setting);
  61. String prefixTitle = String.format("%s%s", struct.getTitleEn(), struct.getTitleZh());
  62. QuestionType questionType = QuestionType.ValueOf(struct.getExtend());
  63. List<QuestionNoRelation> list = questionNoService.listWithRelationByStruct(StructModule.EXERCISE, struct.getId());
  64. // 按顺序组卷
  65. List<ExercisePaper> noPapers = exerciseService.createPaper(prefixTitle, questionType, struct.getParentId(), struct.getId(), exerciseService.paperLength, ExerciseLogic.NO, null, list);
  66. Collection noIds = Transform.getIds(noPapers, SentencePaper.class, "id");
  67. exerciseService.switchPaper(struct.getParentId(), struct.getId(), ExerciseLogic.NO, noIds);
  68. // 按难度组卷
  69. List<Integer> allDifficultIds = new ArrayList<>();
  70. for(QuestionDifficult difficult : QuestionDifficult.all()){
  71. List<QuestionNoRelation> difficultList = list.stream().filter((question)-> question.getQuestion().getDifficult().equals(difficult.key)).collect(Collectors.toList());
  72. List<ExercisePaper> difficultPapers = exerciseService.createPaper(String.format("%s %s", prefixTitle, difficult.key), questionType, struct.getParentId(), struct.getId(), exerciseService.paperLength, ExerciseLogic.DIFFICULT, difficult.key, difficultList);
  73. Collection difficultIds = Transform.getIds(difficultPapers, ExercisePaper.class, "id");
  74. allDifficultIds.addAll(difficultIds);
  75. }
  76. exerciseService.switchPaper(struct.getParentId(), struct.getId(), ExerciseLogic.DIFFICULT, allDifficultIds);
  77. // 按考点组卷
  78. Map<String, List<QuestionNoRelation>> placeMap = new HashMap<>();
  79. for(QuestionNoRelation relation:list){
  80. String place = relation.getQuestion().getPlace();
  81. if (!placeMap.containsKey(place)){
  82. placeMap.put(place, new ArrayList<>());
  83. }
  84. List<QuestionNoRelation> placeList = placeMap.get(place);
  85. placeList.add(relation);
  86. }
  87. List<Integer> allPlaceIds = new ArrayList<>();
  88. for(String place: placeMap.keySet()){
  89. List<QuestionNoRelation> placeList = placeMap.get(place);
  90. List<ExercisePaper> difficultPapers = exerciseService.createPaper(String.format("%s %s", prefixTitle, place), questionType, struct.getParentId(), struct.getId(), exerciseService.paperLength, ExerciseLogic.PLACE, place, placeList);
  91. Collection placeIds = Transform.getIds(difficultPapers, ExercisePaper.class, "id");
  92. allPlaceIds.addAll(placeIds);
  93. }
  94. exerciseService.switchPaper(struct.getParentId(), struct.getId(), ExerciseLogic.PLACE, allPlaceIds);
  95. // 按难易度组卷:保持和下方难易度组卷一致
  96. list.sort((x, y)-> {
  97. // 难度从高到低
  98. Integer xScore = x.getTotalCorrect() * 100/ x.getTotalNumber();
  99. Integer yScore = y.getTotalCorrect() * 100/ y.getTotalNumber();
  100. return yScore.compareTo(xScore);
  101. });
  102. List<ExercisePaper> errorPapers = exerciseService.createPaper(prefixTitle, questionType, struct.getParentId(), struct.getId(), exerciseService.paperLength, ExerciseLogic.ERROR, null, list);
  103. Collection errorIds = Transform.getIds(errorPapers, SentencePaper.class, "id");
  104. exerciseService.switchPaper(struct.getParentId(), struct.getId(), ExerciseLogic.ERROR, errorIds);
  105. }
  106. status.put("progress", 80);
  107. settingService.edit(setting);
  108. // 作文组卷
  109. for(ExerciseStruct struct : p){
  110. if (struct.getLevel()!=3 || !struct.getExtend().equals(QuestionType.AWA.key)){
  111. continue;
  112. }
  113. String prefixTitle = String.format("%s%s", struct.getTitleEn(), struct.getTitleZh());
  114. QuestionType questionType = QuestionType.ValueOf(struct.getExtend());
  115. List<QuestionNoRelation> list = questionNoService.listWithRelationByStruct(StructModule.EXERCISE, struct.getId());
  116. // 按顺序组卷
  117. List<ExercisePaper> noPapers = exerciseService.createPaper(prefixTitle, questionType, struct.getId(), 0, exerciseService.awaLength, ExerciseLogic.NO, null, list);
  118. Collection noIds = Transform.getIds(noPapers, SentencePaper.class, "id");
  119. exerciseService.switchPaper(struct.getId(), 0, ExerciseLogic.NO, noIds);
  120. }
  121. status.put("progress", 80);
  122. settingService.edit(setting);
  123. // 逻辑组卷
  124. for(ExerciseStruct struct : p){
  125. if (struct.getLevel()!=3 || !struct.getExtend().equals(QuestionType.IR.key)){
  126. continue;
  127. }
  128. String prefixTitle = String.format("%s%s", struct.getTitleEn(), struct.getTitleZh());
  129. QuestionType questionType = QuestionType.ValueOf(struct.getExtend());
  130. List<QuestionNoRelation> list = questionNoService.listWithRelationByStruct(StructModule.EXERCISE, struct.getId());
  131. // 按顺序组卷
  132. List<ExercisePaper> noPapers = exerciseService.createPaper(prefixTitle, questionType, struct.getId(), 0, exerciseService.irLength, ExerciseLogic.NO, null, list);
  133. Collection noIds = Transform.getIds(noPapers, SentencePaper.class, "id");
  134. exerciseService.switchPaper(struct.getId(), 0, ExerciseLogic.NO, noIds);
  135. }
  136. status.put("progress", 100);
  137. settingService.edit(setting);
  138. long end = System.currentTimeMillis();
  139. logger.info("自动练习组卷,耗时:" + (end - start) + "毫秒");
  140. }
  141. @Async
  142. public void autoExercisePaperError() {
  143. logger.info("自动练习:难易度组卷,全局难易度判断");
  144. long start = System.currentTimeMillis();
  145. // 按所有4级结构
  146. List<ExerciseStruct> p = exerciseStructService.all();
  147. for(ExerciseStruct struct : p){
  148. if (struct.getLevel()!=4){
  149. continue;
  150. }
  151. String prefixTitle = String.format("%s%s", struct.getTitleEn(), struct.getTitleZh());
  152. QuestionType questionType = QuestionType.ValueOf(struct.getExtend());
  153. List<QuestionNoRelation> list = questionNoService.listWithRelationByStruct(StructModule.EXERCISE, struct.getId());
  154. // 按难易度组卷:保持和上方难易度组卷一致
  155. list.sort((x, y)-> {
  156. // 难度从高到低
  157. Integer xScore = x.getTotalCorrect() * 100/ x.getTotalNumber();
  158. Integer yScore = y.getTotalCorrect() * 100/ y.getTotalNumber();
  159. return yScore.compareTo(xScore);
  160. });
  161. List<ExercisePaper> errorPapers = exerciseService.createPaper(prefixTitle, questionType, struct.getParentId(), struct.getId(), exerciseService.paperLength, ExerciseLogic.ERROR, null, list);
  162. Collection errorIds = Transform.getIds(errorPapers, SentencePaper.class, "id");
  163. exerciseService.switchPaper(struct.getParentId(), struct.getId(), ExerciseLogic.ERROR, errorIds);
  164. }
  165. long end = System.currentTimeMillis();
  166. logger.info("自动练习难易度组卷,耗时:" + (end - start) + "毫秒");
  167. }
  168. @Async
  169. public void autoSentencePaper() {
  170. logger.info("自动长难句:对于试用卷进行自动生成");
  171. long start = System.currentTimeMillis();
  172. Setting setting = settingService.getByKey(SettingKey.SENTENCE_PAPER_STATUS);
  173. JSONObject status = setting.getValue();
  174. if (status == null) {
  175. status = new JSONObject();
  176. }
  177. status.put("progress", 10);
  178. settingService.edit(setting);
  179. // 获取所有组卷长难句题目
  180. List<SentenceQuestion> list = sentenceQuestionService.allPaper();
  181. status.put("progress", 20);
  182. settingService.edit(setting);
  183. // 组正常卷
  184. List<SentencePaper> noPapers = sentenceService.createPaper("长难句", SentenceLogic.NO, list);
  185. status.put("progress", 30);
  186. settingService.edit(setting);
  187. Collection noIds = Transform.getIds(noPapers, SentencePaper.class, "id");
  188. status.put("progress", 40);
  189. settingService.edit(setting);
  190. sentenceService.switchPaper(SentenceLogic.NO, noIds);
  191. status.put("progress", 60);
  192. settingService.edit(setting);
  193. // 组试用卷
  194. List<SentenceQuestion> trailList = list.stream().filter((question)-> question.getIsTrail() > 0).collect(Collectors.toList());
  195. List<SentencePaper> trailPapers = sentenceService.createPaper("长难句试用", SentenceLogic.TRAIL, trailList);
  196. status.put("progress", 70);
  197. settingService.edit(setting);
  198. Collection trailIds = Transform.getIds(trailPapers, SentencePaper.class, "id");
  199. status.put("progress", 80);
  200. settingService.edit(setting);
  201. sentenceService.switchPaper(SentenceLogic.TRAIL, trailIds);
  202. status.put("progress", 100);
  203. settingService.edit(setting);
  204. long end = System.currentTimeMillis();
  205. logger.info("自动长难句组卷,耗时:" + (end - start) + "毫秒");
  206. }
  207. @Async
  208. public void postTextbookToEmail(){
  209. logger.info("发布机经:发送到订阅用户邮箱");
  210. long start = System.currentTimeMillis();
  211. // 获取订阅用户及其邮箱
  212. long end = System.currentTimeMillis();
  213. logger.info("发布机经,耗时:" + (end - start) + "毫秒");
  214. }
  215. }