Commit 3e004bac by 段启岩

Merge remote-tracking branch 'origin/fix-issue#47'

parents 0485dd57 664f2ee9
......@@ -79,7 +79,7 @@ public class PostApi {
//发布动态
try {
postService.publishPost(post);
postService.publishPost(post,postForm.getTopicIds());
return Response.success();
} catch (ProjectServiceException e) {
e.printStackTrace();
......
......@@ -26,4 +26,7 @@ public class PostForm {
@NullOrNotBlank(message = "请传入有效的视频内容")
private String video;
@ElementNotBlank(message = "话题Id不可为空")
private List<String> topicIds;
}
......@@ -6,6 +6,8 @@ import cn.meteor.beyondclouds.modules.project.exception.ProjectServiceException;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.IService;
import java.util.List;
/**
* <p>
* 动态表 服务类
......@@ -19,8 +21,10 @@ public interface IPostService extends IService<Post> {
/**
* 发布动态
* @param post
* @param topicIds
* @throws ProjectServiceException
*/
void publishPost(Post post) throws ProjectServiceException;
void publishPost(Post post , List<String> topicIds) throws ProjectServiceException;
/**
* 删除动态
......
......@@ -9,6 +9,11 @@ import cn.meteor.beyondclouds.modules.post.mapper.PostMapper;
import cn.meteor.beyondclouds.modules.post.service.IPostService;
import cn.meteor.beyondclouds.modules.project.exception.ProjectServiceException;
import cn.meteor.beyondclouds.modules.queue.service.IMessageQueueService;
import cn.meteor.beyondclouds.modules.topic.entity.Topic;
import cn.meteor.beyondclouds.modules.topic.entity.TopicReference;
import cn.meteor.beyondclouds.modules.topic.enums.TopicReferenceType;
import cn.meteor.beyondclouds.modules.topic.service.ITopicReferenceService;
import cn.meteor.beyondclouds.modules.topic.service.ITopicService;
import cn.meteor.beyondclouds.modules.user.entity.User;
import cn.meteor.beyondclouds.modules.user.service.IUserService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
......@@ -18,6 +23,11 @@ import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;
/**
* <p>
......@@ -34,6 +44,20 @@ public class PostServiceImpl extends ServiceImpl<PostMapper, Post> implements IP
private IMessageQueueService messageQueueService;
private ITopicService topicService;
private ITopicReferenceService topicReferenceService;
@Autowired
public void setTopicReferenceService(ITopicReferenceService topicReferenceService) {
this.topicReferenceService = topicReferenceService;
}
@Autowired
public void setTopicService(ITopicService topicService) {
this.topicService = topicService;
}
@Autowired
public void setUserService(IUserService userService) {
this.userService = userService;
......@@ -50,7 +74,7 @@ public class PostServiceImpl extends ServiceImpl<PostMapper, Post> implements IP
* @throws ProjectServiceException
*/
@Override
public void publishPost(Post post) throws ProjectServiceException {
public void publishPost(Post post , List<String> topicIds) throws ProjectServiceException {
//1.判断是否视频和图片都传了
if (null != post.getPictures() && null != post.getVideo()) {
throw new ProjectServiceException(PostErrorCode.NOT_APPEAR_SAME_TIME);
......@@ -79,6 +103,26 @@ public class PostServiceImpl extends ServiceImpl<PostMapper, Post> implements IP
//2.保存动态
save(post);
//处理话题引用
if (!CollectionUtils.isEmpty(topicIds)) {
// 获取要引用的话题,不正确的话题ID会被自动过滤
List<String> existsTopicIds = topicService.listByIds(topicIds).stream().map(Topic::getTopicId).collect(Collectors.toList());
if (!CollectionUtils.isEmpty(existsTopicIds)) {
List<TopicReference> topicReferences = new ArrayList<>();
for (String topicId : existsTopicIds) {
TopicReference topicReference = new TopicReference();
topicReference.setTopicId(topicId);
topicReference.setReferencerId(post.getPostId());
topicReference.setReferencerType(TopicReferenceType.POST_REFERENCE.getType());
topicReferences.add(topicReference);
}
// 批量保存
topicReferenceService.saveBatch(topicReferences);
// 新增话题引用次数
topicService.increaseReferenceCountBatch(existsTopicIds, 1);
}
}
//发送消息到消息队列
messageQueueService.sendDataItemChangeMessage(DataItemChangeMessage.addMessage(DataItemType.POST, post.getPostId(), post.getUserId()));
}
......@@ -103,6 +147,16 @@ public class PostServiceImpl extends ServiceImpl<PostMapper, Post> implements IP
//2.删除动态
removeById(postId);
//3.删除动态里的话题引用
QueryWrapper<TopicReference> topicReferenceQueryWrapper = new QueryWrapper<>();
topicReferenceQueryWrapper.eq("referencer_id", postId);
//所有的话题引用减1
List<String> existsTopicIds = topicReferenceService.list(topicReferenceQueryWrapper).stream().map(TopicReference::getTopicId).collect(Collectors.toList());
topicService.decreaseReferenceCountBatch(existsTopicIds, 1);
topicReferenceService.remove(topicReferenceQueryWrapper);
//发送消息到消息队列
messageQueueService.sendDataItemChangeMessage(DataItemChangeMessage.deleteMessage(DataItemType.POST, post.getPostId(), 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