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

刘基明's avatar
刘基明 committed
3
import com.alibaba.fastjson.JSON;
张辰's avatar
张辰 committed
4
import com.fasterxml.jackson.core.type.TypeReference;
刘基明's avatar
刘基明 committed
5
import com.google.common.collect.Sets;
张辰's avatar
张辰 committed
6 7 8
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
9
import com.tanpu.common.api.CommonResp;
刘基明's avatar
刘基明 committed
10
import com.tanpu.common.constant.ErrorCodeConstant;
刘基明's avatar
刘基明 committed
11
import com.tanpu.common.exception.BizException;
刘基明's avatar
刘基明 committed
12
import com.tanpu.common.util.JsonUtil;
刘基明's avatar
刘基明 committed
13
import com.tanpu.community.api.CommunityConstant;
14 15 16 17
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
18
import com.tanpu.community.api.beans.req.homepage.QueryRecordThemeReq;
刘基明's avatar
刘基明 committed
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;
刘基明's avatar
刘基明 committed
24
import com.tanpu.community.api.beans.req.theme.SynchroThemeReq;
刘基明's avatar
刘基明 committed
25 26
import com.tanpu.community.api.beans.req.theme.ThemeContentReq;
import com.tanpu.community.api.beans.req.theme.ThemeListReq;
刘基明's avatar
刘基明 committed
27
import com.tanpu.community.api.beans.resp.CreateThemeResp;
张辰's avatar
张辰 committed
28
import com.tanpu.community.api.beans.resp.ThemeFullSearchResp;
29
import com.tanpu.community.api.beans.resp.ThemeListResp;
刘基明's avatar
刘基明 committed
30
import com.tanpu.community.api.beans.vo.ImagesDTO;
31
import com.tanpu.community.api.beans.vo.feign.fatools.UserInfoResp;
刘基明's avatar
刘基明 committed
32 33
import com.tanpu.community.api.beans.vo.feign.newsfeed.NewsFeedResReq;
import com.tanpu.community.api.beans.vo.feign.newsfeed.NewsFeedSave4NewCommReq;
刘基明's avatar
刘基明 committed
34 35 36 37 38
import com.tanpu.community.api.enums.BlockTypeEnum;
import com.tanpu.community.api.enums.DeleteTagEnum;
import com.tanpu.community.api.enums.OperationTypeEnum;
import com.tanpu.community.api.enums.ThemeListTypeEnum;
import com.tanpu.community.api.enums.ThemeTypeEnum;
张辰's avatar
张辰 committed
39
import com.tanpu.community.cache.RedisCache;
刘基明's avatar
刘基明 committed
40 41 42 43 44
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
45
import com.tanpu.community.feign.community.FeignClientForCommunity;
刘基明's avatar
刘基明 committed
46
import com.tanpu.community.feign.fatools.FeignClientForFatools;
刘基明's avatar
刘基明 committed
47
import com.tanpu.community.service.*;
张辰's avatar
张辰 committed
48
import com.tanpu.community.service.base.ESService;
张辰's avatar
张辰 committed
49
import com.tanpu.community.util.BizUtils;
50
import com.tanpu.community.util.ConvertUtil;
刘基明's avatar
刘基明 committed
51
import com.tanpu.community.util.RankUtils;
刘基明's avatar
刘基明 committed
52
import com.tanpu.community.util.TencentcloudUtils;
刘基明's avatar
刘基明 committed
53
import com.tanpu.community.util.TimeUtils;
张辰's avatar
张辰 committed
54
import lombok.extern.slf4j.Slf4j;
刘基明's avatar
刘基明 committed
55
import org.apache.commons.collections4.CollectionUtils;
张辰's avatar
张辰 committed
56
import org.apache.commons.collections4.ListUtils;
刘基明's avatar
刘基明 committed
57
import org.apache.commons.io.FileUtils;
刘基明's avatar
刘基明 committed
58
import org.apache.commons.lang3.ObjectUtils;
刘基明's avatar
刘基明 committed
59
import org.apache.commons.lang3.StringUtils;
张辰's avatar
张辰 committed
60
import org.apache.commons.lang3.exception.ExceptionUtils;
刘基明's avatar
刘基明 committed
61
import org.springframework.beans.BeanUtils;
刘基明's avatar
刘基明 committed
62
import org.springframework.beans.factory.annotation.Autowired;
刘基明's avatar
刘基明 committed
63 64 65 66 67 68 69
import org.springframework.beans.factory.annotation.Value;
import org.springframework.core.io.FileSystemResource;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpMethod;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
刘基明's avatar
刘基明 committed
70
import org.springframework.stereotype.Service;
刘基明's avatar
刘基明 committed
71
import org.springframework.transaction.annotation.Transactional;
刘基明's avatar
刘基明 committed
72 73 74
import org.springframework.util.LinkedMultiValueMap;
import org.springframework.util.MultiValueMap;
import org.springframework.web.client.RestTemplate;
刘基明's avatar
刘基明 committed
75

张辰's avatar
张辰 committed
76
import javax.annotation.PostConstruct;
刘基明's avatar
刘基明 committed
77
import javax.annotation.Resource;
刘基明's avatar
刘基明 committed
78
import java.io.File;
张辰's avatar
张辰 committed
79
import java.io.IOException;
刘基明's avatar
刘基明 committed
80
import java.time.LocalDateTime;
刘基明's avatar
刘基明 committed
81
import java.util.*;
刘基明's avatar
刘基明 committed
82
import java.util.stream.Collectors;
刘基明's avatar
刘基明 committed
83

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

张辰's avatar
张辰 committed
86
@Slf4j
刘基明's avatar
刘基明 committed
87 88
@Service
public class ThemeManager {
刘基明's avatar
刘基明 committed
89 90 91 92

    @Value("${tmpfile.dir:/data/tmp/}")
    private String tmpDir;

刘基明's avatar
刘基明 committed
93
    @Resource
刘基明's avatar
刘基明 committed
94
    private ThemeService themeService;
刘基明's avatar
刘基明 committed
95 96 97 98 99 100 101 102

