Complete Message Module.

hikari
PeterAlbus 1 year ago
parent 1b7be7e20a
commit 62ec980496

@ -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));

@ -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")

@ -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,18 +13,18 @@ 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<Comment> getCommentByBlogId(Long blogId)
{
public List<Comment> getCommentByBlogId(Long blogId) {
QueryWrapper<Comment> commentQueryWrapper = new QueryWrapper<>();
commentQueryWrapper.eq("comment_target", 1);
commentQueryWrapper.eq("comment_target_id", blogId);
@ -31,16 +32,14 @@ public class CommentServiceImpl implements CommentService
}
@Override
public List<Comment> getCommentByUserId(Long userId)
{
public List<Comment> getCommentByUserId(Long userId) {
QueryWrapper<Comment> commentQueryWrapper = new QueryWrapper<>();
commentQueryWrapper.eq("comment_target_id", userId);
return commentMapper.selectList(commentQueryWrapper);
}
@Override
public List<Comment> getCommentByCommentId(Long commentId)
{
public List<Comment> getCommentByCommentId(Long commentId) {
QueryWrapper<Comment> commentQueryWrapper = new QueryWrapper<>();
commentQueryWrapper.eq("comment_target", 2);
commentQueryWrapper.eq("comment_target_id", commentId);
@ -48,26 +47,31 @@ public class CommentServiceImpl implements CommentService
}
@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);
}
}

@ -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 = "<span>" + content + "</span>";
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 = "您的博客《<a href='" + "/#/blog?id=" + blogId
+ "'>" + blog.getBlogTitle() + "</a>》收到了来自" + sender.getUserUsername()
+ "的一条评论";
messageTitle = "评论提醒";
} else {
messageContent = "您在博客《<a href='" + "/#/blog?id=" + blogId
+ "'>" + blog.getBlogTitle() + "</a>》下的评论收到了来自" + sender.getUserUsername()
+ "的一条回复";
messageTitle = "回复提醒";
}
sendMessage(targetId, messageContent, messageTitle, true);
}
}

Loading…
Cancel
Save