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.UserInfoNew;
刘基明'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 36 37
@Service
public class RankService {
    @Autowired
    private ThemeService themeService;
    @Autowired
    private CollectionService collectionService;
    @Autowired
    private CommentService commentService;
    @Autowired
    private TopicService topicService;
    @Autowired
    private VisitSummaryService visitSummaryService;

张辰'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 61 62
        if (CollectionUtils.isEmpty(themeEntities)){
            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 71
        Map<String, Integer> visitCountMap = visitSummaryService.getCountMapByTargetIds(themeIds, VisitTypeEnum.THEME_PAGE_VIEW);

刘基明's avatar
刘基明 committed
72 73
        for (ThemeAnalysDO theme : themeAnalysDOS) {
            String themeId = theme.getThemeId();
刘基明's avatar
刘基明 committed
74 75 76 77 78
            theme.setCommentCount(commentCountMap.getOrDefault(themeId,0));
            theme.setLikeCount(likeCountMap.getOrDefault(themeId, 0));
            theme.setForwardCount(forwardCountMap.getOrDefault(themeId,0));
            theme.setCollectCount(bookCountMap.getOrDefault(themeId,0));
            theme.setViewCount(visitCountMap.getOrDefault(themeId,0));
79 80
            //查询用户质量
            String authorId = theme.getAuthorId();
刘基明's avatar
刘基明 committed
81
            UserInfoNew authorInfo = redisCache.getObject(StringUtils.joinWith(CACHE_FEIGN_USER_INFO, authorId),
张辰's avatar
张辰 committed
82
                    60 * 10, () -> feignService.getUserInfoById(authorId), UserInfoNew.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 112
        List<String> topicIds = topicRankQos.stream().map(TopicRankQo::getTopicId).collect(Collectors.toList());
        Map<String, Integer> countMapByTargetIds = visitSummaryService.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 117 118 119
                topic.setViewCount(0);
                topic.setDisscussCount(0);
                continue;
            }
张辰's avatar
张辰 committed
120
            // 浏览量
刘基明's avatar
刘基明 committed
121
            Integer topicPV = countMapByTargetIds.getOrDefault(topic.getTopicId(),0);
刘基明's avatar
刘基明 committed
122 123 124
            Integer themePV = visitSummaryService.queryThemeVisit(themeIds);
            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 141
    /**
     * 最新和热门主题集合
142
     *
刘基明's avatar
刘基明 committed
143 144 145 146
     * @param hotCount
     * @param newCount
     * @return
     */
147 148 149 150 151 152 153 154 155 156
//    public List<String> getHotAndNewThemes(Integer hotCount, Integer newCount, String userId) {
//        Set<String> hotThemeIds = this.hotestThemes.stream().limit(hotCount)
//                .filter(o -> !userId.equals(o.getAuthorId()))
//                .map(ThemeAnalysDO::getThemeId)
//                .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);
//    }
刘基明's avatar
刘基明 committed
157

刘基明's avatar
刘基明 committed
158 159
    /**
     * 话题详情
160
     *
刘基明's avatar
刘基明 committed
161 162 163
     * @param topicId 话题Id
     * @return
     */
164 165
    public TopicRankQo getTopicDetail(String topicId) {
        if (this.rankTopicList.size() == 0) {
刘基明's avatar
刘基明 committed
166 167
            rankTopics();
        }
刘基明's avatar
刘基明 committed
168
        List<TopicRankQo> matchTopic = this.rankTopicList.stream().filter(o -> topicId.equals(o.getTopicId())).limit(1).collect(Collectors.toList());
刘基明's avatar
刘基明 committed
169 170 171 172 173
        matchTopic.add(new TopicRankQo());
        return matchTopic.get(0);
    }


刘基明's avatar
刘基明 committed
174
    public List<TopicRankQo> getRankTopicList(String keyword) {
175
        if (this.rankTopicList.size() == 0) {
刘基明's avatar
刘基明 committed
176 177
            this.rankTopics();
        }
刘基明's avatar
刘基明 committed
178
        if (StringUtils.isEmpty(keyword)) {
刘基明's avatar
刘基明 committed
179
            return rankTopicList;
刘基明's avatar
刘基明 committed
180
        } else {
刘基明's avatar
刘基明 committed
181
            //过滤关键字
刘基明's avatar
刘基明 committed
182
            return this.rankTopicList.stream().filter(o -> o.getTopicTitle().contains(keyword)).collect(Collectors.toList());
刘基明's avatar
刘基明 committed
183
        }
刘基明's avatar
刘基明 committed
184 185 186
    }

    public List<TopicRankQo> getRankTopicListTop4() {
187
        if (this.rankTopicList.size() == 0) {
刘基明's avatar
刘基明 committed
188 189
            this.rankTopics();
        }
刘基明's avatar
刘基明 committed
190 191
        return rankTopicListTop4;
    }
刘基明's avatar
刘基明 committed
192

张辰's avatar
张辰 committed
193 194
    public List<ThemeAnalysDO> getHotestThemes() {
        if (this.hotestThemes.size() == 0) {
195 196
            rankThemes();
        }
张辰's avatar
张辰 committed
197
        return hotestThemes;
198 199
    }

刘基明's avatar
刘基明 committed
200
    public List<String> getRankThemeListByTopic(String topicId) {
张辰's avatar
张辰 committed
201
        if (this.hotestThemes.size() == 0) {
刘基明's avatar
刘基明 committed
202 203
            this.rankThemes();
        }
张辰's avatar
张辰 committed
204
        return hotestThemes.stream().filter(o -> topicId.equals(o.getTopicId()))
刘基明's avatar
刘基明 committed
205 206
                .map(ThemeAnalysDO::getThemeId)
                .collect(Collectors.toList());
刘基明's avatar
刘基明 committed
207
    }
刘基明's avatar
刘基明 committed
208

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