ThemeManager.java 28.8 KB
Newer Older
刘基明's avatar
刘基明 committed
1 2
package com.tanpu.community.manager;

张辰's avatar
张辰 committed
3
import com.fasterxml.jackson.core.type.TypeReference;
刘基明's avatar
刘基明 committed
4
import com.google.common.collect.Sets;
张辰's avatar
张辰 committed
5 6 7
import com.tanpu.biz.common.enums.RelTypeEnum;
import com.tanpu.biz.common.enums.community.CollectionTypeEnum;
import com.tanpu.biz.common.enums.community.ReportTypeEnum;
刘基明's avatar
刘基明 committed
8
import com.tanpu.common.api.CommonResp;
刘基明's avatar
刘基明 committed
9
import com.tanpu.common.constant.ErrorCodeConstant;
刘基明's avatar
刘基明 committed
10
import com.tanpu.common.exception.BizException;
刘基明's avatar
刘基明 committed
11
import com.tanpu.common.util.JsonUtil;
12 13 14 15
import com.tanpu.community.api.beans.qo.ESThemeQo;
import com.tanpu.community.api.beans.qo.FormerThemeQo;
import com.tanpu.community.api.beans.qo.ThemeContentQo;
import com.tanpu.community.api.beans.qo.ThemeQo;
刘基明's avatar
刘基明 committed
16
import com.tanpu.community.api.beans.req.homepage.QueryRecordThemeReq;
刘基明's avatar
刘基明 committed
17 18 19 20 21 22 23
import com.tanpu.community.api.beans.req.theme.CollectThemeReq;
import com.tanpu.community.api.beans.req.theme.CreateThemeReq;
import com.tanpu.community.api.beans.req.theme.ForwardThemeReq;
import com.tanpu.community.api.beans.req.theme.LikeThemeReq;
import com.tanpu.community.api.beans.req.theme.ReportThemeReq;
import com.tanpu.community.api.beans.req.theme.ThemeContentReq;
import com.tanpu.community.api.beans.req.theme.ThemeListReq;
刘基明's avatar
刘基明 committed
24
import com.tanpu.community.api.beans.resp.CreateThemeResp;
张辰's avatar
张辰 committed
25
import com.tanpu.community.api.beans.resp.ThemeFullSearchResp;
26
import com.tanpu.community.api.beans.resp.ThemeListResp;
27
import com.tanpu.community.api.beans.vo.feign.fatools.UserInfoResp;
28
import com.tanpu.community.api.enums.*;
张辰's avatar
张辰 committed
29
import com.tanpu.community.cache.RedisCache;
刘基明's avatar
刘基明 committed
30 31 32 33 34
import com.tanpu.community.dao.entity.community.BlackListEntity;
import com.tanpu.community.dao.entity.community.CollectionEntity;
import com.tanpu.community.dao.entity.community.CommentEntity;
import com.tanpu.community.dao.entity.community.ThemeAttachmentEntity;
import com.tanpu.community.dao.entity.community.ThemeEntity;
刘基明's avatar
刘基明 committed
35
import com.tanpu.community.feign.fatools.FeignClientForFatools;
刘基明's avatar
刘基明 committed
36
import com.tanpu.community.service.*;
张辰's avatar
张辰 committed
37
import com.tanpu.community.service.base.ESService;
张辰's avatar
张辰 committed
38
import com.tanpu.community.util.BizUtils;
39
import com.tanpu.community.util.ConvertUtil;
刘基明's avatar
刘基明 committed
40
import com.tanpu.community.util.RankUtils;
刘基明's avatar
刘基明 committed
41
import com.tanpu.community.util.TencentcloudUtils;
刘基明's avatar
刘基明 committed
42
import com.tanpu.community.util.TimeUtils;
张辰's avatar
张辰 committed
43
import lombok.extern.slf4j.Slf4j;
刘基明's avatar
刘基明 committed
44
import org.apache.commons.collections4.CollectionUtils;
张辰's avatar
张辰 committed
45
import org.apache.commons.collections4.ListUtils;
刘基明's avatar
刘基明 committed
46
import org.apache.commons.lang3.StringUtils;
张辰's avatar
张辰 committed
47
import org.apache.commons.lang3.exception.ExceptionUtils;
刘基明's avatar
刘基明 committed
48
import org.springframework.beans.BeanUtils;
刘基明's avatar
刘基明 committed
49 50
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
刘基明's avatar
刘基明 committed
51
import org.springframework.transaction.annotation.Transactional;
刘基明's avatar
刘基明 committed
52

刘基明's avatar
刘基明 committed
53
import javax.annotation.Resource;
刘基明's avatar
刘基明 committed
54 55 56 57 58 59 60
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
刘基明's avatar
刘基明 committed
61
import java.util.stream.Collectors;
刘基明's avatar
刘基明 committed
62

63
import static com.tanpu.community.api.constants.RedisKeyConstant.*;
张辰's avatar
张辰 committed
64

张辰's avatar
张辰 committed
65
@Slf4j
刘基明's avatar
刘基明 committed
66 67
@Service
public class ThemeManager {
刘基明's avatar
刘基明 committed
68
    @Resource
刘基明's avatar
刘基明 committed
69
    private ThemeService themeService;
刘基明's avatar
刘基明 committed
70 71 72 73 74 75 76 77

    @Autowired
    private CollectionService collectionService;

    @Autowired
    private CommentService commentService;

