TextbookPaperService.java 5.5 KB

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