NotificationService.java 16.1 KB
Newer Older
刘基明's avatar
刘基明 committed
1 2 3
package com.tanpu.community.service;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
刘基明's avatar
刘基明 committed
4 5
import com.tanpu.common.api.CommonResp;
import com.tanpu.common.exception.BizException;
刘基明's avatar
刘基明 committed
6 7
import com.tanpu.common.util.JsonUtil;
import com.tanpu.common.uuid.UuidGenHelper;
刘基明's avatar
刘基明 committed
8 9
import com.tanpu.community.api.beans.vo.feign.fatools.UserInfoResp;
import com.tanpu.community.api.constants.RedisKeyConstant;
刘基明's avatar
刘基明 committed
10
import com.tanpu.community.api.enums.DeleteTagEnum;
刘基明's avatar
刘基明 committed
11
import com.tanpu.community.api.enums.NotificationTypeEnum;
刘基明's avatar
刘基明 committed
12
import com.tanpu.community.cache.RedisCache;
刘基明's avatar
刘基明 committed
13 14 15 16
import com.tanpu.community.dao.entity.NotificationForwardDO;
import com.tanpu.community.dao.entity.NotificationLikeDO;
import com.tanpu.community.dao.entity.community.NotificationEntity;
import com.tanpu.community.dao.mapper.community.NotificationMapper;
刘基明's avatar
刘基明 committed
17 18
import com.tanpu.community.feign.fatools.FeignClientForFatools;
import com.tanpu.community.util.TimeUtils;
刘基明's avatar
刘基明 committed
19
import org.apache.commons.lang3.StringUtils;
刘基明's avatar
刘基明 committed
20 21
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
刘基明's avatar
刘基明 committed
22
import org.springframework.transaction.annotation.Transactional;
刘基明's avatar
刘基明 committed
23 24

import javax.annotation.Resource;
刘基明's avatar
刘基明 committed
25
import java.time.LocalDateTime;
刘基明's avatar
刘基明 committed
26
import java.util.Arrays;
刘基明's avatar
刘基明 committed
27 28 29 30 31 32 33 34 35 36 37
import java.util.List;

@Service
public class NotificationService {

    @Resource
    private NotificationMapper notificationMapper;

    @Autowired
    private UuidGenHelper uuidGenHelper;

刘基明's avatar
刘基明 committed
38 39 40 41 42 43 44
    @Autowired
    private FeignClientForFatools feignClientForFatools;


