RankService.java 4.29 KB
Newer Older
刘基明's avatar
刘基明 committed
1 2 3 4 5 6 7 8 9
package com.tanpu.community.service;

import com.tanpu.community.api.beans.qo.ThemeAnalysDO;
import com.tanpu.community.api.beans.qo.TopicHotQo;
import com.tanpu.community.api.enums.CollectionTypeEnum;
import com.tanpu.community.api.enums.TopicStatusEnum;
import com.tanpu.community.dao.entity.community.ThemeEntity;
import com.tanpu.community.dao.entity.community.TopicEntity;
import com.tanpu.community.util.ConvertUtil;
刘基明's avatar
刘基明 committed
10
import org.apache.commons.collections4.CollectionUtils;
刘基明's avatar
刘基明 committed
11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;
import java.util.stream.Collectors;

@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
31
    public List<String> rankThemeList = new ArrayList<>();
刘基明's avatar
刘基明 committed
32

刘基明's avatar
刘基明 committed
33
    public List<TopicHotQo> rankTopicList = new ArrayList<>();
刘基明's avatar
刘基明 committed
34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56


    public void rankThemes() {
        List<ThemeEntity> themeEntities = themeService.queryAll();
        List<ThemeAnalysDO> themeAnalysDOS = ConvertUtil.themeEntityToAnalysDOs(themeEntities);
        for (ThemeAnalysDO theme : themeAnalysDOS) {
            String themeId = theme.getThemeId();
            Integer likeCount = collectionService.getCountByTypeAndId(themeId, CollectionTypeEnum.LIKE_THEME);
            Integer bookCount = collectionService.getCountByTypeAndId(themeId, CollectionTypeEnum.COLLECT_THEME);
            Integer commentCount = commentService.getCommentCountByThemeId(themeId);
            Integer forwardCount = themeService.getForwardCountById(themeId);
            Integer viewCount = visitSummaryService.queryThemeVisit(themeId);
            theme.setCommentCount(commentCount);
            theme.setLikeCount(likeCount);
            theme.setForwardCount(forwardCount);
            theme.setCollectCount(bookCount);
            theme.setViewCount(viewCount);
        }
        Map<ThemeAnalysDO, Double> map = themeAnalysDOS.stream().collect(Collectors.toMap(o -> o, ThemeAnalysDO::getRank));
        rankThemeList = map.entrySet().stream().sorted(Map.Entry.comparingByValue(Comparator.reverseOrder())).map(e -> e.getKey().getThemeId()).collect(Collectors.toList());

    }

刘基明's avatar
刘基明 committed
57 58 59 60 61 62
    /**
     * 计算话题热度
     *
     * @return
     */
    public void rankTopics() {
刘基明's avatar
刘基明 committed
63 64 65
        List<TopicEntity> topicEntities = topicService.queryAll();
        List<TopicHotQo> topicHotQos = ConvertUtil.topicEntityToHotQos(topicEntities);
        if (topicHotQos.size() == 0) {
刘基明's avatar
刘基明 committed
66
            return;
刘基明's avatar
刘基明 committed
67 68 69
        }
        for (TopicHotQo topic : topicHotQos) {
            List<String> themeIds = themeService.queryThemeIdsByTopic(topic.getTopicId());
刘基明's avatar
刘基明 committed
70 71 72 73 74
            if (CollectionUtils.isEmpty(themeIds)){
                topic.setViewCount(0);
                topic.setDisscussCount(0);
                continue;
            }
刘基明's avatar
刘基明 committed
75 76 77 78 79 80 81 82 83
            //浏览量
            Integer topicPV = visitSummaryService.queryTopicDetailVisit(topic.getTopicId());
            Integer themePV = visitSummaryService.queryThemeVisit(themeIds);
            topic.setViewCount(topicPV + themePV);
            //讨论数=发布主题贴数+回复总数
            Integer commentCount = commentService.getCommentCountByThemeIds(themeIds);
            topic.setDisscussCount(themeIds.size() + commentCount);
        }
        Map<TopicHotQo, Integer> map = topicHotQos.stream().collect(Collectors.toMap(o -> o, TopicHotQo::getRank));
刘基明's avatar
刘基明 committed
84
        List<TopicHotQo> rankList = map.entrySet().stream().sorted(Map.Entry.comparingByValue(Comparator.reverseOrder())).map(e -> e.getKey()).limit(4).collect(Collectors.toList());
刘基明's avatar
刘基明 committed
85
        rankList.get(0).setType(TopicStatusEnum.HOTTEST.getCode());
刘基明's avatar
刘基明 committed
86 87
        rankTopicList = rankList;
        return;
刘基明's avatar
刘基明 committed
88 89 90

    }

刘基明's avatar
刘基明 committed
91 92
    public List<String> getHotAndNewThemes(Integer hotCount, Integer newCount) {
        Set<String> hotThemeIds = this.rankThemeList.stream().limit(hotCount).collect(Collectors.toSet());
刘基明's avatar
刘基明 committed
93 94 95 96 97 98 99
        Set<String> newThemeIds = themeService.selectExcludeUser(null, null, newCount)
                .stream().map(ThemeEntity::getThemeId).collect(Collectors.toSet());
        hotThemeIds.addAll(newThemeIds);
        return new ArrayList<>(hotThemeIds);
    }

}