EventAspect.java 3.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. package nju.seec.helper.aspect.event;
  2. import com.google.common.collect.ImmutableMap;
  3. import lombok.SneakyThrows;
  4. import nju.seec.helper.sys.dataanalysis.api.EventApi;
  5. import nju.seec.helper.sys.dataanalysis.constant.EventAction;
  6. import nju.seec.helper.sys.dataanalysis.constant.EventType;
  7. import nju.seec.helper.aspect.auth.LoginUser;
  8. import nju.seec.helper.dao.CourseDAO;
  9. import nju.seec.helper.dao.QuizDAO;
  10. import nju.seec.helper.dto.comment.CommentDTO;
  11. import org.aspectj.lang.JoinPoint;
  12. import org.aspectj.lang.annotation.AfterReturning;
  13. import org.aspectj.lang.annotation.Aspect;
  14. import org.springframework.stereotype.Component;
  15. import org.springframework.transaction.annotation.Transactional;
  16. import java.time.LocalDateTime;
  17. import java.util.Collections;
  18. import java.util.Map;
  19. import java.util.Objects;
  20. import java.util.stream.Stream;
  21. /**
  22. * @author cst
  23. */
  24. @Aspect
  25. @Component
  26. public class EventAspect {
  27. private final Map<EventType, Map<EventAction, EventHandler>> eventHandleMap;
  28. public EventAspect(CourseDAO courseDAO, QuizDAO quizDAO, EventApi eventApi) {
  29. this.eventHandleMap = ImmutableMap.of(
  30. EventType.LESSON, ImmutableMap.of(
  31. EventAction.PARTICIPATE, ((joinPoint, user) -> Stream.of(joinPoint.getArgs())
  32. .filter(o -> o instanceof Long)
  33. .map(o -> (Long) o)
  34. .findFirst()
  35. .ifPresent(courseId ->
  36. eventApi.reportEvent(eventApi.createParticipateClassEventDTO(String.valueOf(user.getPid()), LocalDateTime.now(), courseDAO.findCourseById(courseId)))
  37. )))
  38. , EventType.POST, ImmutableMap.of(
  39. EventAction.INITIATE, ((joinPoint, user) -> Stream.of(joinPoint.getArgs())
  40. .filter(o -> o instanceof CommentDTO)
  41. .map(o -> (CommentDTO) o)
  42. .findFirst()
  43. .ifPresent(commentDTO ->
  44. eventApi.reportEvent(eventApi.createInitPostEventDTO(String.valueOf(user.getPid()), LocalDateTime.now(), Objects.requireNonNull(commentDTO).getTitle()))
  45. )))
  46. , EventType.QUIZ, ImmutableMap.of(
  47. EventAction.COMPLETE, ((joinPoint, user) -> Stream.of(joinPoint.getArgs())
  48. .filter(o -> o instanceof Long)
  49. .map(o -> (Long) o)
  50. .findFirst()
  51. .ifPresent(quizId ->
  52. eventApi.reportEvent(eventApi.createCompleteQuizEventDTO(String.valueOf(user.getPid()), LocalDateTime.now(), quizDAO.findQuizById(quizId)))
  53. )))
  54. );
  55. }
  56. @FunctionalInterface
  57. private interface EventHandler {
  58. /**
  59. * 处理事件
  60. *
  61. * @param joinPoint 插入点
  62. * @param user 登录用户
  63. */
  64. void handleEvent(JoinPoint joinPoint, LoginUser user);
  65. }
  66. private final EventHandler doNothing = (joinPoint, user) -> {
  67. };
  68. @Transactional(readOnly = true)
  69. @SneakyThrows
  70. @AfterReturning("@annotation(event) && args(user,..)")
  71. public void reportEvent(JoinPoint joinPoint, Event event, LoginUser user) {
  72. eventHandleMap.getOrDefault(event.type(), Collections.emptyMap())
  73. .getOrDefault(event.action(), doNothing)
  74. .handleEvent(joinPoint, user);
  75. }
  76. }