    @Autowired
    private CollectionService collectionService;

    @Autowired
    private CommentService commentService;

    @Autowired
刘基明's avatar
刘基明 committed
103
    private FollowRelService followRelService;
刘基明's avatar
刘基明 committed
104

刘基明's avatar
刘基明 committed
105 106 107
    @Autowired
    private BlackListService blackListService;

刘基明's avatar
刘基明 committed
108 109 110
    @Autowired
    private ThemeAttachmentService themeAttachmentService;

刘基明's avatar
刘基明 committed
111
    @Resource
刘基明's avatar
刘基明 committed
112
    private BatchFeignCallService batchFeignCallService;
刘基明's avatar
刘基明 committed
113

刘基明's avatar
刘基明 committed
114
    @Autowired
刘基明's avatar
刘基明 committed
115
    private VisitLogService visitLogService;
刘基明's avatar
刘基明 committed
116

117 118 119
    @Autowired
    private ReportLogService reportLogService;

刘基明's avatar
刘基明 committed
120 121 122
    @Autowired
    private RankService rankService;

张辰's avatar
张辰 committed
123 124 125
    @Autowired
    private ESService esService;

刘基明's avatar
刘基明 committed
126 127 128
    @Autowired
    private FeignClientForFatools feignClientForFatools;

张辰's avatar
张辰 committed
129 130 131
    @Autowired
    private RedisCache redisCache;

132 133 134
    @Autowired
    private RecommendService recommendService;

刘基明's avatar
刘基明 committed
135 136 137
    @Resource
    private RestTemplate restTemplate;

张辰's avatar
张辰 committed
138 139 140 141 142 143 144 145
    @PostConstruct
    public void init() throws IOException {
        File f = new File(tmpDir);
        log.info("create directory {}", tmpDir);
        if (!f.exists()) {
            FileUtils.forceMkdir(f);
        }
    }
刘基明's avatar
刘基明 committed
146

刘基明's avatar
刘基明 committed
147 148 149 150
    // 专栏
    @Autowired
    private FeignClientForCommunity feignClientForCommunity;

张辰's avatar
张辰 committed
151 152 153 154
    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
155 156
            excludeIds = StringUtils.isBlank(l) ? new ArrayList<>() : JsonUtil.toBean(l, new TypeReference<List<String>>() {
            });
张辰's avatar
张辰 committed
157 158 159 160
        } else {
            excludeIds = new ArrayList<>();
        }

张辰's avatar
张辰 committed
161
        Integer from = (pageNo - 1) * pageSize;
张辰's avatar
张辰 committed
162
        ThemeFullSearchResp resp = new ThemeFullSearchResp();
张辰's avatar
张辰 committed
163

164 165
        String[] keywords = StringUtils.split(keyword, " ");

张辰's avatar
张辰 committed
166
        // 按时间倒叙查询
167
        List<ESThemeQo> esIds = esService.queryThemeIdByContentAndTitle(keywords, from, pageSize * 5);
张辰's avatar
张辰 committed
168 169
        if (esIds.isEmpty()) {
            return resp;
张辰's avatar
张辰 committed
170 171
        }

张辰's avatar
张辰 committed
172 173 174 175 176 177
        // 排除已经展示过的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
178 179 180 181 182 183
        resp.themes.sort(new Comparator<ThemeQo>() {
            @Override
            public int compare(ThemeQo o1, ThemeQo o2) {
                return o2.createTime.compareTo(o1.createTime);
            }
        });
184 185
        // 截取关键词出现的那一部分段落
        for (ThemeQo theme : resp.themes) {
186
            theme.briefContent4FullSearch = BizUtils.getThemeContent(keywords, theme);
187
        }
张辰's avatar
张辰 committed
188 189

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

        return resp;
张辰's avatar
张辰 committed
193
    }
刘基明's avatar
刘基明 committed
194

张辰's avatar
张辰 committed
195

刘基明's avatar
刘基明 committed
196
    /**
197
     * 发表主题(修改)
刘基明's avatar
刘基明 committed
198
     */
张辰's avatar
张辰 committed
199
    @Transactional
刘基明's avatar
刘基明 committed
200
    public CreateThemeResp publishTheme(CreateThemeReq req, String userId) {
刘基明's avatar
刘基明 committed
201

刘基明's avatar
刘基明 committed
202
        // 校验参数
刘基明's avatar
刘基明 committed
203
        checkAttachment(req.getContent());
刘基明's avatar
刘基明 committed
204
        // 转播权限校验
刘基明's avatar
刘基明 committed
205
        liveRelayCheck(userId, req.getContent());
206

刘基明's avatar
刘基明 committed
207
        // 保存主题表
刘基明's avatar
刘基明 committed
208
        ThemeEntity themeEntity = new ThemeEntity();
刘基明's avatar
刘基明 committed
209
        BeanUtils.copyProperties(req, themeEntity);
刘基明's avatar
刘基明 committed
210
        themeEntity.setAuthorId(userId);
刘基明's avatar
刘基明 committed
211
        // 腾讯云敏感词校验
刘基明's avatar
刘基明 committed
212 213
        checkContent(req);
        themeEntity.setContent(JsonUtil.toJson(req.getContent()));
刘基明's avatar
刘基明 committed
214

刘基明's avatar
刘基明 committed
215

刘基明's avatar
刘基明 committed
216
        if (StringUtils.isBlank(req.getEditThemeId())) {
刘基明's avatar
刘基明 committed
217
            // 新建
刘基明's avatar
刘基明 committed
218
            themeService.insertTheme(themeEntity);
刘基明's avatar
刘基明 committed
219
            // 同步到专栏
刘基明's avatar
刘基明 committed
220
            if (1 == req.getSyncToNewComm()) {
刘基明's avatar
刘基明 committed
221
                synchronizeToNewsFeed(req, themeEntity.getThemeId(), userId);
刘基明's avatar
刘基明 committed
222 223
            }

刘基明's avatar
刘基明 committed
224
        } else {
刘基明's avatar
刘基明 committed
225
            // 修改
刘基明's avatar
刘基明 committed
226
            themeService.update(themeEntity, req.getEditThemeId());
刘基明's avatar
刘基明 committed
227
            themeEntity.setThemeId(req.getEditThemeId());
刘基明's avatar
刘基明 committed
228
            this.evictThemeCache(req.getEditThemeId());
刘基明's avatar
刘基明 committed
229
        }
刘基明's avatar
刘基明 committed
230
        // 保存附件表
刘基明's avatar
刘基明 committed
231
        List<ThemeAttachmentEntity> themeAttachments = ConvertUtil.themeReqToAttachmentList(req, themeEntity.getThemeId());
刘基明's avatar
刘基明 committed
232
        if (StringUtils.isNotEmpty(req.getEditThemeId())) {
刘基明's avatar
刘基明 committed
233
            // 修改需要刪除
刘基明's avatar
刘基明 committed
234
            themeAttachmentService.deleteByThemeId(req.getEditThemeId());
刘基明's avatar
刘基明 committed
235
        }
刘基明's avatar
刘基明 committed
236
        themeAttachmentService.insertList(themeAttachments);
刘基明's avatar
刘基明 committed
237

张辰's avatar
张辰 committed
238 239 240 241 242
        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
243

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

刘基明's avatar
刘基明 committed
246
        return CreateThemeResp.builder().themeId(themeEntity.getThemeId()).build();
刘基明's avatar
刘基明 committed
247
    }
