# 显示系统通知
---

## DAO
```xml
int selectNoticUnReadCount(int userId, String topic);
```
动态查询,如果 `selectNoticUnReadCount` 不传入 topic 参数,则查询所有系统通知的未读数量。
## Service
```java
/**
* 查询某个主题下最新的系统通知
* @param userId
* @param topic
* @return
*/
public Message findLatestNotice(int userId, String topic) {
return messageMapper.selectLatestNotice(userId, topic);
}
/**
* 查询某个主题下包含的系统通知数量
* @param userId
* @param topic
* @return
*/
public int findNoticeCount(int userId, String topic) {
return messageMapper.selectNoticeCount(userId, topic);
}
/**
* 查询未读的系统通知数量
* @param userId
* @param topic
* @return
*/
public int findNoticeUnReadCount(int userId, String topic) {
return messageMapper.selectNoticeUnReadCount(userId, topic);
}
/**
* 查询某个主题所包含的通知列表
* @param userId
* @param topic
* @param offset
* @param limit
* @return
*/
public List findNotices(int userId, String topic, int offset, int limit) {
return messageMapper.selectNotices(userId, topic, offset, limit);
}
```
## Controller
```java
/**
* 通知列表(只显示最新一条消息)
* @param model
* @return
*/
@GetMapping("/notice/list")
public String getNoticeList(Model model) {
User user = hostHolder.getUser();
// 查询评论类通知
Message message = messageService.findLatestNotice(user.getId(), TOPIC_COMMNET);
// 封装通知需要的各种数据
if (message != null) {
Map messageVO = new HashMap<>();
messageVO.put("message", message);
String content = HtmlUtils.htmlUnescape(message.getContent());
Map data = JSONObject.parseObject(content, HashMap.class);
messageVO.put("user", userService.findUserById((Integer) data.get("userId")));
messageVO.put("entityType", data.get("entityType"));
messageVO.put("entityId", data.get("entityId"));
messageVO.put("postId", data.get("postId"));
int count = messageService.findNoticeCount(user.getId(), TOPIC_COMMNET);
messageVO.put("count", count);
int unread = messageService.findNoticeUnReadCount(user.getId(), TOPIC_COMMNET);
messageVO.put("unread", unread);
model.addAttribute("commentNotice", messageVO);
}
// 查询点赞类通知
...........
// 查询关注类通知
...........
// 查询未读消息数量
int letterUnreadCount = messageService.findLetterUnreadCount(user.getId(), null);
model.addAttribute("letterUnreadCount", letterUnreadCount);
int noticeUnreadCount = messageService.findNoticeUnReadCount(user.getId(), null);
model.addAttribute("noticeUnreadCount", noticeUnreadCount);
return "/site/notice";
}
/**
* 查询某个主题所包含的通知列表
* @param topic
* @param page
* @param model
* @return
*/
@GetMapping("/notice/detail/{topic}")
public String getNoticeDetail(@PathVariable("topic") String topic, Page page, Model model) {
User user = hostHolder.getUser();
page.setLimit(5);
page.setPath("/notice/detail/" + topic);
page.setRows(messageService.findNoticeCount(user.getId(), topic));
List noticeList = messageService.findNotices(user.getId(), topic,page.getOffset(), page.getLimit());
List