NotificationManager.java 13.2 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 5
import com.tanpu.common.api.CommonResp;
import com.tanpu.common.exception.BizException;
刘基明's avatar
刘基明 committed
6
import com.tanpu.common.util.JsonUtil;
刘基明's avatar
刘基明 committed
7 8
import com.tanpu.community.api.beans.qo.FormerThemeQo;
import com.tanpu.community.api.beans.qo.NotificationQo;
刘基明's avatar
刘基明 committed
9
import com.tanpu.community.api.beans.qo.ThemeContentQo;
刘基明's avatar
刘基明 committed
10 11
import com.tanpu.community.api.beans.qo.ThemeNotifyQo;
import com.tanpu.community.api.beans.qo.ThemeQo;
刘基明's avatar
刘基明 committed
12
import com.tanpu.community.api.beans.qo.UserBriefInfoQO;
刘基明's avatar
刘基明 committed
13 14
import com.tanpu.community.api.beans.req.notification.NotifyQueryReq;
import com.tanpu.community.api.beans.vo.feign.fatools.UserInfoResp;
刘基明's avatar
刘基明 committed
15
import com.tanpu.community.api.constants.RedisKeyConstant;
刘基明's avatar
刘基明 committed
16
import com.tanpu.community.api.enums.NotificationTypeEnum;
刘基明's avatar
刘基明 committed
17
import com.tanpu.community.api.enums.ThemeTypeEnum;
刘基明's avatar
刘基明 committed
18
import com.tanpu.community.cache.RedisCache;
刘基明's avatar
刘基明 committed
19 20 21 22
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;
刘基明's avatar
刘基明 committed
23
import com.tanpu.community.dao.entity.community.NotificationEntity;
刘基明's avatar
刘基明 committed
24
import com.tanpu.community.dao.entity.community.ThemeEntity;
刘基明's avatar
刘基明 committed
25 26
import com.tanpu.community.feign.fatools.FeignClientForFatools;
import com.tanpu.community.service.BatchFeignCallService;
刘基明's avatar
刘基明 committed
27 28 29
import com.tanpu.community.service.CollectionService;
import com.tanpu.community.service.CommentService;
import com.tanpu.community.service.FollowRelService;
刘基明's avatar
刘基明 committed
30 31 32 33
import com.tanpu.community.service.NotificationService;
import com.tanpu.community.service.ThemeService;
import com.tanpu.community.service.TopicService;
import com.tanpu.community.util.ConvertUtil;
刘基明's avatar
刘基明 committed
34
import org.apache.commons.collections4.CollectionUtils;
刘基明's avatar
刘基明 committed
35 36 37 38
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
刘基明's avatar
刘基明 committed
39
import java.text.MessageFormat;
40
import java.time.LocalDateTime;
刘基明's avatar
刘基明 committed
41 42
import java.util.Collections;
import java.util.HashSet;
刘基明's avatar
刘基明 committed
43
import java.util.List;
刘基明's avatar
刘基明 committed
44
import java.util.Set;
刘基明's avatar
刘基明 committed
45 46 47 48 49 50

import static com.tanpu.community.api.constants.RedisKeyConstant.CACHE_FEIGN_USER_INFO;

@Service
public class NotificationManager {

刘基明's avatar
刘基明 committed
51
    @Resource
刘基明's avatar
刘基明 committed
52 53
    private NotificationService notificationService;

刘基明's avatar
刘基明 committed
54
    @Resource
刘基明's avatar
刘基明 committed
55 56
    private TopicService topicService;

刘基明's avatar
刘基明 committed
57
    @Resource
刘基明's avatar
刘基明 committed
58 59 60 61 62
    private ThemeService themeService;

