TextbookService.java 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350
  1. package com.qxgmat.service.extend;
  2. import com.nuliji.tools.Tools;
  3. import com.nuliji.tools.Transform;
  4. import com.qxgmat.data.constants.enums.ServiceKey;
  5. import com.qxgmat.data.constants.enums.TextbookSubject;
  6. import com.qxgmat.data.constants.enums.logic.SentenceLogic;
  7. import com.qxgmat.data.constants.enums.logic.TextbookLogic;
  8. import com.qxgmat.data.constants.enums.module.QuestionModule;
  9. import com.qxgmat.data.constants.enums.module.QuestionNoModule;
  10. import com.qxgmat.data.dao.entity.*;
  11. import com.qxgmat.data.relation.entity.TextbookQuestionRelation;
  12. import com.qxgmat.service.UserServiceService;
  13. import com.qxgmat.service.inline.*;
  14. import org.springframework.beans.factory.annotation.Value;
  15. import org.springframework.stereotype.Service;
  16. import org.springframework.transaction.annotation.Transactional;
  17. import javax.annotation.Resource;
  18. import java.text.SimpleDateFormat;
  19. import java.util.*;
  20. import java.util.stream.Collectors;
  21. @Service
  22. public class TextbookService {
  23. SimpleDateFormat stringToDate = new SimpleDateFormat("yyyy-MM-dd");
  24. SimpleDateFormat dateToString = new SimpleDateFormat("MM月dd日");
  25. SimpleDateFormat dateToDay = new SimpleDateFormat("dd日");
  26. @Value("${paper.textbookLength}")
  27. private Integer paperLength;
  28. @Resource
  29. private TextbookPaperService textbookPaperService;
  30. @Resource
  31. private QuestionService questionService;
  32. @Resource
  33. private QuestionNoService questionNoService;
  34. @Resource
  35. private TextbookQuestionService textbookQuestionService;
  36. @Resource
  37. private TextbookLibraryService textbookLibraryService;
  38. @Resource
  39. private UserTextbookEnrollService userTextbookEnrollService;
  40. @Resource
  41. private TextbookTopicService textbookTopicService;
  42. @Resource
  43. private UserServiceService userServiceService;
  44. /**
  45. * 报名
  46. * @param userId
  47. * @param date
  48. */
  49. @Transactional
  50. public void enroll(Integer userId, Date date){
  51. UserTextbookEnroll in = userTextbookEnrollService.getByUser(userId);
  52. if (in != null){
  53. return;
  54. }
  55. userTextbookEnrollService.add(UserTextbookEnroll.builder()
  56. .userId(userId)
  57. .date(date)
  58. .build());
  59. }
  60. /**
  61. * 取消报名
  62. * @param userId
  63. */
  64. @Transactional
  65. public void unEnroll(Integer userId){
  66. UserTextbookEnroll in = userTextbookEnrollService.getByUser(userId);
  67. if (in == null){
  68. return;
  69. }
  70. userTextbookEnrollService.delete(in.getId());
  71. }
  72. /**
  73. * 添加新一期的换库
  74. * @param entity
  75. * @return
  76. */
  77. @Transactional
  78. public TextbookLibrary replace(TextbookLibrary entity){
  79. TextbookLibrary last = textbookLibraryService.getLatest();
  80. if (last != null){
  81. // 将最后一期设定库尾
  82. last.setEndDate(entity.getStartDate());
  83. textbookLibraryService.edit(last);
  84. String prefixTitle = generatePrefixTitle(last);
  85. textbookPaperService.updateTitle(last.getId(), prefixTitle, paperLength);
  86. textbookQuestionService.updateTitle(last.getId(), prefixTitle);
  87. }
  88. return textbookLibraryService.add(entity);
  89. }
  90. /**
  91. * 添加机经题目:加入题库,关联题目,并关联机经paper
  92. * @param relation
  93. * @return
  94. */
  95. @Transactional
  96. public TextbookQuestion addQuestion(TextbookQuestionRelation relation){
  97. Question question = relation.getQuestion();
  98. question.setQuestionModule(QuestionModule.TEXTBOOK.key);
  99. question = questionService.add(question);
  100. QuestionNo questionNo = QuestionNo.builder()
  101. .questionId(question.getId())
  102. .module(QuestionNoModule.TEXTBOOK.key)
  103. .no(relation.getNo())
  104. .title(relation.getTitle())
  105. .build();
  106. questionNo = questionNoService.add(questionNo);
  107. // 绑定关系
  108. relation.setQuestionId(question.getId());
  109. relation.setQuestionNoId(questionNo.getId());
  110. TextbookLibrary library = textbookLibraryService.get(relation.getLibraryId());
  111. String prefixTitle = generatePrefixTitle(library);
  112. relation.setTitle(textbookQuestionService.generateTitle(prefixTitle, relation.getNo()));
  113. // 保存年份
  114. relation.setYear(String.valueOf(Tools.yearNumber(library.getStartDate())));
  115. TextbookQuestion textbookQuestion = textbookQuestionService.add(relation);
  116. // 绑定关系
  117. if (question.getQuestionType().equals(TextbookLogic.DS.key)){
  118. addQuestionToPaper(library, textbookQuestion, TextbookLogic.DS);
  119. addQuestionToPaper(library, textbookQuestion, TextbookLogic.DS_PS);
  120. }else if (question.getQuestionType().equals(TextbookLogic.PS.key)){
  121. addQuestionToPaper(library, textbookQuestion, TextbookLogic.PS);
  122. addQuestionToPaper(library, textbookQuestion, TextbookLogic.DS_PS);
  123. }
  124. textbookQuestionService.edit(textbookQuestion);
  125. return textbookQuestion;
  126. }
  127. /**
  128. * 更新机经题目:更新题库
  129. * @param relation
  130. * @return
  131. */
  132. @Transactional
  133. public TextbookQuestion editQuestion(TextbookQuestionRelation relation){
  134. TextbookQuestion textbookQuestion = textbookQuestionService.get(relation.getId());
  135. Question question = relation.getQuestion();
  136. question = questionService.edit(question);
  137. QuestionNo questionNo = questionNoService.get(textbookQuestion.getQuestionNoId());
  138. if (relation.getTitle() != null) questionNo.setTitle(relation.getTitle());
  139. if (relation.getNo() != null) questionNo.setNo(relation.getNo());
  140. questionNo = questionNoService.edit(questionNo);
  141. textbookQuestion = textbookQuestionService.edit(relation);
  142. return textbookQuestion;
  143. }
  144. /**
  145. * 更新换库表题目数
  146. * @param libraryId
  147. * @param subject
  148. */
  149. public void updateLibraryNo(Integer libraryId, String subject){
  150. TextbookTopic last = textbookTopicService.lastByLibrary(libraryId,subject);
  151. TextbookLibrary library = textbookLibraryService.get(last.getLibraryId());
  152. TextbookLibrary tmp = TextbookLibrary.builder()
  153. .id(library.getId())
  154. .build();
  155. switch(TextbookSubject.ValueOf(subject)){
  156. case QUANT:
  157. if (!last.getNo().equals(library.getQuantNumber())){
  158. tmp.setQuantNumber(last.getNo());
  159. textbookLibraryService.edit(tmp);
  160. }
  161. break;
  162. case IR:
  163. if (!last.getNo().equals(library.getIrNumber())){
  164. tmp.setIrNumber(last.getNo());
  165. textbookLibraryService.edit(tmp);
  166. }
  167. break;
  168. case RC:
  169. if (!last.getNo().equals(library.getRcNumber())){
  170. tmp.setRcNumber(last.getNo());
  171. textbookLibraryService.edit(tmp);
  172. }
  173. break;
  174. }
  175. }
  176. /**
  177. * 根据用户权限更新资源信息
  178. * @param user
  179. * @param textbookLibrary
  180. */
  181. public void refreshLibraryResource(User user, TextbookLibrary textbookLibrary){
  182. // 处理权限
  183. if (user != null){
  184. if (!userServiceService.hasService(user.getId(), ServiceKey.TEXTBOOK)){
  185. // 移除数据
  186. textbookLibrary.setRc("");
  187. textbookLibrary.setIr("");
  188. textbookLibrary.setQuant("");
  189. }else{
  190. // 替换为水印地址
  191. textbookLibrary.setRc("/api/my/download/textbook?subject=rc");
  192. textbookLibrary.setIr("/api/my/download/textbook?subject=ir");
  193. textbookLibrary.setQuant("/api/my/download/textbook?subject=quant");
  194. }
  195. }else{
  196. textbookLibrary.setRc("");
  197. textbookLibrary.setIr("");
  198. textbookLibrary.setQuant("");
  199. }
  200. }
  201. /**
  202. * 根据用户权限更新资源信息
  203. * @param user
  204. * @param textbookLibraryList
  205. */
  206. public void refreshLibraryResource(User user, List<TextbookLibrary> textbookLibraryList){
  207. // 处理权限
  208. if (user != null){
  209. if (!userServiceService.hasService(user.getId(), ServiceKey.TEXTBOOK)) {
  210. for (TextbookLibrary textbookLibrary : textbookLibraryList) {
  211. textbookLibrary.setRc("");
  212. textbookLibrary.setIr("");
  213. textbookLibrary.setQuant("");
  214. }
  215. }else{
  216. for (TextbookLibrary textbookLibrary : textbookLibraryList) {
  217. // 替换为水印地址
  218. textbookLibrary.setRc("/api/my/download/textbook?subject=rc");
  219. textbookLibrary.setIr("/api/my/download/textbook?subject=ir");
  220. textbookLibrary.setQuant("/api/my/download/textbook?subject=quant");
  221. }
  222. }
  223. }else{
  224. for(TextbookLibrary textbookLibrary : textbookLibraryList){
  225. textbookLibrary.setRc("");
  226. textbookLibrary.setIr("");
  227. textbookLibrary.setQuant("");
  228. }
  229. }
  230. }
  231. /**
  232. * 根据用户权限更新资源信息
  233. * @param user
  234. * @param textbookLibraryHistoryList
  235. */
  236. public void refreshHistoryResource(User user, List<TextbookLibraryHistory> textbookLibraryHistoryList){
  237. // 处理权限
  238. if (user != null){
  239. if (!userServiceService.hasService(user.getId(), ServiceKey.TEXTBOOK)) {
  240. for (TextbookLibraryHistory history : textbookLibraryHistoryList) {
  241. history.setRc("");
  242. history.setIr("");
  243. history.setQuant("");
  244. }
  245. }
  246. }else{
  247. for(TextbookLibraryHistory history : textbookLibraryHistoryList){
  248. history.setRc("");
  249. history.setIr("");
  250. history.setQuant("");
  251. }
  252. }
  253. }
  254. private void addQuestionToPaper(TextbookLibrary library, TextbookQuestion question, TextbookLogic logic){
  255. String prefixTitle = generatePrefixTitle(library);
  256. // 获取最后一个paper
  257. TextbookPaper paper = textbookPaperService.getLastByLibrary(logic, question.getLibraryId());
  258. Integer no = 0;
  259. if (paper == null || paper.getQuestionNumber().equals(paperLength)){
  260. if (paper != null){
  261. no = paper.getNo();
  262. }
  263. paper = textbookPaperService.add(TextbookPaper.builder()
  264. .logic(SentenceLogic.NO.key)
  265. .year(question.getYear())
  266. .libraryId(question.getLibraryId())
  267. .title(textbookPaperService.generateTitle(prefixTitle, paperLength, no, 1))
  268. .no(no+1)
  269. .questionNumber(1)
  270. .status(1)
  271. .questionNoIds(new Integer[]{question.getId()}).build()
  272. );
  273. }else{
  274. // 加入
  275. List<Integer> questionNoList = Arrays.stream(paper.getQuestionNoIds()).collect(Collectors.toList());
  276. questionNoList.add(question.getQuestionNoId());
  277. List<Integer> textbookList = Arrays.stream(paper.getQuestionTIds()).collect(Collectors.toList());
  278. textbookList.add(question.getId());
  279. paper.setQuestionNumber(paper.getQuestionNumber() + 1);
  280. paper.setQuestionNoIds(questionNoList.toArray(new Integer[0]));
  281. paper.setQuestionTIds(textbookList.toArray(new Integer[0]));
  282. paper.setTitle(textbookPaperService.generateTitle(prefixTitle, paperLength, paper.getNo(), paper.getQuestionNumber()));
  283. paper = textbookPaperService.edit(paper);
  284. }
  285. }
  286. private void removeQuestionFromPaper(TextbookQuestion question){
  287. List<TextbookPaper> paperList = textbookPaperService.listByQuestion(question);
  288. for(TextbookPaper paper : paperList){
  289. paper.setQuestionNumber(paper.getQuestionNumber() - 1);
  290. List<Integer> questionNoList = Arrays.stream(paper.getQuestionNoIds()).collect(Collectors.toList());
  291. questionNoList.remove(question.getQuestionNoId());
  292. List<Integer> textbookList = Arrays.stream(paper.getQuestionNoIds()).collect(Collectors.toList());
  293. textbookList.remove(question.getId());
  294. paper.setQuestionNoIds(questionNoList.toArray(new Integer[0]));
  295. paper.setQuestionTIds(textbookList.toArray(new Integer[0]));
  296. textbookPaperService.edit(paper);
  297. }
  298. }
  299. private String generatePrefixTitle(TextbookLibrary library){
  300. String startDate = dateToString.format(library.getStartDate());
  301. String endDate;
  302. if (library.getEndDate()==null){
  303. // 至今
  304. endDate = "Now";
  305. }else if(library.getStartDate().equals(library.getEndDate())){
  306. // 同月
  307. endDate = dateToString.format(library.getEndDate());
  308. }else{
  309. endDate = dateToString.format(library.getEndDate());
  310. }
  311. return String.format("%s-%s", startDate, endDate);
  312. }
  313. }