刘基明's avatar
刘基明 committed
248

刘基明's avatar
刘基明 committed
249
    private void synchronizeToNewsFeed(CreateThemeReq req, String themeId, String userId) {
刘基明's avatar
刘基明 committed
250
        if (!ThemeTypeEnum.DISCUSSION.getCode().equals(req.getThemeType())) {
刘基明's avatar
刘基明 committed
251 252 253
            // 只有讨论类型才能同步专栏
            throw new BizException("长文类型无法同步专栏");
        }
刘基明's avatar
刘基明 committed
254 255 256
        NewsFeedSave4NewCommReq newsFeedReq = new NewsFeedSave4NewCommReq();
        newsFeedReq.setNewsFeedId(themeId);
        newsFeedReq.setUserId(userId);
刘基明's avatar
刘基明 committed
257 258 259
        ArrayList<NewsFeedResReq> feedList = new ArrayList<>();
        for (ThemeContentReq themeContentReq : req.getContent()) {
            // 文字内容添加到content
刘基明's avatar
刘基明 committed
260
            if (RelTypeEnum.TEXT.type.equals(themeContentReq.getType())) {
刘基明's avatar
刘基明 committed
261
                newsFeedReq.setContent(themeContentReq.getValue());
刘基明's avatar
刘基明 committed
262
            } else if (RelTypeEnum.MULTIPLE_IMAGE.type.equals(themeContentReq.getType())) {
刘基明's avatar
刘基明 committed
263 264

                List<ImagesDTO> imgList = themeContentReq.getImgList();
刘基明's avatar
刘基明 committed
265 266 267 268
                imgList.forEach(img -> {
                    feedList.add(convertImg(img, userId));
                });
            } else {
刘基明's avatar
刘基明 committed
269 270 271 272 273 274 275 276
                //其他类型的附件
                feedList.add(NewsFeedResReq.builder().relType(Integer.parseInt(themeContentReq.getType()))
                        .relId(themeContentReq.getValue())
                        .productType(themeContentReq.getProductType())
                        .remark(themeContentReq.getRemark())
                        .build());
            }
        }
刘基明's avatar
刘基明 committed
277
        newsFeedReq.setNewsFeedResList(feedList);
刘基明's avatar
刘基明 committed
278 279
        CommonResp response = feignClientForCommunity.saveNewsFeed4NewComm(newsFeedReq);
        if (response.isNotSuccess() || !ObjectUtils.anyNotNull(response.getData())) {
刘基明's avatar
刘基明 committed
280 281 282
            throw new BizException("同步圈子调用失败");
        }

刘基明's avatar
刘基明 committed
283 284 285

    }

刘基明's avatar
刘基明 committed
286

