RankService.java 7.71 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;
5
import com.tanpu.community.api.beans.vo.feign.fatools.UserInfoResp;
刘基明's avatar
刘基明 committed
6 7
import com.tanpu.community.api.enums.CollectionTypeEnum;
import com.tanpu.community.api.enums.TopicStatusEnum;
刘基明's avatar
刘基明 committed
8
import com.tanpu.community.api.enums.VisitTypeEnum;
9
import com.tanpu.community.cache.RedisCache;
刘基明's avatar
刘基明 committed
10 11
import com.tanpu.community.dao.entity.community.ThemeEntity;
import com.tanpu.community.dao.entity.community.TopicEntity;
12
import com.tanpu.community.feign.fatools.FeignClientForFatools;
刘基明's avatar
刘基明 committed
13
import com.tanpu.community.util.ConvertUtil;
刘基明's avatar
刘基明 committed
14
import org.apache.commons.collections4.CollectionUtils;
15
import org.apache.commons.lang3.StringUtils;
刘基明's avatar
刘基明 committed
16 17 18
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

19
import javax.annotation.Resource;
刘基明's avatar
刘基明 committed
20 21 22
import java.util.*;
import java.util.stream.Collectors;

刘基明's avatar
刘基明 committed
23
import static com.tanpu.community.api.constants.RedisKeyConstant.CACHE_FEIGN_USER_INFO;
24

刘基明's avatar
刘基明 committed
25 26 27 28 29 30 31 32 33 34 35
@Service
public class RankService {
    @Autowired
    private ThemeService themeService;
    @Autowired
    private CollectionService collectionService;
    @Autowired
    private CommentService commentService;
    @Autowired
    private TopicService topicService;
    @Autowired
刘基明's avatar
刘基明 committed
36
    private VisitLogService visitLogService;
刘基明's avatar
刘基明 committed
37

张辰's avatar
张辰 committed
38 39 40
    @Autowired
    private FeignService feignService;

41 42 43 44 45 46
    @Autowired
    private RedisCache redisCache;

