package com.tanpu.community.service; import com.tanpu.community.api.beans.qo.ThemeAnalysDO; import com.tanpu.community.api.beans.qo.TopicHotQo; import com.tanpu.community.api.enums.CollectionTypeEnum; import com.tanpu.community.api.enums.TopicStatusEnum; import com.tanpu.community.dao.entity.community.ThemeEntity; import com.tanpu.community.dao.entity.community.TopicEntity; import com.tanpu.community.util.ConvertUtil; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; import java.util.*; import java.util.stream.Collectors; @Service public class RankService { @Autowired private ThemeService themeService; @Autowired private CollectionService collectionService; @Autowired private CommentService commentService; @Autowired private TopicService topicService; @Autowired private VisitSummaryService visitSummaryService; public List<String> rankThemeList = new ArrayList<>(); public List<TopicHotQo> rankTopicList = new ArrayList<>(); public void rankThemes() { List<ThemeEntity> themeEntities = themeService.queryAll(); List<ThemeAnalysDO> themeAnalysDOS = ConvertUtil.themeEntityToAnalysDOs(themeEntities); for (ThemeAnalysDO theme : themeAnalysDOS) { String themeId = theme.getThemeId(); Integer likeCount = collectionService.getCountByTypeAndId(themeId, CollectionTypeEnum.LIKE_THEME); Integer bookCount = collectionService.getCountByTypeAndId(themeId, CollectionTypeEnum.COLLECT_THEME); Integer commentCount = commentService.getCommentCountByThemeId(themeId); Integer forwardCount = themeService.getForwardCountById(themeId); Integer viewCount = visitSummaryService.queryThemeVisit(themeId); theme.setCommentCount(commentCount); theme.setLikeCount(likeCount); theme.setForwardCount(forwardCount); theme.setCollectCount(bookCount); theme.setViewCount(viewCount); } Map<ThemeAnalysDO, Double> map = themeAnalysDOS.stream().collect(Collectors.toMap(o -> o, ThemeAnalysDO::getRank)); rankThemeList = map.entrySet().stream().sorted(Map.Entry.comparingByValue(Comparator.reverseOrder())).map(e -> e.getKey().getThemeId()).collect(Collectors.toList()); } /** * 计算话题热度 * * @return */ public void rankTopics() { List<TopicEntity> topicEntities = topicService.queryAll(); List<TopicHotQo> topicHotQos = ConvertUtil.topicEntityToHotQos(topicEntities); if (topicHotQos.size() == 0) { return; } for (TopicHotQo topic : topicHotQos) { List<String> themeIds = themeService.queryThemeIdsByTopic(topic.getTopicId()); //浏览量 Integer topicPV = visitSummaryService.queryTopicDetailVisit(topic.getTopicId()); Integer themePV = visitSummaryService.queryThemeVisit(themeIds); topic.setViewCount(topicPV + themePV); //讨论数=发布主题贴数+回复总数 Integer commentCount = commentService.getCommentCountByThemeIds(themeIds); topic.setDisscussCount(themeIds.size() + commentCount); } Map<TopicHotQo, Integer> map = topicHotQos.stream().collect(Collectors.toMap(o -> o, TopicHotQo::getRank)); List<TopicHotQo> rankList = map.entrySet().stream().sorted(Map.Entry.comparingByValue(Comparator.reverseOrder())).map(e -> e.getKey()).limit(4).collect(Collectors.toList()); rankList.get(0).setType(TopicStatusEnum.HOTTEST.getCode()); rankTopicList = rankList; return; } public List<String> getHotAndNewThemes(Integer hotCount, Integer newCount) { Set<String> hotThemeIds = this.rankThemeList.stream().limit(hotCount).collect(Collectors.toSet()); Set<String> newThemeIds = themeService.selectExcludeUser(null, null, newCount) .stream().map(ThemeEntity::getThemeId).collect(Collectors.toSet()); hotThemeIds.addAll(newThemeIds); return new ArrayList<>(hotThemeIds); } }