ExportService.java 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285
  1. package com.qxgmat.service.extend;
  2. import com.alibaba.fastjson.JSONArray;
  3. import com.alibaba.fastjson.JSONObject;
  4. import com.nuliji.tools.Transform;
  5. import com.nuliji.tools.exception.ParameterException;
  6. import com.qxgmat.data.constants.enums.QuestionType;
  7. import com.qxgmat.data.constants.enums.user.AskTarget;
  8. import com.qxgmat.data.constants.enums.user.ExportType;
  9. import com.qxgmat.data.dao.entity.*;
  10. import com.qxgmat.data.inline.ExportCourseNote;
  11. import com.qxgmat.data.inline.ExportQuestion;
  12. import com.qxgmat.data.relation.entity.QuestionNoRelation;
  13. import com.qxgmat.data.relation.entity.UserNoteCourseRelation;
  14. import com.qxgmat.data.relation.entity.UserQuestionRelation;
  15. import com.qxgmat.service.UserNoteCourseService;
  16. import com.qxgmat.service.UserNoteQuestionService;
  17. import com.qxgmat.service.UserQuestionService;
  18. import com.qxgmat.service.inline.*;
  19. import org.springframework.beans.factory.annotation.Autowired;
  20. import org.springframework.stereotype.Service;
  21. import javax.annotation.Resource;
  22. import java.util.*;
  23. @Service
  24. public class ExportService {
  25. @Autowired
  26. private UserNoteQuestionService userNoteQuestionService;
  27. @Autowired
  28. private UserNoteCourseService userNoteCourseService;
  29. @Autowired
  30. private CourseNoService courseNoService;
  31. @Autowired
  32. private QuestionService questionService;
  33. @Autowired
  34. private QuestionNoService questionNoService;
  35. @Autowired
  36. private UserAskQuestionService userAskQuestionService;
  37. @Autowired
  38. private UserQuestionService userQuestionService;
  39. @Resource
  40. private UserExportService userExportService;
  41. public UserExport addQuestionCollect(Integer userId, JSONObject setting){
  42. JSONArray questionNoIds = setting.getJSONArray("questionNoIds");
  43. if (questionNoIds.size() ==0){
  44. throw new ParameterException("不可小于1题,请重新选择");
  45. }
  46. if (questionNoIds.size() >100){
  47. throw new ParameterException("不可多余100题,请重新选择");
  48. }
  49. JSONArray include = setting.getJSONArray("include");
  50. List<UserQuestionRelation> relationList = this.listByQuestionNoIds(userId, questionNoIds.toJavaList(Integer.class), false);
  51. JSONObject content = new JSONObject();
  52. JSONArray list = new JSONArray();
  53. for(UserQuestionRelation relation : relationList){
  54. if (!include.contains(AskTarget.QUESTION.key)){
  55. relation.getQuestion().setStem(null);
  56. if (relation.getNote() != null) relation.getNote().setQuestionContent(null);
  57. }
  58. if (!include.contains(AskTarget.QX.key)){
  59. relation.getQuestion().setQxContent(null);
  60. if (relation.getNote() != null) relation.getNote().setQxContent(null);
  61. }
  62. if (!include.contains(AskTarget.OFFICIAL.key)){
  63. relation.getQuestion().setOfficialContent(null);
  64. if (relation.getNote() != null) relation.getNote().setOfficialContent(null);
  65. }
  66. if (!include.contains(AskTarget.ASSOCIATION.key)){
  67. relation.setAssociations(new ArrayList<>());
  68. if (relation.getNote() != null) relation.getNote().setAssociationContent(null);
  69. }
  70. if (!include.contains(AskTarget.QA.key)){
  71. relation.setAsks(new ArrayList<>());
  72. if (relation.getNote() != null) relation.getNote().setQaContent(null);
  73. }
  74. if (!include.contains("note")){
  75. relation.setNote(null);
  76. }
  77. list.add(Transform.convert(relation, ExportQuestion.class));
  78. }
  79. content.put("list", list);
  80. int count = userExportService.countByUser(userId, ExportType.QUESTION_COLLECT);
  81. UserExport export = UserExport.builder()
  82. .userId(userId)
  83. .type(ExportType.QUESTION_COLLECT.key)
  84. .setting(setting)
  85. .content(content)
  86. .no(count+1)
  87. .build();
  88. return userExportService.add(export);
  89. }
  90. public UserExport addQuestionError(Integer userId, JSONObject setting){
  91. JSONArray questionNoIds = setting.getJSONArray("questionNoIds");
  92. if (questionNoIds.size() ==0){
  93. throw new ParameterException("不可小于1题,请重新选择");
  94. }
  95. if (questionNoIds.size() >100){
  96. throw new ParameterException("不可多余100题,请重新选择");
  97. }
  98. JSONArray include = setting.getJSONArray("include");
  99. List<UserQuestionRelation> relationList = this.listByQuestionNoIds(userId, questionNoIds.toJavaList(Integer.class), true);
  100. JSONObject content = new JSONObject();
  101. JSONArray list = new JSONArray();
  102. for(UserQuestionRelation relation : relationList){
  103. if (!include.contains(AskTarget.QUESTION.key)){
  104. relation.getQuestion().setStem(null);
  105. if (relation.getNote() != null) relation.getNote().setQuestionContent(null);
  106. }
  107. if (!include.contains(AskTarget.QX.key)){
  108. relation.getQuestion().setQxContent(null);
  109. if (relation.getNote() != null) relation.getNote().setQxContent(null);
  110. }
  111. if (!include.contains(AskTarget.OFFICIAL.key)){
  112. relation.getQuestion().setOfficialContent(null);
  113. if (relation.getNote() != null) relation.getNote().setOfficialContent(null);
  114. }
  115. if (!include.contains(AskTarget.ASSOCIATION.key)){
  116. relation.setAssociations(new ArrayList<>());
  117. if (relation.getNote() != null) relation.getNote().setAssociationContent(null);
  118. }
  119. if (!include.contains(AskTarget.QA.key)){
  120. relation.setAsks(new ArrayList<>());
  121. if (relation.getNote() != null) relation.getNote().setQaContent(null);
  122. }
  123. if (!include.contains("note")){
  124. relation.setNote(null);
  125. }
  126. list.add(Transform.convert(relation, ExportQuestion.class));
  127. }
  128. content.put("list", list);
  129. int count = userExportService.countByUser(userId, ExportType.QUESTION_ERROR);
  130. UserExport export = UserExport.builder()
  131. .userId(userId)
  132. .type(ExportType.QUESTION_ERROR.key)
  133. .setting(setting)
  134. .content(content)
  135. .no(count + 1)
  136. .build();
  137. return userExportService.add(export);
  138. }
  139. public UserExport addQuestionNote(Integer userId, JSONObject setting){
  140. JSONArray questionNoIds = setting.getJSONArray("questionNoIds");
  141. if (questionNoIds.size() ==0){
  142. throw new ParameterException("不可小于1题,请重新选择");
  143. }
  144. if (questionNoIds.size() >100){
  145. throw new ParameterException("不可多余100题,请重新选择");
  146. }
  147. JSONArray include = setting.getJSONArray("include");
  148. List<UserQuestionRelation> relationList = this.listByQuestionNoIds(userId, questionNoIds.toJavaList(Integer.class), true);
  149. JSONObject content = new JSONObject();
  150. JSONArray list = new JSONArray();
  151. for(UserQuestionRelation relation : relationList){
  152. if (!include.contains(AskTarget.QUESTION.key)){
  153. relation.getQuestion().setStem(null);
  154. if (relation.getNote() != null) relation.getNote().setQuestionContent(null);
  155. }
  156. if (!include.contains(AskTarget.QX.key)){
  157. relation.getQuestion().setQxContent(null);
  158. if (relation.getNote() != null) relation.getNote().setQxContent(null);
  159. }
  160. if (!include.contains(AskTarget.OFFICIAL.key)){
  161. relation.getQuestion().setOfficialContent(null);
  162. if (relation.getNote() != null) relation.getNote().setOfficialContent(null);
  163. }
  164. if (!include.contains(AskTarget.ASSOCIATION.key)){
  165. relation.setAssociations(new ArrayList<>());
  166. if (relation.getNote() != null) relation.getNote().setAssociationContent(null);
  167. }
  168. if (!include.contains(AskTarget.QA.key)){
  169. relation.setAsks(new ArrayList<>());
  170. if (relation.getNote() != null) relation.getNote().setQaContent(null);
  171. }
  172. if (!include.contains("note")){
  173. relation.setNote(null);
  174. }
  175. list.add(Transform.convert(relation, ExportQuestion.class));
  176. }
  177. content.put("list", list);
  178. int count = userExportService.countByUser(userId, ExportType.NOTE_QUESTION);
  179. UserExport export = UserExport.builder()
  180. .userId(userId)
  181. .type(ExportType.NOTE_QUESTION.key)
  182. .setting(setting)
  183. .content(content)
  184. .no(count + 1)
  185. .build();
  186. return userExportService.add(export);
  187. }
  188. public UserExport addCourseNote(Integer userId, JSONObject setting){
  189. JSONArray courseNoIds = setting.getJSONArray("courseNoIds");
  190. if (courseNoIds.size() ==0){
  191. throw new ParameterException("不可小于1题,请重新选择");
  192. }
  193. if (courseNoIds.size() >100){
  194. throw new ParameterException("不可多余100题,请重新选择");
  195. }
  196. List<UserNoteCourse> noteList = userNoteCourseService.listByCourseNo(userId, courseNoIds.toJavaList(Integer.class));
  197. List<CourseNo> courseNoList = courseNoService.select(courseNoIds.toJavaList(Integer.class));
  198. Map courseNoMap = Transform.getMap(courseNoList, CourseNo.class, "id");
  199. JSONObject content = new JSONObject();
  200. JSONArray list = new JSONArray();
  201. for(UserNoteCourse note : noteList){
  202. UserNoteCourseRelation relation = Transform.convert(note, UserNoteCourseRelation.class);
  203. relation.setCourseNo((CourseNo)courseNoMap.get(note.getCourseNoId()));
  204. list.add(Transform.convert(relation, ExportCourseNote.class));
  205. }
  206. content.put("list", list);
  207. int count = userExportService.countByUser(userId, ExportType.NOTE_COURSE);
  208. UserExport export = UserExport.builder()
  209. .userId(userId)
  210. .type(ExportType.NOTE_COURSE.key)
  211. .setting(setting)
  212. .content(content)
  213. .no(count + 1)
  214. .build();
  215. return userExportService.add(export);
  216. }
  217. private List<UserQuestionRelation> listByQuestionNoIds(Integer userId, Collection questionNoIds, boolean needAnswer){
  218. List<QuestionNo> questionNoList = questionNoService.select(questionNoIds);
  219. Collection questionIds = Transform.getIds(questionNoList, QuestionNo.class, "questionId");
  220. List<UserQuestion> userQuestionList = null;
  221. if (needAnswer){
  222. // 查询所有id的最后一次做题记录
  223. userQuestionList = userQuestionService.listWithLast(userId, questionNoIds);
  224. }else{
  225. userQuestionList = new ArrayList<>();
  226. for(Object questionNoId : questionNoIds){
  227. userQuestionList.add(UserQuestion.builder()
  228. .questionNoId((Integer)questionNoId)
  229. .build());
  230. }
  231. }
  232. List<UserQuestionRelation> relationList = Transform.convert(userQuestionList, UserQuestionRelation.class);
  233. Transform.combine(relationList, questionNoList, UserQuestionRelation.class, "questionNoId", "questionNo", QuestionNo.class, "id");
  234. // Map questionNoMap = Transform.getMap(questionNoList, QuestionNo.class, "id", "questionId");
  235. // Transform.combine(relationList, questionNoMap, UserQuestionRelation.class, "questionNoId", "questionId");
  236. List<Question> questionList = questionService.select(questionIds);
  237. for(Question question : questionList){
  238. if (QuestionType.ValueOf(question.getQuestionType()) == QuestionType.IR){
  239. throw new ParameterException("“综合推理IR”暂时无法导出。");
  240. }
  241. }
  242. Transform.combine(relationList, questionList, UserQuestionRelation.class, "questionId", "question", Question.class, "id");
  243. // 题源联想
  244. Map<Integer, Integer[]> questionIdsMap = new HashMap<>();
  245. for(Question q : questionList){
  246. questionIdsMap.put(q.getId(), q.getAssociationContent());
  247. }
  248. Map questionMap = questionService.groupByMap(questionIdsMap);
  249. Transform.combine(relationList, questionMap, UserQuestionRelation.class, "questionId", "associations");
  250. // 相关问答
  251. Map<Object, Collection<UserAskQuestion>> userAskMap = userAskQuestionService.groupByQuestionNo(questionIds);
  252. Transform.combine(relationList, userAskMap, UserQuestionRelation.class, "questionId", "asks");
  253. // 笔记
  254. List<UserNoteQuestion> userNoteQuestionList = userNoteQuestionService.listByUserAndQuestions(userId, questionIds);
  255. Transform.combine(relationList, userNoteQuestionList, UserQuestionRelation.class, "questionId", "note", UserNoteQuestion.class, "questionId");
  256. return relationList;
  257. }
  258. }