TopicManager.java 9.85 KB
package com.tanpu.community.manager;

import com.tanpu.common.exception.BizException;
import com.tanpu.community.api.beans.qo.TopicDetailQo;
import com.tanpu.community.api.beans.qo.TopicHotQo;
import com.tanpu.community.api.beans.qo.TopicTitileQo;
import com.tanpu.community.api.beans.req.page.Page;
import com.tanpu.community.api.beans.req.topic.TopicSearchReq;
import com.tanpu.community.api.beans.vo.TopicDTO;
import com.tanpu.community.api.beans.vo.TopicDataAnalysDTO;
import com.tanpu.community.api.constants.RedisKeyConstant;
import com.tanpu.community.api.enums.CollectionTypeEnum;
import com.tanpu.community.dao.entity.community.TopicEntity;
import com.tanpu.community.service.*;
import com.tanpu.community.util.ConvertUtil;
import com.tanpu.community.util.PageUtils;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashSet;
import java.util.List;
import java.util.Set;

@Service
public class TopicManager {

    @Autowired
    private TopicService topicService;

    @Autowired
    private RedisService redisService;

    @Autowired
    private ThemeService themeService;

    @Autowired
    private CollectionService collectionService;

    @Autowired
    private CommentService commentService;

    @Autowired
    private VisitSummaryService visitSummaryService;
    @Autowired
    private RankService rankService;

    //新增话题
    public void insertTopic(String topicTitle, String userId) {
        if (topicService.queryByTitile(topicTitle) == null) {
            topicService.addTopic(topicTitle, userId);
        }
        return;

    }

    //话题详情列表
    public List<TopicDTO> getAllTopicDetail() {
        return ConvertUtil.topicEntitiesToDTOs(topicService.queryAll());
    }


    //话题简介列表
    public Page<TopicTitileQo> getAllTopicBriefInfo(TopicSearchReq req) {
        List<TopicEntity> allTopic = topicService.queryByKeyword(req.getSearchKeyword());
        List<TopicTitileQo> topicTitileQos = ConvertUtil.topicEntitiesToBriefDTOs(allTopic);

        for (TopicTitileQo topicQo : topicTitileQos) {
            //讨论数=发布主题贴数+回复总数
            List<String> themeIds = themeService.queryThemeIdsByTopic(topicQo.getTopicId());
            Integer commentCount = commentService.getCommentCountByThemeIds(themeIds);
            topicQo.setDiscussionCount(String.valueOf(themeIds.size() + commentCount));
            //浏览量
            Integer topicPV = visitSummaryService.queryTopicDetailVisit(topicQo.getTopicId());
            Integer themePV = visitSummaryService.queryThemeVisit(themeIds);
            topicQo.setViewCount(String.valueOf(topicPV + themePV));
        }
        //TODO 判断顶置
        return PageUtils.page(req.getPage(), topicTitileQos);
    }

    //话题详情页
    public TopicDetailQo getDetail(String topicId) {
        TopicEntity topicEntity = topicService.queryById(topicId);
        if (topicEntity == null) {
            throw new BizException("找不到话题,id:" + topicId);
        }
        TopicDetailQo topicDetailQo = new TopicDetailQo();
        BeanUtils.copyProperties(topicEntity, topicDetailQo);
        List<String> themeIds = themeService.queryThemeIdsByTopic(topicId);
        if (CollectionUtils.isEmpty(themeIds)) {
            topicDetailQo.setViewCount(visitSummaryService.queryTopicDetailVisit(topicId));
            topicDetailQo.setDisscussCount(0);
            return topicDetailQo;
        }

        //浏览量
        Integer topicPV = visitSummaryService.queryTopicDetailVisit(topicId);
        Integer themePV = visitSummaryService.queryThemeVisit(themeIds);
        topicDetailQo.setViewCount(topicPV + themePV);
        //讨论数=发布主题贴数+回复总数
        Integer commentCount = commentService.getCommentCountByThemeIds(themeIds);
        topicDetailQo.setDisscussCount(themeIds.size() + commentCount);
        return topicDetailQo;
    }


    public List<TopicHotQo> getHotTopicTitles() {
//        List<TopicEntity> topicEntities = topicService.queryAll();
//        List<TopicHotQo> topicHotQos = ConvertUtil.topicEntityToHotQos(topicEntities);
//        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));
//        map.entrySet().stream().sorted(Comparator.)
//
//        //TODO 添加类型:热 新 顶
//        topicHotQos.get(0).setType(1);
//        topicHotQos.get(2).setType(2);
//        return topicHotQos;
        return  rankService.rankTopics();
    }

    public void setTopTopic(String topicId, boolean setTop) {
        TopicEntity topicEntity = topicService.queryById(topicId);
        if (topicEntity == null) {
            throw new BizException("找不到话题,id:" + topicId);
        }
        if (setTop) {
            topicService.updateTopicToTop(topicId);
        } else {
            topicService.updateTopicNotTop(topicId);
        }
    }

