diff --git a/src/main/java/com/peteralbus/controller/MessageController.java b/src/main/java/com/peteralbus/controller/MessageController.java index 02992d4..0dfdeb1 100644 --- a/src/main/java/com/peteralbus/controller/MessageController.java +++ b/src/main/java/com/peteralbus/controller/MessageController.java @@ -18,10 +18,28 @@ import org.springframework.web.bind.annotation.RestController; public class MessageController { private final MessageService messageService; + @RequestMapping("/getInBoxMessage") + public Result getInBoxMessage() { + if (!StpUtil.isLogin()) { + return ResultUtil.error(403,"未登录"); + } + Long userId = StpUtil.getLoginIdAsLong(); + return ResultUtil.success(messageService.queryMessageByTargetId(userId)); + } + + @RequestMapping("/getOutBoxMessage") + public Result getOutBoxMessage() { + if (!StpUtil.isLogin()) { + return ResultUtil.error(403,"未登录"); + } + Long userId = StpUtil.getLoginIdAsLong(); + return ResultUtil.success(messageService.queryMessageBySenderId(userId)); + } + @RequestMapping("/getUnreadMessageCount") public Result getUnreadMessageCount() { if (!StpUtil.isLogin()) { - return ResultUtil.error(400,"未登录"); + return ResultUtil.success(0,"未登录"); } Long userId = StpUtil.getLoginIdAsLong(); return ResultUtil.success(messageService.getUnreadMessageCount(userId)); diff --git a/src/main/java/com/peteralbus/domain/Message.java b/src/main/java/com/peteralbus/domain/Message.java index 8868f61..905a97f 100644 --- a/src/main/java/com/peteralbus/domain/Message.java +++ b/src/main/java/com/peteralbus/domain/Message.java @@ -28,7 +28,7 @@ public class Message { Long targetId; String messageContent; String messageTitle; - String senderTime; + String senderName; Boolean isRead; @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss") @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss", timezone = "GMT+8") diff --git a/src/main/java/com/peteralbus/service/impl/CommentServiceImpl.java b/src/main/java/com/peteralbus/service/impl/CommentServiceImpl.java index 09a1904..b1ebb27 100644 --- a/src/main/java/com/peteralbus/service/impl/CommentServiceImpl.java +++ b/src/main/java/com/peteralbus/service/impl/CommentServiceImpl.java @@ -4,6 +4,7 @@ import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper; import com.peteralbus.domain.Comment; import com.peteralbus.mapper.CommentMapper; import com.peteralbus.service.CommentService; +import com.peteralbus.util.MessageUtil; import lombok.RequiredArgsConstructor; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; @@ -12,62 +13,65 @@ import java.util.List; /** * The type Comment service. + * * @author PeterAlbus * Created on 2022/3/28. */ @Service @RequiredArgsConstructor(onConstructor = @__(@Autowired)) -public class CommentServiceImpl implements CommentService -{ +public class CommentServiceImpl implements CommentService { private final CommentMapper commentMapper; + private final MessageUtil messageUtil; @Override - public List getCommentByBlogId(Long blogId) - { - QueryWrapper commentQueryWrapper=new QueryWrapper<>(); - commentQueryWrapper.eq("comment_target",1); - commentQueryWrapper.eq("comment_target_id",blogId); + public List getCommentByBlogId(Long blogId) { + QueryWrapper commentQueryWrapper = new QueryWrapper<>(); + commentQueryWrapper.eq("comment_target", 1); + commentQueryWrapper.eq("comment_target_id", blogId); return commentMapper.selectList(commentQueryWrapper); } @Override - public List getCommentByUserId(Long userId) - { - QueryWrapper commentQueryWrapper=new QueryWrapper<>(); - commentQueryWrapper.eq("comment_target_id",userId); + public List getCommentByUserId(Long userId) { + QueryWrapper commentQueryWrapper = new QueryWrapper<>(); + commentQueryWrapper.eq("comment_target_id", userId); return commentMapper.selectList(commentQueryWrapper); } @Override - public List getCommentByCommentId(Long commentId) - { - QueryWrapper commentQueryWrapper=new QueryWrapper<>(); - commentQueryWrapper.eq("comment_target",2); - commentQueryWrapper.eq("comment_target_id",commentId); + public List getCommentByCommentId(Long commentId) { + QueryWrapper commentQueryWrapper = new QueryWrapper<>(); + commentQueryWrapper.eq("comment_target", 2); + commentQueryWrapper.eq("comment_target_id", commentId); return commentMapper.selectList(commentQueryWrapper); } @Override - public Comment getCommentById(Long commentId) - { + public Comment getCommentById(Long commentId) { return commentMapper.selectById(commentId); } @Override - public int addComment(Comment comment) - { - return commentMapper.insert(comment); + public int addComment(Comment comment) { + int result = commentMapper.insert(comment); + if (result > 0) { + if (comment.getCommentTarget().equals(2)) { + Comment comment1 = commentMapper.selectById(comment.getCommentTargetId()); + messageUtil.sendReplyMessage(comment, comment1.getCommentUserId(), comment1.getCommentTargetId()); + } else { + messageUtil.sendReplyMessage(comment, MessageUtil.OWNER_ID, comment.getCommentTargetId()); + } + } + return result; } @Override - public int updateComment(Comment comment) - { + public int updateComment(Comment comment) { return commentMapper.updateById(comment); } @Override - public int deleteComment(Comment comment) - { + public int deleteComment(Comment comment) { return commentMapper.deleteById(comment); } } diff --git a/src/main/java/com/peteralbus/util/MessageUtil.java b/src/main/java/com/peteralbus/util/MessageUtil.java index 139b5f8..3082d2b 100644 --- a/src/main/java/com/peteralbus/util/MessageUtil.java +++ b/src/main/java/com/peteralbus/util/MessageUtil.java @@ -1,12 +1,67 @@ package com.peteralbus.util; +import cn.dev33.satoken.stp.StpUtil; +import com.peteralbus.domain.Blog; +import com.peteralbus.domain.Comment; +import com.peteralbus.domain.Message; +import com.peteralbus.domain.User; +import com.peteralbus.service.BlogService; import com.peteralbus.service.MessageService; +import com.peteralbus.service.UserService; import lombok.RequiredArgsConstructor; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Component; +import java.time.LocalDateTime; + @Component @RequiredArgsConstructor(onConstructor = @__(@Autowired)) public class MessageUtil { private final MessageService messageService; + private final UserService userService; + private final BlogService blogService; + public static final Long OWNER_ID = 1507660309008289794L; + + public void sendMessage(Long targetId, String content, String title, Boolean system) { + content = "" + content + ""; + Message message = new Message(); + if (system) { + message.setSenderId(-1L); + message.setSenderName("系统"); + } else { + if (!StpUtil.isLogin()) { + return; + } + Long userId = StpUtil.getLoginIdAsLong(); + User user = userService.getUserById(userId); + message.setSenderId(userId); + message.setSenderName(user.getUserUsername()); + } + message.setTargetId(targetId); + message.setMessageContent(content); + message.setMessageTitle(title); + message.setIsRead(false); + message.setGmtCreate(LocalDateTime.now()); + messageService.addMessage(message); + } + + public void sendReplyMessage(Comment comment, Long targetId, Long blogId) { + Long senderId = comment.getCommentUserId(); + User sender = userService.getUserById(senderId); + Blog blog = blogService.queryById(blogId); + String messageContent; + String messageTitle; + if (comment.getCommentTarget().equals(1)) { + messageContent = "您的博客《" + blog.getBlogTitle() + "》收到了来自" + sender.getUserUsername() + + "的一条评论"; + messageTitle = "评论提醒"; + } else { + messageContent = "您在博客《" + blog.getBlogTitle() + "》下的评论收到了来自" + sender.getUserUsername() + + "的一条回复"; + messageTitle = "回复提醒"; + } + sendMessage(targetId, messageContent, messageTitle, true); + } }