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

张辰's avatar
张辰 committed
3
import com.tanpu.biz.common.enums.clue.PageEnum;
张辰's avatar
张辰 committed
4 5
import com.tanpu.biz.common.enums.community.CollectionTypeEnum;
import com.tanpu.biz.common.enums.community.TopicStatusEnum;
刘基明's avatar
刘基明 committed
6
import com.tanpu.community.api.beans.qo.ThemeAnalysDO;
刘基明's avatar
刘基明 committed
7
import com.tanpu.community.api.beans.qo.TopicRankQo;
8
import com.tanpu.community.api.beans.vo.feign.fatools.UserInfoResp;
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;
刘基明's avatar
刘基明 committed
12
import com.tanpu.community.util.BizUtils;
刘基明's avatar
刘基明 committed
13
import com.tanpu.community.util.ConvertUtil;
刘基明's avatar
刘基明 committed
14
import com.tanpu.community.util.TimeUtils;
刘基明's avatar
刘基明 committed
15
import org.apache.commons.collections4.CollectionUtils;
16
import org.apache.commons.lang3.StringUtils;
刘基明's avatar
刘基明 committed
17
import org.springframework.beans.factory.annotation.Autowired;
刘基明's avatar
刘基明 committed
18
import org.springframework.beans.factory.annotation.Value;
刘基明's avatar
刘基明 committed
19 20
import org.springframework.stereotype.Service;

21
import javax.annotation.Resource;
刘基明's avatar
刘基明 committed
22 23 24 25 26
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;
import java.util.Map;
刘基明's avatar
刘基明 committed
27 28
import java.util.stream.Collectors;

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

刘基明's avatar
刘基明 committed
31 32
@Service
public class RankService {
刘基明's avatar
刘基明 committed
33

刘基明's avatar
刘基明 committed
34 35 36 37 38 39 40 41 42 43 44 45 46 47
    @Value("${rank.theme.viewRate:0.1}")
    public Double viewRate;
    @Value("${rank.theme.forwardRate:3}")
    public Double forwardRate;
    @Value("${rank.theme.commentRate:2}")
    public Double commentRate;
    @Value("${rank.theme.likeRaten:3}")
    public Double likeRaten;
    @Value("${rank.theme.collectRate:3}")
    public Double collectRate;
    //用户质量权重
    @Value("${rank.theme.userWeightRate:0.9}")
    public Double userWeightRate;
    //初始质量
刘基明's avatar
刘基明 committed
48 49
    @Value("${rank.theme.initialWeight:1.0}")
    public Double initialWeight;
刘基明's avatar
刘基明 committed
50
    //时间系数
刘基明's avatar
刘基明 committed
51
    @Value("${rank.theme.timeRate:0.2}")
刘基明's avatar
刘基明 committed
52 53 54 55
    public Double timeRate;


    @Value("${rank.topic.viewRate:1}")
刘基明's avatar
刘基明 committed
56
    public Double topicViewRate;
刘基明's avatar
刘基明 committed
57
    @Value("${rank.topic.discussRate:3}")
刘基明's avatar
刘基明 committed
58 59 60
    public Double topicDiscussRate;
    @Value("${rank.topic.themeRate:1}")
    public Double topicThemeRate;
刘基明's avatar
刘基明 committed
61
    //时间系数
刘基明's avatar
刘基明 committed
62 63
    @Value("${rank.theme.timeRate:1}")
    public Double topicTimeRate;
刘基明's avatar
刘基明 committed
64

刘基明's avatar
刘基明 committed
65 66 67 68 69 70 71 72 73
    @Autowired
    private ThemeService themeService;
    @Autowired
    private CollectionService collectionService;
    @Autowired
    private CommentService commentService;
    @Autowired
    private TopicService topicService;
    @Autowired
刘基明's avatar
刘基明 committed
74
    private VisitLogService visitLogService;
刘基明's avatar
刘基明 committed
75

张辰's avatar
张辰 committed
76 77 78
    @Autowired
    private FeignService feignService;

79 80 81 82
    @Autowired
    private RedisCache redisCache;

