CourseService.java 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208
  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.module.CourseModule;
  9. import com.qxgmat.data.constants.enums.status.DirectionStatus;
  10. import com.qxgmat.data.dao.CourseMapper;
  11. import com.qxgmat.data.dao.entity.Course;
  12. import com.qxgmat.data.relation.CourseRelationMapper;
  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. import java.util.stream.Collectors;
  19. @Service
  20. public class CourseService extends AbstractService {
  21. private static final Logger logger = LoggerFactory.getLogger(CourseService.class);
  22. @Resource
  23. private CourseMapper courseMapper;
  24. @Resource
  25. private CourseRelationMapper courseRelationMapper;
  26. public Page<Course> listAdmin(int page, int size, String keyword, CourseModule module, Integer structId, Boolean excludeVs, Boolean excludeOnline, String order, DirectionStatus direction){
  27. Example example = new Example(Course.class);
  28. if (keyword != null) {
  29. example.and(
  30. example.createCriteria()
  31. .andLike("title", "%"+keyword+"%")
  32. );
  33. }
  34. if(module != null){
  35. example.and(
  36. example.createCriteria()
  37. .andEqualTo("courseModule", module.key)
  38. );
  39. }
  40. if(structId != null){
  41. example.and(
  42. example.createCriteria()
  43. .orEqualTo("structId", structId)
  44. .orEqualTo("parentStructId", structId)
  45. );
  46. }
  47. if (excludeVs != null) {
  48. example.and(
  49. example.createCriteria()
  50. .andNotEqualTo("courseModule", CourseModule.VS.key)
  51. );
  52. }
  53. if (excludeVs != null) {
  54. example.and(
  55. example.createCriteria()
  56. .andNotEqualTo("courseModule", CourseModule.ONLINE.key)
  57. );
  58. }
  59. if(order == null || order.isEmpty()) order = "id";
  60. switch(direction){
  61. case ASC:
  62. example.orderBy(order).asc();
  63. break;
  64. case DESC:
  65. default:
  66. example.orderBy(order).desc();
  67. }
  68. return select(courseMapper, example, page, size);
  69. }
  70. public Page<Course> list(int page, int size, CourseModule module, Integer structId, String order, DirectionStatus direction){
  71. Example example = new Example(Course.class);
  72. if(module != null){
  73. example.and(
  74. example.createCriteria()
  75. .andEqualTo("courseModule", module.key)
  76. );
  77. }
  78. if(structId != null){
  79. example.and(
  80. example.createCriteria()
  81. .orEqualTo("structId", structId)
  82. .orEqualTo("parentStructId", structId)
  83. );
  84. }
  85. if(order == null || order.isEmpty()) order = "id";
  86. if (direction != null){
  87. switch(direction){
  88. case ASC:
  89. example.orderBy(order).asc();
  90. break;
  91. case DESC:
  92. default:
  93. example.orderBy(order).desc();
  94. }
  95. } else {
  96. example.orderBy(order).desc();
  97. }
  98. return select(courseMapper, example, page, size);
  99. }
  100. public Map<Integer, List<Course>> groupByMap(Map<Integer, Integer[]> courseIdsMap){
  101. Map<Integer, List<Course>> relationMap = new HashMap<>();
  102. List<Integer> ids = new ArrayList<>();
  103. for(Integer[] courseIds : courseIdsMap.values()){
  104. ids.addAll(Arrays.stream(courseIds).collect(Collectors.toList()));
  105. }
  106. List<Course> courseList = select(ids);
  107. Map courseMap = Transform.getMap(courseList, Course.class, "id");
  108. for(Integer k: courseIdsMap.keySet()){
  109. List<Course> l = new ArrayList<>();
  110. for (Integer courseId : courseIdsMap.get(k)){
  111. l.add((Course)courseMap.get(courseId));
  112. }
  113. relationMap.put(k, l);
  114. }
  115. return relationMap;
  116. }
  117. public List<Course> all(CourseModule module){
  118. Example example = new Example(Course.class);
  119. example.and(
  120. example.createCriteria()
  121. .andEqualTo("courseModule", module.key)
  122. );
  123. return select(courseMapper, example);
  124. }
  125. public List<Course> listByExtend(String questionType, String questionSubject, String videoType){
  126. Example example = new Example(Course.class);
  127. example.and(
  128. example.createCriteria()
  129. .orEqualTo("extend", questionType)
  130. .orEqualTo("extend", questionSubject)
  131. );
  132. if (videoType != null){
  133. example.and(
  134. example.createCriteria()
  135. .andEqualTo("videoType", videoType)
  136. );
  137. }
  138. return select(courseMapper, example);
  139. }
  140. /**
  141. * 累加购买记录,访问记录
  142. * @param sale
  143. */
  144. public void accumulation(Integer courseId, int trail, int sale, int packageSale){
  145. courseRelationMapper.accumulation(courseId, trail, sale, packageSale);
  146. }
  147. public Course add(Course course){
  148. int result = insert(courseMapper, course);
  149. course = one(courseMapper, course.getId());
  150. if(course == null){
  151. throw new SystemException("课程添加失败");
  152. }
  153. return course;
  154. }
  155. public Course edit(Course course){
  156. Course in = one(courseMapper, course.getId());
  157. if(in == null){
  158. throw new ParameterException("课程不存在");
  159. }
  160. int result = update(courseMapper, course);
  161. return course;
  162. }
  163. public boolean delete(Number id){
  164. Course in = one(courseMapper, id);
  165. if(in == null){
  166. throw new ParameterException("课程不存在");
  167. }
  168. int result = delete(courseMapper, id);
  169. return result > 0;
  170. }
  171. public Course get(Number id){
  172. Course in = one(courseMapper, id);
  173. if(in == null){
  174. throw new ParameterException("课程不存在");
  175. }
  176. return in;
  177. }
  178. public Page<Course> select(int page, int pageSize){
  179. return select(courseMapper, page, pageSize);
  180. }
  181. public Page<Course> select(Integer[] ids){
  182. return page(()->select(courseMapper, ids), 1, ids.length);
  183. }
  184. public List<Course> select(Collection ids){
  185. return select(courseMapper, ids);
  186. }
  187. }