UserNoteCourseService.java 6.0 KB

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