    @Resource
    private FeignClientForFatools feignClientForFatools;

47
    //最热
张辰's avatar
张辰 committed
48
    private List<ThemeAnalysDO> hotestThemes = new ArrayList<>();
刘基明's avatar
刘基明 committed
49

刘基明's avatar
刘基明 committed
50 51
    private List<TopicRankQo> rankTopicList = new ArrayList<>();
    private List<TopicRankQo> rankTopicListTop4 = new ArrayList<>();
刘基明's avatar
刘基明 committed
52 53


刘基明's avatar
刘基明 committed
54 55 56
    /**
     * 计算主题热度排行
     */
刘基明's avatar
刘基明 committed
57
    public void rankThemes() {
58
        //7天内所有主题进行热度值排序
刘基明's avatar
刘基明 committed
59
        List<ThemeEntity> themeEntities = themeService.queryRecentdays(7);
刘基明's avatar
刘基明 committed
60
        if (CollectionUtils.isEmpty(themeEntities)) {
刘基明's avatar
刘基明 committed
61 62
            return;
        }
刘基明's avatar
刘基明 committed
63
        List<ThemeAnalysDO> themeAnalysDOS = ConvertUtil.themeEntityToAnalysDOs(themeEntities);
刘基明's avatar
刘基明 committed
64 65 66 67 68 69
        //批量查询
        List<String> themeIds = themeAnalysDOS.stream().map(ThemeAnalysDO::getThemeId).collect(Collectors.toList());
        Map<String, Integer> likeCountMap = collectionService.getCountMapByType(themeIds, CollectionTypeEnum.LIKE_THEME);
        Map<String, Integer> bookCountMap = collectionService.getCountMapByType(themeIds, CollectionTypeEnum.COLLECT_THEME);
        Map<String, Integer> commentCountMap = commentService.getCountMapByThemeIds(themeIds);
        Map<String, Integer> forwardCountMap = themeService.getForwardCountMap(themeIds);
刘基明's avatar
刘基明 committed
70
        Map<String, Integer> visitCountMap = visitLogService.getCountMapByTargetIds(themeIds, VisitTypeEnum.THEME_PAGE_VIEW);
刘基明's avatar
刘基明 committed
71

刘基明's avatar
刘基明 committed
72 73
        for (ThemeAnalysDO theme : themeAnalysDOS) {
            String themeId = theme.getThemeId();
刘基明's avatar
刘基明 committed
74
            theme.setCommentCount(commentCountMap.getOrDefault(themeId, 0));
刘基明's avatar
刘基明 committed
75
            theme.setLikeCount(likeCountMap.getOrDefault(themeId, 0));
刘基明's avatar
刘基明 committed
76 77 78
            theme.setForwardCount(forwardCountMap.getOrDefault(themeId, 0));
            theme.setCollectCount(bookCountMap.getOrDefault(themeId, 0));
            theme.setViewCount(visitCountMap.getOrDefault(themeId, 0));
79 80
            //查询用户质量
            String authorId = theme.getAuthorId();
81 82
            UserInfoResp authorInfo = redisCache.getObject(StringUtils.joinWith("_", CACHE_FEIGN_USER_INFO, authorId),
                    60, () -> feignService.getUserInfoById(authorId), UserInfoResp.class);
83 84 85
            if (authorInfo == null || authorInfo.getLevelGrade() == null) {
                theme.setUserWeight(0.0);
            } else {
张辰's avatar
张辰 committed
86
                // 设置用户权重
87 88 89
                theme.setUserWeight(authorInfo.getLevelGrade() * 1.0);

            }
刘基明's avatar
刘基明 committed
90
        }
刘基明's avatar
刘基明 committed
91
        //打分
张辰's avatar
张辰 committed
92
        Map<ThemeAnalysDO, Double> map = themeAnalysDOS.stream().collect(Collectors.toMap(o -> o, ThemeAnalysDO::calcScore));
刘基明's avatar
刘基明 committed
93
        //排序
张辰's avatar
张辰 committed
94 95 96
        hotestThemes = map.entrySet().stream()
                .sorted(Map.Entry.comparingByValue(Comparator.reverseOrder()))
                .map(e -> e.getKey()).collect(Collectors.toList());
97 98
    }

99

刘基明's avatar
刘基明 committed
100 101 102 103 104 105
    /**
     * 计算话题热度
     *
     * @return
     */
    public void rankTopics() {
刘基明's avatar
刘基明 committed
106
        List<TopicEntity> topicEntities = topicService.queryAll();
刘基明's avatar
刘基明 committed
107
        if (CollectionUtils.isEmpty(topicEntities)) {
刘基明's avatar
刘基明 committed
108
            return;
刘基明's avatar
刘基明 committed
109
        }
刘基明's avatar
刘基明 committed
110
        List<TopicRankQo> topicRankQos = ConvertUtil.topicEntityToHotQos(topicEntities);
刘基明's avatar
刘基明 committed
111
        List<String> topicIds = topicRankQos.stream().map(TopicRankQo::getTopicId).collect(Collectors.toList());
刘基明's avatar
刘基明 committed
112
        Map<String, Integer> countMapByTargetIds = visitLogService.getCountMapByTargetIds(topicIds, VisitTypeEnum.TOPIC_PAGE_VIEW);
刘基明's avatar
刘基明 committed
113
        for (TopicRankQo topic : topicRankQos) {
刘基明's avatar
刘基明 committed
114
            List<String> themeIds = themeService.queryThemeIdsByTopic(topic.getTopicId());
115
            if (CollectionUtils.isEmpty(themeIds)) {
刘基明's avatar
刘基明 committed
116
                topic.setViewCount(countMapByTargetIds.getOrDefault(topic.getTopicId(), 0));
刘基明's avatar
刘基明 committed
117 118 119
                topic.setDisscussCount(0);
                continue;
            }
张辰's avatar
张辰 committed
120
            // 浏览量
刘基明's avatar
刘基明 committed
121 122
            Integer topicPV = countMapByTargetIds.getOrDefault(topic.getTopicId(), 0);
            Integer themePV = visitLogService.queryThemeVisit(themeIds);
刘基明's avatar
刘基明 committed
123 124
            topic.setViewCount(topicPV + themePV);
            //讨论数=发布主题贴数+回复总数
刘基明's avatar
刘基明 committed
125
            Integer commentCount = commentService.getTotalCountByThemeIds(themeIds);
刘基明's avatar
刘基明 committed
126 127
            topic.setDisscussCount(themeIds.size() + commentCount);
        }
刘基明's avatar
刘基明 committed
128
        Map<TopicRankQo, Integer> map = topicRankQos.stream().collect(Collectors.toMap(o -> o, TopicRankQo::getRank));
张辰's avatar
张辰 committed
129 130 131 132
        List<TopicRankQo> rankList = map.entrySet().stream()
                .sorted(Map.Entry.comparingByValue(Comparator.reverseOrder()))
                .map(Map.Entry::getKey)
                .collect(Collectors.toList());
刘基明's avatar
刘基明 committed
133
        rankList.get(0).setType(TopicStatusEnum.HOTTEST.getCode());
刘基明's avatar
刘基明 committed
134 135
        this.rankTopicList = rankList;
        this.rankTopicListTop4 = rankList.stream().limit(4).collect(Collectors.toList());
刘基明's avatar
刘基明 committed
136
        return;
刘基明's avatar
刘基明 committed
137 138 139

    }

刘基明's avatar
刘基明 committed
140
    /**
刘基明's avatar
刘基明 committed
141
     * 从排序列表中返回话题详情
142
     *
刘基明's avatar
刘基明 committed
143 144 145
     * @param topicId 话题Id
     * @return
     */
146 147
    public TopicRankQo getTopicDetail(String topicId) {
        if (this.rankTopicList.size() == 0) {
刘基明's avatar
刘基明 committed
148 149
            rankTopics();
        }
刘基明's avatar
刘基明 committed
150
        List<TopicRankQo> matchTopic = this.rankTopicList.stream().filter(o -> topicId.equals(o.getTopicId())).limit(1).collect(Collectors.toList());
刘基明's avatar
刘基明 committed
151 152 153 154 155
        matchTopic.add(new TopicRankQo());
        return matchTopic.get(0);
    }


刘基明's avatar
刘基明 committed
156
    public List<TopicRankQo> getRankTopicList(String keyword) {
157
        if (this.rankTopicList.size() == 0) {
刘基明's avatar
刘基明 committed
158 159
            this.rankTopics();
        }
刘基明's avatar
刘基明 committed
160
        if (StringUtils.isEmpty(keyword)) {
刘基明's avatar
刘基明 committed
161
            return rankTopicList;
刘基明's avatar
刘基明 committed
162
        } else {
刘基明's avatar
刘基明 committed
163
            //过滤关键字
刘基明's avatar
刘基明 committed
164
            return this.rankTopicList.stream().filter(o -> o.getTopicTitle().contains(keyword)).collect(Collectors.toList());
刘基明's avatar
刘基明 committed
165
        }
刘基明's avatar
刘基明 committed
166 167 168
    }