    @Autowired
刘基明's avatar
刘基明 committed
78
    private FollowRelService followRelService;
刘基明's avatar
刘基明 committed
79

刘基明's avatar
刘基明 committed
80 81 82
    @Autowired
    private BlackListService blackListService;

刘基明's avatar
刘基明 committed
83 84 85
    @Autowired
    private ThemeAttachmentService themeAttachmentService;

刘基明's avatar
刘基明 committed
86
    @Resource
刘基明's avatar
刘基明 committed
87
    private BatchFeignCallService batchFeignCallService;
刘基明's avatar
刘基明 committed
88

刘基明's avatar
刘基明 committed
89
    @Autowired
刘基明's avatar
刘基明 committed
90
    private VisitLogService visitLogService;
刘基明's avatar
刘基明 committed
91

92 93 94
    @Autowired
    private ReportLogService reportLogService;

刘基明's avatar
刘基明 committed
95 96 97
    @Autowired
    private RankService rankService;

张辰's avatar
张辰 committed
98 99 100
    @Autowired
    private ESService esService;

刘基明's avatar
刘基明 committed
101 102 103
    @Autowired
    private FeignClientForFatools feignClientForFatools;

张辰's avatar
张辰 committed
104 105 106
    @Autowired
    private RedisCache redisCache;

107 108 109
    @Autowired
    private RecommendService recommendService;

张辰's avatar
张辰 committed
110 111 112 113
    public ThemeFullSearchResp themeFullSearch(String keyword, Integer pageNo, Integer pageSize, String ident, String userId) {
        List<String> excludeIds;
        if (pageNo > 1) {
            String l = redisCache.get("themeFullSearch_" + ident);
张辰's avatar
张辰 committed
114 115
            excludeIds = StringUtils.isBlank(l) ? new ArrayList<>() : JsonUtil.toBean(l, new TypeReference<List<String>>() {
            });
张辰's avatar
张辰 committed
116 117 118 119
        } else {
            excludeIds = new ArrayList<>();
        }

张辰's avatar
张辰 committed
120
        Integer from = (pageNo - 1) * pageSize;
张辰's avatar
张辰 committed
121
        ThemeFullSearchResp resp = new ThemeFullSearchResp();
张辰's avatar
张辰 committed
122

123 124
        String[] keywords = StringUtils.split(keyword, " ");

张辰's avatar
张辰 committed
125
        // 按时间倒叙查询
126
        List<ESThemeQo> esIds = esService.queryThemeIdByContentAndTitle(keywords, from, pageSize * 5);
张辰's avatar
张辰 committed
127 128
        if (esIds.isEmpty()) {
            return resp;
张辰's avatar
张辰 committed
129 130
        }

张辰's avatar
张辰 committed
131 132 133 134 135 136
        // 排除已经展示过的id
        List<String> filterEsIds = esIds.stream().map(ESThemeQo::getThemeId).filter(tId -> {
            return !excludeIds.contains(tId);
        }).limit(pageSize).collect(Collectors.toList());

        resp.themes = convertEntityToQo(themeService.queryByThemeIds(filterEsIds), userId);
张辰's avatar
张辰 committed
137 138 139 140 141 142
        resp.themes.sort(new Comparator<ThemeQo>() {
            @Override
            public int compare(ThemeQo o1, ThemeQo o2) {
                return o2.createTime.compareTo(o1.createTime);
            }
        });
143 144
        // 截取关键词出现的那一部分段落
        for (ThemeQo theme : resp.themes) {
145
            theme.briefContent4FullSearch = BizUtils.getThemeContent(keywords, theme);
146
        }
张辰's avatar
张辰 committed
147 148

        excludeIds.addAll(resp.themes.stream().map(ThemeQo::getThemeId).collect(Collectors.toList()));
张辰's avatar
张辰 committed
149
        redisCache.put("themeFullSearch_" + ident, excludeIds, 60 * 60 * 6);
张辰's avatar
张辰 committed
150 151

        return resp;
张辰's avatar
张辰 committed
152
    }
刘基明's avatar
刘基明 committed
153

张辰's avatar
张辰 committed
154

刘基明's avatar
刘基明 committed
155
    /**
156
     * 发表主题(修改)
刘基明's avatar
刘基明 committed
157
     */
张辰's avatar
张辰 committed
158
    @Transactional
刘基明's avatar
刘基明 committed
159
    public CreateThemeResp publishTheme(CreateThemeReq req, String userId) {
160 161 162 163
        if (CollectionUtils.isEmpty(req.getContent())) {
            throw new BizException("正文内容不能为空");
        }

刘基明's avatar
刘基明 committed
164
        // 保存主题表
刘基明's avatar
刘基明 committed
165
        ThemeEntity themeEntity = new ThemeEntity();
刘基明's avatar
刘基明 committed
166
        BeanUtils.copyProperties(req, themeEntity);
刘基明's avatar
刘基明 committed
167
        themeEntity.setAuthorId(userId);
刘基明's avatar
刘基明 committed
168
        // 腾讯云敏感词校验
刘基明's avatar
刘基明 committed
169 170
        checkContent(req);
        themeEntity.setContent(JsonUtil.toJson(req.getContent()));
刘基明's avatar
刘基明 committed
171

刘基明's avatar
刘基明 committed
172 173 174
        //附件校验
        checkAttachment(req.getContent());

刘基明's avatar
刘基明 committed
175
        if (StringUtils.isBlank(req.getEditThemeId())) {
刘基明's avatar
刘基明 committed
176
            // 新建
刘基明's avatar
刘基明 committed
177
            themeService.insertTheme(themeEntity);
刘基明's avatar
刘基明 committed
178
        } else {
刘基明's avatar
刘基明 committed
179
            // 修改
刘基明's avatar
刘基明 committed
180
            themeService.update(themeEntity, req.getEditThemeId());
刘基明's avatar
刘基明 committed
181
            themeEntity.setThemeId(req.getEditThemeId());
刘基明's avatar
刘基明 committed
182
            this.evictThemeCache(req.getEditThemeId());
刘基明's avatar
刘基明 committed
183
        }
刘基明's avatar
刘基明 committed
184
        // 保存附件表
刘基明's avatar
刘基明 committed
185
        List<ThemeAttachmentEntity> themeAttachments = ConvertUtil.themeReqToAttachmentList(req, themeEntity.getThemeId());
刘基明's avatar
刘基明 committed
186
        if (StringUtils.isNotEmpty(req.getEditThemeId())) {
刘基明's avatar
刘基明 committed
187
            // 修改需要刪除
刘基明's avatar
刘基明 committed
188
            themeAttachmentService.deleteByThemeId(req.getEditThemeId());
刘基明's avatar
刘基明 committed
189
        }
刘基明's avatar
刘基明 committed
190
        themeAttachmentService.insertList(themeAttachments);
刘基明's avatar
刘基明 committed
191

张辰's avatar
张辰 committed
192 193 194 195 196
        try {
            esService.insertOrUpdateTheme(ConvertUtil.convert(themeEntity));
        } catch (Exception e) {
            log.error("error in save theme to ES. themeId:{}, error:{}", themeEntity.getThemeId(), ExceptionUtils.getStackTrace(e));
        }
刘基明's avatar
刘基明 committed
197

张辰's avatar
张辰 committed
198 199
        redisCache.evict(StringUtils.joinWith("_", CACHE_THEME_ID, themeEntity.getThemeId()));

刘基明's avatar
刘基明 committed
200
        return CreateThemeResp.builder().themeId(themeEntity.getThemeId()).build();
刘基明's avatar
刘基明 committed
201
    }
刘基明's avatar
刘基明 committed
202

刘基明's avatar
刘基明 committed
203 204
    /**
     * 参数校验
刘基明's avatar
刘基明 committed
205
     *
刘基明's avatar
刘基明 committed
206 207
     * @param themeAttachments
     */
刘基明's avatar
刘基明 committed
208
    private void checkAttachment(List<ThemeContentReq> themeAttachments) {
刘基明's avatar
刘基明 committed
209
        for (ThemeContentReq content : themeAttachments) {
刘基明's avatar
刘基明 committed
210
            if (content.getType() == null) {
刘基明's avatar
刘基明 committed
211 212
                throw new IllegalArgumentException("主题内容ThemeContentReq缺少类型");
            }
刘基明's avatar
刘基明 committed
213 214
            if (content.getType().equals(RelTypeEnum.FUND.type)) {
                if (content.getProductType() == null) {
刘基明's avatar
刘基明 committed
215 216 217 218 219 220
                    throw new IllegalArgumentException("附件产品FUND缺少类型");
                }
            }
        }
    }

刘基明's avatar
刘基明 committed
221
    // 转发主题
刘基明's avatar
刘基明 committed
222 223 224 225 226 227 228 229
    public CreateThemeResp forward(ForwardThemeReq req, String userId) {
        ThemeEntity themeEntity = ThemeEntity.builder()
                .content(JsonUtil.toJson(req.getContent()))
                .topicId(req.getTopicId())
                .formerThemeId(req.getFormerThemeId())
                .authorId(userId)
                .themeType(ThemeTypeEnum.FORWARD.getCode())
                .build();
刘基明's avatar
刘基明 committed
230

刘基明's avatar
刘基明 committed
231
        if (StringUtils.isBlank(req.getEditThemeId()) || req.getEditThemeId().equals(req.getFormerThemeId())) {
刘基明's avatar
刘基明 committed
232
            // 新建
刘基明's avatar
刘基明 committed
233 234
            themeService.insertTheme(themeEntity);
        } else {
刘基明's avatar
刘基明 committed
235
            // 修改
刘基明's avatar
刘基明 committed
236 237
            themeService.update(themeEntity, req.getEditThemeId());
            themeEntity.setThemeId(req.getEditThemeId());
刘基明's avatar
刘基明 committed
238
            this.evictThemeCache(req.getEditThemeId());
刘基明's avatar
刘基明 committed
239
        }
刘基明's avatar
刘基明 committed
240 241 242 243 244 245

        try {
            esService.insertOrUpdateTheme(ConvertUtil.convert(themeEntity));
        } catch (Exception e) {
            log.error("error in save theme to ES. themeId:{}, error:{}", themeEntity.getThemeId(), ExceptionUtils.getStackTrace(e));
        }
246 247
        //失效缓存
        redisCache.evict(StringUtils.joinWith("_", THEME_FORWARD_COUNT, themeEntity.getThemeId()));
刘基明's avatar
刘基明 committed
248

刘基明's avatar
刘基明 committed
249 250 251 252
        return CreateThemeResp.builder().themeId(themeEntity.getThemeId()).build();
    }


253
    /**
刘基明's avatar
刘基明 committed
254 255
     * 查询主题列表:推荐/关注/热门/最新
     *
256
     */
刘基明's avatar
刘基明 committed
257
    // 查询主题列表:推荐/关注/热门/最新
刘基明's avatar
刘基明 committed
258
    public ThemeListResp queryList(ThemeListReq req, String userId) {
张辰's avatar
张辰 committed
259 260 261
        List<String> excludeIds;
        if (req.page.pageNumber > 1) {
            String l = redisCache.get("queryThemes_" + req.ident);
张辰's avatar
张辰 committed
262 263
            excludeIds = StringUtils.isBlank(l) ? new ArrayList<>() : JsonUtil.toBean(l, new TypeReference<List<String>>() {
            });
张辰's avatar
张辰 committed
264 265 266 267
        } else {
            excludeIds = new ArrayList<>();
        }

张辰's avatar
张辰 committed
268
        Integer pageStart = (req.page.pageNumber - 1) * req.page.pageSize;
刘基明's avatar
刘基明 committed
269
        Integer pageSize = req.page.pageSize;
270 271

        List<ThemeEntity> themes = new ArrayList<>();
刘基明's avatar
刘基明 committed
272
        // 推荐:由最新,最热和python推荐三个部分组成,默认比例为6,3,1,可在配置文件中配置
刘基明's avatar
刘基明 committed
273
        if (ThemeListTypeEnum.RECOMMEND.getCode().equals(req.getType())) {
刘基明's avatar
刘基明 committed
274

张辰's avatar
张辰 committed
275
            // 需要筛掉用户访问过详情的 & 最近出现在列表页过的.
刘基明's avatar
刘基明 committed
276
            List<String> visitedIds = visitLogService.queryUserRecentVisited(userId);
张辰's avatar
张辰 committed
277
            List<String> excludes = ListUtils.union(excludeIds, visitedIds);
刘基明's avatar
刘基明 committed
278
            List<String> recmdIds = recommendService.getRecommendThemes(pageStart, pageSize, userId, excludes);
张辰's avatar
张辰 committed
279

280
            themes = themeService.queryByThemeIds(recmdIds);
刘基明's avatar
刘基明 committed
281 282

            //排序并去重
刘基明's avatar
刘基明 committed
283 284 285
            themes = RankUtils.sortThemeEntityByIds(themes, recmdIds).stream().limit(pageSize).collect(Collectors.toList());


刘基明's avatar
刘基明 committed
286
        } else if (ThemeListTypeEnum.FOLLOW.getCode().equals(req.getType())) {
张辰's avatar
张辰 committed
287
            // 根据关注列表查询,按时间倒序
刘基明's avatar
刘基明 committed
288
            List<String> fansList = followRelService.queryIdolsByFansId(userId);
张辰's avatar
张辰 committed
289 290
            themes = themeService.queryByUserIdsCreateDesc(fansList, pageStart, pageSize);

张辰's avatar
张辰 committed
291 292 293 294 295 296 297
            if (CollectionUtils.isEmpty(excludeIds) && !themes.isEmpty()) {
                // 说明是从头开始刷,则直接把最新的lastId放到redis中,保留一个月
                Long lastId = themes.stream().map(ThemeEntity::getId).max(Long::compareTo).get();
                redisCache.put(CACHE_IDOL_THEME_LAST_ID + userId, lastId, 60 * 60 * 24 * 7 * 4);
//                visitLogService.addPageView(userId, userId, VisitTypeEnum.FOLLOW_THEME_VIEW);
            }

刘基明's avatar
刘基明 committed
298
        } else if (ThemeListTypeEnum.TOPIC_HOT.getCode().equals(req.getType())) {
张辰's avatar
张辰 committed
299
            // 根据话题查询热门
300 301 302
            if (StringUtils.isEmpty(req.getTopicId())) {
                throw new BizException("TopicId为空");
            }
303

张辰's avatar
张辰 committed
304
            List<String> rankThemeIds = rankService.getRankThemeListByTopic(req.getTopicId(), excludeIds);
张辰's avatar
张辰 committed
305
            rankThemeIds = BizUtils.subList(rankThemeIds, pageStart, pageSize);
306

307 308
            themes = themeService.queryByThemeIds(rankThemeIds);
            themes = RankUtils.sortThemeEntityByIds(themes, rankThemeIds);
张辰's avatar
张辰 committed
309

刘基明's avatar
刘基明 committed
310
        } else if (ThemeListTypeEnum.TOPIC_LATEST.getCode().equals(req.getType())) {
刘基明's avatar
刘基明 committed
311
            //根据话题查询最新
312 313 314
            if (StringUtils.isEmpty(req.getTopicId())) {
                throw new BizException("TopicId为空");
            }
刘基明's avatar
刘基明 committed
315
            themes = themeService.queryNewestByTopic(req.topicId, pageStart, pageSize, excludeIds);
刘基明's avatar
刘基明 committed
316
        }
317

318
        ThemeListResp resp = new ThemeListResp();
刘基明's avatar
刘基明 committed
319
        resp.themes = convertEntityToQo(themes, userId);
320

张辰's avatar
张辰 committed
321
        excludeIds.addAll(resp.themes.stream().map(ThemeQo::getThemeId).collect(Collectors.toList()));
张辰's avatar
张辰 committed
322
        redisCache.put("queryThemes_" + req.ident, excludeIds, 60 * 60 * 6);
张辰's avatar
张辰 committed
323

324
        //组装详情
325
        return resp;
刘基明's avatar
刘基明 committed
326 327
    }

刘基明's avatar
刘基明 committed
328
    // 主题Entity转QO,组装所有信息
329 330 331
    private List<ThemeQo> convertEntityToQo(List<ThemeEntity> themeEntities, String userId) {
        //Entity转Qo
        List<ThemeQo> themeQos = ConvertUtil.themeEntitiesToDTOs(themeEntities);
刘基明's avatar
刘基明 committed
332
        // 批量查询附件detail
333 334 335
        batchFeignCallService.getAttachDetailByBatch(themeQos);
        //其他信息
        for (ThemeQo themeQO : themeQos) {
刘基明's avatar
刘基明 committed
336
            // 通用信息
337
            buildThemeQoExtraInfo(themeQO);
刘基明's avatar
刘基明 committed
338

339
        }
刘基明's avatar
刘基明 committed
340
        // 和用户相关信息
刘基明's avatar
刘基明 committed
341
        buildThemeExtraInfoByUser(userId, themeQos);
刘基明's avatar
刘基明 committed
342

343 344 345
        return themeQos;
    }

刘基明's avatar
刘基明 committed
346
    // 转发对象、点赞、收藏、转发数
347 348
    private void buildThemeQoExtraInfo(ThemeQo themeQo) {
        String themeId = themeQo.getThemeId();
刘基明's avatar
刘基明 committed
349
        // 封装转发对象
刘基明's avatar
刘基明 committed
350
        FormerThemeQo former = redisCache.getObject(StringUtils.joinWith("_", CACHE_FORWARD_THEME_ID, themeQo.getFormerThemeId()), 60,
351 352
                () -> this.getFormerTheme(themeQo.getFormerThemeId()), FormerThemeQo.class);
        themeQo.setFormerTheme(former);
刘基明's avatar
刘基明 committed
353

刘基明's avatar
刘基明 committed
354 355


刘基明's avatar
刘基明 committed
356
        // 点赞,收藏,转发
357 358 359 360 361 362 363 364
        Integer likeCount = redisCache.getObject(StringUtils.joinWith("_", THEME_LIKE_COUNT, themeId), 60,
                () -> collectionService.getCountByTypeAndId(themeId, CollectionTypeEnum.LIKE_THEME), Integer.class);

        Integer commentCount = redisCache.getObject(StringUtils.joinWith("_", THEME_COMMENT_COUNT, themeId), 60,
                () -> commentService.getCommentCountByThemeId(themeId), Integer.class);

        Integer forwardCount = redisCache.getObject(StringUtils.joinWith("_", THEME_FORWARD_COUNT, themeId), 60,
                () -> themeService.getForwardCountById(themeId), Integer.class);
365 366 367
        themeQo.setCommentCount(commentCount);
        themeQo.setLikeCount(likeCount);
        themeQo.setForwardCount(forwardCount);
刘基明's avatar
刘基明 committed
368 369
    }

刘基明's avatar
刘基明 committed
370
    // 组装和当前用户相关信息(单个查询)
371 372
    private void buildThemeExtraInfoByUser(String userId, ThemeQo themeQo) {
        String themeId = themeQo.getThemeId();
刘基明's avatar
刘基明 committed
373
        // 是否关注作者
刘基明's avatar
刘基明 committed
374
        themeQo.setFollow(followRelService.checkFollow(themeQo.getAuthorId(), userId));
刘基明's avatar
刘基明 committed
375
        // 是否点赞
376
        CollectionEntity likeEntity = collectionService.queryCollection(themeId, userId, CollectionTypeEnum.LIKE_THEME);
377
        themeQo.setHasLiked(likeEntity != null);
刘基明's avatar
刘基明 committed
378
        // 是否转发
刘基明's avatar
刘基明 committed
379
        themeQo.setHasForward(themeService.judgeForwardByUser(themeId, userId));
刘基明's avatar
刘基明 committed
380
        // 是否收藏
381
        CollectionEntity collectionEntity = collectionService.queryCollection(themeId, userId, CollectionTypeEnum.COLLECT_THEME);
382 383
        themeQo.setHasCollect(collectionEntity != null);
    }
刘基明's avatar
刘基明 committed
384

刘基明's avatar
刘基明 committed
385 386 387 388
    // 组装和当前用户相关信息(批量查询)
    private void buildThemeExtraInfoByUser(String userId, List<ThemeQo> themeQos) {
        // 批量查询
        List<String> themeIds = themeQos.stream().map(ThemeQo::getThemeId).collect(Collectors.toList());
刘基明's avatar
刘基明 committed
389
        Set<String> idolSet = new HashSet<>(followRelService.queryIdolsByFansId(userId));
刘基明's avatar
刘基明 committed
390 391 392 393 394 395 396 397 398 399 400 401
        Set<String> likeSet = collectionService.getTargets(themeIds, userId, CollectionTypeEnum.LIKE_THEME);
        Set<String> bookSet = collectionService.getTargets(themeIds, userId, CollectionTypeEnum.COLLECT_THEME);
        Set<String> forwardUsers = themeService.getForwardUsers(themeIds);
        // 从set中查找
        for (ThemeQo themeQo : themeQos) {
            themeQo.setFollow(idolSet.contains(themeQo.getAuthorId()));
            themeQo.setHasLiked(likeSet.contains(themeQo.getThemeId()));
            themeQo.setHasCollect(bookSet.contains(themeQo.getThemeId()));
            themeQo.setHasForward(forwardUsers.contains(userId));
        }
    }

刘基明's avatar
刘基明 committed
402 403
    /**
     * 返回用户发布、回复、点赞、收藏的主题列表
404 405 406
     *
     * @param req    查询用户
     * @param userId 当前用户
刘基明's avatar
刘基明 committed
407 408
     * @return
     */
刘基明's avatar
刘基明 committed
409
    public List<ThemeQo> queryThemesByUser(QueryRecordThemeReq req, String userId) {
刘基明's avatar
刘基明 committed
410 411 412 413

        List<ThemeEntity> themeEntities = Collections.emptyList();
        switch (req.getRecordType()) {
            case 1://发布
张辰's avatar
张辰 committed
414
                themeEntities = themeService.queryThemesByUserIdCreateDesc(req.getUserId(), req.getLastId(), req.getPageSize());
刘基明's avatar
刘基明 committed
415
                break;
刘基明's avatar
刘基明 committed
416
            case 2://回复
刘基明's avatar
刘基明 committed
417
                List<ThemeQo> commentThemeList = getCommentThemeQos(req, userId);
刘基明's avatar
刘基明 committed
418
                return commentThemeList;
刘基明's avatar
刘基明 committed
419
            case 3://点赞
420 421
                List<String> likeThemeIds = collectionService.getListByUser(req.getUserId(), CollectionTypeEnum.LIKE_THEME, req.getLastId(), req.getPageSize());
                themeEntities = themeService.queryByThemeIds(likeThemeIds);
422 423
                themeEntities = RankUtils.sortThemeEntityByIds(themeEntities, likeThemeIds);

刘基明's avatar
刘基明 committed
424
                break;
刘基明's avatar
刘基明 committed
425
            case 4://收藏
426
                List<String> collectThemeIds = collectionService.getListByUser(req.getUserId(), CollectionTypeEnum.COLLECT_THEME, req.getLastId(), req.getPageSize());
427 428
                themeEntities = themeService.queryByThemeIds(collectThemeIds, req.getLastId(), req.getPageSize());
                themeEntities = RankUtils.sortThemeEntityByIds(themeEntities, collectThemeIds);
刘基明's avatar
刘基明 committed
429 430 431
                break;
        }
        List<ThemeQo> themeQos = convertEntityToQo(themeEntities, userId);
刘基明's avatar
刘基明 committed
432

刘基明's avatar
刘基明 committed
433 434
        if (userId.equals(req.getUserId()) && !ThemeRecordTypeEnum.COLLECT_LIST.getCode().equals(req.getRecordType())) {
            //如果用户是查询自己的帖子,需要实时查询用户自己的个人信息,防止数据不一致(非收藏类型)
刘基明's avatar
刘基明 committed
435 436 437 438 439
            CommonResp<UserInfoResp> userInfoNewCommonResp = feignClientForFatools.queryUsersListNew(userId);
            if (userInfoNewCommonResp.isNotSuccess()) {
                throw new BizException("内部接口调用失败");
            }
            UserInfoResp user = userInfoNewCommonResp.getData();
440
            themeQos.stream().forEach(o -> reBuildAuthorInfo(o, user));
刘基明's avatar
刘基明 committed
441 442 443
            redisCache.put(StringUtils.joinWith("_", CACHE_FEIGN_USER_INFO, userId), user, 60);
        }

刘基明's avatar
刘基明 committed
444 445 446
        return themeQos;
    }

刘基明's avatar
刘基明 committed
447

刘基明's avatar
刘基明 committed
448
    // 查询正文
449
    public CommonResp<ThemeQo> getThemeDetail(String themeId, String userId) {
张辰's avatar
张辰 committed
450
        ThemeEntity themeEntity = themeService.queryByThemeIdIgnoreDelete(themeId);
刘基明's avatar
刘基明 committed
451 452
        if (themeEntity == null) {
            throw new BizException("找不到帖子id:" + themeId);
刘基明's avatar
刘基明 committed
453
        }
454 455 456 457
        if (themeEntity.getDeleteTag().equals(DeleteTagEnum.DELETED.getCode())) {
            return CommonResp.error(ErrorCodeConstant.UNREACHABLE, null);
        }

刘基明's avatar
刘基明 committed
458
        ThemeQo themeQo = ConvertUtil.themeEntityToQo(themeEntity);
刘基明's avatar
刘基明 committed
459
        //附件
刘基明's avatar
刘基明 committed
460
        batchFeignCallService.getAttachDetail(themeQo);
刘基明's avatar
刘基明 committed
461 462

        //转发、收藏、点赞
463
        buildThemeQoExtraInfo(themeQo);
刘基明's avatar
刘基明 committed
464

刘基明's avatar
刘基明 committed
465 466
        //时间重制
        themeQo.setUpToNowTime(TimeUtils.format(themeEntity.getCreateTime()));
467 468
        // 添加用户相关信息
        buildThemeExtraInfoByUser(userId, themeQo);
张辰's avatar
张辰 committed
469
        return CommonResp.success(themeQo);
刘基明's avatar
刘基明 committed
470 471
    }

刘基明's avatar
刘基明 committed
472 473
    // 点赞/取消点赞
    public void like(LikeThemeReq req, String userId) {
刘基明's avatar
刘基明 committed
474
        if (OperationTypeEnum.CONFIRM.getCode().equals(req.getType())) {
刘基明's avatar
刘基明 committed
475
            collectionService.saveOrUpdate(req.getThemeId(), userId, CollectionTypeEnum.LIKE_THEME);
刘基明's avatar
刘基明 committed
476
        } else if (OperationTypeEnum.CANCEL.getCode().equals(req.getType())) {
刘基明's avatar
刘基明 committed
477 478
            collectionService.delete(req.getThemeId(), userId, CollectionTypeEnum.LIKE_THEME);
        }
479 480
        //失效缓存
        redisCache.evict(StringUtils.joinWith("_", THEME_LIKE_COUNT, req.getThemeId()));
刘基明's avatar
刘基明 committed
481

刘基明's avatar
刘基明 committed
482 483
    }

刘基明's avatar
刘基明 committed
484 485
    //收藏/取消收藏
    public void collect(CollectThemeReq req, String userId) {
刘基明's avatar
刘基明 committed
486
        if (OperationTypeEnum.CONFIRM.getCode().equals(req.getType())) {
刘基明's avatar
刘基明 committed
487
            collectionService.saveOrUpdate(req.getThemeId(), userId, CollectionTypeEnum.COLLECT_THEME);
刘基明's avatar
刘基明 committed
488
        } else if (OperationTypeEnum.CANCEL.getCode().equals(req.getType())) {
刘基明's avatar
刘基明 committed
489 490
            collectionService.delete(req.getThemeId(), userId, CollectionTypeEnum.COLLECT_THEME);
        }
张辰's avatar
张辰 committed
491 492
    }

刘基明's avatar
刘基明 committed
493
    //举报主题
刘基明's avatar
刘基明 committed
494
    @Transactional
刘基明's avatar
刘基明 committed
495 496 497 498 499 500 501
    public void report(ReportThemeReq req, String userId) {
        //更改举报状态
        themeService.updateReportStatus(req.getThemeId());
        //写入举报记录表
        ThemeEntity themeEntity = themeService.queryByThemeId(req.getThemeId());
        reportLogService.insert(ReportTypeEnum.THEME, userId, req.getThemeId(), themeEntity.getAuthorId(), req.getReason());
    }
502

刘基明's avatar
刘基明 committed
503

刘基明's avatar
刘基明 committed
504 505
    //关注用户是否有更新
    public Integer getFollowUpdateCount(String userId) {
张辰's avatar
张辰 committed
506 507
        String lastIdStr = redisCache.get(CACHE_IDOL_THEME_LAST_ID + userId);
        Long lastId = StringUtils.isBlank(lastIdStr) ? 0L : Long.parseLong(lastIdStr);
刘基明's avatar
刘基明 committed
508
        List<String> fansList = followRelService.queryIdolsByFansId(userId);
张辰's avatar
张辰 committed
509
        return themeService.queryCountFromLastId(fansList, lastId);
刘基明's avatar
刘基明 committed
510 511
    }

刘基明's avatar
刘基明 committed
512 513 514 515 516 517
    //    屏蔽(用户)
    public void blockUser(String blockUser, String userId) {
        BlackListEntity selectOne = blackListService.selectOne(blockUser, userId, BlockTypeEnum.USER.getCode());
        if (selectOne == null) {
            blackListService.addBlock(blockUser, userId, BlockTypeEnum.USER);
        }
刘基明's avatar
刘基明 committed
518
    }
刘基明's avatar
刘基明 committed
519

刘基明's avatar
刘基明 committed
520

刘基明's avatar
刘基明 committed
521 522
    //返回被转发主题
    private FormerThemeQo getFormerTheme(String formerThemeId) {
张辰's avatar
张辰 committed
523
        if (StringUtils.isNotBlank(formerThemeId)) {
刘基明's avatar
刘基明 committed
524 525 526 527 528 529 530 531 532 533 534
            ThemeQo formerTheme = ConvertUtil.themeEntityToQo(themeService.queryByThemeId(formerThemeId));
            if (formerTheme != null) {
                batchFeignCallService.getAttachDetail(formerTheme);
                FormerThemeQo f = ConvertUtil.themeQo2FormerThemeQo(formerTheme);
                return f;
            }

        }
        return null;
    }

刘基明's avatar
刘基明 committed
535

刘基明's avatar
刘基明 committed
536
    // 逻辑删除主题,校验用户
刘基明's avatar
刘基明 committed
537 538
    public void delete(String themeId, String userId) {
        themeService.deleteById(themeId, userId);
刘基明's avatar
刘基明 committed
539 540

        this.evictThemeCache(themeId);
刘基明's avatar
刘基明 committed
541
    }
刘基明's avatar
刘基明 committed
542 543


刘基明's avatar
刘基明 committed
544 545
    /**
     * 腾讯云-内容检测
刘基明's avatar
刘基明 committed
546
     *
刘基明's avatar
刘基明 committed
547
     * @param req
刘基明's avatar
刘基明 committed
548
     */
刘基明's avatar
刘基明 committed
549
    private void checkContent(CreateThemeReq req) {
550

551
        if (ThemeTypeEnum.LONG_TEXT.getCode().equals(req.getThemeType()) && req.getTitle().length() > 50) {
552 553 554
            throw new IllegalArgumentException("长文标题不能超过50字");
        }

刘基明's avatar
刘基明 committed
555 556
        StringBuilder sb = new StringBuilder();
        for (ThemeContentReq themeContentReq : req.getContent()) {
刘基明's avatar
刘基明 committed
557
            if (RelTypeEnum.TEXT.type.equals(themeContentReq.getType())) {
刘基明's avatar
刘基明 committed
558 559 560 561
                sb.append(themeContentReq.getValue());
            }
        }
        String content = sb.toString();
刘基明's avatar
刘基明 committed
562
        // 腾讯云接口最多支持5000文字校验
刘基明's avatar
刘基明 committed
563
        // 检查内容是否涉黄违法
刘基明's avatar
刘基明 committed
564
        boolean b;
565
        while (content.length() > 5000) {
刘基明's avatar
刘基明 committed
566 567
            b = TencentcloudUtils.textModeration(content.substring(0, 5000));
            if (!b) {
刘基明's avatar
刘基明 committed
568
                throw new BizException(ErrorCodeConstant.CONTENT_ILLEGAL);
刘基明's avatar
刘基明 committed
569
            }
570
            content = content.substring(5000);
刘基明's avatar
刘基明 committed
571 572 573 574
        }
        b = TencentcloudUtils.textModeration(content);
        if (!b) {
            throw new BizException(ErrorCodeConstant.CONTENT_ILLEGAL);
刘基明's avatar
刘基明 committed
575
        }
刘基明's avatar
刘基明 committed
576 577


刘基明's avatar
刘基明 committed
578 579
    }

刘基明's avatar
刘基明 committed
580 581 582

