NotificationManager.java 11.1 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 34 35 36 37 38 39
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 org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
刘基明's avatar
刘基明 committed
40 41
import java.util.Set;
import java.util.stream.Collectors;
刘基明's avatar
刘基明 committed
42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62

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;

刘基明's avatar
刘基明 committed
63 64 65 66 67 68 69 70 71 72
    @Autowired
    private CommentService commentService;


    @Autowired
    private FollowRelService followRelService;

    @Autowired
    private CollectionService collectionService;

刘基明's avatar
刘基明 committed
73 74 75
    @Autowired
    private RedisCache redisCache;

刘基明's avatar
刘基明 committed
76 77
    public List<ThemeNotifyQo> queryList(NotifyQueryReq req, String userId) {
        List<NotificationEntity> query = notificationService.query(userId, req.getType(), req.getLastId(), req.getPageSize());
刘基明's avatar
刘基明 committed
78
        List<ThemeNotifyQo> themeNotifyQos = ConvertUtil.notificationEntitiy2ThemeQos(query);
刘基明's avatar
刘基明 committed
79 80
        UserInfoResp currentUser = redisCache.getObject(StringUtils.joinWith("_", CACHE_FEIGN_USER_INFO, userId),
                60, () -> this.getUserInfo(userId), UserInfoResp.class);
刘基明's avatar
刘基明 committed
81
        Set<String> followUserIds= followRelService.queryIdolsByFansId(userId).stream().collect(Collectors.toSet());
刘基明's avatar
刘基明 committed
82 83 84 85 86
        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
87 88 89 90 91 92 93 94
            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());
95
            // 封装引用对象
刘基明's avatar
刘基明 committed
96
            if (StringUtils.isNotEmpty(themeNotifyQo.getFormerThemeId())) {
刘基明's avatar
刘基明 committed
97
                ThemeEntity former = themeService.queryByThemeId(themeNotifyQo.getFormerThemeId());
刘基明's avatar
刘基明 committed
98
                if (former != null) {
刘基明's avatar
刘基明 committed
99
                    themeNotifyQo.setFormerUserName(currentUser.getNickName());
100
                    if (StringUtils.isNotBlank(former.getTopicId())){
101
                        // 内部话题
102 103 104
                        themeNotifyQo.setFormerTopicId(former.getTopicId());
                        themeNotifyQo.setFormerTopicTitle(topicService.queryTitleById(former.getTopicId()));
                    }
刘基明's avatar
刘基明 committed
105
                    if (ThemeTypeEnum.DISCUSSION.getCode().equals(former.getThemeType()) || ThemeTypeEnum.FORWARD.getCode().equals(former.getThemeType())) {
刘基明's avatar
刘基明 committed
106 107
                        List<ThemeContentQo> themeContentQos = JsonUtil.toBean(former.getContent(), new TypeReference<List<ThemeContentQo>>() {
                        });
刘基明's avatar
刘基明 committed
108
                        themeNotifyQo.setFormerContent(themeContentQos.get(0).getValue());
刘基明's avatar
刘基明 committed
109
                    }
刘基明's avatar
刘基明 committed
110
                    if (ThemeTypeEnum.LONG_TEXT.getCode().equals(former.getThemeType())) {
刘基明's avatar
刘基明 committed
111
                        themeNotifyQo.setFormerContent(former.getTitle());
刘基明's avatar
刘基明 committed
112 113
                    }
                }
刘基明's avatar
刘基明 committed
114
            }
115
            // 外部话题
刘基明's avatar
刘基明 committed
116
            if (StringUtils.isNotBlank(themeNotifyQo.getTopicId())) {
117
                themeNotifyQo.setTopicTitle(topicService.queryTitleById(themeNotifyQo.getTopicId()));
刘基明's avatar
刘基明 committed
118
            }
刘基明's avatar
刘基明 committed
119
            // 点赞聚合