    public List<TopicRankQo> getRankTopicListTop4() {
169
        if (this.rankTopicList.size() == 0) {
刘基明's avatar
刘基明 committed
170 171
            this.rankTopics();
        }
刘基明's avatar
刘基明 committed
172 173
        return rankTopicListTop4;
    }
刘基明's avatar
刘基明 committed
174

张辰's avatar
张辰 committed
175 176
    public List<ThemeAnalysDO> getHotestThemes() {
        if (this.hotestThemes.size() == 0) {
177 178
            rankThemes();
        }
张辰's avatar
张辰 committed
179
        return hotestThemes;
180 181
    }

张辰's avatar
张辰 committed
182
    public List<String> getRankThemeListByTopic(String topicId, List<String> excludeIds) {
张辰's avatar
张辰 committed
183
        if (this.hotestThemes.size() == 0) {
刘基明's avatar
刘基明 committed
184 185
            this.rankThemes();
        }
张辰's avatar
张辰 committed
186 187 188

        return hotestThemes.stream()
                .filter(o -> topicId.equals(o.getTopicId()) && !excludeIds.contains(o.getThemeId()))
刘基明's avatar
刘基明 committed
189 190
                .map(ThemeAnalysDO::getThemeId)
                .collect(Collectors.toList());
刘基明's avatar
刘基明 committed
191
    }
刘基明's avatar
刘基明 committed
192

刘基明's avatar
刘基明 committed
193
}