CourseDataService.java 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193
  1. package com.qxgmat.service.inline;
  2. import com.github.pagehelper.Page;
  3. import com.nuliji.tools.AbstractService;
  4. import com.nuliji.tools.Transform;
  5. import com.nuliji.tools.exception.ParameterException;
  6. import com.nuliji.tools.exception.SystemException;
  7. import com.nuliji.tools.mybatis.Example;
  8. import com.qxgmat.data.constants.enums.status.DirectionStatus;
  9. import com.qxgmat.data.constants.enums.user.DataType;
  10. import com.qxgmat.data.dao.CourseDataMapper;
  11. import com.qxgmat.data.dao.CourseMapper;
  12. import com.qxgmat.data.dao.entity.Course;
  13. import com.qxgmat.data.dao.entity.CourseData;
  14. import com.qxgmat.data.relation.CourseDataRelationMapper;
  15. import org.slf4j.Logger;
  16. import org.slf4j.LoggerFactory;
  17. import org.springframework.stereotype.Service;
  18. import javax.annotation.Resource;
  19. import java.util.Collection;
  20. import java.util.List;
  21. @Service
  22. public class CourseDataService extends AbstractService {
  23. private static final Logger logger = LoggerFactory.getLogger(CourseDataService.class);
  24. @Resource
  25. private CourseDataMapper courseDataMapper;
  26. @Resource
  27. private CourseDataRelationMapper courseDataRelationMapper;
  28. public Page<CourseData> listAdmin(int page, int size,String keyword, Integer structId, DataType dataType, String order, DirectionStatus direction){
  29. Example example = new Example(CourseData.class);
  30. if(keyword != null){
  31. example.and(
  32. example.createCriteria()
  33. .andLike("title", "%"+keyword+"%")
  34. );
  35. }
  36. if(structId != null){
  37. example.and(
  38. example.createCriteria()
  39. .orEqualTo("structId", structId)
  40. .orEqualTo("parentStructId", structId)
  41. );
  42. }
  43. if(dataType != null){
  44. example.and(
  45. example.createCriteria()
  46. .andEqualTo("dataType", dataType.key)
  47. );
  48. }
  49. if(order == null || order.isEmpty()) order = "id";
  50. switch(direction){
  51. case ASC:
  52. example.orderBy(order).asc();
  53. break;
  54. case DESC:
  55. default:
  56. example.orderBy(order).desc();
  57. }
  58. return select(courseDataMapper, example, page, size);
  59. }
  60. public Page<CourseData> list(int page, int size, Integer structId, DataType dataType, Boolean isNovice, Boolean isOriginal, String order, DirectionStatus direction){
  61. Example example = new Example(CourseData.class);
  62. if(structId != null){
  63. example.and(
  64. example.createCriteria()
  65. .orEqualTo("structId", structId)
  66. .orEqualTo("parentStructId", structId)
  67. );
  68. }
  69. if (isNovice != null){
  70. example.and(
  71. example.createCriteria()
  72. .andEqualTo("isNovice", isNovice ? 1 : 0)
  73. );
  74. }
  75. if (isOriginal != null){
  76. example.and(
  77. example.createCriteria()
  78. .andEqualTo("isOriginal", isOriginal ? 1 : 0)
  79. );
  80. }
  81. if(dataType != null){
  82. example.and(
  83. example.createCriteria()
  84. .andEqualTo("dataType", dataType.key)
  85. );
  86. }
  87. if(order == null || order.isEmpty()) order = "id";
  88. switch(direction){
  89. case ASC:
  90. example.orderBy(order).asc();
  91. break;
  92. case DESC:
  93. default:
  94. example.orderBy(order).desc();
  95. }
  96. return select(courseDataMapper, example, page, size);
  97. }
  98. /**
  99. * 获取用户订阅记录
  100. * @param page
  101. * @param size
  102. * @param userId
  103. * @return
  104. */
  105. public Page<CourseData> listByUser(int page, int size, Integer userId, Integer structId, DataType dataType, String order, DirectionStatus direction){
  106. Page<CourseData> p = page(()->{
  107. courseDataRelationMapper.listByUser(userId, structId, dataType != null ? dataType.key : null, order != null ? order : "id", direction != null ? direction.key : "desc");
  108. }, page, size);
  109. Collection ids = Transform.getIds(p, CourseData.class, "id");
  110. // 获取详细数据
  111. List<CourseData> list = select(ids);
  112. Transform.replace(p, list, CourseData.class, "id");
  113. return p;
  114. }
  115. /**
  116. * 累加购买记录,访问记录
  117. * @param sale
  118. */
  119. public void accumulation(Integer dataId, int sale, int view){
  120. courseDataRelationMapper.accumulation(dataId, sale, view);
  121. }
  122. /**
  123. * 获取长难句资料:添加时根据struct=sentence判断设置
  124. * @return
  125. */
  126. public CourseData getSentence(){
  127. Example example = new Example(CourseData.class);
  128. example.and(
  129. example.createCriteria().andEqualTo("isSentence", 1)
  130. );
  131. return one(courseDataMapper, example);
  132. }
  133. public CourseData add(CourseData courseData){
  134. int result = insert(courseDataMapper, courseData);
  135. courseData = one(courseDataMapper, courseData.getId());
  136. if(courseData == null){
  137. throw new SystemException("资料添加失败");
  138. }
  139. return courseData;
  140. }
  141. public CourseData edit(CourseData courseData){
  142. CourseData in = one(courseDataMapper, courseData.getId());
  143. if(in == null){
  144. throw new ParameterException("资料不存在");
  145. }
  146. int result = update(courseDataMapper, courseData);
  147. return courseData;
  148. }
  149. public boolean delete(Number id){
  150. CourseData in = one(courseDataMapper, id);
  151. if(in == null){
  152. throw new ParameterException("资料不存在");
  153. }
  154. int result = delete(courseDataMapper, id);
  155. return result > 0;
  156. }
  157. public CourseData get(Number id){
  158. CourseData in = one(courseDataMapper, id);
  159. if(in == null){
  160. throw new ParameterException("资料不存在");
  161. }
  162. return in;
  163. }
  164. public Page<CourseData> select(int page, int pageSize){
  165. return select(courseDataMapper, page, pageSize);
  166. }
  167. public Page<CourseData> select(Integer[] ids){
  168. return page(()->select(courseDataMapper, ids), 1, ids.length);
  169. }
  170. public List<CourseData> select(Collection ids){
  171. return select(courseDataMapper, ids);
  172. }
  173. }