|
|
@@ -2,9 +2,9 @@ package nju.seec.helper.aspect.event;
|
|
|
|
|
|
import com.google.common.collect.ImmutableMap;
|
|
|
import lombok.SneakyThrows;
|
|
|
-import nju.seec.helper.api.dataanalysis.EventAction;
|
|
|
import nju.seec.helper.api.dataanalysis.EventApi;
|
|
|
-import nju.seec.helper.api.dataanalysis.EventType;
|
|
|
+import nju.seec.helper.api.dataanalysis.constant.EventAction;
|
|
|
+import nju.seec.helper.api.dataanalysis.constant.EventType;
|
|
|
import nju.seec.helper.aspect.auth.LoginUser;
|
|
|
import nju.seec.helper.dao.CourseDAO;
|
|
|
import nju.seec.helper.dao.QuizDAO;
|
|
|
@@ -12,6 +12,7 @@ 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.core.annotation.Order;
|
|
|
import org.springframework.stereotype.Component;
|
|
|
import org.springframework.transaction.annotation.Transactional;
|
|
|
|
|
|
@@ -25,53 +26,48 @@ import java.util.Objects;
|
|
|
*/
|
|
|
@Aspect
|
|
|
@Component
|
|
|
+@Order(3)
|
|
|
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 -> {
|
|
|
+ EventAction.PARTICIPATE, ((joinPoint, user) -> {
|
|
|
Object[] args = joinPoint.getArgs();
|
|
|
- LoginUser user = null;
|
|
|
Long courseId = null;
|
|
|
for (Object arg : args) {
|
|
|
- if (arg instanceof LoginUser) {
|
|
|
- user = (LoginUser) arg;
|
|
|
- } else if (arg instanceof Long) {
|
|
|
+ if (arg instanceof Long) {
|
|
|
courseId = (Long) arg;
|
|
|
+ break;
|
|
|
}
|
|
|
}
|
|
|
- eventApi.reportEvent(eventApi.createParticipateClassEvent(String.valueOf(Objects.requireNonNull(user).getPid()), LocalDateTime.now(), courseDAO.findCourseById(courseId)));
|
|
|
+ eventApi.reportEvent(eventApi.createParticipateClassEventDTO(String.valueOf(user.getPid()), LocalDateTime.now(), courseDAO.findCourseById(courseId)));
|
|
|
})),
|
|
|
EventType.POST, ImmutableMap.of(
|
|
|
- EventAction.INITIATE, (joinPoint -> {
|
|
|
+ EventAction.INITIATE, ((joinPoint, user) -> {
|
|
|
Object[] args = joinPoint.getArgs();
|
|
|
- LoginUser user = null;
|
|
|
CommentDTO commentDTO = null;
|
|
|
for (Object arg : args) {
|
|
|
- if (arg instanceof LoginUser) {
|
|
|
- user = (LoginUser) arg;
|
|
|
- } else if (arg instanceof CommentDTO) {
|
|
|
+ if (arg instanceof CommentDTO) {
|
|
|
commentDTO = (CommentDTO) arg;
|
|
|
+ break;
|
|
|
}
|
|
|
}
|
|
|
- eventApi.reportEvent(eventApi.createInitPostEvent(String.valueOf(Objects.requireNonNull(user).getPid()), LocalDateTime.now(), Objects.requireNonNull(commentDTO).getTitle()));
|
|
|
+ eventApi.reportEvent(eventApi.createInitPostEventDTO(String.valueOf(user.getPid()), LocalDateTime.now(), Objects.requireNonNull(commentDTO).getTitle()));
|
|
|
})
|
|
|
),
|
|
|
EventType.QUIZ, ImmutableMap.of(
|
|
|
- EventAction.COMPLETE, (joinPoint -> {
|
|
|
+ EventAction.COMPLETE, ((joinPoint, user) -> {
|
|
|
Object[] args = joinPoint.getArgs();
|
|
|
- LoginUser user = null;
|
|
|
Long quizId = null;
|
|
|
for (Object arg : args) {
|
|
|
- if (arg instanceof LoginUser) {
|
|
|
- user = (LoginUser) arg;
|
|
|
- } else if (arg instanceof Long) {
|
|
|
+ if (arg instanceof Long) {
|
|
|
quizId = (Long) arg;
|
|
|
+ break;
|
|
|
}
|
|
|
}
|
|
|
- eventApi.reportEvent(eventApi.createCompleteQuizEvent(String.valueOf(Objects.requireNonNull(user).getPid()), LocalDateTime.now(), quizDAO.findQuizById(quizId)));
|
|
|
+ eventApi.reportEvent(eventApi.createCompleteQuizEventDTO(String.valueOf(user.getPid()), LocalDateTime.now(), quizDAO.findQuizById(quizId)));
|
|
|
})
|
|
|
)
|
|
|
);
|
|
|
@@ -82,20 +78,21 @@ public class EventAspect {
|
|
|
/**
|
|
|
* 处理事件
|
|
|
*
|
|
|
- * @param joinPoint
|
|
|
+ * @param joinPoint 插入点
|
|
|
+ * @param user 登录用户
|
|
|
*/
|
|
|
@Transactional(readOnly = true)
|
|
|
- void handleEvent(JoinPoint joinPoint);
|
|
|
+ void handleEvent(JoinPoint joinPoint, LoginUser user);
|
|
|
}
|
|
|
|
|
|
- private final EventHandler doNothing = (joinPoint) -> {
|
|
|
+ private final EventHandler doNothing = (joinPoint, user) -> {
|
|
|
};
|
|
|
|
|
|
@SneakyThrows
|
|
|
- @AfterReturning("@annotation(event)")
|
|
|
- public void reportEvent(JoinPoint joinPoint, Event event) {
|
|
|
+ @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);
|
|
|
+ .handleEvent(joinPoint, user);
|
|
|
}
|
|
|
}
|