    @Resource
刘基明's avatar
刘基明 committed
83
    private RankLogService rankLogService;
84

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

刘基明's avatar
刘基明 committed
88 89
    private List<TopicRankQo> rankTopicList = new ArrayList<>();
    private List<TopicRankQo> rankTopicListTop4 = new ArrayList<>();
刘基明's avatar
刘基明 committed
90 91


刘基明's avatar
刘基明 committed
92 93 94
    /**
     * 计算主题热度排行
     */
刘基明's avatar
刘基明 committed
95
    public void rankThemes() {
刘基明's avatar
刘基明 committed
96 97

        LocalDateTime start = LocalDateTime.now();
98
        //7天内所有主题进行热度值排序
刘基明's avatar
刘基明 committed
99
        List<ThemeEntity> themeEntities = themeService.queryRecentdays(7);
刘基明's avatar
刘基明 committed
100
        if (CollectionUtils.isEmpty(themeEntities)) {
刘基明's avatar
刘基明 committed
101 102
            return;
        }
刘基明's avatar
刘基明 committed
103
        List<ThemeAnalysDO> themeAnalysDOS = ConvertUtil.themeEntityToAnalysDOs(themeEntities);
刘基明's avatar
刘基明 committed
104 105 106 107 108 109
        //批量查询
        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
110
        Map<String, Integer> visitCountMap = visitLogService.getCountMapByTargetIds(themeIds, PageEnum.COMM_VISIT_THEME.getId());
刘基明's avatar
刘基明 committed
111

刘基明's avatar
刘基明 committed
112 113
        for (ThemeAnalysDO theme : themeAnalysDOS) {
            String themeId = theme.getThemeId();
刘基明's avatar
刘基明 committed
114
            theme.setCommentCount(commentCountMap.getOrDefault(themeId, 0));
刘基明's avatar
刘基明 committed
115
            theme.setLikeCount(likeCountMap.getOrDefault(themeId, 0));
刘基明's avatar
刘基明 committed
116 117 118
            theme.setForwardCount(forwardCountMap.getOrDefault(themeId, 0));
            theme.setCollectCount(bookCountMap.getOrDefault(themeId, 0));
            theme.setViewCount(visitCountMap.getOrDefault(themeId, 0));
119 120
            //查询用户质量
            String authorId = theme.getAuthorId();
121 122
            UserInfoResp authorInfo = redisCache.getObject(StringUtils.joinWith("_", CACHE_FEIGN_USER_INFO, authorId),
                    60, () -> feignService.getUserInfoById(authorId), UserInfoResp.class);
123 124 125
            if (authorInfo == null || authorInfo.getLevelGrade() == null) {
                theme.setUserWeight(0.0);
            } else {
张辰's avatar
张辰 committed
126
                // 设置用户权重
127 128 129
                theme.setUserWeight(authorInfo.getLevelGrade() * 1.0);

            }
刘基明's avatar
刘基明 committed
130 131
            //打分
            this.calculateThemeScore(theme);
刘基明's avatar
刘基明 committed
132
        }
刘基明's avatar
刘基明 committed
133 134
        //排序
        Map<ThemeAnalysDO, Double> map = themeAnalysDOS.stream().collect(Collectors.toMap(o -> o, ThemeAnalysDO::getScore));
刘基明's avatar
刘基明 committed
135
        //排序
张辰's avatar
张辰 committed
136 137 138
        hotestThemes = map.entrySet().stream()
                .sorted(Map.Entry.comparingByValue(Comparator.reverseOrder()))
                .map(e -> e.getKey()).collect(Collectors.toList());
刘基明's avatar
刘基明 committed
139
        //落库
刘基明's avatar
刘基明 committed
140
        rankLogService.logThemeRank(hotestThemes, start, TimeUtils.calMillisTillNow(start));
141 142
    }

143

刘基明's avatar
刘基明 committed
144 145 146 147 148 149
    /**
     * 计算话题热度
     *
     * @return
     */
    public void rankTopics() {
刘基明's avatar
刘基明 committed
150
        LocalDateTime start = LocalDateTime.now();
刘基明's avatar
刘基明 committed
151
        List<TopicEntity> topicEntities = topicService.queryAll();
刘基明's avatar
刘基明 committed
152
        if (CollectionUtils.isEmpty(topicEntities)) {
刘基明's avatar
刘基明 committed
153 154
            this.rankTopicList = new ArrayList<>();
            this.rankTopicListTop4 = new ArrayList<>();
刘基明's avatar
刘基明 committed
155
            return;
刘基明's avatar
刘基明 committed
156
        }
刘基明's avatar
刘基明 committed
157
        List<TopicRankQo> topicRankQos = ConvertUtil.topicEntityToHotQos(topicEntities);
刘基明's avatar
刘基明 committed
158
        List<String> topicIds = topicRankQos.stream().map(TopicRankQo::getTopicId).collect(Collectors.toList());
159
        Map<String, Integer> topicViewMap = visitLogService.getCountMapByTargetIds(topicIds, PageEnum.COMM_VISIT_TOPIC_DETAIL.getId());
刘基明's avatar
刘基明 committed
160
        for (TopicRankQo topic : topicRankQos) {
刘基明's avatar
刘基明 committed
161
            List<String> themeIds = themeService.queryThemeIdsByTopic(topic.getTopicId());
162
            if (CollectionUtils.isEmpty(themeIds)) {
刘基明's avatar
刘基明 committed
163
                topic.setViewCount(topicViewMap.getOrDefault(topic.getTopicId(), 0));
刘基明's avatar
刘基明 committed
164
                topic.setDisscussCount(0);
刘基明's avatar
刘基明 committed
165
                topic.setThemeWeight(0.0);
刘基明's avatar
刘基明 committed
166 167 168 169 170 171 172 173 174 175 176 177 178
            } else {
                // 浏览量
                Integer topicPV = topicViewMap.getOrDefault(topic.getTopicId(), 0);
                Integer themePV = visitLogService.queryThemeVisit(themeIds);
                topic.setViewCount(topicPV + themePV + topic.getViewCntAdjust());
                //讨论数=发布主题贴数+回复总数
                Integer commentCount = commentService.getTotalCountByThemeIds(themeIds);
                topic.setDisscussCount(themeIds.size() + commentCount);
                //帖子权重,求和
                double themeSum = getHotestThemes().stream().filter(o -> topic.getTopicId().equals(o.getTopicId()))
                        .mapToDouble(ThemeAnalysDO::getScore)
                        .sum();
                topic.setThemeWeight(themeSum);
刘基明's avatar
刘基明 committed
179
            }
180
            // 打分
刘基明's avatar
刘基明 committed
181
            calculateTopicScore(topic);
182
            // 格式化浏览量、讨论量
刘基明's avatar
刘基明 committed
183 184
            topic.setFormatViewCount(BizUtils.formatCountNumber(topic.getViewCount()));
            topic.setFormatDisscussCount(BizUtils.formatCountNumber(topic.getDisscussCount()));
刘基明's avatar
刘基明 committed
185

刘基明's avatar
刘基明 committed
186
        }
187
        // 排序
刘基明's avatar
刘基明 committed
188
        Map<TopicRankQo, Double> map = topicRankQos.stream().collect(Collectors.toMap(o -> o, TopicRankQo::getScore));
张辰's avatar
张辰 committed
189 190 191 192
        List<TopicRankQo> rankList = map.entrySet().stream()
                .sorted(Map.Entry.comparingByValue(Comparator.reverseOrder()))
                .map(Map.Entry::getKey)
                .collect(Collectors.toList());
刘基明's avatar
刘基明 committed
193
        rankList.get(0).setType(TopicStatusEnum.HOTTEST.getCode());
刘基明's avatar
刘基明 committed
194 195
        this.rankTopicList = rankList;
        this.rankTopicListTop4 = rankList.stream().limit(4).collect(Collectors.toList());
刘基明's avatar
刘基明 committed
196 197

        //落库
刘基明's avatar
刘基明 committed
198
        rankLogService.logTopicRank(rankList, start, TimeUtils.calMillisTillNow(start));
刘基明's avatar
刘基明 committed
199
        return;
刘基明's avatar
刘基明 committed
200 201
    }

刘基明's avatar
刘基明 committed
202
    /**
刘基明's avatar
刘基明 committed
203
     * 从排序列表中返回话题详情
204
     *
刘基明's avatar
刘基明 committed
205 206 207
     * @param topicId 话题Id
     * @return
     */
208 209
    public TopicRankQo getTopicDetail(String topicId) {
        if (this.rankTopicList.size() == 0) {
刘基明's avatar
刘基明 committed
210 211
            rankTopics();
        }
刘基明's avatar
刘基明 committed
212
        List<TopicRankQo> matchTopic = this.rankTopicList.stream().filter(o -> topicId.equals(o.getTopicId())).limit(1).collect(Collectors.toList());
刘基明's avatar
刘基明 committed
213 214 215 216 217
        matchTopic.add(new TopicRankQo());
        return matchTopic.get(0);
    }


刘基明's avatar
刘基明 committed
218
    public List<TopicRankQo> getRankTopicList(String keyword) {
219
        if (this.rankTopicList.size() == 0) {
刘基明's avatar
刘基明 committed
220 221
            this.rankTopics();
        }
刘基明's avatar
刘基明 committed
222
        if (StringUtils.isEmpty(keyword)) {
刘基明's avatar
刘基明 committed
223
            return rankTopicList;
刘基明's avatar
刘基明 committed
224
        } else {
刘基明's avatar
刘基明 committed
225
            //过滤关键字
刘基明's avatar
刘基明 committed
226
            return this.rankTopicList.stream().filter(o -> o.getTopicTitle().contains(keyword)).collect(Collectors.toList());
刘基明's avatar
刘基明 committed
227
        }
刘基明's avatar
刘基明 committed
228 229 230
    }

