Commit c81aca95 by 段启岩

Merge remote-tracking branch 'origin/topic-parser'

# Conflicts:
#	src/main/resources/application.yml
parents 1c29231d f9a117e8
...@@ -79,7 +79,7 @@ public class PostApi { ...@@ -79,7 +79,7 @@ public class PostApi {
//发布动态 //发布动态
try { try {
postService.publishPost(post,postForm.getTopicIds()); postService.publishPost(post);
return Response.success(); return Response.success();
} catch (ProjectServiceException e) { } catch (ProjectServiceException e) {
e.printStackTrace(); e.printStackTrace();
......
...@@ -26,7 +26,4 @@ public class PostForm { ...@@ -26,7 +26,4 @@ public class PostForm {
@NullOrNotBlank(message = "请传入有效的视频内容") @NullOrNotBlank(message = "请传入有效的视频内容")
private String video; private String video;
@ElementNotBlank(message = "话题Id不可为空")
private List<String> topicIds;
} }
...@@ -21,10 +21,9 @@ public interface IPostService extends IService<Post> { ...@@ -21,10 +21,9 @@ public interface IPostService extends IService<Post> {
/** /**
* 发布动态 * 发布动态
* @param post * @param post
* @param topicIds
* @throws ProjectServiceException * @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; ...@@ -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.topic.service.ITopicService;
import cn.meteor.beyondclouds.modules.user.entity.User; import cn.meteor.beyondclouds.modules.user.entity.User;
import cn.meteor.beyondclouds.modules.user.service.IUserService; 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.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper; import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage; import com.baomidou.mybatisplus.core.metadata.IPage;
...@@ -24,6 +25,7 @@ import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl; ...@@ -24,6 +25,7 @@ import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service; import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils; import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.List; import java.util.List;
...@@ -74,7 +76,7 @@ public class PostServiceImpl extends ServiceImpl<PostMapper, Post> implements IP ...@@ -74,7 +76,7 @@ public class PostServiceImpl extends ServiceImpl<PostMapper, Post> implements IP
* @throws ProjectServiceException * @throws ProjectServiceException
*/ */
@Override @Override
public void publishPost(Post post , List<String> topicIds) throws ProjectServiceException { public void publishPost(Post post) throws ProjectServiceException {
//1.判断是否视频和图片都传了 //1.判断是否视频和图片都传了
if (null != post.getPictures() && null != post.getVideo()) { if (null != post.getPictures() && null != post.getVideo()) {
throw new ProjectServiceException(PostErrorCode.NOT_APPEAR_SAME_TIME); throw new ProjectServiceException(PostErrorCode.NOT_APPEAR_SAME_TIME);
...@@ -104,12 +106,17 @@ public class PostServiceImpl extends ServiceImpl<PostMapper, Post> implements IP ...@@ -104,12 +106,17 @@ public class PostServiceImpl extends ServiceImpl<PostMapper, Post> implements IP
save(post); save(post);
//处理话题引用 //处理话题引用
if (!CollectionUtils.isEmpty(topicIds)) { String postContent = post.getContent();
// 获取要引用的话题,不正确的话题ID会被自动过滤 if (!StringUtils.isEmpty(postContent)) {
List<String> existsTopicIds = topicService.listByIds(topicIds).stream().map(Topic::getTopicId).collect(Collectors.toList()); // 解析话题,若话题不存在则创建话题
if (!CollectionUtils.isEmpty(existsTopicIds)) { 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<>(); List<TopicReference> topicReferences = new ArrayList<>();
for (String topicId : existsTopicIds) { for (String topicId : topicIds) {
TopicReference topicReference = new TopicReference(); TopicReference topicReference = new TopicReference();
topicReference.setTopicId(topicId); topicReference.setTopicId(topicId);
topicReference.setReferencerId(post.getPostId()); topicReference.setReferencerId(post.getPostId());
...@@ -119,9 +126,11 @@ public class PostServiceImpl extends ServiceImpl<PostMapper, Post> implements IP ...@@ -119,9 +126,11 @@ public class PostServiceImpl extends ServiceImpl<PostMapper, Post> implements IP
// 批量保存 // 批量保存
topicReferenceService.saveBatch(topicReferences); topicReferenceService.saveBatch(topicReferences);
// 新增话题引用次数 // 新增话题引用次数
topicService.increaseReferenceCountBatch(existsTopicIds, 1); topicService.increaseReferenceCountBatch(topicIds, 1);
} }
} }
//发送消息到消息队列 //发送消息到消息队列
messageQueueService.sendDataItemChangeMessage(DataItemChangeMessage.addMessage(DataItemType.POST, post.getPostId(), post.getUserId())); messageQueueService.sendDataItemChangeMessage(DataItemChangeMessage.addMessage(DataItemType.POST, post.getPostId(), post.getUserId()));
......
...@@ -9,6 +9,7 @@ import com.baomidou.mybatisplus.core.metadata.IPage; ...@@ -9,6 +9,7 @@ import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.IService; import com.baomidou.mybatisplus.extension.service.IService;
import java.util.Collection; import java.util.Collection;
import java.util.List;
/** /**
* <p> * <p>
...@@ -139,4 +140,12 @@ public interface ITopicService extends IService<Topic> { ...@@ -139,4 +140,12 @@ public interface ITopicService extends IService<Topic> {
* @return IPage<Post> * @return IPage<Post>
*/ */
IPage<Post> getPostByTopicName(Integer pageNumber, Integer pageSize, String topicName) throws TopicServiceException; 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 ...@@ -354,4 +354,31 @@ public class TopicServiceImpl extends ServiceImpl<TopicMapper, Topic> implements
return postIPage; 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;
}
} }
package cn.meteor.beyondclouds.util;
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;
import java.util.regex.Pattern;
import java.util.stream.Collectors;
/**
* @author meteor
*/
@Slf4j
public class TopicUtils {
private static final Pattern topicPattern = Pattern.compile("(#([^#]+?)#)");
public static List<String> parseTopics(String str) {
Assert.hasText(str, "str must not be empty");
Matcher matcher = topicPattern.matcher(str);
List<String> topicNames = new ArrayList<>();
while (matcher.find()) {
topicNames.add(matcher.group(2));
}
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("#哈哈哈#今#哈哈哈#天你吃饭了吗#吃了#######"));
}
}
...@@ -4,7 +4,7 @@ spring: ...@@ -4,7 +4,7 @@ spring:
driver-class-name: com.mysql.cj.jdbc.Driver 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 url: jdbc:mysql://127.0.0.1:3306/beyond_clouds?useUnicode=true&characterEncoding=UTF-8&useSSL=false&allowPublicKeyRetrieval=true
username: root username: root
password: 2018006709 password: 100Centa30821%mysql
# 邮箱 # 邮箱
mail: 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