    @Autowired
    private RedisCache redisCache;

刘基明's avatar
刘基明 committed
45

刘基明's avatar
刘基明 committed
46
    public void insert(String operatorId, String notifierId, NotificationTypeEnum type, String targetId, String content) {
刘基明's avatar
刘基明 committed
47
        NotificationEntity entity = NotificationEntity.builder().operatorId(operatorId)
刘基明's avatar
刘基明 committed
48 49 50 51
                .notificationId(uuidGenHelper.getUuidStr())
                .notifiedUserId(notifierId)
                .messageType(type.getCode())
                .content(content)
刘基明's avatar
刘基明 committed
52 53 54
                .operatorId(operatorId)
                .targetId(targetId)
                .build();
刘基明's avatar
刘基明 committed
55
        insert(entity);
刘基明's avatar
刘基明 committed
56 57
    }

刘基明's avatar
刘基明 committed
58
    public void insert(String operatorId, String notifierId, NotificationTypeEnum type, String targetId, String content
刘基明's avatar
刘基明 committed
59
            , LocalDateTime createTime) {
刘基明's avatar
刘基明 committed
60 61 62 63 64 65 66 67 68 69 70 71 72
        NotificationEntity entity = NotificationEntity.builder().operatorId(operatorId)
                .notificationId(uuidGenHelper.getUuidStr())
                .notifiedUserId(notifierId)
                .messageType(type.getCode())
                .content(content)
                .operatorId(operatorId)
                .targetId(targetId)
                .createTime(createTime)
                .updateTime(createTime)
                .build();
        insert(entity);
    }

刘基明's avatar
刘基明 committed
73

刘基明's avatar
刘基明 committed
74
    public void insertForward(String operatorId, String notifierId, String forwardThemeId, String topicId, String text, String themeId) {
75
        NotificationForwardDO forwardDO = NotificationForwardDO.builder().topicId(topicId).content(text).themeId(themeId).build();
刘基明's avatar
刘基明 committed
76
        NotificationEntity entity = NotificationEntity.builder().operatorId(operatorId)
刘基明's avatar
刘基明 committed
77 78
                .notificationId(uuidGenHelper.getUuidStr())
                .notifiedUserId(notifierId)
刘基明's avatar
刘基明 committed
79
                .messageType(NotificationTypeEnum.FORWARD.getCode())
刘基明's avatar
刘基明 committed
80 81
                .content(JsonUtil.toJson(forwardDO))
                .operatorId(operatorId)
82
                .targetId(forwardThemeId)
刘基明's avatar
刘基明 committed
83
                .build();
刘基明's avatar
刘基明 committed
84
        insert(entity);
刘基明's avatar
刘基明 committed
85 86 87

    }

刘基明's avatar
刘基明 committed
88
    @Transactional
刘基明's avatar
刘基明 committed
89
    public void insertLike(String operatorId, String notifierId, String targetId) {
刘基明's avatar
刘基明 committed
90 91
        NotificationEntity entity = notificationMapper.selectOne(new LambdaQueryWrapper<NotificationEntity>()
                .eq(NotificationEntity::getMessageType, NotificationTypeEnum.LIKE.getCode())
刘基明's avatar
刘基明 committed
92
                .eq(NotificationEntity::getTargetId, targetId)
刘基明's avatar
刘基明 committed
93
                .eq(NotificationEntity::getNotifiedUserId, notifierId));
刘基明's avatar
刘基明 committed
94
        if (entity != null) {
刘基明's avatar
刘基明 committed
95 96 97
            NotificationLikeDO notificationLikeDO = JsonUtil.toBean(entity.getContent(), NotificationLikeDO.class);
            notificationLikeDO.addItem(operatorId);
            entity.setContent(JsonUtil.toJson(notificationLikeDO));
刘基明's avatar
刘基明 committed
98
            entity.setUpdateTime(LocalDateTime.now());
刘基明's avatar
刘基明 committed
99
            notificationMapper.updateById(entity);
刘基明's avatar
刘基明 committed
100
        } else {
刘基明's avatar
刘基明 committed
101 102
            NotificationLikeDO notificationLikeDO = new NotificationLikeDO();
            notificationLikeDO.addItem(operatorId);
刘基明's avatar
刘基明 committed
103
            NotificationEntity build = NotificationEntity.builder().operatorId(operatorId)
刘基明's avatar
刘基明 committed
104 105 106 107 108 109 110
                    .notificationId(uuidGenHelper.getUuidStr())
                    .messageType(NotificationTypeEnum.LIKE.getCode())
                    .notifiedUserId(notifierId)
                    .content(JsonUtil.toJson(notificationLikeDO))
                    .targetId(targetId)
                    .operatorId(operatorId)
                    .build();
刘基明's avatar
刘基明 committed
111
            insert(build);
刘基明's avatar
刘基明 committed
112
        }
刘基明's avatar
刘基明 committed
113

刘基明's avatar
刘基明 committed
114 115
    }

刘基明's avatar
刘基明 committed
116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143
    @Transactional
    public void insertLikeComment(String operatorId, String notifierId, String targetId) {
        NotificationEntity entity = notificationMapper.selectOne(new LambdaQueryWrapper<NotificationEntity>()
                .eq(NotificationEntity::getMessageType, NotificationTypeEnum.COMMENT_LIKE.getCode())
                .eq(NotificationEntity::getTargetId, targetId)
                .eq(NotificationEntity::getNotifiedUserId, notifierId));
        if (entity != null) {
            NotificationLikeDO notificationLikeDO = JsonUtil.toBean(entity.getContent(), NotificationLikeDO.class);
            notificationLikeDO.addItem(operatorId);
            entity.setContent(JsonUtil.toJson(notificationLikeDO));
            entity.setUpdateTime(LocalDateTime.now());
            notificationMapper.updateById(entity);
        } else {
            NotificationLikeDO notificationLikeDO = new NotificationLikeDO();
            notificationLikeDO.addItem(operatorId);
            NotificationEntity build = NotificationEntity.builder().operatorId(operatorId)
                    .notificationId(uuidGenHelper.getUuidStr())
                    .messageType(NotificationTypeEnum.COMMENT_LIKE.getCode())
                    .notifiedUserId(notifierId)
                    .content(JsonUtil.toJson(notificationLikeDO))
                    .targetId(targetId)
                    .operatorId(operatorId)
                    .build();
            insert(build);
        }

    }

刘基明's avatar
刘基明 committed
144
    @Transactional
刘基明's avatar
刘基明 committed
145
    public void insert(NotificationEntity entity) {
刘基明's avatar
刘基明 committed
146 147
        notificationMapper.insert(entity);
    }
刘基明's avatar
刘基明 committed
148

刘基明's avatar
刘基明 committed
149

刘基明's avatar
刘基明 committed
150
    public NotificationEntity queryById(String notificationId) {
刘基明's avatar
刘基明 committed
151
        return notificationMapper.selectOne(new LambdaQueryWrapper<NotificationEntity>()
刘基明's avatar
刘基明 committed
152
                .eq(NotificationEntity::getNotificationId, notificationId));
刘基明's avatar
刘基明 committed
153 154 155
    }


刘基明's avatar
刘基明 committed
156
    public List<NotificationEntity> query(String userId, Integer type, String lastId, Integer pageSize) {
刘基明's avatar
刘基明 committed
157 158
        LambdaQueryWrapper<NotificationEntity> queryWrapper = new LambdaQueryWrapper<NotificationEntity>()
                .eq(NotificationEntity::getNotifiedUserId, userId)
刘基明's avatar
刘基明 committed
159
                .orderByDesc(NotificationEntity::getUpdateTime)
刘基明's avatar
刘基明 committed
160 161
                .last("limit " + pageSize);

刘基明's avatar
刘基明 committed
162

刘基明's avatar
刘基明 committed
163
        if (StringUtils.isNotBlank(lastId)) {
刘基明's avatar
刘基明 committed
164 165 166 167
            NotificationEntity lastOne = notificationMapper.selectOne(new LambdaQueryWrapper<NotificationEntity>()
                    .eq(NotificationEntity::getNotificationId, lastId));
            queryWrapper.lt(NotificationEntity::getUpdateTime, lastOne.getCreateTime());
        }
刘基明's avatar
刘基明 committed
168 169 170 171 172
        if (!NotificationTypeEnum.ALL.getCode().equals(type)) {
            if (NotificationTypeEnum.LIKE.getCode().equals(type)) {
                // 类型2点赞返回评论点赞和主题点赞
                queryWrapper.in(NotificationEntity::getMessageType,
                        Arrays.asList(NotificationTypeEnum.LIKE.getCode(), NotificationTypeEnum.COMMENT_LIKE.getCode()));
刘基明's avatar
刘基明 committed
173
            } else if (NotificationTypeEnum.COMMENT.getCode().equals(type)) {
刘基明's avatar
刘基明 committed
174 175 176 177 178 179 180
                // 类型3返回 回复主题和回复评论
                queryWrapper.in(NotificationEntity::getMessageType,
                        Arrays.asList(NotificationTypeEnum.COMMENT.getCode(), NotificationTypeEnum.COMMENT_REPLY.getCode()));
            }else {
                queryWrapper.eq(NotificationEntity::getMessageType, type);

            }
刘基明's avatar
刘基明 committed
181 182 183 184
        }
        return notificationMapper.selectList(queryWrapper);
    }

刘基明's avatar
刘基明 committed
185

刘基明's avatar
刘基明 committed
186 187 188 189
    public void truncate() {
        notificationMapper.delete(new LambdaQueryWrapper<NotificationEntity>()
                .eq(NotificationEntity::getDeleteTag, DeleteTagEnum.NOT_DELETED.getCode()));
    }
刘基明's avatar
刘基明 committed
190

刘基明's avatar
刘基明 committed
191
    public void insertLike(String operatorId, String notifierId, String targetId, LocalDateTime updateTime) {
刘基明's avatar
刘基明 committed
192 193
        NotificationEntity entity = notificationMapper.selectOne(new LambdaQueryWrapper<NotificationEntity>()
                .eq(NotificationEntity::getMessageType, NotificationTypeEnum.LIKE.getCode())
刘基明's avatar
刘基明 committed
194
                .eq(NotificationEntity::getTargetId, targetId)
刘基明's avatar
刘基明 committed
195
                .eq(NotificationEntity::getNotifiedUserId, notifierId));
刘基明's avatar
刘基明 committed
196
        if (entity != null) {
刘基明's avatar
刘基明 committed
197 198 199 200 201 202
            NotificationLikeDO notificationLikeDO = JsonUtil.toBean(entity.getContent(), NotificationLikeDO.class);
            notificationLikeDO.addItem(operatorId);

            entity.setContent(JsonUtil.toJson(notificationLikeDO));
            entity.setUpdateTime(updateTime);
            notificationMapper.updateById(entity);
刘基明's avatar
刘基明 committed
203
        } else {
刘基明's avatar
刘基明 committed
204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219
            NotificationLikeDO notificationLikeDO = new NotificationLikeDO();
            notificationLikeDO.addItem(operatorId);
            NotificationEntity build = NotificationEntity.builder().operatorId(operatorId)
                    .notificationId(uuidGenHelper.getUuidStr())
                    .messageType(NotificationTypeEnum.LIKE.getCode())
                    .notifiedUserId(notifierId)
                    .content(JsonUtil.toJson(notificationLikeDO))
                    .targetId(targetId)
                    .operatorId(operatorId)
                    .createTime(updateTime)
                    .updateTime(updateTime)
                    .build();
            insert(build);
        }

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

刘基明's avatar
刘基明 committed
221
    // 消息通知队列缓存
刘基明's avatar
刘基明 committed
222
    public void putNotifyCache(String notifyUserId, String operatorId, NotificationTypeEnum type) {
刘基明's avatar
刘基明 committed
223 224 225
        UserInfoResp userInfo = getUserInfo(operatorId);
        redisCache.incr(RedisKeyConstant.MESSAGE_NOTIFY_COUNT + notifyUserId);
        redisCache.set(RedisKeyConstant.MESSAGE_NOTIFY_LAST_TIME + notifyUserId, TimeUtils.format(LocalDateTime.now()), 60 * 60 * 24 * 30);
226 227
        if (NotificationTypeEnum.COMMENT_LIKE.equals(type)) {
            redisCache.set(RedisKeyConstant.MESSAGE_NOTIFY_LAST_MSG + notifyUserId, userInfo.getNickName() + "点赞了你的评论", 60 * 60 * 24 * 30);
刘基明's avatar
刘基明 committed
228
        } else if (NotificationTypeEnum.FOLLOW.equals(type)) {
229
            redisCache.set(RedisKeyConstant.MESSAGE_NOTIFY_LAST_MSG + notifyUserId, userInfo.getNickName() + "关注了你", 60 * 60 * 24 * 30);
刘基明's avatar
刘基明 committed
230 231
        } else if (NotificationTypeEnum.COMMENT_REPLY.equals(type)) {
            redisCache.set(RedisKeyConstant.MESSAGE_NOTIFY_LAST_MSG + notifyUserId, "您有一条评论被回复啦,快去看看吧~", 60 * 60 * 24 * 30);
232 233 234
        } else {
            redisCache.set(RedisKeyConstant.MESSAGE_NOTIFY_LAST_MSG + notifyUserId, userInfo.getNickName() + type.getType() + "了你的内容", 60 * 60 * 24 * 30);
        }
刘基明's avatar
刘基明 committed
235
    }
刘基明's avatar
刘基明 committed
236 237


238
    // 删除评论通知及缓存,并更新
239
    public void deleteCommentNotify(String notifyUserId, String operatorId, String commentId, LocalDateTime commentTime) {
240
        // 物理删除
241 242
        NotificationEntity commentNotify = notificationMapper.selectOne(new LambdaQueryWrapper<NotificationEntity>()
                .eq(NotificationEntity::getTargetId, commentId)
243
                .eq(NotificationEntity::getOperatorId, operatorId)
244 245
                .eq(NotificationEntity::getNotifiedUserId, notifyUserId)
                .eq(NotificationEntity::getMessageType, NotificationTypeEnum.COMMENT.getCode()));
246 247 248
        NotificationEntity last = notificationMapper.selectOne(new LambdaQueryWrapper<NotificationEntity>().eq(NotificationEntity::getNotifiedUserId, notifyUserId)
                .orderByDesc(NotificationEntity::getUpdateTime)
                .last("limit 1"));
刘基明's avatar
刘基明 committed
249
        if (commentNotify == null || last == null) {
250 251
            return;
        }
252 253 254 255 256 257 258
        notificationMapper.deleteById(commentNotify.getId());
        // 如果删除的评论是最後一條通知,则需要从库中再找一条最新的
        if (last.getNotificationId().equals(commentNotify.getNotificationId())) {
            last = notificationMapper.selectOne(new LambdaQueryWrapper<NotificationEntity>().eq(NotificationEntity::getNotifiedUserId, notifyUserId)
                    .orderByDesc(NotificationEntity::getUpdateTime)
                    .last("limit 1"));
            if (last == null) {
259
                // 没有其他通知,置空
260 261 262 263
                redisCache.evict(RedisKeyConstant.MESSAGE_NOTIFY_LAST_MSG + notifyUserId);
                redisCache.evict(RedisKeyConstant.MESSAGE_NOTIFY_LAST_TIME + notifyUserId);

            } else {
264
                // 有通知
265 266 267 268 269 270 271 272 273 274 275 276
                UserInfoResp userInfo = getUserInfo(last.getOperatorId());
                redisCache.set(RedisKeyConstant.MESSAGE_NOTIFY_LAST_TIME + notifyUserId, TimeUtils.format(last.getUpdateTime()), 60 * 60 * 24 * 30);
                if (NotificationTypeEnum.FOLLOW.getCode().equals(last.getMessageType())) {
                    redisCache.set(RedisKeyConstant.MESSAGE_NOTIFY_LAST_MSG + notifyUserId, userInfo.getNickName() + "关注了你", 60 * 60 * 24 * 30);
                } else {
                    redisCache.set(RedisKeyConstant.MESSAGE_NOTIFY_LAST_MSG + notifyUserId
                            , userInfo.getNickName() + NotificationTypeEnum.lookup(last.getMessageType()) + "了你的内容"
                            , 60 * 60 * 24 * 30);
                }
            }
        }

277
        // 处理更新数量
278
        String lastTime = redisCache.get(RedisKeyConstant.MESSAGE_NOTIFY_QUERY_TIME + notifyUserId);
279
        LocalDateTime lastQueryTime = StringUtils.isNotBlank(lastTime) ? JsonUtil.toBean(lastTime, LocalDateTime.class) : LocalDateTime.now();
280
        // 如果删除的评论时时间在红点提示时间内,则缓存数-1
281
        if (StringUtils.isBlank(lastTime) || TimeUtils.lessThan(lastQueryTime, commentTime)) {
282 283 284 285
            redisCache.decr(RedisKeyConstant.MESSAGE_NOTIFY_COUNT + notifyUserId);
        }
    }

刘基明's avatar
刘基明 committed
286 287 288 289 290 291 292 293

    // init用
    public void putNotifyCache(String notifyUserId, String operatorId, NotificationTypeEnum type, LocalDateTime updateTime) {
        UserInfoResp userInfo = getUserInfo(operatorId);
        redisCache.incr(RedisKeyConstant.MESSAGE_NOTIFY_COUNT + notifyUserId);
        String cacheTime = redisCache.get(RedisKeyConstant.MESSAGE_NOTIFY_LAST_TIME + notifyUserId);
        if (StringUtils.isBlank(cacheTime) || TimeUtils.lessThan(cacheTime, updateTime)) {
            redisCache.set(RedisKeyConstant.MESSAGE_NOTIFY_LAST_MSG + notifyUserId, userInfo.getNickName() + type.getType() + "了你的内容", 60 * 60 * 24 * 30);
294
            redisCache.set(RedisKeyConstant.MESSAGE_NOTIFY_LAST_TIME + notifyUserId, TimeUtils.format(updateTime), 60 * 60 * 24 * 30);
刘基明's avatar
刘基明 committed
295 296 297 298 299 300 301 302 303 304
        }
    }

    // init用
    public void putNotifyCacheFollow(String notifyUserId, String operatorId, LocalDateTime updateTime) {
        UserInfoResp userInfo = getUserInfo(operatorId);
        redisCache.incr(RedisKeyConstant.MESSAGE_NOTIFY_COUNT + notifyUserId);
        String cacheTime = redisCache.get(RedisKeyConstant.MESSAGE_NOTIFY_LAST_TIME + notifyUserId);
        if (StringUtils.isBlank(cacheTime) || TimeUtils.lessThan(cacheTime, updateTime)) {
            redisCache.set(RedisKeyConstant.MESSAGE_NOTIFY_LAST_MSG + notifyUserId, userInfo.getNickName() + "关注了你", 60 * 60 * 24 * 30);
305
            redisCache.set(RedisKeyConstant.MESSAGE_NOTIFY_LAST_TIME + notifyUserId, TimeUtils.format(updateTime), 60 * 60 * 24 * 30);
刘基明's avatar
刘基明 committed
306 307 308

        }
    }
309 310 311 312 313 314

    public void deleteComment(String commentId) {

    }


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