package com.nowcoder.community.controller;
import com.alibaba.fastjson.JSONObject;
import com.nowcoder.community.entity.Message;
import com.nowcoder.community.entity.Page;
import com.nowcoder.community.entity.User;
import com.nowcoder.community.service.MessageService;
import com.nowcoder.community.service.UserService;
import com.nowcoder.community.util.CommunityConstant;
import com.nowcoder.community.util.CommunityUtil;
import com.nowcoder.community.util.HostHolder;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.util.HtmlUtils;
import java.util.*;
/**
* @author coolsen
* @version 1.0.0
* @ClassName MessageController.java
* @Description MessageController
* @createTime 5/8/2020 8:13 PM
*/
@Controller
public class MessageController implements CommunityConstant {
@Autowired
private MessageService messageService;
@Autowired
private HostHolder hostHolder;
@Autowired
private UserService userService;
// 私信列表
@RequestMapping(path = "/letter/list", method = RequestMethod.GET)
public String getLetterList(Model model, Page page) {
User user = hostHolder.getUser();
// 分页信息
page.setLimit(5);
page.setPath("/letter/list");
page.setRows(messageService.findConversationCount(user.getId()));
// 会话列表
List<Message> conversationList = messageService.findConversations(
user.getId(), page.getOffset(), page.getLimit());
List<Map<String, Object>> conversations = new ArrayList<>();
if (conversationList != null) {
for (Message message : conversationList) {
Map<String, Object> map = new HashMap<>();
map.put("conversation", message);
map.put("letterCount", messageService.findLetterCount(message.getConversationId()));
map.put("unreadCount", messageService.findLetterUnreadCount(user.getId(), message.getConversationId()));
int targetId = user.getId() == message.getFromId() ? message.getToId() : message.getFromId();
map.put("target", userService.findUserById(targetId));
conversations.add(map);
}
}
model.addAttribute("conversations", conversations);
// 查询未读消息数量
int letterUnreadCount = messageService.findLetterUnreadCount(user.getId(), null);
model.addAttribute("letterUnreadCount", letterUnreadCount);
return "/site/letter";
}
/**
* @throws
* @title 私信详情
* @description
* @author coolsen
* @updateTime 5/8/2020 8:17 PM
*/
@RequestMapping(path = "/letter/detail/{conversationId}", method = RequestMethod.GET)
public String getLetterDetail(@PathVariable("conversationId") String conversationId, Page page, Model model) {
// 分页信息
page.setLimit(5);
page.setPath("/letter/detail/" + conversationId);
page.setRows(messageService.findLetterCount(conversationId));
// 私信列表
List<Message> letterList = messageService.findLetters(conversationId, page.getOffset(), page.getLimit());
List<Map<String, Object>> letters = new ArrayList<>();
if (letterList != null) {
for (Message message : letterList) {
Map<String, Object> map = new HashMap<>();
map.put("letter", message);
map.put("fromUser", userService.findUserById(message.getFromId()));
letters.add(map);
}
}
model.addAttribute("letters", letters);
// 私信目标
model.addAttribute("target", getLetterTarget(conversationId));
// 设置已读
List<Integer> ids = getLetterIds(letterList);
if (!ids.isEmpty()) {
messageService.readMessage(ids);
}
return "/site/letter-detail";
}
/**
* @throws
* @title
* @description 获取私信发送目标用户
* @author coolsen
* @updateTime 5/8/2020 8:20 PM
*/
private User getLetterTarget(String conversationId) {
String[] ids = conversationId.split("_");
int id0 = Integer.parseInt(ids[0]);
int id1 = Integer.parseInt(ids[1]);
if (hostHolder.getUser().getId() == id0) {
return userService.findUserById(id1);
} else {
return userService.findUserById(id0);
}
}
/**
* @throws
* @title
* @description 获取未读的私信列表
* @author coolsen
* @updateTime 5/8/2020 8:33 PM
*/
private List<Integer> getLetterIds(List<Message> letterList) {
List<Integer> ids = new ArrayList<>();
if (letterList != null) {
for (Message message : letterList) {
if (hostHolder.getUser().getId() == message.getToId() && message.getStatus() == 0) {
ids.add(message.getId());
}
}
}
return ids;
}
@RequestMapping(path = "/letter/send", method = RequestMethod.POST)
@ResponseBody
public String sendLetter(String toName, String content) {
User target = userService.findUserByName(toName);
if (target == null) {
return CommunityUtil.getJSONString(1, "目标用户不存在!");
}
Message message = new Message();
message.setFromId(hostHolder.getUser().getId());
message.setToId(target.getId());
if (message.getFromId() < message.getToId()) {
message.setConversationId(message.getFromId() + "_" + message.getToId());
} else {
message.setConversationId(message.getToId() + "_" + message.getFromId());
}
message.setContent(content);
message.setCreateTime(new Date());
messageService.addMessage(message);
return CommunityUtil.getJSONString(0);
}
// 通知列表,指系统发送的通知
@RequestMapping(path = "/notice/list", method = RequestMethod.GET)
public String getNoticeList(Model model) {
User user = hostHolder.getUser();
// 查询评论类通知
Message message = messageService.findLatestNotice(user.getId(), TOPIC_COMMENT);
if (message != null) {
Map<String, Object> messageVO = new HashMap<>();
messageVO.put("message", message);
// 将转义字符还原回去
String content = HtmlUtils.htmlUnescape(message.getContent());
// 还原为map
Map<String, Object> 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_COMMENT);
messageVO.put("count", count);
int unread = messageService.findNoticeUnreadCount(user.getId(), TOPIC_COMMENT);
messageVO.put("unread", unread);
model.addAttribute("commentNotice", messageVO);
}
// 查询点赞类通知
message = messageService.findLatestNotice(user.getId(), TOPIC_LIKE);
if (message != null) {
Map<String, Object> messageVO = new HashMap<>();
messageVO.put("message", message);
String content = HtmlUtils.htmlUnescape(message.getContent());
Map<String, Object> data = JSONObject.parseObject(content, Ha
没有合适的资源?快使用搜索试试~ 我知道了~
Java开发的仿牛客网社区源码.zip
共2000个文件
java:2448个
html:1218个
js:265个
1.该资源内容由用户上传,如若侵权请联系客服进行举报
2.虚拟产品一经售出概不退款(资源遇到问题,请及时私信上传者)
2.虚拟产品一经售出概不退款(资源遇到问题,请及时私信上传者)
版权申诉
0 下载量 144 浏览量
2022-12-16
11:13:07
上传
评论
收藏 56.1MB ZIP 举报
温馨提示
Java开发的仿牛客网社区源码.zip
资源推荐
资源详情
资源评论
收起资源包目录
Java开发的仿牛客网社区源码.zip (2000个子文件)
global.css 2KB
global.css 2KB
global.css 2KB
global.css 2KB
global.css 2KB
global.css 2KB
global.css 2KB
global.css 2KB
global.css 2KB
global.css 2KB
global.css 2KB
global.css 2KB
global.css 2KB
global.css 2KB
global.css 2KB
global.css 2KB
global.css 2KB
global.css 2KB
global.css 2KB
global.css 2KB
global.css 2KB
global.css 2KB
global.css 2KB
global.css 2KB
global.css 2KB
global.css 2KB
global.css 2KB
global.css 2KB
global.css 2KB
global.css 2KB
global.css 2KB
global.css 2KB
global.css 2KB
global.css 2KB
global.css 2KB
global.css 2KB
global.css 2KB
global.css 2KB
global.css 2KB
global.css 2KB
global.css 2KB
global.css 2KB
global.css 2KB
global.css 2KB
global.css 2KB
global.css 2KB
global.css 2KB
discuss-detail.css 243B
discuss-detail.css 243B
discuss-detail.css 243B
discuss-detail.css 243B
discuss-detail.css 243B
discuss-detail.css 243B
discuss-detail.css 243B
discuss-detail.css 243B
discuss-detail.css 243B
discuss-detail.css 243B
discuss-detail.css 243B
discuss-detail.css 243B
discuss-detail.css 243B
discuss-detail.css 243B
discuss-detail.css 243B
discuss-detail.css 243B
discuss-detail.css 243B
discuss-detail.css 243B
discuss-detail.css 243B
discuss-detail.css 243B
discuss-detail.css 243B
discuss-detail.css 243B
discuss-detail.css 243B
discuss-detail.css 243B
discuss-detail.css 243B
discuss-detail.css 243B
discuss-detail.css 243B
discuss-detail.css 243B
discuss-detail.css 243B
discuss-detail.css 243B
discuss-detail.css 243B
discuss-detail.css 243B
discuss-detail.css 243B
discuss-detail.css 243B
discuss-detail.css 243B
discuss-detail.css 243B
discuss-detail.css 243B
discuss-detail.css 243B
discuss-detail.css 243B
discuss-detail.css 243B
discuss-detail.css 243B
discuss-detail.css 243B
discuss-detail.css 243B
discuss-detail.css 243B
discuss-detail.css 243B
discuss-detail.css 243B
discuss-detail.css 243B
letter.css 181B
letter.css 181B
letter.css 181B
letter.css 181B
letter.css 181B
letter.css 181B
共 2000 条
- 1
- 2
- 3
- 4
- 5
- 6
- 20
资源评论
Java程序员-张凯
- 粉丝: 1w+
- 资源: 6746
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
最新资源
- chromedriver-mac-arm64.zip
- 蓝zapro.apk
- chromedriver-linux64.zip
- UCAS研一深度学习实验-MNIST手写数字识别python源码+详细注释(高分项目)
- 基于Python和PyTorch框架完成的一个手写数字识别实验源码(带MINIST手写数字数据集)+详细注释(高分项目)
- 基于Matlab在MNIST数据集上利用CNN完成手写体数字识别任务,并实现单层CNN反向传播算法+源代码+文档说明(高分项目)
- NVIDIA驱动、CUDA和Pytorch及其依赖
- 基于SVM多特征融合的微表情识别python源码+项目说明+详细注释(高分课程设计)
- html动态爱心代码一(附源码)
- c40539bc-071a-486c-9d52-9d0c18d62dac 4.html
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功