Commit 0112d403 authored by 刘基明's avatar 刘基明

消息通知初始化

parent f1a6fd55
...@@ -55,4 +55,16 @@ public class NotificationController { ...@@ -55,4 +55,16 @@ public class NotificationController {
return CommonResp.success(notificationQO); return CommonResp.success(notificationQO);
} }
@AuthLogin
@GetMapping("/init")
@ResponseBody
@ApiOperation(value = "初始化数据")
public CommonResp<Void> initData(){
if (!"admin".equals(userHolder.getUserId())){
return CommonResp.failed("权限不足");
}
notificationManager.init();
return CommonResp.success();
}
} }
...@@ -12,22 +12,32 @@ import com.tanpu.community.api.beans.qo.ThemeQo; ...@@ -12,22 +12,32 @@ import com.tanpu.community.api.beans.qo.ThemeQo;
import com.tanpu.community.api.beans.req.notification.NotifyQueryReq; import com.tanpu.community.api.beans.req.notification.NotifyQueryReq;
import com.tanpu.community.api.beans.vo.feign.fatools.UserInfoResp; import com.tanpu.community.api.beans.vo.feign.fatools.UserInfoResp;
import com.tanpu.community.api.constants.RedisKeyConstant; 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.api.enums.ThemeTypeEnum;
import com.tanpu.community.cache.RedisCache; 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.NotificationEntity;
import com.tanpu.community.dao.entity.community.ThemeEntity; import com.tanpu.community.dao.entity.community.ThemeEntity;
import com.tanpu.community.dao.entity.community.TopicEntity; import com.tanpu.community.dao.entity.community.TopicEntity;
import com.tanpu.community.feign.fatools.FeignClientForFatools; import com.tanpu.community.feign.fatools.FeignClientForFatools;
import com.tanpu.community.service.BatchFeignCallService; 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.NotificationService;
import com.tanpu.community.service.ThemeService; import com.tanpu.community.service.ThemeService;
import com.tanpu.community.service.TopicService; import com.tanpu.community.service.TopicService;
import com.tanpu.community.util.ConvertUtil; import com.tanpu.community.util.ConvertUtil;
import com.tanpu.community.util.TimeUtils;
import org.apache.commons.lang3.StringUtils; import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service; import org.springframework.stereotype.Service;
import javax.annotation.Resource; import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.util.List; import java.util.List;
import static com.tanpu.community.api.constants.RedisKeyConstant.CACHE_FEIGN_USER_INFO; import static com.tanpu.community.api.constants.RedisKeyConstant.CACHE_FEIGN_USER_INFO;
...@@ -50,11 +60,21 @@ public class NotificationManager { ...@@ -50,11 +60,21 @@ public class NotificationManager {
@Autowired @Autowired
private FeignClientForFatools feignClientForFatools; private FeignClientForFatools feignClientForFatools;
@Autowired
private CommentService commentService;
@Autowired
private FollowRelService followRelService;
@Autowired
private CollectionService collectionService;
@Autowired @Autowired
private RedisCache redisCache; private RedisCache redisCache;
public List<ThemeNotifyQo> queryList(NotifyQueryReq req, String userId){ public List<ThemeNotifyQo> queryList(NotifyQueryReq req, String userId) {
List<NotificationEntity> query = notificationService.query(userId, req.getType(), req.getLastId(),req.getPageSize()); List<NotificationEntity> query = notificationService.query(userId, req.getType(), req.getLastId(), req.getPageSize());
List<ThemeNotifyQo> themeNotifyQos = ConvertUtil.notificationEntitiy2ThemeQos(query); List<ThemeNotifyQo> themeNotifyQos = ConvertUtil.notificationEntitiy2ThemeQos(query);
for (ThemeNotifyQo themeNotifyQo : themeNotifyQos) { for (ThemeNotifyQo themeNotifyQo : themeNotifyQos) {
...@@ -70,20 +90,20 @@ public class NotificationManager { ...@@ -70,20 +90,20 @@ public class NotificationManager {
themeNotifyQo.setBelongUserOrgId(userInfo.getBelongUserOrgId()); themeNotifyQo.setBelongUserOrgId(userInfo.getBelongUserOrgId());
themeNotifyQo.setBelongUserOrgName(userInfo.getBelongUserOrgName()); themeNotifyQo.setBelongUserOrgName(userInfo.getBelongUserOrgName());
// 封装转发对象 // 封装转发对象
if (StringUtils.isNotEmpty(themeNotifyQo.getFormerThemeId())){ if (StringUtils.isNotEmpty(themeNotifyQo.getFormerThemeId())) {
ThemeEntity former = themeService.queryByThemeId(themeNotifyQo.getFormerThemeId()); ThemeEntity former = themeService.queryByThemeId(themeNotifyQo.getFormerThemeId());
if (former!=null){ if (former != null) {
if (ThemeTypeEnum.DISCUSSION.getCode().equals(former.getThemeType())){ if (ThemeTypeEnum.DISCUSSION.getCode().equals(former.getThemeType())) {
List<ThemeContentQo> themeContentQos = JsonUtil.toBean(former.getContent(), new TypeReference<List<ThemeContentQo>>() { List<ThemeContentQo> themeContentQos = JsonUtil.toBean(former.getContent(), new TypeReference<List<ThemeContentQo>>() {
}); });
themeNotifyQo.setContent(themeContentQos.get(0).getValue()); themeNotifyQo.setContent(themeContentQos.get(0).getValue());
} }
if (ThemeTypeEnum.LONG_TEXT.getCode().equals(former.getThemeType())){ if (ThemeTypeEnum.LONG_TEXT.getCode().equals(former.getThemeType())) {
themeNotifyQo.setContent(former.getTitle()); themeNotifyQo.setContent(former.getTitle());
} }
} }
} }
if (StringUtils.isNotEmpty(themeNotifyQo.getTopicId())){ if (StringUtils.isNotEmpty(themeNotifyQo.getTopicId())) {
TopicEntity topicEntity = topicService.queryById(themeNotifyQo.getTopicId()); TopicEntity topicEntity = topicService.queryById(themeNotifyQo.getTopicId());
themeNotifyQo.setTopicTitle(topicEntity.getTopicTitle()); themeNotifyQo.setTopicTitle(topicEntity.getTopicTitle());
} }
...@@ -95,22 +115,64 @@ public class NotificationManager { ...@@ -95,22 +115,64 @@ public class NotificationManager {
} }
private UserInfoResp getUserInfo(String authorId) { public NotificationQo queryBriefInfo(String userId) {
CommonResp<UserInfoResp> userInfoNewCommonResp = feignClientForFatools.queryUserInfoNew(authorId); String count = redisCache.get(RedisKeyConstant.MESSAGE_NOTIFY_COUNT + userId);
if (userInfoNewCommonResp.isNotSuccess()) { String lastMsg = redisCache.get(RedisKeyConstant.MESSAGE_NOTIFY_LAST_MSG + userId);
throw new BizException("内部接口调用失败"); String lastTime = redisCache.get(RedisKeyConstant.MESSAGE_NOTIFY_LAST_TIME + userId);
}
return userInfoNewCommonResp.getData();
}
return NotificationQo.builder().message(lastMsg).updateCount(count == null ? 0 : Integer.parseInt(count)).updateTime(lastTime).build();
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);
}
} }
...@@ -127,4 +189,29 @@ public class NotificationManager { ...@@ -127,4 +189,29 @@ public class NotificationManager {
} }
return null; 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();
}
} }
...@@ -27,8 +27,6 @@ public class CollectionService { ...@@ -27,8 +27,6 @@ public class CollectionService {
private CollectionMapper collectionMapper; private CollectionMapper collectionMapper;
// 若不存在则新增,若存在则修改deleteTag // 若不存在则新增,若存在则修改deleteTag
@Transactional @Transactional
public boolean saveOrUpdate(String themeId, String userId, CollectionTypeEnum type) { public boolean saveOrUpdate(String themeId, String userId, CollectionTypeEnum type) {
...@@ -78,7 +76,7 @@ public class CollectionService { ...@@ -78,7 +76,7 @@ public class CollectionService {
//根据用户、主题、类型查询未删除对象 //根据用户、主题、类型查询未删除对象
public Set<String> getTargets(List<String> targetIds, String userId, CollectionTypeEnum type) { public Set<String> getTargets(List<String> targetIds, String userId, CollectionTypeEnum type) {
if (CollectionUtils.isEmpty(targetIds)){ if (CollectionUtils.isEmpty(targetIds)) {
return new HashSet<>(); return new HashSet<>();
} }
LambdaQueryWrapper<CollectionEntity> queryWrapper = new LambdaQueryWrapper<CollectionEntity>() LambdaQueryWrapper<CollectionEntity> queryWrapper = new LambdaQueryWrapper<CollectionEntity>()
...@@ -111,7 +109,7 @@ public class CollectionService { ...@@ -111,7 +109,7 @@ public class CollectionService {
} }
// 根据用户id和行为type获取target_id列表 // 根据用户id和行为type获取target_id列表
public List<String> getListByUser(String userId, CollectionTypeEnum type,String lastId,Integer pageSize) { public List<String> getListByUser(String userId, CollectionTypeEnum type, String lastId, Integer pageSize) {
LambdaQueryWrapper<CollectionEntity> queryWrapper = new LambdaQueryWrapper<CollectionEntity>() LambdaQueryWrapper<CollectionEntity> queryWrapper = new LambdaQueryWrapper<CollectionEntity>()
.eq(CollectionEntity::getUserId, userId) .eq(CollectionEntity::getUserId, userId)
.eq(CollectionEntity::getCollectionType, type.getCode()) .eq(CollectionEntity::getCollectionType, type.getCode())
...@@ -147,7 +145,7 @@ public class CollectionService { ...@@ -147,7 +145,7 @@ public class CollectionService {
} }
LambdaQueryWrapper<CollectionEntity> queryWrapper = new LambdaQueryWrapper<CollectionEntity>() LambdaQueryWrapper<CollectionEntity> queryWrapper = new LambdaQueryWrapper<CollectionEntity>()
.eq(CollectionEntity::getCollectionType, type.getCode()) .eq(CollectionEntity::getCollectionType, type.getCode())
.eq(CollectionEntity::getDeleteTag,DeleteTagEnum.NOT_DELETED.getCode()) .eq(CollectionEntity::getDeleteTag, DeleteTagEnum.NOT_DELETED.getCode())
.in(CollectionEntity::getTargetId, targetIds).groupBy(CollectionEntity::getTargetId); .in(CollectionEntity::getTargetId, targetIds).groupBy(CollectionEntity::getTargetId);
return collectionMapper.selectCountByTargetIds(queryWrapper).stream() return collectionMapper.selectCountByTargetIds(queryWrapper).stream()
.collect(Collectors.toMap(TimesCountEntity::getId, TimesCountEntity::getTimes)); .collect(Collectors.toMap(TimesCountEntity::getId, TimesCountEntity::getTimes));
...@@ -175,4 +173,12 @@ public class CollectionService { ...@@ -175,4 +173,12 @@ public class CollectionService {
return; return;
} }
} }
public List<CollectionEntity> queryALlLikeTheme() {
return collectionMapper.selectList(new LambdaQueryWrapper<CollectionEntity>()
.eq(CollectionEntity::getCollectionType, CollectionTypeEnum.LIKE_THEME.getCode())
.eq(CollectionEntity::getDeleteTag, DeleteTagEnum.NOT_DELETED.getCode())
.orderByAsc(CollectionEntity::getCreateTime));
}
} }
...@@ -154,4 +154,10 @@ public class CommentService { ...@@ -154,4 +154,10 @@ public class CommentService {
redisCache.evict(StringUtils.joinWith("_", THEME_COMMENT_COUNT, themeId)); redisCache.evict(StringUtils.joinWith("_", THEME_COMMENT_COUNT, themeId));
} }
public List<CommentEntity> queryAll() {
return commentMapper.selectList(new LambdaQueryWrapper<CommentEntity>()
.eq(CommentEntity::getDeleteTag,DeleteTagEnum.NOT_DELETED.getCode())
.orderByAsc(CommentEntity::getCreateTime));
}
} }
...@@ -129,4 +129,10 @@ public class FollowRelService { ...@@ -129,4 +129,10 @@ public class FollowRelService {
} }
public List<FollowRelEntity> queryAll() {
// todo 会丢失关注后 取消关注的情况,怎么选择?
return followRelMapper.selectList(new LambdaQueryWrapper<FollowRelEntity>()
.eq(FollowRelEntity::getDeleteTag,DeleteTagEnum.NOT_DELETED.getCode())
.orderByAsc(FollowRelEntity::getUpdateTime));
}
} }
...@@ -3,6 +3,7 @@ package com.tanpu.community.service; ...@@ -3,6 +3,7 @@ package com.tanpu.community.service;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper; import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.tanpu.common.util.JsonUtil; import com.tanpu.common.util.JsonUtil;
import com.tanpu.common.uuid.UuidGenHelper; import com.tanpu.common.uuid.UuidGenHelper;
import com.tanpu.community.api.enums.DeleteTagEnum;
import com.tanpu.community.api.enums.NotificationTypeEnum; import com.tanpu.community.api.enums.NotificationTypeEnum;
import com.tanpu.community.dao.entity.NotificationForwardDO; import com.tanpu.community.dao.entity.NotificationForwardDO;
import com.tanpu.community.dao.entity.NotificationLikeDO; import com.tanpu.community.dao.entity.NotificationLikeDO;
...@@ -14,6 +15,7 @@ import org.springframework.stereotype.Service; ...@@ -14,6 +15,7 @@ import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional; import org.springframework.transaction.annotation.Transactional;
import javax.annotation.Resource; import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.util.List; import java.util.List;
@Service @Service
...@@ -38,6 +40,21 @@ public class NotificationService { ...@@ -38,6 +40,21 @@ public class NotificationService {
insert(entity); insert(entity);
} }
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);
}
public void insertForward(String operatorId,String notifierId,String themeId,String topicId,String text){ public void insertForward(String operatorId,String notifierId,String themeId,String topicId,String text){
NotificationForwardDO forwardDO = NotificationForwardDO.builder().topicId(topicId).content(text).build(); NotificationForwardDO forwardDO = NotificationForwardDO.builder().topicId(topicId).content(text).build();
...@@ -111,5 +128,38 @@ public class NotificationService { ...@@ -111,5 +128,38 @@ public class NotificationService {
} }
public void truncate() {
notificationMapper.delete(new LambdaQueryWrapper<NotificationEntity>()
.eq(NotificationEntity::getDeleteTag, DeleteTagEnum.NOT_DELETED.getCode()));
}
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);
}
}
} }
...@@ -7,6 +7,7 @@ import com.tanpu.common.exception.BizException; ...@@ -7,6 +7,7 @@ import com.tanpu.common.exception.BizException;
import com.tanpu.common.redis.RedisHelper; import com.tanpu.common.redis.RedisHelper;
import com.tanpu.common.uuid.UuidGenHelper; import com.tanpu.common.uuid.UuidGenHelper;
import com.tanpu.community.api.enums.DeleteTagEnum; import com.tanpu.community.api.enums.DeleteTagEnum;
import com.tanpu.community.api.enums.ThemeTypeEnum;
import com.tanpu.community.dao.entity.community.ThemeEntity; import com.tanpu.community.dao.entity.community.ThemeEntity;
import com.tanpu.community.dao.entity.community.TimesCountEntity; import com.tanpu.community.dao.entity.community.TimesCountEntity;
import com.tanpu.community.dao.mapper.community.ThemeMapper; import com.tanpu.community.dao.mapper.community.ThemeMapper;
...@@ -268,4 +269,10 @@ public class ThemeService { ...@@ -268,4 +269,10 @@ public class ThemeService {
.collect(Collectors.toMap(TimesCountEntity::getId, TimesCountEntity::getTimes)); .collect(Collectors.toMap(TimesCountEntity::getId, TimesCountEntity::getTimes));
} }
public List<ThemeEntity> queryAllForward() {
return themeMapper.selectList(new LambdaQueryWrapper<ThemeEntity>()
.eq(ThemeEntity::getThemeType, ThemeTypeEnum.FORWARD.getCode())
.eq(ThemeEntity::getDeleteTag,DeleteTagEnum.NOT_DELETED.getCode())
.orderByAsc(ThemeEntity::getCreateTime));
}
} }
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment