RankService.java 6.12 KB
Newer Older
刘基明's avatar
刘基明 committed
1 2 3
package com.tanpu.community.service;

import com.tanpu.community.api.beans.qo.ThemeAnalysDO;
刘基明's avatar
刘基明 committed
4
import com.tanpu.community.api.beans.qo.TopicRankQo;
刘基明's avatar
刘基明 committed
5 6 7 8 9
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;
刘基明's avatar
刘基明 committed
10
import org.apache.commons.collections4.CollectionUtils;
刘基明's avatar
刘基明 committed
11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29
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;

30
    //最热
刘基明's avatar
刘基明 committed
31
    private List<ThemeAnalysDO> rankThemeList = new ArrayList<>();
刘基明's avatar
刘基明 committed
32

33

刘基明's avatar
刘基明 committed
34 35
    private List<TopicRankQo> rankTopicList = new ArrayList<>();
    private List<TopicRankQo> rankTopicListTop4 = new ArrayList<>();
刘基明's avatar
刘基明 committed
36 37


刘基明's avatar
刘基明 committed
38 39 40
    /**
     * 计算主题热度排行
     */
刘基明's avatar
刘基明 committed
41
    public void rankThemes() {
42 43
        //7天内所有主题进行热度值排序
        List<ThemeEntity> themeEntities = themeService.queryRecentdays(7);
刘基明's avatar
刘基明 committed
44 45 46 47 48 49 50 51 52 53 54 55 56 57
        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);
        }
刘基明's avatar
刘基明 committed
58 59 60
        //打分
        Map<ThemeAnalysDO, Double> map = themeAnalysDOS.stream().collect(Collectors.toMap(o -> o, ThemeAnalysDO::getScore));
        //排序
刘基明's avatar
刘基明 committed
61
        rankThemeList = map.entrySet().stream().sorted(Map.Entry.comparingByValue(Comparator.reverseOrder())).map(e -> e.getKey()).collect(Collectors.toList());
刘基明's avatar
刘基明 committed
62 63 64

    }

65

刘基明's avatar
刘基明 committed
66 67 68 69 70 71
    /**
     * 计算话题热度
     *
     * @return
     */
    public void rankTopics() {
刘基明's avatar
刘基明 committed
72
        List<TopicEntity> topicEntities = topicService.queryAll();
刘基明's avatar
刘基明 committed
73 74
        List<TopicRankQo> topicRankQos = ConvertUtil.topicEntityToHotQos(topicEntities);
        if (topicRankQos.size() == 0) {
刘基明's avatar
刘基明 committed
75
            return;
刘基明's avatar
刘基明 committed
76
        }
刘基明's avatar
刘基明 committed
77
        for (TopicRankQo topic : topicRankQos) {
刘基明's avatar
刘基明 committed
78
            List<String> themeIds = themeService.queryThemeIdsByTopic(topic.getTopicId());
刘基明's avatar
刘基明 committed
79 80 81 82 83
            if (CollectionUtils.isEmpty(themeIds)){
                topic.setViewCount(0);
                topic.setDisscussCount(0);
                continue;
            }
刘基明's avatar
刘基明 committed
84 85 86 87 88 89 90 91
            //浏览量
            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);
        }
刘基明's avatar
刘基明 committed
92 93
        Map<TopicRankQo, Integer> map = topicRankQos.stream().collect(Collectors.toMap(o -> o, TopicRankQo::getRank));
        List<TopicRankQo> rankList = map.entrySet().stream().sorted(Map.Entry.comparingByValue(Comparator.reverseOrder())).map(Map.Entry::getKey).collect(Collectors.toList());
刘基明's avatar
刘基明 committed
94
        rankList.get(0).setType(TopicStatusEnum.HOTTEST.getCode());
刘基明's avatar
刘基明 committed
95 96
        this.rankTopicList = rankList;
        this.rankTopicListTop4 = rankList.stream().limit(4).collect(Collectors.toList());
刘基明's avatar
刘基明 committed
97
        return;
刘基明's avatar
刘基明 committed
98 99 100

    }

刘基明's avatar
刘基明 committed
101 102 103 104 105 106
    /**
     * 最新和热门主题集合
     * @param hotCount
     * @param newCount
     * @return
     */
刘基明's avatar
刘基明 committed
107 108 109 110 111
    public List<String> getHotAndNewThemes(Integer hotCount, Integer newCount,String userId) {
        Set<String> hotThemeIds = this.rankThemeList.stream().limit(hotCount)
                .filter(o->!userId.equals(o.getAuthorId()))
                .map(ThemeAnalysDO::getThemeId)
                .collect(Collectors.toSet());
刘基明's avatar
刘基明 committed
112 113 114 115 116 117
        Set<String> newThemeIds = themeService.selectExcludeUser(null, null, newCount)
                .stream().map(ThemeEntity::getThemeId).collect(Collectors.toSet());
        hotThemeIds.addAll(newThemeIds);
        return new ArrayList<>(hotThemeIds);
    }

刘基明's avatar
刘基明 committed
118 119 120 121 122 123 124 125 126
    /**
     * 话题详情
     * @param topicId 话题Id
     * @return
     */
    public TopicRankQo getTopicDetail(String topicId){
        if (this.rankTopicList.size()==0){
            rankTopics();
        }
刘基明's avatar
刘基明 committed
127
        List<TopicRankQo> matchTopic = this.rankTopicList.stream().filter(o -> topicId.equals(o.getTopicId())).limit(1).collect(Collectors.toList());
刘基明's avatar
刘基明 committed
128 129 130 131 132
        matchTopic.add(new TopicRankQo());
        return matchTopic.get(0);
    }


133

刘基明's avatar
刘基明 committed
134 135 136 137 138 139 140 141 142

    public List<TopicRankQo> getRankTopicList() {
        if (this.rankTopicList.size()==0){
            this.rankTopics();
        }
        return rankTopicList;
    }

    public List<TopicRankQo> getRankTopicListTop4() {
刘基明's avatar
刘基明 committed
143 144 145
        if (this.rankTopicList.size()==0){
            this.rankTopics();
        }
刘基明's avatar
刘基明 committed
146 147
        return rankTopicListTop4;
    }
刘基明's avatar
刘基明 committed
148

149 150 151 152 153 154 155
    public List<ThemeAnalysDO> getRankThemeList() {
        if (this.rankThemeList.size()==0){
            rankThemes();
        }
        return rankThemeList;
    }

刘基明's avatar
刘基明 committed
156
    public List<String> getRankThemeListByTopic(String topicId) {
刘基明's avatar
刘基明 committed
157 158 159
        if (this.rankThemeList.size()==0){
            this.rankThemes();
        }
刘基明's avatar
刘基明 committed
160 161 162
        return rankThemeList.stream().filter(o->topicId.equals(o.getTopicId()))
                .map(ThemeAnalysDO::getThemeId)
                .collect(Collectors.toList());
刘基明's avatar
刘基明 committed
163
    }
刘基明's avatar
刘基明 committed
164
}