    @Resource
    private BatchFeignCallService batchFeignCallService;

刘基明's avatar
刘基明 committed
63
    @Resource
刘基明's avatar
刘基明 committed
64 65
    private FeignClientForFatools feignClientForFatools;

刘基明's avatar
刘基明 committed
66
    @Resource
刘基明's avatar
刘基明 committed
67 68 69
    private CommentService commentService;


刘基明's avatar
刘基明 committed
70
    @Resource
刘基明's avatar
刘基明 committed
71 72
    private FollowRelService followRelService;

刘基明's avatar
刘基明 committed
73
    @Resource
刘基明's avatar
刘基明 committed
74 75
    private CollectionService collectionService;

刘基明's avatar
刘基明 committed
76
    @Resource
刘基明's avatar
刘基明 committed
77 78
    private RedisCache redisCache;

刘基明's avatar
刘基明 committed
79 80
    public List<ThemeNotifyQo> queryList(NotifyQueryReq req, String userId) {
        List<NotificationEntity> query = notificationService.query(userId, req.getType(), req.getLastId(), req.getPageSize());
刘基明's avatar
刘基明 committed
81 82 83
        if (CollectionUtils.isEmpty(query)){
            return Collections.emptyList();
        }
刘基明's avatar
刘基明 committed
84
        List<ThemeNotifyQo> themeNotifyQos = ConvertUtil.notificationEntitiy2ThemeQos(query);
刘基明's avatar
刘基明 committed
85 86
        UserInfoResp currentUser = redisCache.getObject(StringUtils.joinWith("_", CACHE_FEIGN_USER_INFO, userId),
                60, () -> this.getUserInfo(userId), UserInfoResp.class);
刘基明's avatar
刘基明 committed
87
        Set<String> followUserIds = new HashSet<>(followRelService.queryIdolsByFansId(userId));
刘基明's avatar
刘基明 committed
88 89 90 91 92
        for (ThemeNotifyQo themeNotifyQo : themeNotifyQos) {

            // 用户信息
            UserInfoResp userInfo = redisCache.getObject(StringUtils.joinWith("_", CACHE_FEIGN_USER_INFO, themeNotifyQo.getAuthorId()),
                    60, () -> this.getUserInfo(themeNotifyQo.getAuthorId()), UserInfoResp.class);
刘基明's avatar
刘基明 committed
93 94 95 96 97 98 99 100
            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());
101 102 103

            // 评论根据commentEntity找到ThemeId
            if (NotificationTypeEnum.COMMENT.getCode().equals(themeNotifyQo.getMessageType()) ||
刘基明's avatar
刘基明 committed
104
                    NotificationTypeEnum.COMMENT_LIKE.getCode().equals(themeNotifyQo.getMessageType()) ||
刘基明's avatar
刘基明 committed
105
                    NotificationTypeEnum.COMMENT_REPLY.getCode().equals(themeNotifyQo.getMessageType())) {
106 107
                // 评论类型将commentId替换为themeId
                CommentEntity commentEntity = commentService.queryByCommentId(themeNotifyQo.getCommentId());
108 109
                if (commentEntity != null) {
                    themeNotifyQo.setFormerThemeId(commentEntity.getThemeId());
刘基明's avatar
刘基明 committed
110
                    themeNotifyQo.setReplyCommentId(commentEntity.getReplyId());
111 112 113 114
                    if (NotificationTypeEnum.COMMENT_LIKE.getCode().equals(themeNotifyQo.getMessageType())) {
                        themeNotifyQo.setContent(commentEntity.getContent());
                    }
                }
115 116
            }

117
            // 封装引用对象
刘基明's avatar
刘基明 committed
118
            if (StringUtils.isNotEmpty(themeNotifyQo.getFormerThemeId())) {
刘基明's avatar
刘基明 committed
119
                ThemeEntity former = themeService.queryByThemeId(themeNotifyQo.getFormerThemeId());
刘基明's avatar
刘基明 committed
120
                if (former != null) {
刘基明's avatar
刘基明 committed
121
                    themeNotifyQo.setFormerUserName(currentUser.getNickName());
刘基明's avatar
刘基明 committed
122
                    if (StringUtils.isNotBlank(former.getTopicId())) {
123
                        // 内部话题
124 125 126
                        themeNotifyQo.setFormerTopicId(former.getTopicId());
                        themeNotifyQo.setFormerTopicTitle(topicService.queryTitleById(former.getTopicId()));
                    }
刘基明's avatar
刘基明 committed
127
                    if (ThemeTypeEnum.DISCUSSION.getCode().equals(former.getThemeType()) || ThemeTypeEnum.FORWARD.getCode().equals(former.getThemeType())) {
刘基明's avatar
刘基明 committed
128 129
                        List<ThemeContentQo> themeContentQos = JsonUtil.toBean(former.getContent(), new TypeReference<List<ThemeContentQo>>() {
                        });
刘基明's avatar
刘基明 committed
130
                        themeNotifyQo.setFormerContent(themeContentQos.get(0).getValue());
刘基明's avatar
刘基明 committed
131
                    } else if (ThemeTypeEnum.LONG_TEXT.getCode().equals(former.getThemeType())) {
刘基明's avatar
刘基明 committed
132
                        themeNotifyQo.setFormerContent(former.getTitle());
刘基明's avatar
刘基明 committed
133
                    }
刘基明's avatar
刘基明 committed
134
                } else {
刘基明's avatar
刘基明 committed
135
                    // 引用主题已被删除
刘基明's avatar
刘基明 committed
136
                    themeNotifyQo.setFormerThemeId(null);
刘基明's avatar
刘基明 committed
137
                }
刘基明's avatar
刘基明 committed
138
            }
139
            // 外部话题
刘基明's avatar
刘基明 committed
140
            if (StringUtils.isNotBlank(themeNotifyQo.getTopicId())) {
141
                themeNotifyQo.setTopicTitle(topicService.queryTitleById(themeNotifyQo.getTopicId()));
刘基明's avatar
刘基明 committed
142
            }
刘基明's avatar
刘基明 committed
143 144 145 146 147 148
            // 加入退出话题
            if (NotificationTypeEnum.TOPIC_IN.getCode().equals(themeNotifyQo.getMessageType()) ||
                    NotificationTypeEnum.TOPIC_OUT.getCode().equals(themeNotifyQo.getMessageType())) {
                themeNotifyQo.setContent(MessageFormat.format(themeNotifyQo.getContent(), themeNotifyQo.getTopicTitle()));
            }

刘基明's avatar
刘基明 committed
149
            // 评论的评论
刘基明's avatar
刘基明 committed
150
            if (NotificationTypeEnum.COMMENT_REPLY.getCode().equals(themeNotifyQo.getMessageType())) {
刘基明's avatar
刘基明 committed
151 152 153 154 155 156
                CommentEntity commentEntity = commentService.queryByCommentId(themeNotifyQo.getReplyCommentId());
                if (commentEntity != null) {
                    themeNotifyQo.setFormerContent(commentEntity.getContent());
                }
            }

刘基明's avatar
刘基明 committed
157
            // 点赞聚合
刘基明's avatar
刘基明 committed
158
            if (themeNotifyQo.getLikeUserCount() != null && themeNotifyQo.getLikeUserCount() > 1) {
刘基明's avatar
刘基明 committed
159 160
                List<UserBriefInfoQO> likeUsers = themeNotifyQo.getLikeUsers();
                for (UserBriefInfoQO likeUser : likeUsers) {
刘基明's avatar
刘基明 committed
161 162
                    UserInfoResp luser = redisCache.getObject(StringUtils.joinWith("_", CACHE_FEIGN_USER_INFO, likeUser.getUserId()),
                            60, () -> this.getUserInfo(likeUser.getUserId()), UserInfoResp.class);
刘基明's avatar
刘基明 committed
163 164 165 166
                    likeUser.setHeadImageUrl(luser.getHeadImageUrl());
                    likeUser.setNickName(luser.getNickName());
                }
            }
刘基明's avatar
刘基明 committed
167 168
            // 是否关注
            themeNotifyQo.setFollow(followUserIds.contains(themeNotifyQo.getAuthorId()));
刘基明's avatar
刘基明 committed
169
        }
刘基明's avatar
刘基明 committed
170 171

