package com.tanpu.community.manager; import com.tanpu.common.auth.UserHolder; import com.tanpu.common.exception.BizException; import com.tanpu.community.api.beans.qo.TopicRankQo; import com.tanpu.community.api.beans.req.page.Page; import com.tanpu.community.api.beans.req.topic.TopicSearchReq; import com.tanpu.community.api.beans.vo.TopicDataAnalysDTO; import com.tanpu.community.api.constants.RedisKeyConstant; import com.tanpu.community.api.enums.CollectionTypeEnum; import com.tanpu.community.api.enums.VisitTypeEnum; import com.tanpu.community.dao.entity.community.TopicEntity; import com.tanpu.community.service.*; import com.tanpu.community.util.PageUtils; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; import javax.annotation.Resource; import java.util.HashSet; import java.util.List; import java.util.Set; @Service public class TopicManager { @Autowired private TopicService topicService; @Autowired private RedisService redisService; @Autowired private ThemeService themeService; @Autowired private CollectionService collectionService; @Autowired private CommentService commentService; @Autowired private VisitSummaryService visitSummaryService; @Autowired private RankService rankService; @Resource private UserHolder userHolder; //新增话题 public void insertTopic(String topicTitle, String userId) { if (topicService.queryByTitile(topicTitle) == null) { throw new BizException("话题名称已存在:" + topicTitle); } topicService.addTopic(topicTitle, userId); } //首页-话题标签 public List<TopicRankQo> getTop4TopicTitles() { return rankService.getRankTopicListTop4(); } //话题搜索列表 public Page<TopicRankQo> getAllTopicBriefInfo(TopicSearchReq req) { return PageUtils.page(req.getPage(), rankService.getRankTopicList()); } //话题详情页 public TopicRankQo getDetail(String topicId) { //话题详情 visitSummaryService.addPageView(userHolder.getUserId(), topicId, VisitTypeEnum.TOPIC_PAGE_VIEW); return rankService.getTopicDetail(topicId); } public void refreshRedisCache() { List<TopicEntity> topicEntities = topicService.queryAll(); for (TopicEntity topic : topicEntities) { String topicId = topic.getTopicId(); Long viewCountModify = topic.getViewCntAdjust(); List<String> themeIds = themeService.queryThemeIdsByTopic(topicId); Integer likeCountByThemeIds = collectionService.getCountByTypeAndIds(themeIds, CollectionTypeEnum.LIKE_THEME); Integer bookCountByThemeIds = collectionService.getCountByTypeAndIds(themeIds, CollectionTypeEnum.COLLECT_THEME); Long commentCountByThemeIds = (long) commentService.getCommentCountByThemeIds(themeIds); Set<String> postUsers = themeService.getPostUserCount(themeIds); Set<String> commentUsers = commentService.getCommentUserCount(themeIds); HashSet<String> totalUsers = new HashSet<>(postUsers); totalUsers.addAll(commentUsers); redisService.set(RedisKeyConstant.TOPIC_LIKE_COUNT_ + topicId, likeCountByThemeIds); redisService.set(RedisKeyConstant.TOPIC_BOOK_COUNT_ + topicId, bookCountByThemeIds); redisService.set(RedisKeyConstant.TOPIC_COMMENT_COUNT_ + topicId, commentCountByThemeIds); redisService.set(RedisKeyConstant.TOPIC_TOTAL_VIEW_COUNT_ + topicId, commentCountByThemeIds + themeIds.size()); redisService.set(RedisKeyConstant.TOPIC_THEME_COUNT_ + topicId, commentCountByThemeIds + themeIds.size()); redisService.set(RedisKeyConstant.TOPIC_DISCUSS_COUNT_ + topicId, commentCountByThemeIds + themeIds.size()); redisService.set(RedisKeyConstant.TOPIC_POST_USER_COUNT_ + topicId, (long) postUsers.size()); redisService.set(RedisKeyConstant.TOPIC_COMMENT_USER_COUNT_ + topicId, (long) commentUsers.size()); redisService.set(RedisKeyConstant.TOPIC_TOTAL_USER_COUNT_ + topicId, (long) totalUsers.size()); } } //返回数据分析 public TopicDataAnalysDTO queryDataAnalysis(String topicId) { TopicDataAnalysDTO topicDataAnalysDTO = new TopicDataAnalysDTO(); TopicEntity topicEntity = topicService.queryById(topicId); if (topicEntity == null) { throw new BizException("话题未找到,id:" + topicId); } this.refreshRedisCache(); topicDataAnalysDTO.setId(topicId); topicDataAnalysDTO.setTopicTitle(topicEntity.getTopicTitle()); topicDataAnalysDTO.setCommentCount(redisService.getInteger(RedisKeyConstant.TOPIC_COMMENT_COUNT_ + topicId)); topicDataAnalysDTO.setPosterCount(redisService.getInteger(RedisKeyConstant.TOPIC_POST_USER_COUNT_ + topicId)); topicDataAnalysDTO.setReplIierCount(redisService.getInteger(RedisKeyConstant.TOPIC_COMMENT_USER_COUNT_ + topicId)); topicDataAnalysDTO.setThemeCount(redisService.getInteger(RedisKeyConstant.TOPIC_THEME_COUNT_ + topicId)); topicDataAnalysDTO.setUserTotalCount(redisService.getInteger(RedisKeyConstant.TOPIC_TOTAL_USER_COUNT_ + topicId)); topicDataAnalysDTO.setViewTotalCount(redisService.getInteger(RedisKeyConstant.TOPIC_TOTAL_VIEW_COUNT_ + topicId)); topicDataAnalysDTO.setViewPageCount(redisService.getInteger(RedisKeyConstant.TOPIC_PAGE_VIEW_COUNT_ + topicId)); return topicDataAnalysDTO; } }