package nju.seec.helper.aspect.message; import com.google.common.collect.ImmutableMap; import nju.seec.helper.dao.*; import nju.seec.helper.dto.reply.ReplyDTO; import nju.seec.helper.entity.Comment; import nju.seec.helper.entity.Course; import nju.seec.helper.entity.Quiz; import nju.seec.helper.entity.Slide; import nju.seec.helper.enums.MessageType; import nju.seec.helper.enums.QuizState; import nju.seec.helper.enums.SlideState; import nju.seec.helper.service.MessageService; import nju.seec.helper.vo.CommentVO; import nju.seec.helper.vo.NoticeVO; import nju.seec.helper.vo.quiz.QuizVO; 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.util.Collections; import java.util.List; import java.util.Map; import java.util.Set; import java.util.stream.Stream; /** * @author cst */ @Component @Aspect public class MessageAspect { private final Map messageCreatorMap; public MessageAspect(CourseDAO courseDAO, SlideDAO slideDAO, QuizDAO quizDAO, CommentDAO commentDAO, ChooseDAO chooseDAO, MessageService messageService) { messageCreatorMap = ImmutableMap.builder() .put(MessageType.SLIDE_IN_CLASS, ((joinPoint, object) -> Stream.of(joinPoint.getArgs()) .filter(o -> o instanceof Long) .map(o -> (Long) o) .findFirst() .ifPresent(slideId -> { Slide slide = slideDAO.findSlideById(slideId); if (slide.getState() == SlideState.IN_CLASS) { Set studentIds = chooseDAO.findStudentIdsByCourseId(slide.getCourse().getId()); messageService.createMessage(studentIds, MessageType.SLIDE_IN_CLASS, String.format("[%s] - [%s] 已开课", slide.getCourse().getName(), slide.getName()), slide.getId()); } })) ) .put(MessageType.COMMENT_NEW, (joinPoint, object) -> { if (object instanceof CommentVO) { CommentVO commentVO = (CommentVO) object; if (commentVO.getShow()) { Slide slide = slideDAO.findSlideById(commentVO.getSlideId()); messageService.createMessage(Collections.singleton(slide.getTeacher().getId()), MessageType.COMMENT_NEW, String.format("[%s] - [%s] 在第%d页有新评论", slide.getCourse().getName(), slide.getName(), commentVO.getPageNumber()), slide.getId()); } } }) .put(MessageType.COMMENT_REPLY, (joinPoint, object) -> Stream.of(joinPoint.getArgs()) .filter(o -> o instanceof ReplyDTO) .map(o -> (ReplyDTO) o) .findFirst() .ifPresent(replyDTO -> { Comment comment = commentDAO.findCommentById(replyDTO.getCommentId()); Slide slide = slideDAO.findSlideById(comment.getSlideId()); messageService.createMessage(Collections.singleton(comment.getUser().getId()), MessageType.COMMENT_REPLY, String.format("您在 [%s] - [%s] 第%d页发表的讨论帖已收到回复", slide.getCourse().getName(), slide.getName(), comment.getPageNumber()), slide.getId()); }) ) .put(MessageType.NOTICE_NEW, (joinPoint, object) -> { if (object instanceof NoticeVO) { NoticeVO noticeVO = (NoticeVO) object; Set studentIds = chooseDAO.findStudentIdsByCourseId(noticeVO.getCourseId()); Course course = courseDAO.findCourseById(noticeVO.getCourseId()); messageService.createMessage(studentIds, MessageType.NOTICE_NEW, String.format("[%s] 发布了新公告", course.getName()), course.getId()); } }) .put(MessageType.QUIZ_NEW, new MessageCreator() { @Override public void createMessage(JoinPoint joinPoint, Object object) { if (object instanceof QuizVO) { sendMessages(quizDAO.findQuizById(((QuizVO) object).getId())); } else { Stream.of(joinPoint.getArgs()) .filter(o -> o instanceof Slide) .map(o -> (Slide) o) .findFirst() .ifPresent(slide -> { List quizzes = quizDAO.findBySlide(slide); quizzes.forEach(this::sendMessages); }) ; } } private void sendMessages(Quiz quiz) { if (quiz.getState() == QuizState.ONGOING) { Slide slide = quiz.getSlide(); Course course = slide.getCourse(); Set chooseStudentIds = chooseDAO.findStudentIdsByCourseId(course.getId()); messageService.createMessage(chooseStudentIds, MessageType.QUIZ_NEW, String.format("[%s] - [%s] 的测试 [%s] 已开放", course.getName(), slide.getName(), quiz.getName()), quiz.getId()); } } }) .build(); } @Transactional(rollbackFor = Exception.class) @AfterReturning(value = "@annotation(message)", returning = "object") public void createMessage(JoinPoint joinPoint, Message message, Object object) { messageCreatorMap.getOrDefault(message.value(), doNothing).createMessage(joinPoint, object); } @FunctionalInterface interface MessageCreator { /** * 创建消息 * * @param joinPoint * @param object */ void createMessage(JoinPoint joinPoint, Object object); } private final MessageCreator doNothing = ((joinPoint, object) -> { }); }