From e86b576050ba8bbe0e7dfe892d1f2ea7940d70f1 Mon Sep 17 00:00:00 2001 From: xupei Date: Fri, 23 Aug 2024 17:34:38 +0800 Subject: [PATCH] =?UTF-8?q?TASK:=E5=8A=A8=E6=80=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../ai/da/model/dto/GetNotificationDTO.java | 2 +- .../com/ai/da/model/vo/NotificationVO.java | 2 + .../com/ai/da/service/PortfolioService.java | 2 + .../impl/MessageCenterServiceImpl.java | 81 +++++++++++++++++-- .../da/service/impl/PortfolioServiceImpl.java | 13 +++ 5 files changed, 94 insertions(+), 6 deletions(-) diff --git a/src/main/java/com/ai/da/model/dto/GetNotificationDTO.java b/src/main/java/com/ai/da/model/dto/GetNotificationDTO.java index 781ffc59..a5ff536c 100644 --- a/src/main/java/com/ai/da/model/dto/GetNotificationDTO.java +++ b/src/main/java/com/ai/da/model/dto/GetNotificationDTO.java @@ -11,6 +11,6 @@ import lombok.EqualsAndHashCode; @Data public class GetNotificationDTO extends PageQueryBaseVo { - @ApiModelProperty("system/like/comment/follow") + @ApiModelProperty("system/like/comment/follow/newPosted") private String type; } \ No newline at end of file diff --git a/src/main/java/com/ai/da/model/vo/NotificationVO.java b/src/main/java/com/ai/da/model/vo/NotificationVO.java index 32699ce6..b54bb4ec 100644 --- a/src/main/java/com/ai/da/model/vo/NotificationVO.java +++ b/src/main/java/com/ai/da/model/vo/NotificationVO.java @@ -24,4 +24,6 @@ public class NotificationVO extends Notification { private Integer isFollow; + private String canvas; + } diff --git a/src/main/java/com/ai/da/service/PortfolioService.java b/src/main/java/com/ai/da/service/PortfolioService.java index a67b5e4c..bf551509 100644 --- a/src/main/java/com/ai/da/service/PortfolioService.java +++ b/src/main/java/com/ai/da/service/PortfolioService.java @@ -58,4 +58,6 @@ public interface PortfolioService extends IService { Integer getIfFollowed(Long followeeId, Long followerId); Long getPortfolioCount(Long accountId); + + List getFolloweeList(Long accountId); } diff --git a/src/main/java/com/ai/da/service/impl/MessageCenterServiceImpl.java b/src/main/java/com/ai/da/service/impl/MessageCenterServiceImpl.java index 83d719d6..3f6ce11a 100644 --- a/src/main/java/com/ai/da/service/impl/MessageCenterServiceImpl.java +++ b/src/main/java/com/ai/da/service/impl/MessageCenterServiceImpl.java @@ -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 getAllTypeMessageUnreadCount() { - List> typeCount = baseMapper.getTypeCount(UserContext.getUserHolder().getId()); + Long accountId = UserContext.getUserHolder().getId(); + List> typeCount = baseMapper.getTypeCount(accountId); Map 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 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 queryWrapper = new QueryWrapper<>(); if (!StringUtils.isNullOrEmpty(getNotificationDTO.getType())) { queryWrapper.eq("type", getNotificationDTO.getType()); @@ -116,7 +134,6 @@ public class MessageCenterServiceImpl extends ServiceImpl notificationIdList, String type) { - if (type.equals("system")) { setReadStatusSystem(notificationIdList); } else { @@ -269,5 +284,61 @@ public class MessageCenterServiceImpl extends ServiceImpl followeeList = portfolioService.getFolloweeList(accountId); + // 1.2 查询我关注的用户在我上次查看动态之后发布的作品数量 + String lastViewTime = redisUtil.getFromString(lastViewNewPostedTimeKey + ":" + accountId); + + QueryWrapper 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 getNewPosted(Long accountId, Integer page, Integer size){ + + // 1、获取关注用户发布的所有作品 + // 1.1 获取我关注的所有用户 + List followeeList = portfolioService.getFolloweeList(accountId); + // 1.2 分页查询我关注的用户发布的作品 + QueryWrapper queryWrapper = new QueryWrapper<>(); + queryWrapper.in("account_id", followeeList).orderByDesc("create_date"); + Page portfolioPage = portfolioService.getBaseMapper().selectPage(new Page<>(page, size), queryWrapper); + // 2、组装返回的数据 + + IPage 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); + } } diff --git a/src/main/java/com/ai/da/service/impl/PortfolioServiceImpl.java b/src/main/java/com/ai/da/service/impl/PortfolioServiceImpl.java index d0d5aef3..04948a34 100644 --- a/src/main/java/com/ai/da/service/impl/PortfolioServiceImpl.java +++ b/src/main/java/com/ai/da/service/impl/PortfolioServiceImpl.java @@ -1085,4 +1085,17 @@ public class PortfolioServiceImpl extends ServiceImpl getFolloweeList(Long accountId){ + QueryWrapper qw = new QueryWrapper<>(); + qw.eq("follower_id", accountId).select("followee_id"); + + List userFollows = userFollowMapper.selectList(qw); + return userFollows.stream().map(UserFollow::getFolloweeId).collect(Collectors.toList()); + } + }