    /**
     * 直播类型做转播检查
刘基明's avatar
刘基明 committed
583
     *
刘基明's avatar
刘基明 committed
584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600
     * @param userId
     * @param contents
     */
    private void liveRelayCheck(String userId, List<ThemeContentReq> contents) {
        for (ThemeContentReq content : contents) {
            if (content != null && content.getType().equals(RelTypeEnum.LIVE.type)) {
                CommonResp<Set<String>> notRelayResp = feignClientForFatools.getNotRelaySet(userId, Sets.newHashSet(content.getValue()));
                if (!notRelayResp.isSuccess()) {
                    throw new BizException("转播失败");
                }
                if (CollectionUtils.isEmpty(notRelayResp.getData())) {
                    throw new BizException("9999", "很抱歉!您需要购买或报名成功后才可以添加这个直播哦~");
                }
            }
        }
    }

刘基明's avatar
刘基明 committed
601

刘基明's avatar
刘基明 committed
602 603 604 605 606 607 608 609 610 611
    /**
     * 查询用户评论过的主题,并封装成转发主题结构
     *
     * @param req
     * @param userId 当前用户
     * @return
     */
    private List<ThemeQo> getCommentThemeQos(QueryRecordThemeReq req, String userId) {
        List<ThemeQo> commentThemeList = new ArrayList<>();
        List<ThemeEntity> themeEntities;
刘基明's avatar
刘基明 committed
612
        // 评论列表
刘基明's avatar
刘基明 committed
613
        List<CommentEntity> commentEntities = commentService.queryCommentsByUserId(req.getUserId(), req.getLastId(), req.getPageSize());
刘基明's avatar
刘基明 committed
614
        // 当前用户信息
615 616
        UserInfoResp userInfo = redisCache.getObject(StringUtils.joinWith("_", CACHE_FEIGN_USER_INFO, req.getUserId()),
                60, () -> this.getUserInfo(req.getUserId()), UserInfoResp.class);
刘基明's avatar
刘基明 committed
617 618 619 620 621 622
        Set<String> replyThemeIds = commentEntities.stream().map(CommentEntity::getThemeId).collect(Collectors.toSet());
        if (CollectionUtils.isEmpty(replyThemeIds)) {
            return commentThemeList;
        }
        themeEntities = themeService.queryByThemeIds(new ArrayList<>(replyThemeIds));
        List<ThemeQo> themeQos = convertEntityToQo(themeEntities, userId);
刘基明's avatar
刘基明 committed
623
        // 组装附件
刘基明's avatar
刘基明 committed
624
        batchFeignCallService.getAttachDetailByBatch(themeQos);
刘基明's avatar
刘基明 committed
625
        // 主题列表
刘基明's avatar
刘基明 committed
626 627
        Map<String, ThemeQo> themeMap = themeQos.stream()
                .collect(Collectors.toMap(ThemeQo::getThemeId, o -> o));
刘基明's avatar
刘基明 committed
628
        // 主题+评论封装转发对象
刘基明's avatar
刘基明 committed
629 630 631 632 633 634 635
        for (CommentEntity commentEntity : commentEntities) {
            String themeId = commentEntity.getThemeId();
            //评论内容包装到ThemeContentQo里
            ThemeContentQo commentContent = ThemeContentQo.builder()
                    .type(RelTypeEnum.TEXT.type)
                    .value(commentEntity.getContent())
                    .build();
刘基明's avatar
刘基明 committed
636

刘基明's avatar
刘基明 committed
637

刘基明's avatar
刘基明 committed
638
            ThemeQo commentThemeQo = ThemeQo.builder()
639 640 641 642 643 644 645 646
                    .authorId(userInfo.getUserId())
                    .nickName(userInfo.getNickName())
                    .userImg(userInfo.getHeadImageUrl())
                    .userType(userInfo.getUserType())
                    .levelGrade(userInfo.getLevelGrade())
                    .userInvestorType(userInfo.getUserInvestorType())
                    .belongUserOrgId(userInfo.getBelongUserOrgId())
                    .belongUserOrgName(userInfo.getBelongUserOrgName())
刘基明's avatar
刘基明 committed
647
                    .content(Collections.singletonList(commentContent))
刘基明's avatar
刘基明 committed
648 649
                    .commentId(commentEntity.getCommentId())
                    .themeType(ThemeTypeEnum.RES_COMMENT.getCode())
刘基明's avatar
刘基明 committed
650
                    .follow(followRelService.checkFollow(userId, userId))
刘基明's avatar
刘基明 committed
651
                    .build();
刘基明's avatar
刘基明 committed
652 653 654

            //原主题包装到formerThemeQo中
            ThemeQo themeQo = themeMap.get(themeId);
刘基明's avatar
刘基明 committed
655
            if (themeQo != null) {
刘基明's avatar
刘基明 committed
656
                FormerThemeQo f = ConvertUtil.themeQo2FormerThemeQo(themeQo);
刘基明's avatar
刘基明 committed
657
                commentThemeQo.setFormerTheme(f);
刘基明's avatar
刘基明 committed
658
            }
刘基明's avatar
刘基明 committed
659 660
            commentThemeList.add(commentThemeQo);
        }
刘基明's avatar
刘基明 committed
661
        return commentThemeList;
刘基明's avatar
刘基明 committed
662 663
    }

664

665 666
    private UserInfoResp getUserInfo(String authorId) {
        CommonResp<UserInfoResp> userInfoNewCommonResp = feignClientForFatools.queryUsersListNew(authorId);
667 668 669 670 671 672
        if (userInfoNewCommonResp.isNotSuccess()) {
            throw new BizException("内部接口调用失败");
        }
        return userInfoNewCommonResp.getData();
    }

673
    private void reBuildAuthorInfo(ThemeQo themeQo, UserInfoResp userInfo) {
刘基明's avatar
刘基明 committed
674 675 676 677 678 679 680 681 682 683 684 685 686 687 688
        themeQo.setNickName(userInfo.getNickName());
        themeQo.setUserImg(userInfo.getHeadImageUrl());
        themeQo.setUserIntroduction(userInfo.getIntroduction());
        //认证标签相关
        themeQo.setUserType(userInfo.getUserType());
        themeQo.setLevelGrade(userInfo.getLevelGrade());
        themeQo.setUserInvestorType(userInfo.getUserInvestorType());
        themeQo.setBelongUserOrgId(userInfo.getBelongUserOrgId());
        themeQo.setBelongUserOrgName(userInfo.getBelongUserOrgName());
        //工作室相关
        themeQo.setWorkshopName(userInfo.getWorkshopName());
        themeQo.setWorkshopStatus(userInfo.getWorkshopStatus());
        themeQo.setWorkshopIntroduction(userInfo.getWorkshopIntroduction());
    }

刘基明's avatar
刘基明 committed
689
    private void evictThemeCache(String themeId){
刘基明's avatar
刘基明 committed
690
        redisCache.evict(StringUtils.joinWith("_", CACHE_FORWARD_THEME_ID, themeId));
刘基明's avatar
刘基明 committed
691 692 693
        redisCache.evict(StringUtils.joinWith("_", CACHE_THEME_ID, themeId));
    }

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