MessageAspect.java 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. package nju.seec.helper.aspect.message;
  2. import com.google.common.collect.ImmutableMap;
  3. import nju.seec.helper.dao.*;
  4. import nju.seec.helper.dto.reply.ReplyDTO;
  5. import nju.seec.helper.entity.Comment;
  6. import nju.seec.helper.entity.Course;
  7. import nju.seec.helper.entity.Quiz;
  8. import nju.seec.helper.entity.Slide;
  9. import nju.seec.helper.enums.MessageType;
  10. import nju.seec.helper.enums.QuizState;
  11. import nju.seec.helper.enums.SlideState;
  12. import nju.seec.helper.service.MessageService;
  13. import nju.seec.helper.vo.CommentVO;
  14. import nju.seec.helper.vo.NoticeVO;
  15. import nju.seec.helper.vo.quiz.QuizVO;
  16. import org.aspectj.lang.JoinPoint;
  17. import org.aspectj.lang.annotation.AfterReturning;
  18. import org.aspectj.lang.annotation.Aspect;
  19. import org.springframework.stereotype.Component;
  20. import org.springframework.transaction.annotation.Transactional;
  21. import java.util.Collections;
  22. import java.util.List;
  23. import java.util.Map;
  24. import java.util.Set;
  25. import java.util.stream.Stream;
  26. /**
  27. * @author cst
  28. */
  29. @Component
  30. @Aspect
  31. public class MessageAspect {
  32. private final Map<MessageType, MessageCreator> messageCreatorMap;
  33. public MessageAspect(CourseDAO courseDAO, SlideDAO slideDAO, QuizDAO quizDAO, CommentDAO commentDAO, ChooseDAO chooseDAO, MessageService messageService) {
  34. messageCreatorMap = ImmutableMap.<MessageType, MessageCreator>builder()
  35. .put(MessageType.SLIDE_IN_CLASS, ((joinPoint, object) ->
  36. Stream.of(joinPoint.getArgs())
  37. .filter(o -> o instanceof Long)
  38. .map(o -> (Long) o)
  39. .findFirst()
  40. .ifPresent(slideId -> {
  41. Slide slide = slideDAO.findSlideById(slideId);
  42. if (slide.getState() == SlideState.IN_CLASS) {
  43. Set<Long> studentIds = chooseDAO.findStudentIdsByCourseId(slide.getCourse().getId());
  44. messageService.createMessage(studentIds, MessageType.SLIDE_IN_CLASS, String.format("[%s] - [%s] 已开课", slide.getCourse().getName(), slide.getName()), slide.getId());
  45. }
  46. }))
  47. )
  48. .put(MessageType.COMMENT_NEW, (joinPoint, object) -> {
  49. if (object instanceof CommentVO) {
  50. CommentVO commentVO = (CommentVO) object;
  51. if (commentVO.getShow()) {
  52. Slide slide = slideDAO.findSlideById(commentVO.getSlideId());
  53. messageService.createMessage(Collections.singleton(slide.getTeacher().getId()), MessageType.COMMENT_NEW, String.format("[%s] - [%s] 在第%d页有新评论", slide.getCourse().getName(), slide.getName(), commentVO.getPageNumber()), slide.getId());
  54. }
  55. }
  56. })
  57. .put(MessageType.COMMENT_REPLY, (joinPoint, object) ->
  58. Stream.of(joinPoint.getArgs())
  59. .filter(o -> o instanceof ReplyDTO)
  60. .map(o -> (ReplyDTO) o)
  61. .findFirst()
  62. .ifPresent(replyDTO -> {
  63. Comment comment = commentDAO.findCommentById(replyDTO.getCommentId());
  64. Slide slide = slideDAO.findSlideById(comment.getSlideId());
  65. messageService.createMessage(Collections.singleton(comment.getUser().getId()), MessageType.COMMENT_REPLY, String.format("您在 [%s] - [%s] 第%d页发表的讨论帖已收到回复", slide.getCourse().getName(), slide.getName(), comment.getPageNumber()), slide.getId());
  66. })
  67. )
  68. .put(MessageType.NOTICE_NEW, (joinPoint, object) -> {
  69. if (object instanceof NoticeVO) {
  70. NoticeVO noticeVO = (NoticeVO) object;
  71. Set<Long> studentIds = chooseDAO.findStudentIdsByCourseId(noticeVO.getCourseId());
  72. Course course = courseDAO.findCourseById(noticeVO.getCourseId());
  73. messageService.createMessage(studentIds, MessageType.NOTICE_NEW, String.format("[%s] 发布了新公告", course.getName()), course.getId());
  74. }
  75. })
  76. .put(MessageType.QUIZ_NEW, new MessageCreator() {
  77. @Override
  78. public void createMessage(JoinPoint joinPoint, Object object) {
  79. if (object instanceof QuizVO) {
  80. sendMessages(quizDAO.findQuizById(((QuizVO) object).getId()));
  81. } else {
  82. Stream.of(joinPoint.getArgs())
  83. .filter(o -> o instanceof Slide)
  84. .map(o -> (Slide) o)
  85. .findFirst()
  86. .ifPresent(slide -> {
  87. List<Quiz> quizzes = quizDAO.findBySlide(slide);
  88. quizzes.forEach(this::sendMessages);
  89. })
  90. ;
  91. }
  92. }
  93. private void sendMessages(Quiz quiz) {
  94. if (quiz.getState() == QuizState.ONGOING) {
  95. Slide slide = quiz.getSlide();
  96. Course course = slide.getCourse();
  97. Set<Long> chooseStudentIds = chooseDAO.findStudentIdsByCourseId(course.getId());
  98. messageService.createMessage(chooseStudentIds, MessageType.QUIZ_NEW, String.format("[%s] - [%s] 的测试 [%s] 已开放", course.getName(), slide.getName(), quiz.getName()), quiz.getId());
  99. }
  100. }
  101. })
  102. .build();
  103. }
  104. @Transactional(rollbackFor = Exception.class)
  105. @AfterReturning(value = "@annotation(message)", returning = "object")
  106. public void createMessage(JoinPoint joinPoint, Message message, Object object) {
  107. messageCreatorMap.getOrDefault(message.value(), doNothing).createMessage(joinPoint, object);
  108. }
  109. @FunctionalInterface
  110. interface MessageCreator {
  111. /**
  112. * 创建消息
  113. *
  114. * @param joinPoint
  115. * @param object
  116. */
  117. void createMessage(JoinPoint joinPoint, Object object);
  118. }
  119. private final MessageCreator doNothing = ((joinPoint, object) -> {
  120. });
  121. }