TASK:动态

This commit is contained in:
2024-08-23 17:34:38 +08:00
parent 8a774aa328
commit e86b576050
5 changed files with 94 additions and 6 deletions

View File

@@ -5,15 +5,18 @@ import com.ai.da.common.context.UserContext;
import com.ai.da.common.response.PageBaseResponse;
import com.ai.da.common.utils.CopyUtil;
import com.ai.da.common.utils.MinioUtil;
import com.ai.da.common.utils.RedisUtil;
import com.ai.da.common.websocket.NotificationConnection;
import com.ai.da.mapper.primary.CanvasMapper;
import com.ai.da.mapper.primary.NotificationMapper;
import com.ai.da.mapper.primary.SysNotificationReadStatusMapper;
import com.ai.da.mapper.primary.entity.Account;
import com.ai.da.mapper.primary.entity.Notification;
import com.ai.da.mapper.primary.entity.Portfolio;
import com.ai.da.mapper.primary.entity.SysNotificationReadStatus;
import com.ai.da.model.dto.GetNotificationDTO;
import com.ai.da.model.vo.NotificationVO;
import com.ai.da.model.dto.PublishSysNotificationDTO;
import com.ai.da.model.vo.NotificationVO;
import com.ai.da.service.AccountService;
import com.ai.da.service.MessageCenterService;
import com.ai.da.service.PortfolioService;
@@ -27,11 +30,13 @@ import com.google.gson.Gson;
import com.mysql.cj.util.StringUtils;
import io.netty.util.internal.StringUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import java.io.IOException;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.*;
import java.util.stream.Collectors;
@@ -50,16 +55,24 @@ public class MessageCenterServiceImpl extends ServiceImpl<NotificationMapper, No
private MinioUtil minioUtil;
@Resource
private PortfolioService portfolioService;
@Resource
private CanvasMapper canvasMapper;
@Resource
private RedisUtil redisUtil;
@Value("${redis.key.newPosted}")
private String lastViewNewPostedTimeKey;
@Override
public Map<String, Long> getAllTypeMessageUnreadCount() {
List<Map<String, Object>> typeCount = baseMapper.getTypeCount(UserContext.getUserHolder().getId());
Long accountId = UserContext.getUserHolder().getId();
List<Map<String, Object>> typeCount = baseMapper.getTypeCount(accountId);
Map<String, Long> msgTypeCount = typeCount.stream()
.collect(Collectors.toMap(
map -> (String) map.get("type"),
map -> Objects.isNull(map.get("count")) ? 0L : (Long) map.get("count")));
msgTypeCount.put("system", getUnreadSystemNotification());
msgTypeCount.put("newPosted", getNewPostedCount(accountId));
log.info(msgTypeCount.toString());
// 整理数据 加上系统消息未读数
return msgTypeCount;
@@ -69,6 +82,11 @@ public class MessageCenterServiceImpl extends ServiceImpl<NotificationMapper, No
@Override
public PageBaseResponse<NotificationVO> getHistoryNotification(GetNotificationDTO getNotificationDTO) {
Long accountId = UserContext.getUserHolder().getId();
// 查动态
if (!StringUtils.isNullOrEmpty(getNotificationDTO.getType()) && getNotificationDTO.getType().equals("newPosted")){
return getNewPosted(accountId, getNotificationDTO.getPage(), getNotificationDTO.getSize());
}
QueryWrapper<Notification> queryWrapper = new QueryWrapper<>();
if (!StringUtils.isNullOrEmpty(getNotificationDTO.getType())) {
queryWrapper.eq("type", getNotificationDTO.getType());
@@ -116,7 +134,6 @@ public class MessageCenterServiceImpl extends ServiceImpl<NotificationMapper, No
pushMessage(notification.getType(), notification.getSenderId());
}
/**
* 只记录唯一点赞和关注
* 重复点赞、关注只会记录最新的一次操作
@@ -140,7 +157,6 @@ public class MessageCenterServiceImpl extends ServiceImpl<NotificationMapper, No
return Boolean.FALSE;
}
// 消息推送 只需要返回当前操作类型和该操作未读消息数量
public void pushMessage(String type, Long receiverId) {
// 推送消息到前端
@@ -216,7 +232,6 @@ public class MessageCenterServiceImpl extends ServiceImpl<NotificationMapper, No
// 设置个人消息的已读状态 (允许一次已读多条个人消息)
public Boolean setReadStatus(List<Long> notificationIdList, String type) {
if (type.equals("system")) {
setReadStatusSystem(notificationIdList);
} else {
@@ -269,5 +284,61 @@ public class MessageCenterServiceImpl extends ServiceImpl<NotificationMapper, No
prePushMessage(notification);
}
public Long getNewPostedCount(Long accountId){
// 1.1 获取我关注的所有用户
List<Long> followeeList = portfolioService.getFolloweeList(accountId);
// 1.2 查询我关注的用户在我上次查看动态之后发布的作品数量
String lastViewTime = redisUtil.getFromString(lastViewNewPostedTimeKey + ":" + accountId);
QueryWrapper<Portfolio> queryWrapper = new QueryWrapper<>();
queryWrapper.in("account_id", followeeList);
if (!StringUtil.isNullOrEmpty(lastViewTime)){
queryWrapper.gt("create_date", lastViewTime);
}else {
return 0L;
}
return portfolioService.getBaseMapper().selectCount(queryWrapper);
}
/**
* 获取关注用户发布的新作品 分页查询
*/
public PageBaseResponse<NotificationVO> getNewPosted(Long accountId, Integer page, Integer size){
// 1、获取关注用户发布的所有作品
// 1.1 获取我关注的所有用户
List<Long> followeeList = portfolioService.getFolloweeList(accountId);
// 1.2 分页查询我关注的用户发布的作品
QueryWrapper<Portfolio> queryWrapper = new QueryWrapper<>();
queryWrapper.in("account_id", followeeList).orderByDesc("create_date");
Page<Portfolio> portfolioPage = portfolioService.getBaseMapper().selectPage(new Page<>(page, size), queryWrapper);
// 2、组装返回的数据
IPage<NotificationVO> convert = portfolioPage.convert(o -> {
NotificationVO notificationVO = CopyUtil.copyObject(o, NotificationVO.class);
notificationVO.setPortfolioId(o.getId());
notificationVO.setSenderId(o.getAccountId());
notificationVO.setReceiverId(accountId);
notificationVO.setCreateTime(o.getCreateDate());
notificationVO.setType("newPosted");
Account account = accountService.getById(o.getAccountId());
notificationVO.setUserName(account.getUserName());
String avatar = StringUtil.isNullOrEmpty(account.getAvatar()) ? CommonConstant.DEFAULT_AVATAR : account.getAvatar();
notificationVO.setAvatar(minioUtil.getPreSignedUrl(avatar, CommonConstant.MINIO_IMAGE_EXPIRE_TIME));
notificationVO.setPortfolioName(o.getPortfolioName());
String canvas = canvasMapper.selectById(o.getCanvasId()).getUrl();
notificationVO.setCanvas(minioUtil.getPreSignedUrl(canvas, CommonConstant.MINIO_IMAGE_EXPIRE_TIME));
String format = LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"));
// 增加访问时间记录
redisUtil.addToString(lastViewNewPostedTimeKey + ":" + accountId, format);
return notificationVO;
});
return PageBaseResponse.success(convert);
}
}

View File

@@ -1085,4 +1085,17 @@ public class PortfolioServiceImpl extends ServiceImpl<PortfolioMapper, Portfolio
return baseMapper.selectCount(queryWrapper);
}
/**
* 获取关注列表
* @param accountId
* @return
*/
public List<Long> getFolloweeList(Long accountId){
QueryWrapper<UserFollow> qw = new QueryWrapper<>();
qw.eq("follower_id", accountId).select("followee_id");
List<UserFollow> userFollows = userFollowMapper.selectList(qw);
return userFollows.stream().map(UserFollow::getFolloweeId).collect(Collectors.toList());
}
}