    public void setTopicConceal(String topicId, boolean setConceal) {
        TopicEntity topicEntity = topicService.queryById(topicId);
        if (topicEntity == null) {
            throw new BizException("找不到话题,id:" + topicId);
        }
        if (setConceal) {
            topicService.updateTopicToConceal(topicId);
        } else {
            topicService.updateTopicNotConceal(topicId);
        }
    }


    public void modifyViewCount(String topicId, Long modifyMount) {
        TopicEntity topicEntity = topicService.queryById(topicId);
        if (topicEntity == null) {
            throw new BizException("找不到话题,id:" + topicId);
        }
        topicService.modifyViewCount(topicId, modifyMount);
        if (modifyMount > 0) {
            redisService.incr(RedisKeyConstant.TOPIC_TOTAL_VIEW_COUNT_ + topicId, modifyMount);
        } else {
            redisService.decr(RedisKeyConstant.TOPIC_TOTAL_VIEW_COUNT_ + topicId, -modifyMount);
        }

    }


    public void refreshRedisCache() {
        List<TopicEntity> topicEntities = topicService.queryAll();
        for (TopicEntity topic : topicEntities) {
            String topicId = topic.getTopicId();
            Long viewCountModify = topic.getviewCntAdjust();
            List<String> themeIds = themeService.queryThemeIdsByTopic(topicId);
            Integer likeCountByThemeIds = collectionService.getCountByTypeAndIds(themeIds, CollectionTypeEnum.LIKE_THEME);
            Integer bookCountByThemeIds = collectionService.getCountByTypeAndIds(themeIds, CollectionTypeEnum.COLLECT_THEME);
            Long commentCountByThemeIds = (long) commentService.getCommentCountByThemeIds(themeIds);
            Set<String> postUsers = themeService.getPostUserCount(themeIds);
            Set<String> commentUsers = commentService.getCommentUserCount(themeIds);
            HashSet<String> totalUsers = new HashSet<>(postUsers);
            totalUsers.addAll(commentUsers);
            redisService.set(RedisKeyConstant.TOPIC_LIKE_COUNT_ + topicId, likeCountByThemeIds);
            redisService.set(RedisKeyConstant.TOPIC_BOOK_COUNT_ + topicId, bookCountByThemeIds);
            redisService.set(RedisKeyConstant.TOPIC_COMMENT_COUNT_ + topicId, commentCountByThemeIds);


            redisService.set(RedisKeyConstant.TOPIC_TOTAL_VIEW_COUNT_ + topicId, commentCountByThemeIds + themeIds.size());
            redisService.set(RedisKeyConstant.TOPIC_THEME_COUNT_ + topicId, commentCountByThemeIds + themeIds.size());
            redisService.set(RedisKeyConstant.TOPIC_DISCUSS_COUNT_ + topicId, commentCountByThemeIds + themeIds.size());
            redisService.set(RedisKeyConstant.TOPIC_POST_USER_COUNT_ + topicId, (long) postUsers.size());
            redisService.set(RedisKeyConstant.TOPIC_COMMENT_USER_COUNT_ + topicId, (long) commentUsers.size());
            redisService.set(RedisKeyConstant.TOPIC_TOTAL_USER_COUNT_ + topicId, (long) totalUsers.size());


        }

    }

    //后台管理:返回数据分析
    public TopicDataAnalysDTO queryDataAnalysis(String topicId) {
        TopicDataAnalysDTO topicDataAnalysDTO = new TopicDataAnalysDTO();
        TopicEntity topicEntity = topicService.queryById(topicId);
        if (topicEntity == null) {
            throw new BizException("话题未找到,id:" + topicId);
        }
        this.refreshRedisCache();
        topicDataAnalysDTO.setId(topicId);
        topicDataAnalysDTO.setTopicTitle(topicEntity.getTopicTitle());
        topicDataAnalysDTO.setCommentCount(redisService.getInteger(RedisKeyConstant.TOPIC_COMMENT_COUNT_ + topicId));
        topicDataAnalysDTO.setPosterCount(redisService.getInteger(RedisKeyConstant.TOPIC_POST_USER_COUNT_ + topicId));
        topicDataAnalysDTO.setReplIierCount(redisService.getInteger(RedisKeyConstant.TOPIC_COMMENT_USER_COUNT_ + topicId));
        topicDataAnalysDTO.setThemeCount(redisService.getInteger(RedisKeyConstant.TOPIC_THEME_COUNT_ + topicId));
        topicDataAnalysDTO.setUserTotalCount(redisService.getInteger(RedisKeyConstant.TOPIC_TOTAL_USER_COUNT_ + topicId));
        topicDataAnalysDTO.setViewTotalCount(redisService.getInteger(RedisKeyConstant.TOPIC_TOTAL_VIEW_COUNT_ + topicId));
        topicDataAnalysDTO.setViewPageCount(redisService.getInteger(RedisKeyConstant.TOPIC_PAGE_VIEW_COUNT_ + topicId));
        return topicDataAnalysDTO;
    }
}