UserNoteCourseService.java 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194
  1. package com.qxgmat.service;
  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.status.DirectionStatus;
  8. import com.qxgmat.data.dao.UserNoteCourseMapper;
  9. import com.qxgmat.data.dao.entity.UserNoteCourse;
  10. import org.slf4j.Logger;
  11. import org.slf4j.LoggerFactory;
  12. import org.springframework.stereotype.Service;
  13. import org.springframework.transaction.annotation.Transactional;
  14. import javax.annotation.Resource;
  15. import java.util.*;
  16. @Service
  17. public class UserNoteCourseService extends AbstractService {
  18. private static final Logger logger = LoggerFactory.getLogger(UserNoteCourseService.class);
  19. @Resource
  20. private UserNoteCourseMapper userNoteCourseMapper;
  21. public Page<UserNoteCourse> listByCourse(int page, int size, String keyword, Integer userId, Integer courseId, String order, DirectionStatus direction){
  22. Example example = new Example(UserNoteCourse.class);
  23. example.and(
  24. example.createCriteria()
  25. .andEqualTo("userId", userId)
  26. .andEqualTo("courseId", courseId)
  27. );
  28. if (keyword != null)
  29. example.and(
  30. example.createCriteria()
  31. .orLike("content", "%"+keyword+"%")
  32. );
  33. if(order == null || order.isEmpty()) order = "id";
  34. switch(direction){
  35. case ASC:
  36. example.orderBy(order).asc();
  37. break;
  38. case DESC:
  39. default:
  40. example.orderBy(order).desc();
  41. }
  42. return select(userNoteCourseMapper, example, page, size);
  43. }
  44. public List<UserNoteCourse> listByCourse(Number courseId){
  45. Example example = new Example(UserNoteCourse.class);
  46. example.and(
  47. example.createCriteria()
  48. .andEqualTo("courseId", courseId)
  49. );
  50. example.orderBy("id").asc();
  51. return select(userNoteCourseMapper, example);
  52. }
  53. /**
  54. * 更新用户笔记:没有则添加
  55. * @param note
  56. * @return
  57. */
  58. @Transactional
  59. public UserNoteCourse update(UserNoteCourse note){
  60. Example example = new Example(UserNoteCourse.class);
  61. example.and(
  62. example.createCriteria()
  63. .andEqualTo("userId", note.getUserId())
  64. .andEqualTo("courseId", note.getCourseId())
  65. .andEqualTo("courseNoId", note.getCourseNoId())
  66. );
  67. UserNoteCourse in = one(userNoteCourseMapper, example);
  68. Date now = new Date();
  69. if(in == null){
  70. // 按实际更新更改更新时间
  71. return add(note);
  72. }else{
  73. note.setId(in.getId());
  74. return edit(note);
  75. }
  76. }
  77. public List<UserNoteCourse> getByCourse(Integer userId, Integer courseId){
  78. Example example = new Example(UserNoteCourse.class);
  79. example.and(
  80. example.createCriteria()
  81. .andEqualTo("userId", userId)
  82. .andEqualTo("courseId", courseId)
  83. );
  84. return select(userNoteCourseMapper, example);
  85. }
  86. /**
  87. * 获取课程记录分组列表
  88. * @param courseIds
  89. * @return
  90. */
  91. public Map<Object, Collection<UserNoteCourse>> groupByCourse(Integer userId, Collection courseIds){
  92. Map<Object, Collection<UserNoteCourse>> relationMap = new HashMap<>();
  93. if(courseIds == null || courseIds.size() == 0) return relationMap;
  94. Example example = new Example(UserNoteCourse.class);
  95. example.and(
  96. example.createCriteria()
  97. .andEqualTo("userId", userId)
  98. .andIn("courseId", courseIds)
  99. );
  100. List<UserNoteCourse> nos = select(userNoteCourseMapper, example);
  101. Collection<UserNoteCourse> list;
  102. for(UserNoteCourse no : nos){
  103. if (!relationMap.containsKey(no.getCourseId())){
  104. list = new ArrayList<>();
  105. relationMap.put(no.getCourseId(), list);
  106. }else{
  107. list = relationMap.get(no.getCourseId());
  108. }
  109. list.add(no);
  110. }
  111. return relationMap;
  112. }
  113. /**
  114. * 删除笔记
  115. * @param userId
  116. * @param courseNoId
  117. * @return
  118. */
  119. @Transactional
  120. public boolean deleteNote(Integer userId, Integer courseNoId){
  121. Example example = new Example(UserNoteCourse.class);
  122. example.and(
  123. example.createCriteria()
  124. .andEqualTo("userId", userId)
  125. .andEqualTo("courseNoId", courseNoId)
  126. );
  127. UserNoteCourse in = one(userNoteCourseMapper, example);
  128. if (in == null){
  129. return true;
  130. }
  131. return delete(in.getId());
  132. }
  133. public UserNoteCourse add(UserNoteCourse message){
  134. int result = insert(userNoteCourseMapper, message);
  135. message = one(userNoteCourseMapper, message.getId());
  136. if(message == null){
  137. throw new SystemException("笔记添加失败");
  138. }
  139. return message;
  140. }
  141. public UserNoteCourse edit(UserNoteCourse message){
  142. UserNoteCourse in = one(userNoteCourseMapper, message.getId());
  143. if(in == null){
  144. throw new ParameterException("笔记不存在");
  145. }
  146. int result = update(userNoteCourseMapper, message);
  147. return message;
  148. }
  149. public boolean delete(Number id){
  150. UserNoteCourse in = one(userNoteCourseMapper, id);
  151. if(in == null){
  152. throw new ParameterException("笔记不存在");
  153. }
  154. int result = delete(userNoteCourseMapper, id);
  155. return result > 0;
  156. }
  157. public UserNoteCourse get(Number id){
  158. UserNoteCourse in = one(userNoteCourseMapper, id);
  159. if(in == null){
  160. throw new ParameterException("笔记不存在");
  161. }
  162. return in;
  163. }
  164. public Page<UserNoteCourse> select(int page, int pageSize){
  165. return select(userNoteCourseMapper, page, pageSize);
  166. }
  167. public Page<UserNoteCourse> select(Integer[] ids){
  168. return page(()->select(userNoteCourseMapper, ids), 1, ids.length);
  169. }
  170. public List<UserNoteCourse> select(Collection ids){
  171. return select(userNoteCourseMapper, ids);
  172. }
  173. }