        redisCache.evict(RedisKeyConstant.MESSAGE_NOTIFY_COUNT + userId);
172 173
        // 更新查询时间,用于删除评论
        redisCache.set(RedisKeyConstant.MESSAGE_NOTIFY_QUERY_TIME + userId,
刘基明's avatar
刘基明 committed
174
                JsonUtil.toJson(LocalDateTime.now()), 60 * 60 * 24 * 30);
刘基明's avatar
刘基明 committed
175

刘基明's avatar
刘基明 committed
176 177 178 179
        return themeNotifyQos;
    }


刘基明's avatar
刘基明 committed
180
    // 查询消息通知概览
刘基明's avatar
刘基明 committed
181 182 183 184 185 186 187 188
    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();
    }

    // 初始化所有消息通知
刘基明's avatar
刘基明 committed
189
    public void initAllData() {
刘基明's avatar
刘基明 committed
190 191 192 193 194 195
        notificationService.truncate();
        // 关注
        List<FollowRelEntity> followRelEntities = followRelService.queryAll();
        for (FollowRelEntity followRelEntity : followRelEntities) {
            notificationService.insert(followRelEntity.getFansId(), followRelEntity.getIdolId(), NotificationTypeEnum.FOLLOW
                    , followRelEntity.getFansId(), "", followRelEntity.getUpdateTime());
刘基明's avatar
刘基明 committed
196

刘基明's avatar
刘基明 committed
197
            notificationService.putNotifyCacheFollow(followRelEntity.getIdolId(), followRelEntity.getFansId(), followRelEntity.getUpdateTime());
刘基明's avatar
刘基明 committed
198 199 200 201 202 203 204 205 206 207 208
        }
        // 转发
        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();
刘基明's avatar
刘基明 committed
209 210 211 212
            NotificationForwardDO content = NotificationForwardDO.builder()
                    .content(s.length() > 500 ? s.substring(0, 500) : s)
                    .themeId(themeEntity.getThemeId())
                    .topicId(themeEntity.getTopicId()).build();
刘基明's avatar
刘基明 committed
213 214
            notificationService.insert(themeEntity.getAuthorId(), former.getAuthorId(), NotificationTypeEnum.FORWARD
                    , themeEntity.getFormerThemeId(), JsonUtil.toJson(content), themeEntity.getUpdateTime());
刘基明's avatar
刘基明 committed
215
            notificationService.putNotifyCache(former.getAuthorId(), themeEntity.getAuthorId(), NotificationTypeEnum.FORWARD, themeEntity.getUpdateTime());
刘基明's avatar
刘基明 committed
216 217 218 219 220 221 222 223 224 225
        }
        // 评论
        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
