CoursePackageService.java 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  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.module.CourseModule;
  8. import com.qxgmat.data.constants.enums.status.DirectionStatus;
  9. import com.qxgmat.data.dao.CourseNoMapper;
  10. import com.qxgmat.data.dao.CoursePackageMapper;
  11. import com.qxgmat.data.dao.entity.Course;
  12. import com.qxgmat.data.dao.entity.CourseNo;
  13. import com.qxgmat.data.dao.entity.CoursePackage;
  14. import com.qxgmat.data.relation.CoursePackageRelationMapper;
  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.Arrays;
  20. import java.util.Collection;
  21. import java.util.List;
  22. import java.util.stream.Collectors;
  23. @Service
  24. public class CoursePackageService extends AbstractService {
  25. private static final Logger logger = LoggerFactory.getLogger(CoursePackageService.class);
  26. @Resource
  27. private CoursePackageMapper coursePackageMapper;
  28. @Resource
  29. private CoursePackageRelationMapper coursePackageRelationMapper;
  30. public Page<CoursePackage> listAdmin(int page, int size,String keyword, String order, DirectionStatus direction){
  31. Example example = new Example(CoursePackage.class);
  32. if(keyword != null){
  33. example.and(
  34. example.createCriteria()
  35. .andLike("title", "%"+keyword+"%")
  36. );
  37. }
  38. if(order == null || order.isEmpty()) order = "id";
  39. switch(direction){
  40. case ASC:
  41. example.orderBy(order).asc();
  42. break;
  43. case DESC:
  44. default:
  45. example.orderBy(order).desc();
  46. }
  47. return select(coursePackageMapper, example, page, size);
  48. }
  49. public Page<CoursePackage> list(int page, int size, Integer structId, Boolean isSpecial, String order, DirectionStatus direction){
  50. Example example = new Example(CoursePackage.class);
  51. if(structId != null){
  52. example.and(
  53. example.createCriteria()
  54. .orEqualTo("structId", structId)
  55. );
  56. }
  57. if (isSpecial != null){
  58. example.and(
  59. example.createCriteria()
  60. .orEqualTo("isSpecial", isSpecial ? 1: 0)
  61. );
  62. }
  63. if(order == null || order.isEmpty()) order = "id";
  64. if (direction != null){
  65. switch(direction){
  66. case ASC:
  67. example.orderBy(order).asc();
  68. break;
  69. case DESC:
  70. default:
  71. example.orderBy(order).desc();
  72. }
  73. } else {
  74. example.orderBy(order).desc();
  75. }
  76. return select(coursePackageMapper, example, page, size);
  77. }
  78. /**
  79. * 查找符合课程列表的套餐
  80. * @param ids
  81. * @return
  82. */
  83. public CoursePackage combineCourse(Collection ids){
  84. if (ids==null || ids.size() == 0) return null;
  85. List<CoursePackage> all = select(coursePackageMapper);
  86. for(CoursePackage p : all){
  87. if(ids.containsAll(Arrays.stream(p.getCourseIds()).collect(Collectors.toList()))){
  88. return p;
  89. }
  90. }
  91. return null;
  92. }
  93. /**
  94. * 累加购买记录
  95. * @param sale
  96. */
  97. public void accumulation(Integer packageId, int sale){
  98. coursePackageRelationMapper.accumulation(packageId, sale);
  99. }
  100. public CoursePackage add(CoursePackage coursePackage){
  101. int result = insert(coursePackageMapper, coursePackage);
  102. coursePackage = one(coursePackageMapper, coursePackage.getId());
  103. if(coursePackage == null){
  104. throw new SystemException("套餐添加失败");
  105. }
  106. return coursePackage;
  107. }
  108. public CoursePackage edit(CoursePackage coursePackage){
  109. CoursePackage in = one(coursePackageMapper, coursePackage.getId());
  110. if(in == null){
  111. throw new ParameterException("套餐不存在");
  112. }
  113. int result = update(coursePackageMapper, coursePackage);
  114. return coursePackage;
  115. }
  116. public boolean delete(Number id){
  117. CoursePackage in = one(coursePackageMapper, id);
  118. if(in == null){
  119. throw new ParameterException("套餐不存在");
  120. }
  121. int result = delete(coursePackageMapper, id);
  122. return result > 0;
  123. }
  124. public CoursePackage get(Number id){
  125. CoursePackage in = one(coursePackageMapper, id);
  126. if(in == null){
  127. throw new ParameterException("套餐不存在");
  128. }
  129. return in;
  130. }
  131. public Page<CoursePackage> select(int page, int pageSize){
  132. return select(coursePackageMapper, page, pageSize);
  133. }
  134. public Page<CoursePackage> select(Integer[] ids){
  135. return page(()->select(coursePackageMapper, ids), 1, ids.length);
  136. }
  137. public List<CoursePackage> select(Collection ids){
  138. return select(coursePackageMapper, ids);
  139. }
  140. }