RankService.java 8.28 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.BizUtils;
刘基明's avatar
刘基明 committed
14
import com.tanpu.community.util.ConvertUtil;
刘基明's avatar
刘基明 committed
15
import org.apache.commons.collections4.CollectionUtils;
16
import org.apache.commons.lang3.StringUtils;
刘基明's avatar
刘基明 committed
17 18 19
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

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

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

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

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

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

    @Resource
    private FeignClientForFatools feignClientForFatools;

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

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


刘基明's avatar
刘基明 committed
55 56 57
    /**
     * 计算主题热度排行
     */
刘基明's avatar
刘基明 committed
58
    public void rankThemes() {
59
        //7天内所有主题进行热度值排序
刘基明's avatar
刘基明 committed
60
        List<ThemeEntity> themeEntities = themeService.queryRecentdays(7);
刘基明's avatar
刘基明 committed
61
        if (CollectionUtils.isEmpty(themeEntities)) {
刘基明's avatar
刘基明 committed
62 63
            return;
        }
刘基明's avatar
刘基明 committed
64
        List<ThemeAnalysDO> themeAnalysDOS = ConvertUtil.themeEntityToAnalysDOs(themeEntities);
刘基明's avatar
刘基明 committed
65 66 67 68 69 70
        //批量查询
        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
71
        Map<String, Integer> visitCountMap = visitLogService.getCountMapByTargetIds(themeIds, VisitTypeEnum.THEME_PAGE_VIEW);
刘基明's avatar
刘基明 committed
72

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

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

100

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

    }

刘基明's avatar
刘基明 committed
149
    /**
刘基明's avatar
刘基明 committed
150
     * 从排序列表中返回话题详情
151
     *
刘基明's avatar
刘基明 committed
152 153 154
     * @param topicId 话题Id
     * @return
     */
155 156
    public TopicRankQo getTopicDetail(String topicId) {
        if (this.rankTopicList.size() == 0) {
刘基明's avatar
刘基明 committed
157 158
            rankTopics();
        }
刘基明's avatar
刘基明 committed
159
        List<TopicRankQo> matchTopic = this.rankTopicList.stream().filter(o -> topicId.equals(o.getTopicId())).limit(1).collect(Collectors.toList());
刘基明's avatar
刘基明 committed
160 161 162 163 164
        matchTopic.add(new TopicRankQo());
        return matchTopic.get(0);
    }


刘基明's avatar
刘基明 committed
165
    public List<TopicRankQo> getRankTopicList(String keyword) {
166
        if (this.rankTopicList.size() == 0) {
刘基明's avatar
刘基明 committed
167 168
            this.rankTopics();
        }
刘基明's avatar
刘基明 committed
169
        if (StringUtils.isEmpty(keyword)) {
刘基明's avatar
刘基明 committed
170
            return rankTopicList;
刘基明's avatar
刘基明 committed
171
        } else {
刘基明's avatar
刘基明 committed
172
            //过滤关键字
刘基明's avatar
刘基明 committed
173
            return this.rankTopicList.stream().filter(o -> o.getTopicTitle().contains(keyword)).collect(Collectors.toList());
刘基明's avatar
刘基明 committed
174
        }
刘基明's avatar
刘基明 committed
175 176 177
    }

    public List<TopicRankQo> getRankTopicListTop4() {
178
        if (this.rankTopicList.size() == 0) {
刘基明's avatar
刘基明 committed
179 180
            this.rankTopics();
        }
刘基明's avatar
刘基明 committed
181 182
        return rankTopicListTop4;
    }
刘基明's avatar
刘基明 committed
183

张辰's avatar
张辰 committed
184 185
    public List<ThemeAnalysDO> getHotestThemes() {
        if (this.hotestThemes.size() == 0) {
186 187
            rankThemes();
        }
张辰's avatar
张辰 committed
188
        return hotestThemes;
189 190
    }

张辰's avatar
张辰 committed
191
    public List<String> getRankThemeListByTopic(String topicId, List<String> excludeIds) {
张辰's avatar
张辰 committed
192
        if (this.hotestThemes.size() == 0) {
刘基明's avatar
刘基明 committed
193 194
            this.rankThemes();
        }
张辰's avatar
张辰 committed
195 196 197

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

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