226
                    , commentEntity.getCommentId(), s, commentEntity.getUpdateTime());
刘基明's avatar
刘基明 committed
227
            notificationService.putNotifyCache(themeEntity.getAuthorId(), commentEntity.getAuthorId(), NotificationTypeEnum.COMMENT, commentEntity.getUpdateTime());
刘基明's avatar
刘基明 committed
228
        }
刘基明's avatar
刘基明 committed
229

刘基明's avatar
刘基明 committed
230 231 232 233 234 235 236 237
        // 点赞
        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());
刘基明's avatar
刘基明 committed
238
            notificationService.putNotifyCache(themeEntity.getAuthorId(), collectionEntity.getUserId(), NotificationTypeEnum.LIKE, collectionEntity.getUpdateTime());
刘基明's avatar
刘基明 committed
239
        }
刘基明's avatar
刘基明 committed
240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255

    }

    //返回被转发主题
    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;
    }
刘基明's avatar
刘基明 committed
256 257 258 259 260 261 262 263 264


    private UserInfoResp getUserInfo(String authorId) {
        CommonResp<UserInfoResp> userInfoNewCommonResp = feignClientForFatools.queryUserInfoNew(authorId);
        if (userInfoNewCommonResp.isNotSuccess()) {
            throw new BizException("内部接口调用失败");
        }
        return userInfoNewCommonResp.getData();
    }
刘基明's avatar
刘基明 committed
265
}