刘基明's avatar
刘基明 committed
120 121 122
            if (themeNotifyQo.getLikeUserCount()!=null && themeNotifyQo.getLikeUserCount()>1) {
                List<UserBriefInfoQO> likeUsers = themeNotifyQo.getLikeUsers();
                for (UserBriefInfoQO likeUser : likeUsers) {
刘基明's avatar
刘基明 committed
123 124
                    UserInfoResp luser = redisCache.getObject(StringUtils.joinWith("_", CACHE_FEIGN_USER_INFO, likeUser.getUserId()),
                            60, () -> this.getUserInfo(likeUser.getUserId()), UserInfoResp.class);
刘基明's avatar
刘基明 committed
125 126 127 128
                    likeUser.setHeadImageUrl(luser.getHeadImageUrl());
                    likeUser.setNickName(luser.getNickName());
                }
            }
刘基明's avatar
刘基明 committed
129 130
            // 是否关注
            themeNotifyQo.setFollow(followUserIds.contains(themeNotifyQo.getAuthorId()));
刘基明's avatar
刘基明 committed
131
        }
刘基明's avatar
刘基明 committed
132 133 134

        redisCache.evict(RedisKeyConstant.MESSAGE_NOTIFY_COUNT + userId);

刘基明's avatar
刘基明 committed
135 136 137 138
        return themeNotifyQos;
    }


刘基明's avatar
刘基明 committed
139
    // 查询消息通知概览
刘基明's avatar
刘基明 committed
140 141 142 143
    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);
刘基明's avatar
刘基明 committed
144

刘基明's avatar
刘基明 committed
145 146 147 148
        return NotificationQo.builder().message(lastMsg).updateCount(count == null ? 0 : Integer.parseInt(count)).updateTime(lastTime).build();
    }

    // 初始化所有消息通知
刘基明's avatar
刘基明 committed
149
    public void initAllData() {
刘基明's avatar
刘基明 committed
150 151 152 153 154 155
        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
156 157

            notificationService.putNotifyCacheFollow(followRelEntity.getIdolId(), followRelEntity.getFansId(),followRelEntity.getUpdateTime());
刘基明's avatar
刘基明 committed
158 159 160 161 162 163 164 165 166 167 168
        }
        // 转发
        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
169 170 171 172
            NotificationForwardDO content = NotificationForwardDO.builder()
                    .content(s.length() > 500 ? s.substring(0, 500) : s)
                    .themeId(themeEntity.getThemeId())
                    .topicId(themeEntity.getTopicId()).build();
刘基明's avatar
刘基明 committed
173 174
            notificationService.insert(themeEntity.getAuthorId(), former.getAuthorId(), NotificationTypeEnum.FORWARD
                    , themeEntity.getFormerThemeId(), JsonUtil.toJson(content), themeEntity.getUpdateTime());
刘基明's avatar
刘基明 committed
175
            notificationService.putNotifyCache(former.getAuthorId(), themeEntity.getAuthorId(), NotificationTypeEnum.FORWARD,themeEntity.getUpdateTime());
刘基明's avatar
刘基明 committed
176 177 178 179 180 181 182 183 184 185 186
        }
        // 评论
        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());
刘基明's avatar
刘基明 committed
187
            notificationService.putNotifyCache(themeEntity.getAuthorId(), commentEntity.getAuthorId(), NotificationTypeEnum.COMMENT, commentEntity.getUpdateTime());
刘基明's avatar
刘基明 committed
188
        }
刘基明's avatar
刘基明 committed
189

刘基明's avatar
刘基明 committed
190 191 192 193 194 195 196 197
        // 点赞
        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
198
            notificationService.putNotifyCache(themeEntity.getAuthorId(), collectionEntity.getUserId(), NotificationTypeEnum.LIKE, collectionEntity.getUpdateTime());
刘基明's avatar
刘基明 committed
199
        }
刘基明's avatar
刘基明 committed
200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215

    }

    //返回被转发主题
    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
216 217 218 219 220 221 222 223 224 225



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