RankService.java 4.06 KB
Newer Older
刘基明's avatar
刘基明 committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29
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;
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
30
    public List<String> rankThemeList = new ArrayList<>();
刘基明's avatar
刘基明 committed
31

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


    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
56 57 58 59 60 61
    /**
     * 计算话题热度
     *
     * @return
     */
    public void rankTopics() {
刘基明's avatar
刘基明 committed
62 63 64
        List<TopicEntity> topicEntities = topicService.queryAll();
        List<TopicHotQo> topicHotQos = ConvertUtil.topicEntityToHotQos(topicEntities);
        if (topicHotQos.size() == 0) {
刘基明's avatar
刘基明 committed
65
            return;
刘基明's avatar
刘基明 committed
66 67 68 69 70 71 72 73 74 75 76 77
        }
        for (TopicHotQo topic : topicHotQos) {
            List<String> themeIds = themeService.queryThemeIdsByTopic(topic.getTopicId());
            //浏览量
            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
78
        List<TopicHotQo> rankList = map.entrySet().stream().sorted(Map.Entry.comparingByValue(Comparator.reverseOrder())).map(e -> e.getKey()).limit(4).collect(Collectors.toList());
刘基明's avatar
刘基明 committed
79
        rankList.get(0).setType(TopicStatusEnum.HOTTEST.getCode());
刘基明's avatar
刘基明 committed
80 81
        rankTopicList = rankList;
        return;
刘基明's avatar
刘基明 committed
82 83 84

    }

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

}