TextbookPaperService.java 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  1. package com.qxgmat.service.inline;
  2. import com.github.pagehelper.Page;
  3. import com.nuliji.tools.AbstractService;
  4. import com.nuliji.tools.PageResult;
  5. import com.nuliji.tools.Transform;
  6. import com.nuliji.tools.exception.ParameterException;
  7. import com.nuliji.tools.exception.SystemException;
  8. import com.nuliji.tools.mybatis.Example;
  9. import com.qxgmat.data.constants.enums.logic.TextbookLogic;
  10. import com.qxgmat.data.dao.TextbookPaperMapper;
  11. import com.qxgmat.data.dao.entity.*;
  12. import com.qxgmat.data.relation.TextbookPaperRelationMapper;
  13. import org.slf4j.Logger;
  14. import org.slf4j.LoggerFactory;
  15. import org.springframework.stereotype.Service;
  16. import javax.annotation.Resource;
  17. import java.util.*;
  18. @Service
  19. public class TextbookPaperService extends AbstractService {
  20. private static final Logger logger = LoggerFactory.getLogger(TextbookPaperService.class);
  21. @Resource
  22. private TextbookPaperMapper textbookPaperMapper;
  23. @Resource
  24. private TextbookPaperRelationMapper textbookPaperRelationMapper;
  25. /**
  26. * 查找组卷
  27. * @param page
  28. * @param size
  29. * @param libraryId
  30. * @param userId
  31. * @param logic
  32. * @param times
  33. * @return
  34. */
  35. public Page<TextbookPaper> list(int page, int size, Number libraryId, Number userId, TextbookLogic logic, String year, Integer times){
  36. String logicKey = logic != null ? logic.key : "";
  37. Page<TextbookPaper> p = page(()->{
  38. textbookPaperRelationMapper.listWithUser(libraryId, userId, logicKey, year, times);
  39. },page, size);
  40. Collection ids = Transform.getIds(p, TextbookPaper.class, "id");
  41. // 获取详细数据
  42. List<TextbookPaper> list = select(ids);
  43. Transform.replace(p, list, TextbookPaper.class, "id");
  44. return p;
  45. }
  46. /**
  47. * 更新所有组卷title
  48. * @param libraryId
  49. * @param prefixTitle
  50. */
  51. public void updateTitle(Integer libraryId, String prefixTitle, Integer length){
  52. Example example = new Example(TextbookLibrary.class);
  53. example.and(
  54. example.createCriteria()
  55. .andEqualTo("libraryId", libraryId)
  56. );
  57. List<TextbookPaper> paperList = select(textbookPaperMapper, example);
  58. for(TextbookPaper paper : paperList){
  59. paper.setTitle(generateTitle(prefixTitle, length, paper.getNo(), paper.getQuestionNumber()));
  60. edit(paper);
  61. }
  62. }
  63. /**
  64. * 生成试卷名称
  65. * @param prefixTitle
  66. * @param no
  67. * @param questionNumber
  68. * @return
  69. */
  70. public String generateTitle(String prefixTitle, Integer length, Integer no, Integer questionNumber){
  71. return String.format("%s#%d~%d", prefixTitle, (no - 1) * length + 1, (no - 1) * length + questionNumber);
  72. }
  73. /**
  74. * 管理后台查询列表
  75. * @param page
  76. * @param keyword
  77. * @return
  78. */
  79. public Page<TextbookPaper> listAdmin(int page, int size, String keyword){
  80. Example example = new Example(TextbookLibrary.class);
  81. if(keyword != null)
  82. example.and(
  83. example.createCriteria()
  84. .orLike("title", "%"+keyword+"%")
  85. );
  86. example.orderBy("id").desc();
  87. return page(()->select(textbookPaperMapper, example), page, size);
  88. }
  89. public List<TextbookPaper> listByQuestion(TextbookQuestion question){
  90. Example example = new Example(SentencePaper.class);
  91. example.and(
  92. example.createCriteria()
  93. .andCondition(String.format(formatSet, question.getId(), "question_no_ids"))
  94. );
  95. return select(textbookPaperMapper, example);
  96. }
  97. public TextbookPaper getLastByLibrary(TextbookLogic logic, Integer libraryId){
  98. Example example = new Example(TextbookPaper.class);
  99. example.and(
  100. example.createCriteria()
  101. .andEqualTo("logic", logic.key)
  102. .andEqualTo("libraryId", libraryId)
  103. );
  104. example.orderBy("createTime").desc();
  105. return one(textbookPaperMapper, example);
  106. }
  107. public TextbookPaper add(TextbookPaper paper){
  108. int result = insert(textbookPaperMapper, paper);
  109. paper = one(textbookPaperMapper, paper.getId());
  110. if(paper == null){
  111. throw new SystemException("题目添加失败");
  112. }
  113. return paper;
  114. }
  115. public TextbookPaper edit(TextbookPaper paper){
  116. TextbookPaper in = one(textbookPaperMapper, paper.getId());
  117. if(in == null){
  118. throw new ParameterException("题目不存在");
  119. }
  120. int result = update(textbookPaperMapper, paper);
  121. return paper;
  122. }
  123. public boolean delete(Number id){
  124. TextbookPaper in = one(textbookPaperMapper, id);
  125. if(in == null){
  126. throw new ParameterException("题目不存在");
  127. }
  128. int result = delete(textbookPaperMapper, id);
  129. return result > 0;
  130. }
  131. public TextbookPaper get(Number id){
  132. TextbookPaper in = one(textbookPaperMapper, id);
  133. if(in == null){
  134. throw new ParameterException("题目不存在");
  135. }
  136. return in;
  137. }
  138. public Page<TextbookPaper> select(int page, int pageSize){
  139. return select(textbookPaperMapper, page, pageSize);
  140. }
  141. public Page<TextbookPaper> select(int page, int pageSize, String keyword){
  142. Example example = new Example(TextbookPaper.class);
  143. example.and(
  144. example.createCriteria()
  145. .andEqualTo("status", 1)
  146. );
  147. if (keyword != null)
  148. example.and(
  149. example.createCriteria()
  150. .andLike("title", "%"+keyword+"%")
  151. );
  152. return select(textbookPaperMapper, example, page, pageSize);
  153. }
  154. public Page<TextbookPaper> select(Integer[] ids){
  155. return page(()-> select(textbookPaperMapper, ids), 1, ids.length);
  156. }
  157. public List<TextbookPaper> select(Collection ids){
  158. return select(textbookPaperMapper, ids);
  159. }
  160. }