package com.tanpu.community.manager; import com.fasterxml.jackson.core.type.TypeReference; 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.FormerThemeQo; import com.tanpu.community.api.beans.qo.NotificationQo; import com.tanpu.community.api.beans.qo.ThemeContentQo; import com.tanpu.community.api.beans.qo.ThemeNotifyQo; import com.tanpu.community.api.beans.qo.ThemeQo; import com.tanpu.community.api.beans.req.notification.NotifyQueryReq; import com.tanpu.community.api.beans.vo.feign.fatools.UserInfoResp; import com.tanpu.community.api.constants.RedisKeyConstant; import com.tanpu.community.api.enums.NotificationTypeEnum; import com.tanpu.community.api.enums.ThemeTypeEnum; import com.tanpu.community.cache.RedisCache; import com.tanpu.community.dao.entity.NotificationForwardDO; import com.tanpu.community.dao.entity.community.CollectionEntity; import com.tanpu.community.dao.entity.community.CommentEntity; import com.tanpu.community.dao.entity.community.FollowRelEntity; import com.tanpu.community.dao.entity.community.NotificationEntity; import com.tanpu.community.dao.entity.community.ThemeEntity; import com.tanpu.community.dao.entity.community.TopicEntity; import com.tanpu.community.feign.fatools.FeignClientForFatools; import com.tanpu.community.service.BatchFeignCallService; import com.tanpu.community.service.CollectionService; import com.tanpu.community.service.CommentService; import com.tanpu.community.service.FollowRelService; import com.tanpu.community.service.NotificationService; import com.tanpu.community.service.ThemeService; import com.tanpu.community.service.TopicService; import com.tanpu.community.util.ConvertUtil; import com.tanpu.community.util.TimeUtils; import org.apache.commons.lang3.StringUtils; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; import javax.annotation.Resource; import java.time.LocalDateTime; import java.util.List; import static com.tanpu.community.api.constants.RedisKeyConstant.CACHE_FEIGN_USER_INFO; @Service public class NotificationManager { @Autowired private NotificationService notificationService; @Autowired private TopicService topicService; @Autowired private ThemeService themeService; @Resource private BatchFeignCallService batchFeignCallService; @Autowired private FeignClientForFatools feignClientForFatools; @Autowired private CommentService commentService; @Autowired private FollowRelService followRelService; @Autowired private CollectionService collectionService; @Autowired private RedisCache redisCache; public List<ThemeNotifyQo> queryList(NotifyQueryReq req, String userId) { List<NotificationEntity> query = notificationService.query(userId, req.getType(), req.getLastId(), req.getPageSize()); List<ThemeNotifyQo> themeNotifyQos = ConvertUtil.notificationEntitiy2ThemeQos(query); for (ThemeNotifyQo themeNotifyQo : themeNotifyQos) { // 用户信息 UserInfoResp userInfo = redisCache.getObject(StringUtils.joinWith("_", CACHE_FEIGN_USER_INFO, themeNotifyQo.getAuthorId()), 60, () -> this.getUserInfo(themeNotifyQo.getAuthorId()), UserInfoResp.class); themeNotifyQo.setAuthorId(userInfo.getUserId()); themeNotifyQo.setNickName(userInfo.getNickName()); themeNotifyQo.setUserImg(userInfo.getHeadImageUrl()); themeNotifyQo.setUserType(userInfo.getUserType()); themeNotifyQo.setLevelGrade(userInfo.getLevelGrade()); themeNotifyQo.setUserInvestorType(userInfo.getUserInvestorType()); themeNotifyQo.setBelongUserOrgId(userInfo.getBelongUserOrgId()); themeNotifyQo.setBelongUserOrgName(userInfo.getBelongUserOrgName()); // 封装转发对象 if (StringUtils.isNotEmpty(themeNotifyQo.getFormerThemeId())) { ThemeEntity former = themeService.queryByThemeId(themeNotifyQo.getFormerThemeId()); if (former != null) { if (ThemeTypeEnum.DISCUSSION.getCode().equals(former.getThemeType())) { List<ThemeContentQo> themeContentQos = JsonUtil.toBean(former.getContent(), new TypeReference<List<ThemeContentQo>>() { }); themeNotifyQo.setContent(themeContentQos.get(0).getValue()); } if (ThemeTypeEnum.LONG_TEXT.getCode().equals(former.getThemeType())) { themeNotifyQo.setContent(former.getTitle()); } } } if (StringUtils.isNotEmpty(themeNotifyQo.getTopicId())) { TopicEntity topicEntity = topicService.queryById(themeNotifyQo.getTopicId()); themeNotifyQo.setTopicTitle(topicEntity.getTopicTitle()); } } redisCache.evict(RedisKeyConstant.MESSAGE_NOTIFY_COUNT + userId); return themeNotifyQos; } public NotificationQo queryBriefInfo(String userId) { String count = redisCache.get(RedisKeyConstant.MESSAGE_NOTIFY_COUNT + userId); String lastMsg = redisCache.get(RedisKeyConstant.MESSAGE_NOTIFY_LAST_MSG + userId); String lastTime = redisCache.get(RedisKeyConstant.MESSAGE_NOTIFY_LAST_TIME + userId); return NotificationQo.builder().message(lastMsg).updateCount(count == null ? 0 : Integer.parseInt(count)).updateTime(lastTime).build(); } // 初始化所有消息通知 public void init() { notificationService.truncate(); // 关注 List<FollowRelEntity> followRelEntities = followRelService.queryAll(); for (FollowRelEntity followRelEntity : followRelEntities) { notificationService.insert(followRelEntity.getFansId(), followRelEntity.getIdolId(), NotificationTypeEnum.FOLLOW , followRelEntity.getFansId(), "", followRelEntity.getUpdateTime()); messageNotifyFollow(followRelEntity.getIdolId(), followRelEntity.getFansId()); } // 转发 List<ThemeEntity> themeEntities = themeService.queryAllForward(); for (ThemeEntity themeEntity : themeEntities) { ThemeEntity former = themeService.queryByThemeId(themeEntity.getFormerThemeId()); if (former == null) { continue; } List<ThemeContentQo> themeContentQos = JsonUtil.toBean(themeEntity.getContent(), new TypeReference<List<ThemeContentQo>>() { }); String s = themeContentQos.get(0).getValue(); NotificationForwardDO content = NotificationForwardDO.builder().content(s.length() > 500 ? s.substring(0, 500) : s).topicId(themeEntity.getTopicId()).build(); notificationService.insert(themeEntity.getAuthorId(), former.getAuthorId(), NotificationTypeEnum.FORWARD , themeEntity.getFormerThemeId(), JsonUtil.toJson(content), themeEntity.getUpdateTime()); messageNotify(former.getAuthorId(), themeEntity.getAuthorId(), NotificationTypeEnum.FORWARD); } // 评论 List<CommentEntity> commentEntities = commentService.queryAll(); for (CommentEntity commentEntity : commentEntities) { ThemeEntity themeEntity = themeService.queryByThemeId(commentEntity.getThemeId()); if (themeEntity == null) { continue; } String s = commentEntity.getContent(); notificationService.insert(commentEntity.getAuthorId(), themeEntity.getAuthorId(), NotificationTypeEnum.COMMENT , commentEntity.getThemeId(), s, commentEntity.getUpdateTime()); messageNotify(themeEntity.getAuthorId(), commentEntity.getAuthorId(), NotificationTypeEnum.COMMENT); } // 点赞 List<CollectionEntity> collectionEntities = collectionService.queryALlLikeTheme(); for (CollectionEntity collectionEntity : collectionEntities) { ThemeEntity themeEntity = themeService.queryByThemeId(collectionEntity.getTargetId()); if (themeEntity == null) { continue; } notificationService.insertLike(collectionEntity.getUserId(), themeEntity.getAuthorId(), collectionEntity.getTargetId(), collectionEntity.getUpdateTime()); messageNotify(themeEntity.getAuthorId(), collectionEntity.getUserId(), NotificationTypeEnum.LIKE); } } //返回被转发主题 private FormerThemeQo getFormerTheme(String formerThemeId) { if (StringUtils.isNotBlank(formerThemeId)) { ThemeQo formerTheme = ConvertUtil.themeEntityToQo(themeService.queryByThemeId(formerThemeId)); if (formerTheme != null) { batchFeignCallService.getAttachDetail(formerTheme); FormerThemeQo f = ConvertUtil.themeQo2FormerThemeQo(formerTheme); return f; } } return null; } // 消息通知队列缓存 private void messageNotify(String notifyUserId, String operatorId, NotificationTypeEnum type) { UserInfoResp userInfo = getUserInfo(operatorId); redisCache.incr(RedisKeyConstant.MESSAGE_NOTIFY_COUNT + notifyUserId); redisCache.put(RedisKeyConstant.MESSAGE_NOTIFY_LAST_MSG + notifyUserId, userInfo.getNickName() + type.getType() + "了你的内容", 60 * 60 * 24 * 30); redisCache.put(RedisKeyConstant.MESSAGE_NOTIFY_LAST_TIME + notifyUserId, TimeUtils.format(LocalDateTime.now()), 60 * 60 * 24 * 30); } // 消息通知队列缓存 private void messageNotifyFollow(String notifyUserId, String operatorId) { UserInfoResp userInfo = getUserInfo(operatorId); redisCache.incr(RedisKeyConstant.MESSAGE_NOTIFY_COUNT + notifyUserId); redisCache.put(RedisKeyConstant.MESSAGE_NOTIFY_LAST_MSG + notifyUserId, userInfo.getNickName() + "关注了你", 60 * 60 * 24 * 30); redisCache.put(RedisKeyConstant.MESSAGE_NOTIFY_LAST_TIME + notifyUserId, TimeUtils.format(LocalDateTime.now()), 60 * 60 * 24 * 30); } private UserInfoResp getUserInfo(String authorId) { CommonResp<UserInfoResp> userInfoNewCommonResp = feignClientForFatools.queryUserInfoNew(authorId); if (userInfoNewCommonResp.isNotSuccess()) { throw new BizException("内部接口调用失败"); } return userInfoNewCommonResp.getData(); } }