TextbookTopicService.java 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  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.TextbookSubject;
  8. import com.qxgmat.data.constants.enums.TopicQuality;
  9. import com.qxgmat.data.constants.enums.status.DirectionStatus;
  10. import com.qxgmat.data.dao.TextbookTopicMapper;
  11. import com.qxgmat.data.dao.entity.TextbookTopic;
  12. import org.slf4j.Logger;
  13. import org.slf4j.LoggerFactory;
  14. import org.springframework.stereotype.Service;
  15. import javax.annotation.Resource;
  16. import java.util.Arrays;
  17. import java.util.Collection;
  18. import java.util.List;
  19. import java.util.stream.Collectors;
  20. @Service
  21. public class TextbookTopicService extends AbstractService {
  22. private static final Logger logger = LoggerFactory.getLogger(TextbookTopicService.class);
  23. @Resource
  24. private TextbookTopicMapper textbookTopicMapper;
  25. public Page<TextbookTopic> listAdmin(int page, int pageSize, String textbookSubject, Number libraryId, String keyword, TopicQuality quality, String order, DirectionStatus direction){
  26. Example example = new Example(TextbookTopic.class);
  27. if (textbookSubject!=null){
  28. example.and(
  29. example.createCriteria()
  30. .andEqualTo("textbookSubject", textbookSubject)
  31. );
  32. }
  33. if (libraryId!=null){
  34. example.and(
  35. example.createCriteria()
  36. .andEqualTo("libraryId", libraryId)
  37. );
  38. }
  39. if (keyword!=null){
  40. example.and(
  41. example.createCriteria()
  42. .andLike("keyword", "%"+keyword+"%")
  43. );
  44. }
  45. if (quality!=null){
  46. example.and(
  47. example.createCriteria()
  48. .andEqualTo("quality", quality.key)
  49. );
  50. }
  51. if(order == null || order.isEmpty()) order = "id";
  52. switch(direction){
  53. case ASC:
  54. example.orderBy(order).asc();
  55. break;
  56. case DESC:
  57. default:
  58. example.orderBy(order).desc();
  59. }
  60. return select(textbookTopicMapper, example, page, pageSize);
  61. }
  62. /**
  63. * 获取换库表中的最后一题
  64. * @param libraryId
  65. * @return
  66. */
  67. public TextbookTopic lastByLibrary(Integer libraryId, String questionSubject){
  68. Example example = new Example(TextbookTopic.class);
  69. example.and(
  70. example.createCriteria()
  71. .andEqualTo("libraryId", libraryId)
  72. .andEqualTo("textbookSubject", questionSubject)
  73. );
  74. example.orderBy("no").desc();
  75. return one(textbookTopicMapper, example);
  76. }
  77. public Page<TextbookTopic> list(int page, int size, Integer libraryId, TextbookSubject subject, String keyword, String quality, Boolean isOld, String order, DirectionStatus direction){
  78. Example example = new Example(TextbookTopic.class);
  79. example.and(
  80. example.createCriteria()
  81. .andEqualTo("libraryId", libraryId)
  82. .andEqualTo("textbookSubject", subject.key)
  83. );
  84. if (keyword != null){
  85. example.and(
  86. example.createCriteria()
  87. .andEqualTo("keyword", keyword)
  88. );
  89. }
  90. if (quality != null){
  91. example.and(
  92. example.createCriteria()
  93. .andEqualTo("quality", quality)
  94. );
  95. }
  96. if (isOld != null){
  97. example.and(
  98. example.createCriteria()
  99. .andEqualTo("isOld", isOld ? 1 : 0)
  100. );
  101. }
  102. if(order==null||order.isEmpty()) order = "id";
  103. if (direction != null){
  104. switch(direction){
  105. case ASC:
  106. example.orderBy(order).asc();
  107. break;
  108. case DESC:
  109. default:
  110. example.orderBy(order).desc();
  111. }
  112. } else {
  113. example.orderBy(order).desc();
  114. }
  115. return select(textbookTopicMapper, example, page, size);
  116. }
  117. public TextbookTopic getByNo(Integer libraryId, String subject, Integer no){
  118. Example example = new Example(TextbookTopic.class);
  119. example.and(
  120. example.createCriteria()
  121. .andEqualTo("libraryId", libraryId)
  122. .andEqualTo("textbookSubject", subject)
  123. .andEqualTo("no", no)
  124. );
  125. return one(textbookTopicMapper, example);
  126. }
  127. public TextbookTopic add(TextbookTopic ad){
  128. int result = insert(textbookTopicMapper, ad);
  129. ad = one(textbookTopicMapper, ad.getId());
  130. if(ad == null){
  131. throw new SystemException("记录添加失败");
  132. }
  133. return ad;
  134. }
  135. public TextbookTopic edit(TextbookTopic ad){
  136. TextbookTopic in = one(textbookTopicMapper, ad.getId());
  137. if(in == null){
  138. throw new ParameterException("记录不存在");
  139. }
  140. int result = update(textbookTopicMapper, ad);
  141. return ad;
  142. }
  143. public boolean delete(Number id){
  144. TextbookTopic in = one(textbookTopicMapper, id);
  145. if(in == null){
  146. throw new ParameterException("记录不存在");
  147. }
  148. int result = delete(textbookTopicMapper, id);
  149. return result > 0;
  150. }
  151. public TextbookTopic get(Number id){
  152. TextbookTopic in = one(textbookTopicMapper, id);
  153. if(in == null){
  154. throw new ParameterException("记录不存在");
  155. }
  156. return in;
  157. }
  158. public Page<TextbookTopic> select(int page, int pageSize){
  159. return select(textbookTopicMapper, page, pageSize);
  160. }
  161. public Page<TextbookTopic> select(Integer[] ids){
  162. return page(()->select(textbookTopicMapper, ids), 1, ids.length);
  163. }
  164. public List<TextbookTopic> select(Collection ids){
  165. return select(textbookTopicMapper, ids);
  166. }
  167. }