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

import com.tanpu.common.api.CommonResp;
import com.tanpu.common.auth.UserHolder;
import com.tanpu.common.constant.ErrorCodeConstant;
import com.tanpu.community.api.beans.qo.DiscussionAeraQo;
import com.tanpu.community.api.beans.qo.TopicFollowQo;
import com.tanpu.community.api.beans.qo.TopicPageDetailQo;
import com.tanpu.community.api.beans.qo.TopicRankQo;
import com.tanpu.community.api.beans.req.page.Page;
import com.tanpu.community.api.beans.req.topic.FollowTopicReq;
import com.tanpu.community.api.beans.req.topic.TopicDiscussionReq;
import com.tanpu.community.api.beans.req.topic.TopicSearchReq;
import com.tanpu.community.api.beans.vo.feign.fatools.UserInfoResp;
import com.tanpu.community.api.enums.OperationTypeEnum;
import com.tanpu.community.api.enums.TopicSpecialPermissionEnum;
import com.tanpu.community.dao.entity.community.ThemeEntity;
import com.tanpu.community.dao.entity.community.TopicEntity;
import com.tanpu.community.dao.mapper.community.TopicFollowRelMapper;
import com.tanpu.community.service.FeignService;
import com.tanpu.community.service.RankService;
import com.tanpu.community.service.ThemeService;
import com.tanpu.community.service.TopicService;
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.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Service
public class TopicManager {


    @Resource
    private TopicService topicService;
    @Resource
    private RankService rankService;

    @Resource
    private UserHolder userHolder;

    @Resource
    TopicFollowRelMapper topicFollowRelMapper;
    @Resource
    private ThemeService themeService;
    @Resource
    private FeignService feignService;

    // 首页-话题标签
    public List<TopicRankQo> getTop4TopicTitles() {
        List<TopicRankQo> rankTopicListTop4 = rankService.getRankTopicListTop4();
        //检查权限
        topicService.batchCheckPermission(rankTopicListTop4, userHolder.getUserId());

        return rankTopicListTop4;
    }

    // 话题列表
    public Page<TopicRankQo> getAllTopicRankInfo(TopicSearchReq req) {
        // 全量排序,内存分页
        List<TopicRankQo> topicList = rankService.getRankTopicList(req.getSearchKeyword());
        Page<TopicRankQo> result = PageUtils.page(req.getPage(), topicList);
        // 添加权限
        topicService.batchCheckPermission(result.getContent(), userHolder.getUserId());
        return result;
    }

    // 关键字搜索话题(前2个)
    public List<TopicRankQo> getTopicRankList(String keyword) {
        // 全量排序,内存分页
        List<TopicRankQo> topicList = rankService.getRankTopicList(keyword);
        if (CollectionUtils.isEmpty(topicList)) return topicList;
        // 只取前两条
        if (topicList.size() > 2) topicList = topicList.subList(0, 2);
        // 添加权限
        topicService.batchCheckPermission(topicList, userHolder.getUserId());
        return topicList;
    }

    /**
     * 关注话题(讨论区)列表
     *
     * @param keyword
     */

    public List<TopicFollowQo> getFollowTopicList(String keyword) {

        String userId = userHolder.getUserId();
        // 查库
        List<TopicFollowQo> topicFollowQos = topicService.queryFollowTopic(keyword, userId);
        // 先从缓存中获取浏览量
        Map<String, TopicRankQo> topicMap = rankService.getRankTopicList(null).stream().collect(Collectors.toMap(TopicRankQo::getTopicId, o -> o, (a, b) -> a));

        topicFollowQos.stream().forEach(o -> {
            if (topicMap.containsKey(o.getTopicId())) {
                TopicRankQo topicRankQo = topicMap.get(o.getTopicId());
                BeanUtils.copyProperties(topicRankQo, o);
                o.setHasPermission(true);
            }
        });


        // 最新讨论
        themeService.queryCommentForTopic(topicFollowQos, userId);

        // 排序
        List<TopicFollowQo> res = topicFollowQos.stream().filter(o -> o.checkTopicName(keyword)).sorted(Comparator.comparing(TopicFollowQo::getSpecialPermission, Comparator.reverseOrder()).
                thenComparing(TopicFollowQo::getLastThemeSecond, Comparator.nullsFirst(Long::compareTo).reversed()))
                .collect(Collectors.toList());

        return res;
    }