    public List<TopicRankQo> getRankTopicListTop4() {
231
        if (this.rankTopicList.size() == 0) {
刘基明's avatar
刘基明 committed
232 233
            this.rankTopics();
        }
刘基明's avatar
刘基明 committed
234 235
        return rankTopicListTop4;
    }
刘基明's avatar
刘基明 committed
236

张辰's avatar
张辰 committed
237 238
    public List<ThemeAnalysDO> getHotestThemes() {
        if (this.hotestThemes.size() == 0) {
239 240
            rankThemes();
        }
张辰's avatar
张辰 committed
241
        return hotestThemes;
242 243
    }

张辰's avatar
张辰 committed
244
    public List<String> getRankThemeListByTopic(String topicId, List<String> excludeIds) {
张辰's avatar
张辰 committed
245
        if (this.hotestThemes.size() == 0) {
刘基明's avatar
刘基明 committed
246 247
            this.rankThemes();
        }
张辰's avatar
张辰 committed
248 249 250

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

刘基明's avatar
刘基明 committed
255

张辰's avatar
张辰 committed
256 257
    // todo 这里用户层面只考虑了用户的gradelevel,后续可以为用户的历史贴子打分。根据用户发表的历史帖子的质量,为用户评分。例如,历史帖子的关注度,用户的粉丝数量。
    // todo 可以考虑一下话题的热度。
刘基明's avatar
刘基明 committed
258 259 260 261 262 263 264 265
    private void calculateThemeScore(ThemeAnalysDO theme) {
        // 质量=帖子质量+用户质量
        double w = theme.getViewCount() * viewRate
                + theme.getForwardCount() * forwardRate
                + theme.getCommentCount() * commentRate
                + theme.getLikeCount() * likeRaten
                + theme.getCollectCount() * collectRate
                + Math.pow(theme.getUserWeight(), userWeightRate);
266
        double score = (w + initialWeight) / Math.pow(theme.getMinutesTillNow() + 1, timeRate);
刘基明's avatar
刘基明 committed
267 268 269 270 271 272 273 274 275
        theme.setScore(score);
    }


    public void calculateTopicScore(TopicRankQo topic) {
        //顶置话题
        if (topic.getIsTop() > 0) {
            topic.setScore(Double.MAX_VALUE);
        }
刘基明's avatar
刘基明 committed
276
        Double socre = ((topic.getDisscussCount() * topicDiscussRate + topic.getViewCount() * topicViewRate)
277
                / Math.pow(topic.getMinutesTillNow() + 1, topicTimeRate))
刘基明's avatar
刘基明 committed
278
                + topic.getThemeWeight() * topicThemeRate;
刘基明's avatar
刘基明 committed
279 280
        topic.setScore(socre);
    }
刘基明's avatar
刘基明 committed
281
}