Commit 8da61f2b by 段启岩

发布博客时判断标签和话题是否存在,并更新话题引用次数

parent a7a32246
...@@ -12,7 +12,9 @@ import cn.meteor.beyondclouds.modules.blog.exception.BlogCategoryServiceExceptio ...@@ -12,7 +12,9 @@ import cn.meteor.beyondclouds.modules.blog.exception.BlogCategoryServiceExceptio
import cn.meteor.beyondclouds.modules.blog.exception.BlogServiceException; import cn.meteor.beyondclouds.modules.blog.exception.BlogServiceException;
import cn.meteor.beyondclouds.modules.blog.mapper.BlogMapper; import cn.meteor.beyondclouds.modules.blog.mapper.BlogMapper;
import cn.meteor.beyondclouds.modules.blog.service.*; import cn.meteor.beyondclouds.modules.blog.service.*;
import cn.meteor.beyondclouds.modules.tag.entity.Tag;
import cn.meteor.beyondclouds.modules.tag.service.ITagService; import cn.meteor.beyondclouds.modules.tag.service.ITagService;
import cn.meteor.beyondclouds.modules.topic.entity.Topic;
import cn.meteor.beyondclouds.modules.topic.entity.TopicReference; import cn.meteor.beyondclouds.modules.topic.entity.TopicReference;
import cn.meteor.beyondclouds.modules.topic.service.ITopicReferenceService; import cn.meteor.beyondclouds.modules.topic.service.ITopicReferenceService;
import cn.meteor.beyondclouds.modules.topic.service.ITopicService; import cn.meteor.beyondclouds.modules.topic.service.ITopicService;
...@@ -24,6 +26,7 @@ import lombok.extern.java.Log; ...@@ -24,6 +26,7 @@ import lombok.extern.java.Log;
import org.springframework.beans.BeanUtils; import org.springframework.beans.BeanUtils;
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.transaction.annotation.Transactional;
import org.springframework.util.Assert; import org.springframework.util.Assert;
import org.springframework.util.CollectionUtils; import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils; import org.springframework.util.StringUtils;
...@@ -84,6 +87,7 @@ public class BlogServiceImpl extends ServiceImpl<BlogMapper, Blog> implements IB ...@@ -84,6 +87,7 @@ public class BlogServiceImpl extends ServiceImpl<BlogMapper, Blog> implements IB
* @throws BlogCategoryServiceException * @throws BlogCategoryServiceException
*/ */
@Override @Override
@Transactional(rollbackFor = Exception.class)
public void publishBlog(Blog blog, String content, List<String> topicIds, List<String> tagIds) throws BlogCategoryServiceException { public void publishBlog(Blog blog, String content, List<String> topicIds, List<String> tagIds) throws BlogCategoryServiceException {
//如果为不可查看则默认为不可评论和转载 //如果为不可查看则默认为不可评论和转载
...@@ -111,23 +115,33 @@ public class BlogServiceImpl extends ServiceImpl<BlogMapper, Blog> implements IB ...@@ -111,23 +115,33 @@ public class BlogServiceImpl extends ServiceImpl<BlogMapper, Blog> implements IB
//4.判断是否引用话题 //4.判断是否引用话题
if (!CollectionUtils.isEmpty(topicIds)) { if (!CollectionUtils.isEmpty(topicIds)) {
// 获取要引用的话题,不正确的话题ID会被自动过滤
List<Topic> topicList = topicService.listByIds(topicIds);
List<String> existsTopicIds = topicList.stream().map(Topic::getTopicId).collect(Collectors.toList());
if (!CollectionUtils.isEmpty(existsTopicIds)) {
List<TopicReference> topicReferenceList = new ArrayList<>(); List<TopicReference> topicReferenceList = new ArrayList<>();
for (String topicId : topicIds) { for (String topicId : existsTopicIds) {
TopicReference topicReference = new TopicReference(); TopicReference topicReference = new TopicReference();
topicReference.setTopicId(topicId); topicReference.setTopicId(topicId);
topicReference.setReferencerId(blog.getBlogId()); topicReference.setReferencerId(blog.getBlogId());
topicReference.setReferencerType(0); topicReference.setReferencerType(0);
topicReferenceList.add(topicReference); topicReferenceList.add(topicReference);
} }
// 批量保存 // 批量保存
topicReferenceService.saveBatch(topicReferenceList); topicReferenceService.saveBatch(topicReferenceList);
// 更新话题引用次数
existsTopicIds.forEach(topicId -> {
topicService.increaseReferenceCount(topicId, 1);
});
}
} }
//5.判断是否引用标签 //5.判断是否引用标签
if (!CollectionUtils.isEmpty(tagIds)) { if (!CollectionUtils.isEmpty(tagIds)) {
List<String> existsTagIds = tagService.listByIds(tagIds).stream().map(Tag::getTagId).collect(Collectors.toList());
if (!CollectionUtils.isEmpty(existsTagIds)) {
List<BlogTag> blogTagList = new ArrayList<>(); List<BlogTag> blogTagList = new ArrayList<>();
for (String tagId : tagIds) { for (String tagId : existsTagIds) {
BlogTag blogTag = new BlogTag(); BlogTag blogTag = new BlogTag();
blogTag.setBlogId(blog.getBlogId()); blogTag.setBlogId(blog.getBlogId());
blogTag.setTagId(tagId); blogTag.setTagId(tagId);
...@@ -136,6 +150,7 @@ public class BlogServiceImpl extends ServiceImpl<BlogMapper, Blog> implements IB ...@@ -136,6 +150,7 @@ public class BlogServiceImpl extends ServiceImpl<BlogMapper, Blog> implements IB
blogTagService.saveBatch(blogTagList); blogTagService.saveBatch(blogTagList);
} }
} }
}
/** /**
* 删除博客 * 删除博客
......
...@@ -64,5 +64,19 @@ public interface ITopicService extends IService<Topic> { ...@@ -64,5 +64,19 @@ public interface ITopicService extends IService<Topic> {
*/ */
IPage<User> getTopicsFollower(Integer page, Integer size, String topicId); IPage<User> getTopicsFollower(Integer page, Integer size, String topicId);
/**
* 获取话题分页
* @param page
* @param size
* @param userId
* @return
*/
IPage<Topic> getTopicPage(Integer page, Integer size, String userId); IPage<Topic> getTopicPage(Integer page, Integer size, String userId);
/**
* 增加话题引用次数
* @param topicId
* @param count
*/
void increaseReferenceCount(String topicId, int count);
} }
...@@ -185,5 +185,13 @@ public class TopicServiceImpl extends ServiceImpl<TopicMapper, Topic> implements ...@@ -185,5 +185,13 @@ public class TopicServiceImpl extends ServiceImpl<TopicMapper, Topic> implements
return page(page, topicQueryWrapper); return page(page, topicQueryWrapper);
} }
@Override
public void increaseReferenceCount(String topicId, int count) {
Topic topic = getById(topicId);
if (null != topic) {
topic.setReferenceCount(topic.getReferenceCount() + count);
updateById(topic);
}
}
} }
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