NotificationService.java 9.08 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 27 28 29 30 31 32 33 34 35 36
import java.util.List;

@Service
public class NotificationService {

    @Resource
    private NotificationMapper notificationMapper;

    @Autowired
    private UuidGenHelper uuidGenHelper;

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


    @Autowired
    private RedisCache redisCache;

刘基明's avatar
刘基明 committed
44

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

刘基明's avatar
刘基明 committed
57 58 59 60 61 62 63 64 65 66 67 68 69 70 71
    public void insert(String operatorId, String notifierId, NotificationTypeEnum type, String targetId, String content
            , LocalDateTime createTime){
        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
72

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

    }

刘基明's avatar
刘基明 committed
87

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

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

刘基明's avatar
刘基明 committed
115 116 117 118
    @Transactional
    public void insert(NotificationEntity entity){
        notificationMapper.insert(entity);
    }
刘基明's avatar
刘基明 committed
119

刘基明's avatar
刘基明 committed
120 121 122 123 124 125 126 127 128 129

    public NotificationEntity queryById(String notificationId){
        return notificationMapper.selectOne(new LambdaQueryWrapper<NotificationEntity>()
                .eq(NotificationEntity::getNotificationId,notificationId));
    }


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

刘基明's avatar
刘基明 committed
133 134 135 136 137 138

        if (StringUtils.isNotBlank(lastId)){
            NotificationEntity lastOne = notificationMapper.selectOne(new LambdaQueryWrapper<NotificationEntity>()
                    .eq(NotificationEntity::getNotificationId, lastId));
            queryWrapper.lt(NotificationEntity::getUpdateTime, lastOne.getCreateTime());
        }
刘基明's avatar
刘基明 committed
139 140 141 142 143 144
        if (!NotificationTypeEnum.ALL.getCode().equals(type)){
            queryWrapper.eq(NotificationEntity::getMessageType,type);
        }
        return notificationMapper.selectList(queryWrapper);
    }

刘基明's avatar
刘基明 committed
145

刘基明's avatar
刘基明 committed
146 147 148 149
    public void truncate() {
        notificationMapper.delete(new LambdaQueryWrapper<NotificationEntity>()
                .eq(NotificationEntity::getDeleteTag, DeleteTagEnum.NOT_DELETED.getCode()));
    }
刘基明's avatar
刘基明 committed
150

刘基明's avatar
刘基明 committed
151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179
    public void insertLike(String operatorId, String notifierId, String targetId, LocalDateTime updateTime){
        NotificationEntity entity = notificationMapper.selectOne(new LambdaQueryWrapper<NotificationEntity>()
                .eq(NotificationEntity::getMessageType, NotificationTypeEnum.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(updateTime);
            notificationMapper.updateById(entity);
        }else {
            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
180

刘基明's avatar
刘基明 committed
181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196
    // 消息通知队列缓存
    public void putNotifyCache(String notifyUserId, String operatorId, NotificationTypeEnum type){
        UserInfoResp userInfo = getUserInfo(operatorId);
        redisCache.incr(RedisKeyConstant.MESSAGE_NOTIFY_COUNT + notifyUserId);
        redisCache.set(RedisKeyConstant.MESSAGE_NOTIFY_LAST_MSG + notifyUserId, userInfo.getNickName()+type.getType()+"了你的内容", 60 * 60 * 24 * 30);
        redisCache.set(RedisKeyConstant.MESSAGE_NOTIFY_LAST_TIME + notifyUserId, TimeUtils.format(LocalDateTime.now()), 60 * 60 * 24 * 30);
    }


    // 消息通知队列缓存
    public void putNotifyCacheFollow(String notifyUserId, String operatorId) {
        UserInfoResp userInfo = getUserInfo(operatorId);
        redisCache.incr(RedisKeyConstant.MESSAGE_NOTIFY_COUNT + notifyUserId);
        redisCache.set(RedisKeyConstant.MESSAGE_NOTIFY_LAST_MSG + notifyUserId, userInfo.getNickName() + "关注了你", 60 * 60 * 24 * 30);
        redisCache.set(RedisKeyConstant.MESSAGE_NOTIFY_LAST_TIME + notifyUserId, TimeUtils.format(LocalDateTime.now()), 60 * 60 * 24 * 30);
    }
刘基明's avatar
刘基明 committed
197

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

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