刘基明's avatar
刘基明 committed
287
    private NewsFeedResReq convertImg(ImagesDTO img, String userId) {
刘基明's avatar
刘基明 committed
288 289
        String imgUrl = img.getRemark();
        String[] arr = StringUtils.split(imgUrl, ".");
刘基明's avatar
刘基明 committed
290
        String suffix = arr[arr.length - 1];
张辰's avatar
张辰 committed
291
        String fileName = tmpDir + imgUrl.substring(imgUrl.lastIndexOf('/') + 1);
刘基明's avatar
刘基明 committed
292

刘基明's avatar
刘基明 committed
293 294 295
        ResponseEntity<byte[]> resp = restTemplate.getForEntity(img.getRemark(), byte[].class);
        byte[] rst = resp.getBody();
        File f = new File(fileName);
刘基明's avatar
刘基明 committed
296

刘基明's avatar
刘基明 committed
297 298 299 300 301 302 303 304 305
        try {
            FileUtils.writeByteArrayToFile(f, rst);

            // 调用对方接口
            HttpHeaders headers = new HttpHeaders();
            headers.setContentType(MediaType.MULTIPART_FORM_DATA);
            headers.set("uid", userId);
            MultiValueMap<String, Object> params = new LinkedMultiValueMap<String, Object>();
            HashMap<String, Object> item = new HashMap<>();
刘基明's avatar
刘基明 committed
306
            item.put("filetype", suffix);
刘基明's avatar
刘基明 committed
307 308 309 310 311 312 313 314 315
            item.put("refid", img.getRelId());
            item.put("mode", 0);
            item.put("userId", userId);
            FileSystemResource br = new FileSystemResource(f);

            params.add("file", br);
            params.add("item", item);
            HttpEntity<MultiValueMap<String, Object>> requestEntity = new HttpEntity<MultiValueMap<String, Object>>(params, headers);
            // tp-community-svc
刘基明's avatar
刘基明 committed
316
            ResponseEntity<String> response = restTemplate.exchange(CommunityConstant.OLD_FILE_UPLOAD_URL, HttpMethod.POST, requestEntity, String.class);
刘基明's avatar
刘基明 committed
317 318 319 320
            log.info("new-community uploadThemePic returns {}", JSON.toJSONString(response));
            if (StringUtils.isBlank(response.getBody())) {
                throw new RuntimeException("response body is blank");
            }
刘基明's avatar
刘基明 committed
321 322
            CommonResp<LinkedHashMap<String, String>> responseBody = JsonUtil.toBean(response.getBody(), CommonResp.class);
            if (!responseBody.isSuccess()) {
刘基明's avatar
刘基明 committed
323 324
                throw new RuntimeException("reponse is not success");
            }
刘基明's avatar
刘基明 committed
325 326 327 328
            HashMap<String, String> data = responseBody.getData();
            return NewsFeedResReq.builder().relId(data.get("fileId"))
                    .relType(Integer.parseInt(RelTypeEnum.IMAGE_FILE.type))
                    .remark(data.get("fileurl")).build();
刘基明's avatar
刘基明 committed
329 330


刘基明's avatar
刘基明 committed
331 332 333 334 335 336 337 338 339 340
        } catch (Exception e) {
            log.error("error in handleSyncImg for imgUrl: {}", img.getRemark(), e);
            throw new RuntimeException(e);
        } finally {
            try {
                FileUtils.forceDelete(f);
            } catch (Exception e) {
                // do nothing
            }
        }
刘基明's avatar
刘基明 committed
341

刘基明's avatar
刘基明 committed
342
    }
刘基明's avatar
刘基明 committed
343 344


刘基明's avatar
刘基明 committed
345 346
    /**
     * 参数校验
刘基明's avatar
刘基明 committed
347
     *
刘基明's avatar
刘基明 committed
348 349
     * @param themeAttachments
     */
刘基明's avatar
刘基明 committed
350
    private void checkAttachment(List<ThemeContentReq> themeAttachments) {
刘基明's avatar
刘基明 committed
351 352 353
        if (CollectionUtils.isEmpty(themeAttachments)) {
            throw new BizException("正文内容不能为空");
        }
刘基明's avatar
刘基明 committed
354
        for (ThemeContentReq content : themeAttachments) {
刘基明's avatar
刘基明 committed
355
            if (content.getType() == null) {
刘基明's avatar
刘基明 committed
356 357
                throw new IllegalArgumentException("主题内容ThemeContentReq缺少类型");
            }
刘基明's avatar
刘基明 committed
358 359
            Set<String> types = Arrays.stream(RelTypeEnum.values()).map(o -> o.type).collect(Collectors.toSet());
            if (!types.contains(content.getType())) {
刘基明's avatar
刘基明 committed
360 361
                throw new IllegalArgumentException("主题内容ThemeContentReq类型错误");
            }
刘基明's avatar
刘基明 committed
362 363
            if (content.getType().equals(RelTypeEnum.FUND.type)) {
                if (content.getProductType() == null) {
刘基明's avatar
刘基明 committed
364 365 366 367 368 369
                    throw new IllegalArgumentException("附件产品FUND缺少类型");
                }
            }
        }
    }

刘基明's avatar
刘基明 committed
370
    // 转发主题
刘基明's avatar
刘基明 committed
371 372 373 374 375 376 377 378
    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
379

刘基明's avatar
刘基明 committed
380
        if (StringUtils.isBlank(req.getEditThemeId()) || req.getEditThemeId().equals(req.getFormerThemeId())) {
刘基明's avatar
刘基明 committed
381
            // 新建
刘基明's avatar
刘基明 committed
382 383
            themeService.insertTheme(themeEntity);
        } else {
刘基明's avatar
刘基明 committed
384
            // 修改
刘基明's avatar
刘基明 committed
385 386
            themeService.update(themeEntity, req.getEditThemeId());
            themeEntity.setThemeId(req.getEditThemeId());
刘基明's avatar
刘基明 committed
387
            this.evictThemeCache(req.getEditThemeId());
刘基明's avatar
刘基明 committed
388
        }
刘基明's avatar
刘基明 committed
389 390 391 392 393 394

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

刘基明's avatar
刘基明 committed
398 399 400 401
        return CreateThemeResp.builder().themeId(themeEntity.getThemeId()).build();
    }


402
    /**
刘基明's avatar
刘基明 committed
403
     * 查询主题列表:推荐/关注/热门/最新
404
     */
刘基明's avatar
刘基明 committed
405
    // 查询主题列表:推荐/关注/热门/最新
