Commit af9fedee by 段启岩

更新用户统计信息逻辑

parent fb93d201
...@@ -3,15 +3,20 @@ package cn.meteor.beyondclouds.modules.user.listener; ...@@ -3,15 +3,20 @@ package cn.meteor.beyondclouds.modules.user.listener;
import cn.meteor.beyondclouds.core.listener.DataItemChangeListener; import cn.meteor.beyondclouds.core.listener.DataItemChangeListener;
import cn.meteor.beyondclouds.core.queue.message.DataItemChangeMessage; import cn.meteor.beyondclouds.core.queue.message.DataItemChangeMessage;
import cn.meteor.beyondclouds.core.queue.message.DataItemType; import cn.meteor.beyondclouds.core.queue.message.DataItemType;
import cn.meteor.beyondclouds.modules.blog.entity.Blog;
import cn.meteor.beyondclouds.modules.blog.service.IBlogService; import cn.meteor.beyondclouds.modules.blog.service.IBlogService;
import cn.meteor.beyondclouds.modules.post.entity.Post;
import cn.meteor.beyondclouds.modules.post.service.IPostService; import cn.meteor.beyondclouds.modules.post.service.IPostService;
import cn.meteor.beyondclouds.modules.project.entity.Project;
import cn.meteor.beyondclouds.modules.project.service.IProjectService; import cn.meteor.beyondclouds.modules.project.service.IProjectService;
import cn.meteor.beyondclouds.modules.question.entity.Question;
import cn.meteor.beyondclouds.modules.question.service.IQuestionService; import cn.meteor.beyondclouds.modules.question.service.IQuestionService;
import cn.meteor.beyondclouds.modules.queue.service.IMessageQueueService; import cn.meteor.beyondclouds.modules.queue.service.IMessageQueueService;
import cn.meteor.beyondclouds.modules.user.entity.UserFollow; import cn.meteor.beyondclouds.modules.user.entity.UserFollow;
import cn.meteor.beyondclouds.modules.user.entity.UserStatistics; import cn.meteor.beyondclouds.modules.user.entity.UserStatistics;
import cn.meteor.beyondclouds.modules.user.service.IUserFollowService; import cn.meteor.beyondclouds.modules.user.service.IUserFollowService;
import cn.meteor.beyondclouds.modules.user.service.IUserStatisticsService; import cn.meteor.beyondclouds.modules.user.service.IUserStatisticsService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper; import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component; import org.springframework.stereotype.Component;
...@@ -30,6 +35,19 @@ public class UserStatisticsListener implements DataItemChangeListener { ...@@ -30,6 +35,19 @@ public class UserStatisticsListener implements DataItemChangeListener {
private IMessageQueueService messageQueueService; private IMessageQueueService messageQueueService;
private IBlogService blogService;
private IPostService postService;
private IProjectService projectService;
private IQuestionService questionService;
@Autowired
public void setQuestionService(IQuestionService questionService) {
this.questionService = questionService;
}
@Autowired @Autowired
public UserStatisticsListener(IUserStatisticsService userStatisticsService, IUserFollowService userFollowService, IBlogService blogService, IProjectService projectService, IPostService postService, IQuestionService questionService) { public UserStatisticsListener(IUserStatisticsService userStatisticsService, IUserFollowService userFollowService, IBlogService blogService, IProjectService projectService, IPostService postService, IQuestionService questionService) {
this.userStatisticsService = userStatisticsService; this.userStatisticsService = userStatisticsService;
...@@ -37,116 +55,109 @@ public class UserStatisticsListener implements DataItemChangeListener { ...@@ -37,116 +55,109 @@ public class UserStatisticsListener implements DataItemChangeListener {
} }
@Autowired @Autowired
public void setMessageQueueService(IMessageQueueService messageQueueService) { public void setBlogService(IBlogService blogService) {
this.messageQueueService = messageQueueService; this.blogService = blogService;
} }
@Override @Autowired
public void onDataItemAdd(DataItemChangeMessage dataItemChangeMessage) { public void setPostService(IPostService postService) {
DataItemType itemType = dataItemChangeMessage.getItemType(); this.postService = postService;
Serializable itemId = dataItemChangeMessage.getItemId();
String operatorId = dataItemChangeMessage.getOperatorId();
// 关注用户
if (itemType.equals(DataItemType.USER_FOLLOW)) {
UserFollow userFollow = userFollowService.getById(itemId);
// 新增被关注用户的粉丝量
increaseUserStatisticValue(userFollow.getFollowedId(), "fans_num");
// 新增关注者的关注量
increaseUserStatisticValue(userFollow.getFollowerId(), "followed_num");
} }
// 发布博客 @Autowired
if (itemType.equals(DataItemType.BLOG)) { public void setProjectService(IProjectService projectService) {
// 新增发博客的用户的博客发布量 this.projectService = projectService;
increaseUserStatisticValue(operatorId, "blog_num");
} }
// 发布项目 @Autowired
if (itemType.equals(DataItemType.PROJECT)) { public void setMessageQueueService(IMessageQueueService messageQueueService) {
// 新增发项目的用户的项目发布量 this.messageQueueService = messageQueueService;
increaseUserStatisticValue(operatorId, "project_num");
} }
// 发布动态 @Override
if (itemType.equals(DataItemType.POST)) { public void onDataItemAdd(DataItemChangeMessage dataItemChangeMessage) {
// 新增发动态的用户的动态发布量 onDataChange(dataItemChangeMessage);
increaseUserStatisticValue(operatorId, "post_num");
} }
// 发布问答 @Override
if (itemType.equals(DataItemType.QUESTION)) { public void onDataItemDelete(DataItemChangeMessage dataItemChangeMessage) {
// 新增发问答的用户的问答发布量 onDataChange(dataItemChangeMessage);
increaseUserStatisticValue(operatorId, "question_num");
} }
// 修改完用户统计信息后发送用户统计信息修改的通知
messageQueueService.sendDataItemChangeMessage(DataItemChangeMessage.updateMessage(DataItemType.USER_STATISTICS, itemId, (String) itemId));
}
@Override /**
public void onDataItemDelete(DataItemChangeMessage dataItemChangeMessage) { * 汇总ADD和delete消息
* @param dataItemChangeMessage
*/
private void onDataChange(DataItemChangeMessage dataItemChangeMessage) {
DataItemType itemType = dataItemChangeMessage.getItemType(); DataItemType itemType = dataItemChangeMessage.getItemType();
Serializable itemId = dataItemChangeMessage.getItemId(); Serializable itemId = dataItemChangeMessage.getItemId();
String operatorId = dataItemChangeMessage.getOperatorId(); String operatorId = dataItemChangeMessage.getOperatorId();
// 取关用户 // 关注用户
if (itemType.equals(DataItemType.USER_FOLLOW)) { if (itemType.equals(DataItemType.USER_FOLLOW)) {
UserFollow userFollow = userFollowService.getById(itemId); UserFollow userFollow = userFollowService.getById(itemId);
// 减少被关注用户的粉丝量 // 更新被关注用户的粉丝量
decreaseUserStatisticValue(userFollow.getFollowedId(), "fans_num"); QueryWrapper<UserFollow> followedUserFansNumQueryWrapper = new QueryWrapper<>();
// 减少关注者的关注量 followedUserFansNumQueryWrapper.eq("followed_id", userFollow.getFollowedId());
decreaseUserStatisticValue(userFollow.getFollowerId(), "followed_num"); long followedUserFansNum = userFollowService.count(followedUserFansNumQueryWrapper);
updateUserStatisticValue(userFollow.getFollowedId(), "fans_num", followedUserFansNum);
// 更新关注者的关注量
QueryWrapper<UserFollow> followerUserFollowedNumQueryWrapper = new QueryWrapper<>();
followerUserFollowedNumQueryWrapper.eq("follower_id", userFollow.getFollowerId());
long followerUserFollowedNum = userFollowService.count(followerUserFollowedNumQueryWrapper);
updateUserStatisticValue(userFollow.getFollowerId(), "fans_num", followerUserFollowedNum);
} }
// 删除博客 // 发布博客
if (itemType.equals(DataItemType.BLOG)) { if (itemType.equals(DataItemType.BLOG)) {
// 减少发博客的用户的博客发布量 // 更新博客发布量
decreaseUserStatisticValue(operatorId, "blog_num"); QueryWrapper<Blog> blogQueryWrapper = new QueryWrapper<>();
blogQueryWrapper.eq("user_id", itemId);
updateUserStatisticValue((String) itemId, "blog_num", blogService.count(blogQueryWrapper));
} }
// 删除项目 // 发布项目
if (itemType.equals(DataItemType.PROJECT)) { if (itemType.equals(DataItemType.PROJECT)) {
// 减少发项目的用户的项目发布量 // 更新项目发布量
decreaseUserStatisticValue(operatorId, "project_num"); QueryWrapper<Project> projectQueryWrapper = new QueryWrapper<>();
projectQueryWrapper.eq("user_id", itemId);
updateUserStatisticValue((String) itemId, "project_num", projectService.count(projectQueryWrapper));
} }
// 删除动态 // 发布动态
if (itemType.equals(DataItemType.POST)) { if (itemType.equals(DataItemType.POST)) {
// 减少发动态的用户的动态发布量 // 更新动态发布量
decreaseUserStatisticValue(operatorId, "post_num"); QueryWrapper<Post> postQueryWrapper = new QueryWrapper<>();
postQueryWrapper.eq("user_id", itemId);
updateUserStatisticValue((String) itemId, "post_num", postService.count(postQueryWrapper));
} }
// 删除问答 // 发布问答
if (itemType.equals(DataItemType.QUESTION)) { if (itemType.equals(DataItemType.QUESTION)) {
// 减少发问答的用户的问答发布量 // 更新问答发布量
decreaseUserStatisticValue(operatorId, "question_num"); QueryWrapper<Question> questionQueryWrapper = new QueryWrapper<>();
questionQueryWrapper.eq("user_id", itemId);
updateUserStatisticValue((String) itemId, "question_num", questionService.count(questionQueryWrapper));
} }
// 修改完用户统计信息后发送用户统计信息修改的通知 // 修改完用户统计信息后发送用户统计信息修改的通知
messageQueueService.sendDataItemChangeMessage(DataItemChangeMessage.updateMessage(DataItemType.USER_STATISTICS, itemId, (String) itemId)); messageQueueService.sendDataItemChangeMessage(DataItemChangeMessage.updateMessage(DataItemType.USER_STATISTICS, itemId, (String) itemId));
}
/**
* 对用户统计信息进行加1操作
* @param userId
* @param column
*/
private void increaseUserStatisticValue(String userId, String column) {
updateUserStatistics(userId, column + " = " + column + " + 1");
} }
/** /**
* 对用户统计信息进行减1操作 * 更新用户统计信息
* @param userId * @param userId
* @param column * @param column
* @param value
*/ */
private void decreaseUserStatisticValue(String userId, String column) { private <T>void updateUserStatisticValue(String userId, String column, T value) {
updateUserStatistics(userId, column + " = " + column + " - 1"); updateUserStatistics(userId, column + " = " + value);
} }
/** /**
* 更新用户的统计信息 * 更新用户的统计信息
* @param userId * @param userId
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment