diff --git a/src/main/java/com/rymcu/vertical/dto/NotificationDTO.java b/src/main/java/com/rymcu/vertical/dto/NotificationDTO.java new file mode 100644 index 0000000..c9d69f7 --- /dev/null +++ b/src/main/java/com/rymcu/vertical/dto/NotificationDTO.java @@ -0,0 +1,20 @@ +package com.rymcu.vertical.dto; + +import com.rymcu.vertical.entity.Notification; +import lombok.Data; + +/** + * @author ronger + */ +@Data +public class NotificationDTO extends Notification { + + private Integer idNotification; + + private String dataTitle; + + private String dataUrl; + + private Author author; + +} diff --git a/src/main/java/com/rymcu/vertical/entity/Notification.java b/src/main/java/com/rymcu/vertical/entity/Notification.java index a104f2a..83ade86 100644 --- a/src/main/java/com/rymcu/vertical/entity/Notification.java +++ b/src/main/java/com/rymcu/vertical/entity/Notification.java @@ -43,7 +43,7 @@ public class Notification implements Serializable,Cloneable { * 数据摘要 */ @Column(name = "data_summary") - private String dataSummary ; + private String dataSummary; /** * 是否已读 */ diff --git a/src/main/java/com/rymcu/vertical/service/NotificationService.java b/src/main/java/com/rymcu/vertical/service/NotificationService.java index 4342adf..14537b5 100644 --- a/src/main/java/com/rymcu/vertical/service/NotificationService.java +++ b/src/main/java/com/rymcu/vertical/service/NotificationService.java @@ -1,8 +1,8 @@ package com.rymcu.vertical.service; import com.rymcu.vertical.core.service.Service; +import com.rymcu.vertical.dto.NotificationDTO; import com.rymcu.vertical.entity.Notification; -import org.apache.ibatis.annotations.Param; import java.util.List; @@ -23,7 +23,7 @@ public interface NotificationService extends Service { * @param idUser * @return */ - List findNotifications(Integer idUser); + List findNotifications(Integer idUser); /** * 获取消息数据 diff --git a/src/main/java/com/rymcu/vertical/service/impl/NotificationServiceImpl.java b/src/main/java/com/rymcu/vertical/service/impl/NotificationServiceImpl.java index 38d2593..f4ae200 100644 --- a/src/main/java/com/rymcu/vertical/service/impl/NotificationServiceImpl.java +++ b/src/main/java/com/rymcu/vertical/service/impl/NotificationServiceImpl.java @@ -1,13 +1,23 @@ package com.rymcu.vertical.service.impl; import com.rymcu.vertical.core.service.AbstractService; +import com.rymcu.vertical.dto.ArticleDTO; +import com.rymcu.vertical.dto.Author; +import com.rymcu.vertical.dto.NotificationDTO; +import com.rymcu.vertical.entity.Comment; import com.rymcu.vertical.entity.Notification; +import com.rymcu.vertical.entity.User; import com.rymcu.vertical.mapper.NotificationMapper; +import com.rymcu.vertical.service.ArticleService; +import com.rymcu.vertical.service.CommentService; import com.rymcu.vertical.service.NotificationService; +import com.rymcu.vertical.service.UserService; +import com.rymcu.vertical.util.BeanCopierUtil; import org.springframework.stereotype.Service; import org.springframework.transaction.annotation.Transactional; import javax.annotation.Resource; +import java.util.ArrayList; import java.util.List; /** @@ -18,28 +28,84 @@ public class NotificationServiceImpl extends AbstractService imple @Resource private NotificationMapper notificationMapper; + @Resource + private ArticleService articleService; + @Resource + private CommentService commentService; + @Resource + private UserService userService; @Override - public List findUnreadNotifications(Integer idUser){ + public List findUnreadNotifications(Integer idUser) { List list = notificationMapper.selectUnreadNotifications(idUser); return list; } @Override - public List findNotifications(Integer idUser) { + public List findNotifications(Integer idUser) { List list = notificationMapper.selectNotifications(idUser); - return list; + List notifications = new ArrayList<>(); + list.forEach(notification -> { + NotificationDTO notificationDTO = genNotification(notification); + notifications.add(notificationDTO); + }); + return notifications; + } + + private NotificationDTO genNotification(Notification notification) { + NotificationDTO notificationDTO = new NotificationDTO(); + BeanCopierUtil.copy(notification, notificationDTO); + ArticleDTO article; + Comment comment; + User user; + switch (notification.getDataType()) { + case "0": + // 系统公告/帖子 + article = articleService.findArticleDTOById(notification.getDataId(), 0); + notificationDTO.setDataTitle("系统公告"); + notificationDTO.setDataUrl(article.getArticlePermalink()); + user = userService.findById(article.getArticleAuthorId().toString()); + notificationDTO.setAuthor(genAuthor(user)); + break; + case "1": + // 评论 + comment = commentService.findById(notification.getDataId().toString()); + article = articleService.findArticleDTOById(comment.getCommentArticleId(), 0); + notificationDTO.setDataTitle(article.getArticleTitle()); + notificationDTO.setDataUrl(comment.getCommentSharpUrl()); + user = userService.findById(comment.getCommentAuthorId().toString()); + notificationDTO.setAuthor(genAuthor(user)); + break; + case "2": + // 回帖 + comment = commentService.findById(notification.getDataId().toString()); + Comment originalComment = commentService.findById(comment.getCommentOriginalCommentId().toString()); + notificationDTO.setDataTitle(originalComment.getCommentContent()); + notificationDTO.setDataUrl(comment.getCommentSharpUrl()); + user = userService.findById(comment.getCommentAuthorId().toString()); + notificationDTO.setAuthor(genAuthor(user)); + break; + } + return notificationDTO; + } + + private Author genAuthor(User user) { + Author author = new Author(); + author.setUserNickname(user.getNickname()); + author.setUserAvatarURL(user.getAvatarUrl()); + author.setIdUser(user.getIdUser()); + return author; } @Override public Notification findNotification(Integer idUser, Integer dataId, String dataType) { - return notificationMapper.selectNotification(idUser,dataId,dataType); + return notificationMapper.selectNotification(idUser, dataId, dataType); } @Override @Transactional(rollbackFor = Exception.class) public Integer save(Integer idUser, Integer dataId, String dataType, String dataSummary) { - return notificationMapper.insertNotification(idUser,dataId,dataType,dataSummary); + return notificationMapper.insertNotification(idUser, dataId, dataType, dataSummary); } @Override diff --git a/src/main/java/com/rymcu/vertical/util/Utils.java b/src/main/java/com/rymcu/vertical/util/Utils.java index c4b6983..7b55a2f 100644 --- a/src/main/java/com/rymcu/vertical/util/Utils.java +++ b/src/main/java/com/rymcu/vertical/util/Utils.java @@ -2,6 +2,7 @@ package com.rymcu.vertical.util; import com.github.pagehelper.PageInfo; import com.rymcu.vertical.dto.ArticleDTO; +import com.rymcu.vertical.dto.NotificationDTO; import com.rymcu.vertical.entity.Notification; import com.rymcu.vertical.entity.User; import org.apache.shiro.SecurityUtils; @@ -182,4 +183,15 @@ public class Utils { return ip; } + + public static Map getNotificationDTOsGlobalResult(PageInfo pageInfo) { + Map map = new HashMap(2); + map.put("notifications", pageInfo.getList()); + Map pagination = new HashMap(4); + pagination.put("pageSize",pageInfo.getPageSize()); + pagination.put("total",pageInfo.getTotal()); + pagination.put("currentPage",pageInfo.getPageNum()); + map.put("pagination", pagination); + return map; + } } diff --git a/src/main/java/com/rymcu/vertical/web/api/notification/NotificationController.java b/src/main/java/com/rymcu/vertical/web/api/notification/NotificationController.java index b613e98..2b184e1 100644 --- a/src/main/java/com/rymcu/vertical/web/api/notification/NotificationController.java +++ b/src/main/java/com/rymcu/vertical/web/api/notification/NotificationController.java @@ -4,6 +4,7 @@ import com.github.pagehelper.PageHelper; import com.github.pagehelper.PageInfo; import com.rymcu.vertical.core.result.GlobalResult; import com.rymcu.vertical.core.result.GlobalResultGenerator; +import com.rymcu.vertical.dto.NotificationDTO; import com.rymcu.vertical.entity.Notification; import com.rymcu.vertical.entity.User; import com.rymcu.vertical.service.NotificationService; @@ -31,9 +32,9 @@ public class NotificationController { public GlobalResult notifications(@RequestParam(defaultValue = "0") Integer page, @RequestParam(defaultValue = "10") Integer rows) throws BaseApiException { User user = UserUtils.getWxCurrentUser(); PageHelper.startPage(page, rows); - List list = notificationService.findNotifications(user.getIdUser()); - PageInfo pageInfo = new PageInfo(list); - Map map = Utils.getNotificationsGlobalResult(pageInfo); + List list = notificationService.findNotifications(user.getIdUser()); + PageInfo pageInfo = new PageInfo(list); + Map map = Utils.getNotificationDTOsGlobalResult(pageInfo); return GlobalResultGenerator.genSuccessResult(map); }