刘基明's avatar
刘基明 committed
406
    public ThemeListResp queryList(ThemeListReq req, String userId) {
刘基明's avatar
刘基明 committed
407 408
        List<String> excludeIds = new ArrayList<>();
        LocalDateTime firstThemeTime = LocalDateTime.now();
张辰's avatar
张辰 committed
409 410
        if (req.page.pageNumber > 1) {
            String l = redisCache.get("queryThemes_" + req.ident);
刘基明's avatar
刘基明 committed
411
            if (!StringUtils.isBlank(l)) {
刘基明's avatar
刘基明 committed
412 413 414 415 416
                excludeIds = JsonUtil.toBean(l, new TypeReference<List<String>>() {
                });
                firstThemeTime = themeService.queryByThemeIdIgnoreDelete(excludeIds.get(0)).getCreateTime();
            }

张辰's avatar
张辰 committed
417 418
        }

刘基明's avatar
刘基明 committed
419

张辰's avatar
张辰 committed
420
        Integer pageStart = (req.page.pageNumber - 1) * req.page.pageSize;
刘基明's avatar
刘基明 committed
421
        Integer pageSize = req.page.pageSize;
422 423

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

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

432
            // 加载第一页时,为防止首页显示空列表,从推荐池中再捞出已看过帖子
刘基明's avatar
刘基明 committed
433 434 435
            if (req.page.pageNumber <= 3 && recmdIds.size() < pageSize) {
                List<String> reSearchIds=ListUtils.union(excludeIds, recmdIds);
                recmdIds.addAll(recommendService.getRecommendThemes(pageStart, pageSize, userId, reSearchIds, firstThemeTime));
436
            }
刘基明's avatar
刘基明 committed
437

438 439
            themes = themeService.queryByThemeIds(recmdIds);
            // 排序并去重
刘基明's avatar
刘基明 committed
440 441 442
            themes = RankUtils.sortThemeEntityByIds(themes, recmdIds).stream().limit(pageSize).collect(Collectors.toList());


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

张辰's avatar
张辰 committed
448 449 450 451 452 453 454
            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
455
        } else if (ThemeListTypeEnum.TOPIC_HOT.getCode().equals(req.getType())) {
张辰's avatar
张辰 committed
456
            // 根据话题查询热门
457 458 459
            if (StringUtils.isEmpty(req.getTopicId())) {
                throw new BizException("TopicId为空");
            }
460

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

464 465
            themes = themeService.queryByThemeIds(rankThemeIds);
            themes = RankUtils.sortThemeEntityByIds(themes, rankThemeIds);
张辰's avatar
张辰 committed
466

刘基明's avatar
刘基明 committed
467
        } else if (ThemeListTypeEnum.TOPIC_LATEST.getCode().equals(req.getType())) {
刘基明's avatar
刘基明 committed
468
            //根据话题查询最新
469 470 471
            if (StringUtils.isEmpty(req.getTopicId())) {
                throw new BizException("TopicId为空");
            }
刘基明's avatar
刘基明 committed
472
            themes = themeService.queryNewestByTopic(req.topicId, pageStart, pageSize, excludeIds);
刘基明's avatar
刘基明 committed
473
        }
474

475
        ThemeListResp resp = new ThemeListResp();
刘基明's avatar
刘基明 committed
476
        resp.themes = convertEntityToQo(themes, userId);
477

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

481
        //组装详情
482
        return resp;
刘基明's avatar
刘基明 committed
483 484
    }

刘基明's avatar
刘基明 committed
485
    // 主题Entity转QO,组装所有信息
486 487 488
    private List<ThemeQo> convertEntityToQo(List<ThemeEntity> themeEntities, String userId) {
        //Entity转Qo
        List<ThemeQo> themeQos = ConvertUtil.themeEntitiesToDTOs(themeEntities);
刘基明's avatar
刘基明 committed
489
        // 批量查询附件detail
490 491 492
        batchFeignCallService.getAttachDetailByBatch(themeQos);
        //其他信息
        for (ThemeQo themeQO : themeQos) {
刘基明's avatar
刘基明 committed
493
            // 通用信息
494
            buildThemeQoExtraInfo(themeQO);
刘基明's avatar
刘基明 committed
495

496
        }
刘基明's avatar
刘基明 committed
497
        // 和用户相关信息
刘基明's avatar
刘基明 committed
498
        buildThemeExtraInfoByUser(userId, themeQos);
刘基明's avatar
刘基明 committed
499

500 501 502
        return themeQos;
    }

刘基明's avatar
刘基明 committed
503
    // 转发对象、点赞、收藏、转发数
504 505
    private void buildThemeQoExtraInfo(ThemeQo themeQo) {
        String themeId = themeQo.getThemeId();
刘基明's avatar
刘基明 committed
506
        // 封装转发对象
刘基明's avatar
刘基明 committed
507
        FormerThemeQo former = redisCache.getObject(StringUtils.joinWith("_", CACHE_FORWARD_THEME_ID, themeQo.getFormerThemeId()), 60,
508 509
                () -> this.getFormerTheme(themeQo.getFormerThemeId()), FormerThemeQo.class);
        themeQo.setFormerTheme(former);
刘基明's avatar
刘基明 committed
510

刘基明's avatar
刘基明 committed
511

刘基明's avatar
刘基明 committed
512
        // 点赞,收藏,转发
513 514 515 516 517 518 519 520
        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);
521 522 523
        themeQo.setCommentCount(commentCount);
        themeQo.setLikeCount(likeCount);
        themeQo.setForwardCount(forwardCount);
刘基明's avatar
刘基明 committed
524 525
    }

刘基明's avatar
刘基明 committed
526
    // 组装和当前用户相关信息(单个查询)
527 528
    private void buildThemeExtraInfoByUser(String userId, ThemeQo themeQo) {
        String themeId = themeQo.getThemeId();
刘基明's avatar
刘基明 committed
529
        // 是否关注作者
刘基明's avatar
刘基明 committed
530
        themeQo.setFollow(followRelService.checkFollow(themeQo.getAuthorId(), userId));
刘基明's avatar
刘基明 committed
531
        // 是否点赞
532
        CollectionEntity likeEntity = collectionService.queryCollection(themeId, userId, CollectionTypeEnum.LIKE_THEME);
533
        themeQo.setHasLiked(likeEntity != null);
刘基明's avatar
刘基明 committed
534
        // 是否转发
刘基明's avatar
刘基明 committed
535
        themeQo.setHasForward(themeService.judgeForwardByUser(themeId, userId));
刘基明's avatar
刘基明 committed
536
        // 是否收藏
537
        CollectionEntity collectionEntity = collectionService.queryCollection(themeId, userId, CollectionTypeEnum.COLLECT_THEME);
538 539
        themeQo.setHasCollect(collectionEntity != null);
    }
刘基明's avatar
刘基明 committed
540

