| 1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283 |
- package nju.seec.helper.aspect.event;
- import com.google.common.collect.ImmutableMap;
- import lombok.SneakyThrows;
- import nju.seec.helper.sys.dataanalysis.api.EventApi;
- import nju.seec.helper.sys.dataanalysis.constant.EventAction;
- import nju.seec.helper.sys.dataanalysis.constant.EventType;
- import nju.seec.helper.aspect.auth.LoginUser;
- import nju.seec.helper.dao.CourseDAO;
- import nju.seec.helper.dao.QuizDAO;
- import nju.seec.helper.dto.comment.CommentDTO;
- import org.aspectj.lang.JoinPoint;
- import org.aspectj.lang.annotation.AfterReturning;
- import org.aspectj.lang.annotation.Aspect;
- import org.springframework.stereotype.Component;
- import org.springframework.transaction.annotation.Transactional;
- import java.time.LocalDateTime;
- import java.util.Collections;
- import java.util.Map;
- import java.util.Objects;
- import java.util.stream.Stream;
- /**
- * @author cst
- */
- @Aspect
- @Component
- public class EventAspect {
- private final Map<EventType, Map<EventAction, EventHandler>> eventHandleMap;
- public EventAspect(CourseDAO courseDAO, QuizDAO quizDAO, EventApi eventApi) {
- this.eventHandleMap = ImmutableMap.of(
- EventType.LESSON, ImmutableMap.of(
- EventAction.PARTICIPATE, ((joinPoint, user) -> Stream.of(joinPoint.getArgs())
- .filter(o -> o instanceof Long)
- .map(o -> (Long) o)
- .findFirst()
- .ifPresent(courseId ->
- eventApi.reportEvent(eventApi.createParticipateClassEventDTO(String.valueOf(user.getPid()), LocalDateTime.now(), courseDAO.findCourseById(courseId)))
- )))
- , EventType.POST, ImmutableMap.of(
- EventAction.INITIATE, ((joinPoint, user) -> Stream.of(joinPoint.getArgs())
- .filter(o -> o instanceof CommentDTO)
- .map(o -> (CommentDTO) o)
- .findFirst()
- .ifPresent(commentDTO ->
- eventApi.reportEvent(eventApi.createInitPostEventDTO(String.valueOf(user.getPid()), LocalDateTime.now(), Objects.requireNonNull(commentDTO).getTitle()))
- )))
- , EventType.QUIZ, ImmutableMap.of(
- EventAction.COMPLETE, ((joinPoint, user) -> Stream.of(joinPoint.getArgs())
- .filter(o -> o instanceof Long)
- .map(o -> (Long) o)
- .findFirst()
- .ifPresent(quizId ->
- eventApi.reportEvent(eventApi.createCompleteQuizEventDTO(String.valueOf(user.getPid()), LocalDateTime.now(), quizDAO.findQuizById(quizId)))
- )))
- );
- }
- @FunctionalInterface
- private interface EventHandler {
- /**
- * 处理事件
- *
- * @param joinPoint 插入点
- * @param user 登录用户
- */
- void handleEvent(JoinPoint joinPoint, LoginUser user);
- }
- private final EventHandler doNothing = (joinPoint, user) -> {
- };
- @Transactional(readOnly = true)
- @SneakyThrows
- @AfterReturning("@annotation(event) && args(user,..)")
- public void reportEvent(JoinPoint joinPoint, Event event, LoginUser user) {
- eventHandleMap.getOrDefault(event.type(), Collections.emptyMap())
- .getOrDefault(event.action(), doNothing)
- .handleEvent(joinPoint, user);
- }
- }
|