package com.tanpu.community.manager; import com.google.common.collect.Sets; import com.tanpu.common.api.CommonResp; import com.tanpu.common.exception.BizException; import com.tanpu.common.util.JsonUtil; import com.tanpu.community.api.beans.qo.*; import com.tanpu.community.api.beans.req.homepage.QueryRecordThemeReq; import com.tanpu.community.api.beans.req.theme.*; import com.tanpu.community.api.beans.resp.CreateThemeResp; import com.tanpu.community.api.beans.resp.ThemeFullSearchResp; import com.tanpu.community.api.enums.*; import com.tanpu.community.cache.RedisCache; import com.tanpu.community.dao.entity.community.*; import com.tanpu.community.dao.entity.user.UserInfoEntity; import com.tanpu.community.feign.fatools.FeignClientForFatools; import com.tanpu.community.service.*; import com.tanpu.community.service.base.ESService; import com.tanpu.community.util.ConvertUtil; import com.tanpu.community.util.RankUtils; import com.tanpu.community.util.TencentcloudUtils; import lombok.extern.slf4j.Slf4j; import org.apache.commons.collections4.CollectionUtils; import org.apache.commons.lang3.StringUtils; import org.apache.commons.lang3.exception.ExceptionUtils; import org.springframework.beans.BeanUtils; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; import org.springframework.transaction.annotation.Transactional; import javax.annotation.Resource; import java.time.LocalDateTime; import java.util.*; import java.util.stream.Collectors; import static com.tanpu.community.api.constants.RedisKeyConstant.CACHE_FORMER_THEME_ID; import static com.tanpu.community.api.constants.RedisKeyConstant.CACHE_THEME_ID; @Slf4j @Service public class ThemeManager { @Resource private ThemeService themeService; @Autowired private CollectionService collectionService; @Autowired private CommentService commentService; @Autowired private FollowRelService followRelService; @Autowired private BlackListService blackListService; @Autowired private UserInfoService userInfoService; @Autowired private ThemeAttachmentService themeAttachmentService; @Resource private BatchFeignCallService batchFeignCallService; @Autowired private VisitSummaryService visitSummaryService; @Autowired private ReportLogService reportLogService; @Autowired private RankService rankService; @Autowired private ESService esService; @Autowired private FeignClientForFatools feignClientForFatools; @Autowired private RedisCache redisCache; @Autowired private RecommendService recommendService; public ThemeFullSearchResp themeFullSearch(String keyword, Integer pageNo, Integer pageSize, List<String> excludeIds, String userId) { Integer from = (pageNo - 1) * pageSize; ThemeFullSearchResp resp = new ThemeFullSearchResp(); // 按时间倒叙查询 // todo redis List<ESThemeQo> esIds = esService.queryThemeIdByContentAndTitle(keyword, from, pageSize * 5); if (esIds.isEmpty()) { return resp; } // 排除已经展示过的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); resp.excludeIds.addAll(filterEsIds); return resp; } /** * 发表主题(可修改) */ @Transactional public CreateThemeResp publishTheme(CreateThemeReq req, String userId) { //保存主题表 ThemeEntity themeEntity = new ThemeEntity(); BeanUtils.copyProperties(req, themeEntity); themeEntity.setAuthorId(userId); themeEntity.setContent(JsonUtil.toJson(req.getContent())); //腾讯云敏感词校验 checkContent(themeEntity.getContent()); if (StringUtils.isEmpty(req.getEditThemeId())) { //新建 themeService.insertTheme(themeEntity); } else { //修改 themeService.update(themeEntity, req.getEditThemeId()); themeEntity.setThemeId(req.getEditThemeId()); } //保存附件表 List<ThemeAttachmentEntity> themeAttachments = ConvertUtil.themeReqToAttachmentList(req, themeEntity.getThemeId()); if (StringUtils.isNotEmpty(req.getEditThemeId())) { //修改需要刪除 themeAttachmentService.deleteByThemeId(req.getEditThemeId()); } 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)); } redisCache.evict(StringUtils.joinWith("_", CACHE_THEME_ID, themeEntity.getThemeId())); return CreateThemeResp.builder().themeId(themeEntity.getThemeId()).build(); } //转发主题 public CreateThemeResp forward(ForwardThemeReq req, String userId) { ThemeEntity targetTheme = themeService.queryByThemeId(req.getFormerThemeId()); ThemeEntity themeEntity = ThemeEntity.builder() .content(JsonUtil.toJson(req.getContent())) .topicId(req.getTopicId()) .formerThemeId(req.getFormerThemeId()) .authorId(userId) .themeType(ThemeTypeEnum.FORWARD.getCode()) .build(); if (StringUtils.isEmpty(req.getEditThemeId()) || req.getEditThemeId() == req.getFormerThemeId()) { //新建 themeService.insertTheme(themeEntity); } else { //修改 themeService.update(themeEntity, req.getEditThemeId()); themeEntity.setThemeId(req.getEditThemeId()); } return CreateThemeResp.builder().themeId(themeEntity.getThemeId()).build(); } //查询主题列表:推荐/关注/热门/最新 public List<ThemeQo> queryThemes(ThemeListReq req, String userId) { List<ThemeEntity> themeEntities = new ArrayList<>(); if (ThemeListTypeEnum.RECOMMEND.getCode().equals(req.getType())) { //推荐 List<String> recommendThemeIds = recommendService.getRecommendThemes(req.getLastId(), req.getPageSize(), userId); themeEntities = themeService.queryByThemeIds(recommendThemeIds); themeEntities = RankUtils.sortThemeEntityByIds(themeEntities, recommendThemeIds); } else if (ThemeListTypeEnum.FOLLOW.getCode().equals(req.getType())) { //关注 if (StringUtils.isEmpty(req.getLastId())) { visitSummaryService.addPageView(userId, userId, VisitTypeEnum.FOLLOW_THEME_VIEW); } //根据关注列表查询 List<String> fansList = followRelService.queryFansByFollowerId(userId); themeEntities = themeService.queryByUserIds(fansList, req.getLastId(), req.getPageSize()); } else if (ThemeListTypeEnum.TOPIC_HOT.getCode().equals(req.getType())) { //根据话题查询热门 if (StringUtils.isEmpty(req.getTopicId())) throw new BizException("TopicId为空"); List<String> rankThemeIds = rankService.getRankThemeListByTopic(req.getTopicId()); themeEntities = themeService.queryByThemeIdsExcludeUser(rankThemeIds, null, req.getLastId(), req.getPageSize()); themeEntities = RankUtils.sortThemeEntityByIds(themeEntities, rankThemeIds); } else if (ThemeListTypeEnum.TOPIC_LATEST.getCode().equals(req.getType())) { //根据话题查询最新 if (StringUtils.isEmpty(req.getTopicId())) throw new BizException("TopicId为空"); themeEntities = themeService.queryByTopic(req.getTopicId(), req.getLastId(), req.getPageSize()); } //组装详情 return convertEntityToQo(themeEntities, userId); } public List<ThemeQo> searchThemes(ThemeSearchReq req, String userId) { List<ThemeEntity> themeEntities = themeService.search(req.getKeyword(), req.getLastId(), req.getPageSize()); return convertEntityToQo(themeEntities, userId); } // 返回用户发布、回复、点赞、收藏的主题列表 public List<ThemeQo> queryThemesByUser(QueryRecordThemeReq req, String userId) { List<ThemeEntity> themeEntities = Collections.emptyList(); switch (req.getRecordType()) { case 1://发布 themeEntities = themeService.queryThemesByUserId(req.getUserId(), req.getLastId(), req.getPageSize()); break; case 2://回复 List<ThemeQo> commentThemeList = getCommentThemeQos(req, userId); return commentThemeList; case 3://点赞 Set<String> likeThemeIds = collectionService.getListByUser(req.getUserId(), CollectionTypeEnum.LIKE_THEME); themeEntities = themeService.queryByThemeIds(new ArrayList<>(likeThemeIds), req.getLastId(), req.getPageSize()); break; case 4://收藏 Set<String> collectThemeIds = collectionService.getListByUser(req.getUserId(), CollectionTypeEnum.COLLECT_THEME); themeEntities = themeService.queryByThemeIds(new ArrayList<>(collectThemeIds), req.getLastId(), req.getPageSize()); break; } List<ThemeQo> themeQos = convertEntityToQo(themeEntities, userId); return themeQos; } //查询正文 public ThemeQo getDetail(String themeId, String userId) { //进入详情 visitSummaryService.addPageView(userId, themeId, VisitTypeEnum.THEME_PAGE_VIEW); ThemeQo themeQo = redisCache.getObject(StringUtils.joinWith("_", CACHE_THEME_ID, themeId), 60, () -> this.getDetailForCommon(themeId), ThemeQo.class); buildThemeExtraInfoByUser(userId,themeQo); return themeQo; } //正文通用信息,与用户无关,可使用缓存 private ThemeQo getDetailForCommon(String themeId) { ThemeEntity themeEntity = themeService.queryByThemeId(themeId); if (themeEntity == null) { throw new BizException("找不到帖子id:" + themeId); } ThemeQo themeQo = ConvertUtil.themeEntityToQo(themeEntity); //附件 batchFeignCallService.getAttachDetail(themeQo); //转发、收藏、点赞 buildThemeQoExtraInfo( themeQo); return themeQo; } // 点赞/取消点赞 public void like(LikeThemeReq req, String userId) { if (OperationTypeEnum.CONFIRM.getCode().equals(req.getType())) { collectionService.addIfNotExist(req.getThemeId(), userId, CollectionTypeEnum.LIKE_THEME); } else if (OperationTypeEnum.CANCEL.getCode().equals(req.getType())) { collectionService.delete(req.getThemeId(), userId, CollectionTypeEnum.LIKE_THEME); } } //收藏/取消收藏 public void collect(CollectThemeReq req, String userId) { if (OperationTypeEnum.CONFIRM.getCode().equals(req.getType())) { collectionService.addIfNotExist(req.getThemeId(), userId, CollectionTypeEnum.COLLECT_THEME); } else if (OperationTypeEnum.CANCEL.getCode().equals(req.getType())) { collectionService.delete(req.getThemeId(), userId, CollectionTypeEnum.COLLECT_THEME); } } //举报主题 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()); } //关注用户是否有更新 public Integer getFollowUpdateCount(String userId) { LocalDateTime lastViewTime = visitSummaryService.queryLatestViewFollow(userId); List<String> fansList = followRelService.queryFansByFollowerId(userId); return themeService.queryCountFromLastTime(fansList, lastViewTime); } // 屏蔽(用户) 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); } } // 屏蔽(主题) public void blockTheme(String themeId, String userId) { blackListService.addBlock(themeId, userId, BlockTypeEnum.THEME); BlackListEntity selectOne = blackListService.selectOne(themeService.queryByThemeId(themeId).getAuthorId(), userId, BlockTypeEnum.USER.getCode()); if (selectOne == null) { blackListService.addBlock(themeService.queryByThemeId(themeId).getAuthorId(), userId, BlockTypeEnum.USER); } } // 解除屏蔽(主题) public void unblockTheme(String themeId, String userId) { blackListService.removeBlackList(themeId, userId, BlockTypeEnum.USER); } //主题Entity转QO private List<ThemeQo> convertEntityToQo(List<ThemeEntity> themeEntities, String userId) { //Entity转Qo List<ThemeQo> themeQos = ConvertUtil.themeEntitiesToDTOs(themeEntities); //批量查询附件detail batchFeignCallService.getAttachDetailByBatch(themeQos); //其他信息 for (ThemeQo themeQO : themeQos) { buildThemeQoExtraInfo( themeQO); buildThemeExtraInfoByUser(userId,themeQO); } return themeQos; } //转发对象、点赞、收藏、转发数 private void buildThemeQoExtraInfo( ThemeQo themeQo) { String themeId = themeQo.getThemeId(); //封装转发对象 FormerThemeQo former = redisCache.getObject(StringUtils.joinWith("_", CACHE_FORMER_THEME_ID, themeId), 60, () -> this.getFormerTheme(themeQo.getFormerThemeId()), FormerThemeQo.class); themeQo.setFormerTheme(former); //点赞,收藏,转发 Integer likeCount = collectionService.getCountByTypeAndId(themeId, CollectionTypeEnum.LIKE_THEME); Integer commentCount = commentService.getCommentCountByThemeId(themeId); Integer forwardCount = themeService.getForwardCountById(themeId); themeQo.setCommentCount(commentCount); themeQo.setLikeCount(likeCount); themeQo.setForwardCount(forwardCount); } //组装和当前用户相关信息 private void buildThemeExtraInfoByUser(String userId, ThemeQo themeQo){ String themeId = themeQo.getThemeId(); //是否关注作者 String authorId = themeQo.getAuthorId(); Set<String> fansSet = new HashSet<>(followRelService.queryFansByFollowerId(userId)); themeQo.setFollow(fansSet.contains(authorId)); //是否点赞 CollectionEntity likeEntity = collectionService.getNotDeleteTargetCollection(themeId, userId, CollectionTypeEnum.LIKE_THEME); themeQo.setHasLiked(likeEntity != null); //是否转发 Integer forwardCountByUser = themeService.getForwardCountByUser(themeId, userId); themeQo.setHasForward(forwardCountByUser > 0); //是否收藏 CollectionEntity collectionEntity = collectionService.getNotDeleteTargetCollection(themeId, userId, CollectionTypeEnum.COLLECT_THEME); themeQo.setHasCollect(collectionEntity != null); } //组装被转发主题 private void buildFormerTheme(ThemeQo themeQo) { String formerThemeId = themeQo.getFormerThemeId(); if (StringUtils.isNotEmpty(formerThemeId)) { ThemeQo formerTheme = ConvertUtil.themeEntityToQo(themeService.queryByThemeId(formerThemeId)); if (formerTheme != null) { //单个查询详情 batchFeignCallService.getAttachDetail(formerTheme); FormerThemeQo f = ConvertUtil.themeQo2FormerThemeQo(formerTheme); themeQo.setFormerTheme(f); } } } //返回被转发主题 private FormerThemeQo getFormerTheme(String formerThemeId) { if (StringUtils.isNotEmpty(formerThemeId)) { ThemeQo formerTheme = ConvertUtil.themeEntityToQo(themeService.queryByThemeId(formerThemeId)); if (formerTheme != null) { batchFeignCallService.getAttachDetail(formerTheme); FormerThemeQo f = ConvertUtil.themeQo2FormerThemeQo(formerTheme); return f; } } return null; } //逻辑删除 public void delete(String themeId) { themeService.deleteById(themeId); } /** * 腾讯云-内容检测 * * @param content */ private void checkContent(String content) { // 腾讯云接口最多支持5000文字校验,超过5000执行2次 // 检查内容是否涉黄违法 CommonResp commonResp = TencentcloudUtils.textModeration(content.length() > 5000 ? content.substring(0, 5000) : content); if (!commonResp.isSuccess()) { throw new BizException("内容校验失败,请检查内容后重新发送"); } if (content.length() > 5000) { CommonResp commonResp2 = TencentcloudUtils.textModeration(content.substring(5000, content.length())); if (!commonResp2.isSuccess()) { throw new BizException("内容校验失败,请检查内容后重新发送"); } } return; } /** * 直播类型做转播检查 * * @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", "很抱歉!您需要购买或报名成功后才可以添加这个直播哦~"); } } } } /** * 查询用户评论过的主题,并封装成转发主题结构 * * @param req * @param userId 当前用户 * @return */ private List<ThemeQo> getCommentThemeQos(QueryRecordThemeReq req, String userId) { List<ThemeQo> commentThemeList = new ArrayList<>(); List<ThemeEntity> themeEntities; //评论列表 List<CommentEntity> commentEntities = commentService.queryThemesByUserId(req.getUserId(), req.getLastId(), req.getPageSize()); //当前用户信息 UserInfoEntity userInfoEntity = userInfoService.queryUserById(req.getUserId()); 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); //组装附件 batchFeignCallService.getAttachDetailByBatch(themeQos); //主题列表 Map<String, ThemeQo> themeMap = themeQos.stream() .collect(Collectors.toMap(ThemeQo::getThemeId, o -> o)); //主题+评论封装转发对象 for (CommentEntity commentEntity : commentEntities) { String themeId = commentEntity.getThemeId(); //评论内容包装到ThemeContentQo里 ThemeContentQo commentContent = ThemeContentQo.builder() .type(RelTypeEnum.TEXT.type) .value(commentEntity.getContent()) .build(); //原主题包装到FormerThemeQo中 ThemeQo themeQo = themeMap.get(themeId); FormerThemeQo f = ConvertUtil.themeQo2FormerThemeQo(themeQo); //ThemeContentQo和原主题包装到FormerThemeQo中包装到ThemeQo中 ThemeQo commentThemeQo = ThemeQo.builder() .authorId(userInfoEntity.getId()) .nickName(userInfoEntity.getUiUsernameMp()) .userImg(userInfoEntity.getUiHeadimgMp()) .content(Arrays.asList(commentContent)) .formerTheme(f) .commentId(commentEntity.getCommentId()) .themeType(ThemeTypeEnum.RES_COMMENT.getCode()) .build(); commentThemeList.add(commentThemeQo); } return commentThemeList; } }