UserNoteCourseService.java 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  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.dao.UserNoteCourseMapper;
  8. import com.qxgmat.data.dao.entity.UserNoteCourse;
  9. import org.slf4j.Logger;
  10. import org.slf4j.LoggerFactory;
  11. import org.springframework.stereotype.Service;
  12. import org.springframework.transaction.annotation.Transactional;
  13. import javax.annotation.Resource;
  14. import java.util.*;
  15. @Service
  16. public class UserNoteCourseService extends AbstractService {
  17. private static final Logger logger = LoggerFactory.getLogger(UserNoteCourseService.class);
  18. @Resource
  19. private UserNoteCourseMapper userNoteCourseMapper;
  20. /**
  21. * 更新用户笔记:没有则添加
  22. * @param note
  23. * @return
  24. */
  25. @Transactional
  26. public UserNoteCourse update(UserNoteCourse note){
  27. Example example = new Example(UserNoteCourse.class);
  28. example.and(
  29. example.createCriteria()
  30. .andEqualTo("userId", note.getUserId())
  31. .andEqualTo("courseId", note.getCourseId())
  32. .andEqualTo("courseNoId", note.getCourseNoId())
  33. );
  34. UserNoteCourse in = one(userNoteCourseMapper, example);
  35. Date now = new Date();
  36. if(in == null){
  37. // 按实际更新更改更新时间
  38. return add(note);
  39. }else{
  40. note.setId(in.getId());
  41. return edit(note);
  42. }
  43. }
  44. /**
  45. * 获取课程记录分组列表
  46. * @param courseIds
  47. * @return
  48. */
  49. public Map<Object, Collection<UserNoteCourse>> groupByCourse(Collection courseIds){
  50. Map<Object, Collection<UserNoteCourse>> relationMap = new HashMap<>();
  51. Example example = new Example(UserNoteCourse.class);
  52. example.and(
  53. example.createCriteria()
  54. .andIn("courseIds", courseIds)
  55. );
  56. List<UserNoteCourse> nos = select(userNoteCourseMapper, example);
  57. Collection<UserNoteCourse> list;
  58. for(UserNoteCourse no : nos){
  59. if (!relationMap.containsKey(no.getCourseId())){
  60. list = new ArrayList<>();
  61. relationMap.put(no.getCourseId(), list);
  62. }else{
  63. list = relationMap.get(no.getCourseId());
  64. }
  65. list.add(no);
  66. }
  67. return relationMap;
  68. }
  69. public UserNoteCourse add(UserNoteCourse message){
  70. int result = insert(userNoteCourseMapper, message);
  71. message = one(userNoteCourseMapper, message.getId());
  72. if(message == null){
  73. throw new SystemException("笔记添加失败");
  74. }
  75. return message;
  76. }
  77. public UserNoteCourse edit(UserNoteCourse message){
  78. UserNoteCourse in = one(userNoteCourseMapper, message.getId());
  79. if(in == null){
  80. throw new ParameterException("笔记不存在");
  81. }
  82. int result = update(userNoteCourseMapper, message);
  83. return message;
  84. }
  85. public boolean delete(Number id){
  86. UserNoteCourse in = one(userNoteCourseMapper, id);
  87. if(in == null){
  88. throw new ParameterException("笔记不存在");
  89. }
  90. int result = delete(userNoteCourseMapper, id);
  91. return result > 0;
  92. }
  93. public UserNoteCourse get(Number id){
  94. UserNoteCourse in = one(userNoteCourseMapper, id);
  95. if(in == null){
  96. throw new ParameterException("笔记不存在");
  97. }
  98. return in;
  99. }
  100. public Page<UserNoteCourse> select(int page, int pageSize){
  101. return select(userNoteCourseMapper, page, pageSize);
  102. }
  103. public Page<UserNoteCourse> select(Integer[] ids){
  104. return page(()->select(userNoteCourseMapper, ids), 1, ids.length);
  105. }
  106. public List<UserNoteCourse> select(Collection ids){
  107. return select(userNoteCourseMapper, ids);
  108. }
  109. }