刘基明's avatar
刘基明 committed
541 542 543 544
    // 组装和当前用户相关信息(批量查询)
    private void buildThemeExtraInfoByUser(String userId, List<ThemeQo> themeQos) {
        // 批量查询
        List<String> themeIds = themeQos.stream().map(ThemeQo::getThemeId).collect(Collectors.toList());
刘基明's avatar
刘基明 committed
545
        Set<String> idolSet = new HashSet<>(followRelService.queryIdolsByFansId(userId));
刘基明's avatar
刘基明 committed
546 547 548 549 550 551 552 553 554 555 556 557
        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
558 559
    /**
     * 返回用户发布、回复、点赞、收藏的主题列表
560 561 562
     *
     * @param req    查询用户
     * @param userId 当前用户
刘基明's avatar
刘基明 committed
563 564
     * @return
     */
刘基明's avatar
刘基明 committed
565
    public List<ThemeQo> queryThemesByUser(QueryRecordThemeReq req, String userId) {
刘基明's avatar
刘基明 committed
566 567 568 569

        List<ThemeEntity> themeEntities = Collections.emptyList();
        switch (req.getRecordType()) {
            case 1://发布
张辰's avatar
张辰 committed
570
                themeEntities = themeService.queryThemesByUserIdCreateDesc(req.getUserId(), req.getLastId(), req.getPageSize());
刘基明's avatar
刘基明 committed
571
                break;
刘基明's avatar
刘基明 committed
572
            case 2://回复
刘基明's avatar
刘基明 committed
573
                List<ThemeQo> commentThemeList = getCommentThemeQos(req, userId);
刘基明's avatar
刘基明 committed
574
                return commentThemeList;
刘基明's avatar
刘基明 committed
575
            case 3://点赞
576 577
                List<String> likeThemeIds = collectionService.getListByUser(req.getUserId(), CollectionTypeEnum.LIKE_THEME, req.getLastId(), req.getPageSize());
                themeEntities = themeService.queryByThemeIds(likeThemeIds);
578 579
                themeEntities = RankUtils.sortThemeEntityByIds(themeEntities, likeThemeIds);

刘基明's avatar
刘基明 committed
580
                break;
刘基明's avatar
刘基明 committed
581
            case 4://收藏
582
                List<String> collectThemeIds = collectionService.getListByUser(req.getUserId(), CollectionTypeEnum.COLLECT_THEME, req.getLastId(), req.getPageSize());
583 584
                themeEntities = themeService.queryByThemeIds(collectThemeIds, req.getLastId(), req.getPageSize());
                themeEntities = RankUtils.sortThemeEntityByIds(themeEntities, collectThemeIds);
刘基明's avatar
刘基明 committed
585 586 587
                break;
        }
        List<ThemeQo> themeQos = convertEntityToQo(themeEntities, userId);
刘基明's avatar
刘基明 committed
588

刘基明's avatar
刘基明 committed
589
        if (userId.equals(req.getUserId())) {
刘基明's avatar
刘基明 committed
590
            //如果用户是查询自己的帖子,需要实时查询用户自己的个人信息,防止数据不一致(非收藏类型)
刘基明's avatar
刘基明 committed
591 592 593 594 595
            CommonResp<UserInfoResp> userInfoNewCommonResp = feignClientForFatools.queryUsersListNew(userId);
            if (userInfoNewCommonResp.isNotSuccess()) {
                throw new BizException("内部接口调用失败");
            }
            UserInfoResp user = userInfoNewCommonResp.getData();
刘基明's avatar
刘基明 committed
596 597 598 599 600
            themeQos.forEach(o -> {
                if (o.getAuthorId().equals(userId)) {
                    reBuildAuthorInfo(o, user);
                }
            });
刘基明's avatar
刘基明 committed
601 602 603
            redisCache.put(StringUtils.joinWith("_", CACHE_FEIGN_USER_INFO, userId), user, 60);
        }

刘基明's avatar
刘基明 committed
604 605 606
        return themeQos;
    }

刘基明's avatar
刘基明 committed
607

刘基明's avatar
刘基明 committed
608
    // 查询正文
609
    public CommonResp<ThemeQo> getThemeDetail(String themeId, String userId) {
张辰's avatar
张辰 committed
610
        ThemeEntity themeEntity = themeService.queryByThemeIdIgnoreDelete(themeId);
刘基明's avatar
刘基明 committed
611 612
        if (themeEntity == null) {
            throw new BizException("找不到帖子id:" + themeId);
刘基明's avatar
刘基明 committed
613
        }
614 615 616 617
        if (themeEntity.getDeleteTag().equals(DeleteTagEnum.DELETED.getCode())) {
            return CommonResp.error(ErrorCodeConstant.UNREACHABLE, null);
        }

刘基明's avatar
刘基明 committed
618
        ThemeQo themeQo = ConvertUtil.themeEntityToQo(themeEntity);
刘基明's avatar
刘基明 committed
619
        //附件
刘基明's avatar
刘基明 committed
620
        batchFeignCallService.getAttachDetail(themeQo);
刘基明's avatar
刘基明 committed
621 622

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

刘基明's avatar
刘基明 committed
625 626
        //时间重制
        themeQo.setUpToNowTime(TimeUtils.format(themeEntity.getCreateTime()));
627 628
        // 添加用户相关信息
        buildThemeExtraInfoByUser(userId, themeQo);
张辰's avatar
张辰 committed
629
        return CommonResp.success(themeQo);
刘基明's avatar
刘基明 committed
630 631
    }

刘基明's avatar
刘基明 committed
632 633
    // 点赞/取消点赞
    public void like(LikeThemeReq req, String userId) {
刘基明's avatar
刘基明 committed
634
        if (OperationTypeEnum.CONFIRM.getCode().equals(req.getType())) {
刘基明's avatar
刘基明 committed
635
            collectionService.saveOrUpdate(req.getThemeId(), userId, CollectionTypeEnum.LIKE_THEME);
刘基明's avatar
刘基明 committed
636
        } else if (OperationTypeEnum.CANCEL.getCode().equals(req.getType())) {
刘基明's avatar
刘基明 committed
637 638
            collectionService.delete(req.getThemeId(), userId, CollectionTypeEnum.LIKE_THEME);
        }
639 640
        //失效缓存
        redisCache.evict(StringUtils.joinWith("_", THEME_LIKE_COUNT, req.getThemeId()));
刘基明's avatar
刘基明 committed
641

刘基明's avatar
刘基明 committed
642 643
    }

刘基明's avatar
刘基明 committed
644 645
    //收藏/取消收藏
    public void collect(CollectThemeReq req, String userId) {
刘基明's avatar
刘基明 committed
646
        if (OperationTypeEnum.CONFIRM.getCode().equals(req.getType())) {
刘基明's avatar
刘基明 committed
647
            collectionService.saveOrUpdate(req.getThemeId(), userId, CollectionTypeEnum.COLLECT_THEME);
刘基明's avatar
刘基明 committed
648
        } else if (OperationTypeEnum.CANCEL.getCode().equals(req.getType())) {
刘基明's avatar
刘基明 committed
649 650
            collectionService.delete(req.getThemeId(), userId, CollectionTypeEnum.COLLECT_THEME);
        }
张辰's avatar
张辰 committed
651 652
    }

刘基明's avatar
刘基明 committed
653
    //举报主题
刘基明's avatar
刘基明 committed
654
    @Transactional
刘基明's avatar
刘基明 committed
655 656 657 658 659 660 661
    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());
    }
