TextbookTopicService.java 6.1 KB

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