✨ 消息功能优化
This commit is contained in:
parent
5b66d56232
commit
5a8d158658
17
src/main/java/com/rymcu/vertical/dto/ArticleSearchDTO.java
Normal file
17
src/main/java/com/rymcu/vertical/dto/ArticleSearchDTO.java
Normal file
@ -0,0 +1,17 @@
|
||||
package com.rymcu.vertical.dto;
|
||||
|
||||
import lombok.Data;
|
||||
|
||||
/**
|
||||
* @author ronger
|
||||
*/
|
||||
@Data
|
||||
public class ArticleSearchDTO {
|
||||
|
||||
private String searchText;
|
||||
|
||||
private String topicUri;
|
||||
|
||||
private String tag;
|
||||
|
||||
}
|
@ -8,6 +8,9 @@ import javax.servlet.http.HttpServletRequest;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
/**
|
||||
* @author ronger
|
||||
*/
|
||||
public interface CommentService extends Service<Comment> {
|
||||
|
||||
List<CommentDTO> getArticleComments(Integer idArticle);
|
||||
|
@ -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<Notification> {
|
||||
* @param idUser
|
||||
* @return
|
||||
*/
|
||||
List<Notification> findNotifications(Integer idUser);
|
||||
List<NotificationDTO> findNotifications(Integer idUser);
|
||||
|
||||
/**
|
||||
* 获取消息数据
|
||||
@ -48,5 +48,5 @@ public interface NotificationService extends Service<Notification> {
|
||||
* 标记消息已读
|
||||
* @param id
|
||||
*/
|
||||
void readNotification(Integer id);
|
||||
Integer readNotification(Integer id);
|
||||
}
|
||||
|
@ -83,7 +83,7 @@ public class CommentServiceImpl extends AbstractService<Comment> implements Comm
|
||||
comment.setCreatedTime(new Date());
|
||||
commentMapper.insertSelective(comment);
|
||||
StringBuilder commentSharpUrl = new StringBuilder(article.getArticlePermalink());
|
||||
commentSharpUrl.append("/comment/").append(comment.getIdComment());
|
||||
commentSharpUrl.append("#comment-").append(comment.getIdComment());
|
||||
commentMapper.updateCommentSharpUrl(comment.getIdComment(), commentSharpUrl.toString());
|
||||
|
||||
String commentContent = comment.getCommentContent();
|
||||
|
@ -1,13 +1,25 @@
|
||||
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;
|
||||
import java.util.Objects;
|
||||
|
||||
/**
|
||||
* @author ronger
|
||||
@ -17,6 +29,12 @@ public class NotificationServiceImpl extends AbstractService<Notification> imple
|
||||
|
||||
@Resource
|
||||
private NotificationMapper notificationMapper;
|
||||
@Resource
|
||||
private ArticleService articleService;
|
||||
@Resource
|
||||
private CommentService commentService;
|
||||
@Resource
|
||||
private UserService userService;
|
||||
|
||||
@Override
|
||||
public List<Notification> findUnreadNotifications(Integer idUser) {
|
||||
@ -25,9 +43,53 @@ public class NotificationServiceImpl extends AbstractService<Notification> imple
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<Notification> findNotifications(Integer idUser) {
|
||||
public List<NotificationDTO> findNotifications(Integer idUser) {
|
||||
List<Notification> list = notificationMapper.selectNotifications(idUser);
|
||||
return list;
|
||||
List<NotificationDTO> 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":
|
||||
// 关注
|
||||
break;
|
||||
case "2":
|
||||
// 回帖
|
||||
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;
|
||||
}
|
||||
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
|
||||
@ -36,12 +98,14 @@ public class NotificationServiceImpl extends AbstractService<Notification> imple
|
||||
}
|
||||
|
||||
@Override
|
||||
@Transactional(rollbackFor = Exception.class)
|
||||
public Integer save(Integer idUser, Integer dataId, String dataType, String dataSummary) {
|
||||
return notificationMapper.insertNotification(idUser, dataId, dataType, dataSummary);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void readNotification(Integer id) {
|
||||
notificationMapper.readNotification(id);
|
||||
@Transactional(rollbackFor = Exception.class)
|
||||
public Integer readNotification(Integer id) {
|
||||
return notificationMapper.readNotification(id);
|
||||
}
|
||||
}
|
||||
|
@ -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;
|
||||
@ -39,7 +40,6 @@ public class Utils {
|
||||
|
||||
/**
|
||||
*一般检查工具密码比对 add by xlf 2018-11-8
|
||||
*
|
||||
* @param pwd
|
||||
* @param enpwd 加密的密码
|
||||
* @return
|
||||
@ -78,10 +78,9 @@ public class Utils {
|
||||
|
||||
/**
|
||||
* 获取配置文件内属性
|
||||
*
|
||||
* @param key 键值
|
||||
* @return 属性值
|
||||
*/
|
||||
* */
|
||||
public static String getProperty(String key){
|
||||
return env.getProperty(key);
|
||||
}
|
||||
@ -112,7 +111,7 @@ public class Utils {
|
||||
timeAgo = hours+" 小时前 ";
|
||||
}else {
|
||||
int minutes = (int) ((to - from)/(1000 * 60));
|
||||
if (minutes < 5) {
|
||||
if(minutes == 0){
|
||||
timeAgo = " 刚刚 ";
|
||||
}else {
|
||||
timeAgo = minutes+" 分钟前 ";
|
||||
@ -184,4 +183,15 @@ public class Utils {
|
||||
|
||||
return ip;
|
||||
}
|
||||
|
||||
public static Map getNotificationDTOsGlobalResult(PageInfo<NotificationDTO> 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;
|
||||
}
|
||||
}
|
||||
|
@ -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<Notification> list = notificationService.findNotifications(user.getIdUser());
|
||||
PageInfo<Notification> pageInfo = new PageInfo(list);
|
||||
Map map = Utils.getNotificationsGlobalResult(pageInfo);
|
||||
List<NotificationDTO> list = notificationService.findNotifications(user.getIdUser());
|
||||
PageInfo<NotificationDTO> pageInfo = new PageInfo(list);
|
||||
Map map = Utils.getNotificationDTOsGlobalResult(pageInfo);
|
||||
return GlobalResultGenerator.genSuccessResult(map);
|
||||
}
|
||||
|
||||
@ -48,8 +49,12 @@ public class NotificationController {
|
||||
}
|
||||
|
||||
@PutMapping("/read/{id}")
|
||||
public void read(@PathVariable Integer id) {
|
||||
notificationService.readNotification(id);
|
||||
public GlobalResult read(@PathVariable Integer id) {
|
||||
Integer result = notificationService.readNotification(id);
|
||||
if (result == 0) {
|
||||
return GlobalResultGenerator.genErrorResult("标记已读失败");
|
||||
}
|
||||
return GlobalResultGenerator.genSuccessResult("标记已读成功");
|
||||
}
|
||||
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user