662

刘基明's avatar
刘基明 committed
663

刘基明's avatar
刘基明 committed
664 665
    //关注用户是否有更新
    public Integer getFollowUpdateCount(String userId) {
张辰's avatar
张辰 committed
666 667
        String lastIdStr = redisCache.get(CACHE_IDOL_THEME_LAST_ID + userId);
        Long lastId = StringUtils.isBlank(lastIdStr) ? 0L : Long.parseLong(lastIdStr);
刘基明's avatar
刘基明 committed
668
        List<String> fansList = followRelService.queryIdolsByFansId(userId);
张辰's avatar
张辰 committed
669
        return themeService.queryCountFromLastId(fansList, lastId);
刘基明's avatar
刘基明 committed
670 671
    }

刘基明's avatar
刘基明 committed
672 673 674 675 676 677
    //    屏蔽(用户)
    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
678
    }
刘基明's avatar
刘基明 committed
679

刘基明's avatar
刘基明 committed
680

刘基明's avatar
刘基明 committed
681 682
    //返回被转发主题
    private FormerThemeQo getFormerTheme(String formerThemeId) {
张辰's avatar
张辰 committed
683
        if (StringUtils.isNotBlank(formerThemeId)) {
刘基明's avatar
刘基明 committed
684 685 686 687 688 689 690 691 692 693 694
            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
695

刘基明's avatar
刘基明 committed
696
    // 逻辑删除主题,校验用户
刘基明's avatar
刘基明 committed
697 698
    public void delete(String themeId, String userId) {
        themeService.deleteById(themeId, userId);
刘基明's avatar
刘基明 committed
699 700

        this.evictThemeCache(themeId);
刘基明's avatar
刘基明 committed
701
    }
刘基明's avatar
刘基明 committed
702

刘基明's avatar
刘基明 committed
703
    // 从专栏同步
刘基明's avatar
刘基明 committed
704 705 706
    public void convertFromNewsFeed(SynchroThemeReq req2) {
        String userId = req2.getUserId();
        CreateThemeReq req = new CreateThemeReq();
707
        BeanUtils.copyProperties(req2, req);
刘基明's avatar
刘基明 committed
708 709 710
        req.setTopicId("");
        req.setTitle("");

刘基明's avatar
刘基明 committed
711 712 713
        // 校验参数
        checkAttachment(req.getContent());
        // 转播权限校验
刘基明's avatar
刘基明 committed
714
        liveRelayCheck(userId, req.getContent());
刘基明's avatar
刘基明 committed
715 716 717 718 719 720

        // 保存主题表
        ThemeEntity themeEntity = new ThemeEntity();
        BeanUtils.copyProperties(req, themeEntity);
        themeEntity.setAuthorId(userId);
        themeEntity.setContent(JsonUtil.toJson(req.getContent()));
刘基明's avatar
刘基明 committed
721
        themeEntity.setThemeId(CommunityConstant.THEME_PREFIX + req2.getThemeId());
刘基明's avatar
刘基明 committed
722 723 724 725 726 727 728 729 730 731 732 733 734 735
        themeService.insertTheme(themeEntity);

        // 保存附件表
        List<ThemeAttachmentEntity> themeAttachments = ConvertUtil.themeReqToAttachmentList(req, themeEntity.getThemeId());
        themeAttachmentService.insertList(themeAttachments);

        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
736

刘基明's avatar
刘基明 committed
737 738
    /**
     * 腾讯云-内容检测
刘基明's avatar
刘基明 committed
739
     *
刘基明's avatar
刘基明 committed
740
     * @param req
刘基明's avatar
刘基明 committed
741
     */
刘基明's avatar
刘基明 committed
742
    private void checkContent(CreateThemeReq req) {
743

744
        if (ThemeTypeEnum.LONG_TEXT.getCode().equals(req.getThemeType()) && req.getTitle().length() > 50) {
745 746 747
            throw new IllegalArgumentException("长文标题不能超过50字");
        }

刘基明's avatar
刘基明 committed
748 749
        StringBuilder sb = new StringBuilder();
        for (ThemeContentReq themeContentReq : req.getContent()) {
刘基明's avatar
刘基明 committed
750
            if (RelTypeEnum.TEXT.type.equals(themeContentReq.getType())) {
刘基明's avatar
刘基明 committed
751 752 753 754
                sb.append(themeContentReq.getValue());
            }
        }
        String content = sb.toString();
刘基明's avatar
刘基明 committed
755
        // 腾讯云接口最多支持5000文字校验
刘基明's avatar
刘基明 committed
756
        // 检查内容是否涉黄违法
刘基明's avatar
刘基明 committed
757
        boolean b;
758
        while (content.length() > 5000) {
刘基明's avatar
刘基明 committed
759 760
            b = TencentcloudUtils.textModeration(content.substring(0, 5000));
            if (!b) {
刘基明's avatar
刘基明 committed
761
                throw new BizException(ErrorCodeConstant.CONTENT_ILLEGAL);
刘基明's avatar
刘基明 committed
762
            }
763
            content = content.substring(5000);
刘基明's avatar
刘基明 committed
764 765 766 767
        }
        b = TencentcloudUtils.textModeration(content);
        if (!b) {
            throw new BizException(ErrorCodeConstant.CONTENT_ILLEGAL);
刘基明's avatar
刘基明 committed
768
        }
刘基明's avatar
刘基明 committed
769 770


刘基明's avatar
刘基明 committed
771 772
    }

刘基明's avatar
刘基明 committed
773 774 775

    /**
     * 直播类型做转播检查
刘基明's avatar
刘基明 committed
776
     *
刘基明's avatar
刘基明 committed
777 778 779 780 781 782 783 784 785 786
     * @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("转播失败");
                }
张辰's avatar
张辰 committed
787
                if (CollectionUtils.isNotEmpty(notRelayResp.getData())) {
刘基明's avatar
刘基明 committed
788 789 790 791 792 793
                    throw new BizException("9999", "很抱歉!您需要购买或报名成功后才可以添加这个直播哦~");
                }
            }
        }
    }

刘基明's avatar
刘基明 committed
794

刘基明's avatar
刘基明 committed
795 796 797 798 799 800 801 802 803 804
    /**
     * 查询用户评论过的主题,并封装成转发主题结构
     *
     * @param req
     * @param userId 当前用户
     * @return
     */
    private List<ThemeQo> getCommentThemeQos(QueryRecordThemeReq req, String userId) {
        List<ThemeQo> commentThemeList = new ArrayList<>();
        List<ThemeEntity> themeEntities;
刘基明's avatar
刘基明 committed
805
        // 评论列表
刘基明's avatar
刘基明 committed
806
        List<CommentEntity> commentEntities = commentService.queryCommentsByUserId(req.getUserId(), req.getLastId(), req.getPageSize());
刘基明's avatar
刘基明 committed
807
        // 当前用户信息
808 809
        UserInfoResp userInfo = redisCache.getObject(StringUtils.joinWith("_", CACHE_FEIGN_USER_INFO, req.getUserId()),
                60, () -> this.getUserInfo(req.getUserId()), UserInfoResp.class);
刘基明's avatar
刘基明 committed
810 811 812 813 814 815
        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
816
        // 组装附件
刘基明's avatar
刘基明 committed
817
        batchFeignCallService.getAttachDetailByBatch(themeQos);
刘基明's avatar
刘基明 committed
818
        // 主题列表
刘基明's avatar
刘基明 committed
819 820
        Map<String, ThemeQo> themeMap = themeQos.stream()
                .collect(Collectors.toMap(ThemeQo::getThemeId, o -> o));
刘基明's avatar
刘基明 committed
821
        // 主题+评论封装转发对象
刘基明's avatar
刘基明 committed
822 823 824 825 826 827 828
        for (CommentEntity commentEntity : commentEntities) {
            String themeId = commentEntity.getThemeId();
            //评论内容包装到ThemeContentQo里
            ThemeContentQo commentContent = ThemeContentQo.builder()
                    .type(RelTypeEnum.TEXT.type)
                    .value(commentEntity.getContent())
                    .build();
刘基明's avatar
刘基明 committed
829

刘基明's avatar
刘基明 committed
830

刘基明's avatar
刘基明 committed
831
            ThemeQo commentThemeQo = ThemeQo.builder()
832 833 834 835 836 837 838 839
                    .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
840
                    .content(Collections.singletonList(commentContent))
刘基明's avatar
刘基明 committed
841 842
                    .commentId(commentEntity.getCommentId())
                    .themeType(ThemeTypeEnum.RES_COMMENT.getCode())
刘基明's avatar
刘基明 committed
843
                    .follow(followRelService.checkFollow(userId, userId))
刘基明's avatar
刘基明 committed
844
                    .build();
刘基明's avatar
刘基明 committed
845 846 847

            //原主题包装到formerThemeQo中
            ThemeQo themeQo = themeMap.get(themeId);
刘基明's avatar
刘基明 committed
848
            if (themeQo != null) {
刘基明's avatar
刘基明 committed
849
                FormerThemeQo f = ConvertUtil.themeQo2FormerThemeQo(themeQo);
刘基明's avatar
刘基明 committed
850
                commentThemeQo.setFormerTheme(f);
刘基明's avatar
刘基明 committed
851
            }
刘基明's avatar
刘基明 committed
852 853
            commentThemeList.add(commentThemeQo);
        }
刘基明's avatar
刘基明 committed
854
        return commentThemeList;
刘基明's avatar
刘基明 committed
855 856
    }

857

858 859
    private UserInfoResp getUserInfo(String authorId) {
        CommonResp<UserInfoResp> userInfoNewCommonResp = feignClientForFatools.queryUsersListNew(authorId);
860 861 862 863 864 865
        if (userInfoNewCommonResp.isNotSuccess()) {
            throw new BizException("内部接口调用失败");
        }
        return userInfoNewCommonResp.getData();
    }

866
    private void reBuildAuthorInfo(ThemeQo themeQo, UserInfoResp userInfo) {
刘基明's avatar
刘基明 committed
867 868 869 870 871 872 873 874 875 876 877 878 879 880 881
        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
882
    private void evictThemeCache(String themeId) {
刘基明's avatar
刘基明 committed
883
        redisCache.evict(StringUtils.joinWith("_", CACHE_FORWARD_THEME_ID, themeId));
刘基明's avatar
刘基明 committed
884 885 886
        redisCache.evict(StringUtils.joinWith("_", CACHE_THEME_ID, themeId));
    }

刘基明's avatar
刘基明 committed
887

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