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

3 4
import com.tanpu.common.api.CommonResp;
import com.tanpu.common.exception.BizException;
刘基明's avatar
刘基明 committed
5
import com.tanpu.community.api.beans.qo.ThemeAnalysDO;
刘基明's avatar
刘基明 committed
6
import com.tanpu.community.api.beans.qo.TopicRankQo;
7
import com.tanpu.community.api.beans.vo.feign.fatools.UserInfoNew;
刘基明's avatar
刘基明 committed
8 9
import com.tanpu.community.api.enums.CollectionTypeEnum;
import com.tanpu.community.api.enums.TopicStatusEnum;
刘基明's avatar
刘基明 committed
10
import com.tanpu.community.api.enums.VisitTypeEnum;
11
import com.tanpu.community.cache.RedisCache;
刘基明's avatar
刘基明 committed
12 13
import com.tanpu.community.dao.entity.community.ThemeEntity;
import com.tanpu.community.dao.entity.community.TopicEntity;
14
import com.tanpu.community.feign.fatools.FeignClientForFatools;
刘基明's avatar
刘基明 committed
15
import com.tanpu.community.util.ConvertUtil;
刘基明's avatar
刘基明 committed
16
import org.apache.commons.collections4.CollectionUtils;
17
import org.apache.commons.lang3.StringUtils;
刘基明's avatar
刘基明 committed
18 19 20
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

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

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

刘基明's avatar
刘基明 committed
27 28 29 30 31 32 33 34 35 36 37 38 39
@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
40 41 42
    @Autowired
    private FeignService feignService;

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

    @Resource
    private FeignClientForFatools feignClientForFatools;

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

52

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


刘基明's avatar
刘基明 committed
57 58 59
    /**
     * 计算主题热度排行
     */
刘基明's avatar
刘基明 committed
60
    public void rankThemes() {
61
        //7天内所有主题进行热度值排序
刘基明's avatar
刘基明 committed
62
        List<ThemeEntity> themeEntities = themeService.queryRecentdays(7);
刘基明's avatar
刘基明 committed
63
        List<ThemeAnalysDO> themeAnalysDOS = ConvertUtil.themeEntityToAnalysDOs(themeEntities);
刘基明's avatar
刘基明 committed
64 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);
        Map<String, Integer> visitCountMap = visitSummaryService.getCountMapByThemeIds(themeIds, VisitTypeEnum.THEME_PAGE_VIEW);
刘基明's avatar
刘基明 committed
71 72
        for (ThemeAnalysDO theme : themeAnalysDOS) {
            String themeId = theme.getThemeId();
刘基明's avatar
刘基明 committed
73 74 75 76 77
            Integer likeCount = likeCountMap.getOrDefault(themeId, 0);
            Integer bookCount = bookCountMap.getOrDefault(themeId,0);
            Integer commentCount = commentCountMap.getOrDefault(themeId,0);
            Integer forwardCount = forwardCountMap.getOrDefault(themeId,0);
            Integer viewCount = visitCountMap.getOrDefault(themeId,0);
刘基明's avatar
刘基明 committed
78 79 80 81 82
            theme.setCommentCount(commentCount);
            theme.setLikeCount(likeCount);
            theme.setForwardCount(forwardCount);
            theme.setCollectCount(bookCount);
            theme.setViewCount(viewCount);
83 84
            //查询用户质量
            String authorId = theme.getAuthorId();
刘基明's avatar
刘基明 committed
85
            UserInfoNew authorInfo = redisCache.getObject(StringUtils.joinWith(CACHE_FEIGN_USER_INFO, authorId),
张辰's avatar
张辰 committed
86
                    60 * 10, () -> feignService.getUserInfoById(authorId), UserInfoNew.class);
87 88 89
            if (authorInfo == null || authorInfo.getLevelGrade() == null) {
                theme.setUserWeight(0.0);
            } else {
张辰's avatar
张辰 committed
90
                // 设置用户权重
91 92 93
                theme.setUserWeight(authorInfo.getLevelGrade() * 1.0);

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

103

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

    }

刘基明's avatar
刘基明 committed
143 144
    /**
     * 最新和热门主题集合
145
     *
刘基明's avatar
刘基明 committed
146 147 148 149
     * @param hotCount
     * @param newCount
     * @return
     */
150
    public List<String> getHotAndNewThemes(Integer hotCount, Integer newCount, String userId) {
张辰's avatar
张辰 committed
151
        Set<String> hotThemeIds = this.hotestThemes.stream().limit(hotCount)
152
                .filter(o -> !userId.equals(o.getAuthorId()))
刘基明's avatar
刘基明 committed
153 154
                .map(ThemeAnalysDO::getThemeId)
                .collect(Collectors.toSet());
刘基明's avatar
刘基明 committed
155 156 157 158 159 160
        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
161 162
    /**
     * 话题详情
163
     *
刘基明's avatar
刘基明 committed
164 165 166
     * @param topicId 话题Id
     * @return
     */
167 168
    public TopicRankQo getTopicDetail(String topicId) {
        if (this.rankTopicList.size() == 0) {
刘基明's avatar
刘基明 committed
169 170
            rankTopics();
        }
刘基明's avatar
刘基明 committed
171
        List<TopicRankQo> matchTopic = this.rankTopicList.stream().filter(o -> topicId.equals(o.getTopicId())).limit(1).collect(Collectors.toList());
刘基明's avatar
刘基明 committed
172 173 174 175 176
        matchTopic.add(new TopicRankQo());
        return matchTopic.get(0);
    }


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

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

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

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

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