Commit f9a117e8 by 段启岩

发布动态话题自动创建和引用

parent b1bb9d58
......@@ -79,7 +79,7 @@ public class PostApi {
//发布动态
try {
postService.publishPost(post,postForm.getTopicIds());
postService.publishPost(post);
return Response.success();
} catch (ProjectServiceException e) {
e.printStackTrace();
......
......@@ -26,7 +26,4 @@ public class PostForm {
@NullOrNotBlank(message = "请传入有效的视频内容")
private String video;
@ElementNotBlank(message = "话题Id不可为空")
private List<String> topicIds;
}
......@@ -21,10 +21,9 @@ public interface IPostService extends IService<Post> {
/**
* 发布动态
* @param post
* @param topicIds
* @throws ProjectServiceException
*/
void publishPost(Post post , List<String> topicIds) throws ProjectServiceException;
void publishPost(Post post) throws ProjectServiceException;
/**
* 删除动态
......
......@@ -16,6 +16,7 @@ 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 cn.meteor.beyondclouds.util.TopicUtils;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
......@@ -24,6 +25,7 @@ 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 org.springframework.util.StringUtils;
import java.util.ArrayList;
import java.util.List;
......@@ -74,7 +76,7 @@ public class PostServiceImpl extends ServiceImpl<PostMapper, Post> implements IP
* @throws ProjectServiceException
*/
@Override
public void publishPost(Post post , List<String> topicIds) throws ProjectServiceException {
public void publishPost(Post post) throws ProjectServiceException {
//1.判断是否视频和图片都传了
if (null != post.getPictures() && null != post.getVideo()) {
throw new ProjectServiceException(PostErrorCode.NOT_APPEAR_SAME_TIME);
......@@ -104,12 +106,17 @@ public class PostServiceImpl extends ServiceImpl<PostMapper, Post> implements IP
save(post);
//处理话题引用
if (!CollectionUtils.isEmpty(topicIds)) {
// 获取要引用的话题,不正确的话题ID会被自动过滤
List<String> existsTopicIds = topicService.listByIds(topicIds).stream().map(Topic::getTopicId).collect(Collectors.toList());
if (!CollectionUtils.isEmpty(existsTopicIds)) {
String postContent = post.getContent();
if (!StringUtils.isEmpty(postContent)) {
// 解析话题,若话题不存在则创建话题
List<String> topicNames = TopicUtils.parseTopics(post.getContent());
if (!CollectionUtils.isEmpty(topicNames)) {
List<Topic> topics = topicService.queryAndCreateByTopicNames(topicNames, post.getUserId());
List<String >topicIds = topics.stream().map(Topic::getTopicId).collect(Collectors.toList());
// 关联话题和动态
List<TopicReference> topicReferences = new ArrayList<>();
for (String topicId : existsTopicIds) {
for (String topicId : topicIds) {
TopicReference topicReference = new TopicReference();
topicReference.setTopicId(topicId);
topicReference.setReferencerId(post.getPostId());
......@@ -119,9 +126,11 @@ public class PostServiceImpl extends ServiceImpl<PostMapper, Post> implements IP
// 批量保存
topicReferenceService.saveBatch(topicReferences);
// 新增话题引用次数
topicService.increaseReferenceCountBatch(existsTopicIds, 1);
topicService.increaseReferenceCountBatch(topicIds, 1);
}
}
//发送消息到消息队列
messageQueueService.sendDataItemChangeMessage(DataItemChangeMessage.addMessage(DataItemType.POST, post.getPostId(), post.getUserId()));
......
......@@ -9,6 +9,7 @@ import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.IService;
import java.util.Collection;
import java.util.List;
/**
* <p>
......@@ -139,4 +140,12 @@ public interface ITopicService extends IService<Topic> {
* @return IPage<Post>
*/
IPage<Post> getPostByTopicName(Integer pageNumber, Integer pageSize, String topicName) throws TopicServiceException;
/**
* 查找并话题,若话题不存在,则创建话题
* @param topicNames
* @param userId
* @return
*/
List<Topic> queryAndCreateByTopicNames(List<String> topicNames, String userId);
}
......@@ -354,4 +354,31 @@ public class TopicServiceImpl extends ServiceImpl<TopicMapper, Topic> implements
return postIPage;
}
@Override
public List<Topic> queryAndCreateByTopicNames(List<String> topicNames, String userId) {
QueryWrapper<Topic> topicQueryWrapper = new QueryWrapper<>();
topicQueryWrapper.in("topic_name", topicNames);
List<Topic> topics = list(topicQueryWrapper);
// 查询不存在的话题
List<String> topicNameExists = topics.stream()
.map(Topic::getTopicName)
.collect(Collectors.toList());
List<String> topicNameNotExists = topicNames.stream()
.filter(topicName -> !topicNameExists.contains(topicName))
.collect(Collectors.toList());
// 创建不存在的话题并添加到topics
topicNameNotExists.forEach(topicName -> {
Topic topic = new Topic();
topic.setTopicName(topicName);
topic.setUserId(userId);
save(topic);
topics.add(topic);
});
return topics;
}
}
......@@ -4,6 +4,7 @@ import lombok.extern.slf4j.Slf4j;
import org.springframework.util.Assert;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.TreeSet;
import java.util.regex.Matcher;
......@@ -27,13 +28,15 @@ public class TopicUtils {
while (matcher.find()) {
topicNames.add(matcher.group(2));
}
// topicNames.stream().
// collect(Collectors.collectingAndThen(Collectors.toCollection());
return null;
topicNames = topicNames.stream().
collect(
Collectors.collectingAndThen(Collectors.toCollection(() -> new HashSet<>()), ArrayList::new)
);
return topicNames;
}
public static void main(String[] args) {
System.out.println(TopicUtils.parseTopics("#哈哈哈#今天你吃饭了吗#吃了#######"));
System.out.println(TopicUtils.parseTopics("#哈哈哈#今#哈哈哈#天你吃饭了吗#吃了#######"));
}
}
......@@ -4,7 +4,7 @@ spring:
driver-class-name: com.mysql.cj.jdbc.Driver
url: jdbc:mysql://127.0.0.1:3306/beyond_clouds?useUnicode=true&characterEncoding=UTF-8&useSSL=false&allowPublicKeyRetrieval=true
username: root
password: 197442
password: 100Centa30821%mysql
# 邮箱
mail:
......
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