    /**
     * 话题详情页
     *
     * @param topicId
     * @return
     */
    public CommonResp<TopicPageDetailQo> getDetail(String topicId) {


        TopicEntity topicEntity = topicService.queryOnlineTopicById(topicId);
        if (topicEntity == null) {
            return CommonResp.error(ErrorCodeConstant.TOPIC_NOT_FOUND.getCode(), "抱歉!该话题已下线。");
        }

        // 检验权限
        if (TopicSpecialPermissionEnum.TRUE.getCode().equals(topicEntity.getSpecialPermission()) &&
                !topicService.checkPermission(topicId, userHolder.getUserId())) {
            return CommonResp.error(ErrorCodeConstant.TOPIC_PERMISSION_ABORT.getCode(), topicService.getPermissionToast(topicId));
        }

        // DATA Transform
        TopicRankQo topicDetail = rankService.getTopicDetail(topicId);
        TopicPageDetailQo result = new TopicPageDetailQo();
        BeanUtils.copyProperties(topicDetail, result);

        // 查询管理员
        result.setManagers(topicService.queryManagerNames(result.getTopicId()));
        // 查询关联产品
        topicService.queryAttachments(result);
        // 是否关注
        result.setHasFollow(topicService.checkFollow(result.getTopicId(), userHolder.getUserId()));


        return CommonResp.success(result);
    }


    /**
     * 关注/取关 话题
     *
     * @param req
     * @param userId
     * @return
     */
    public CommonResp<Void> followTopic(FollowTopicReq req, String userId) {

        // 检验话题权限是否专属
        TopicEntity topicEntity = topicService.queryOnlineTopicById(req.getTopicId());
        if (topicEntity == null) {
            return CommonResp.error(ErrorCodeConstant.TOPIC_NOT_FOUND.getCode(), "抱歉!该话题已下线。");
        }

        if (TopicSpecialPermissionEnum.TRUE.getCode().equals(topicEntity.getSpecialPermission())) {
            return CommonResp.error(ErrorCodeConstant.ILLEGAL_ARGEMENT.getCode(), "专属话题不支持关注");
        }

        if (OperationTypeEnum.CONFIRM.getCode().equals(req.getType())) {
            topicService.addFollowTopic(req.getTopicId(), userId);
        } else if (OperationTypeEnum.CANCEL.getCode().equals(req.getType())) {
            topicService.deleteFollowTopic(req.getTopicId(), userId);
        }
        return CommonResp.success();
    }

    public DiscussionAeraQo getForum(TopicDiscussionReq req) {
        TopicEntity topicEntity = topicService.queryRelateTopic(req);
        // 没有关联话题,或者已下线
        if (topicEntity == null) return null;

        DiscussionAeraQo result = new DiscussionAeraQo();

        List<TopicRankQo> rankTopicList = rankService.getRankTopicList(topicEntity.getTopicTitle());

        List<TopicRankQo> collect = rankTopicList.stream().filter(o -> o.getTopicId().equals(topicEntity.getTopicId())).collect(Collectors.toList());

        BeanUtils.copyProperties(collect.get(0), result);

        if (result.getDisscussCount() != null && result.getDisscussCount() > 99) {
            result.setFormatDisscussCount("99+");
        }

        // 最新讨论
        List<ThemeEntity> themeEntities = themeService.queryNewestByTopic(topicEntity.getTopicId(), 0, 3, Collections.emptyList());
        // 查询用户信息
        List<String> authorIds = new ArrayList<>(themeEntities.stream().map(ThemeEntity::getAuthorId).collect(Collectors.toSet()));
        List<UserInfoResp> queryUsersListNew = feignService.getUserList(authorIds);

        Map<String, UserInfoResp> nameMap = queryUsersListNew.stream().collect(Collectors.toMap(UserInfoResp::getUserId, o -> o));

        result.setThemes(ConvertUtil.themeEntity2Discussion(themeEntities, nameMap));

        return result;
    }
}