小id放前
public class Message {
private int id;
private int fromId; //发送者id
private int toId; //接受者id
private String conversationId; //会话id 小的id在前面 111_112 和112_111属于同一个会话
private String content; //内容
private int status; //状态
}0表示未读 1表示已读 2表示删除
private Date createTime;
@Mapper
public interface MessageMapper {
// 查询当前用户的会话列表,针对每个会话只返回一条最新的私信.
List selectConversations(int userId, int offset, int limit);
// 查询当前用户的会话数量.
int selectConversationCount(int userId);
// 查询某个会话所包含的私信列表.
List selectLetters(String conversationId, int offset, int limit);
// 查询某个会话所包含的私信数量.
int selectLetterCount(String conversationId);
// 查询未读私信的数量
int selectLetterUnreadCount(int userId, String conversationId);
}
@Service
public class MessageService {
@Autowired(required = false)
private MessageMapper messageMapper;
@Autowired
private SensitiveFilter sensitiveFilter;
public List findConversations(int userId, int offset, int limit) {
return messageMapper.selectConversations(userId, offset, limit);
}
public int findConversationCount(int userId) {
return messageMapper.selectConversationCount(userId);
}
public List findLetters(String conversationId, int offset, int limit) {
return messageMapper.selectLetters(conversationId, offset, limit);
}
public int findLetterCount(String conversationId) {
return messageMapper.selectLetterCount(conversationId);
}
public int findLetterUnreadCount(int userId, String conversationId) {
return messageMapper.selectLetterUnreadCount(userId, conversationId);
}
}
//会话列表
@RequestMapping(value = "",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 conversationList=messageService.findConversations(
user.getId(),page.getOffset(),page.getLimit());
//使用list集合类型是map来存储遍历得到的每一个会话的信息
List
//发送消息
@RequestMapping(value = "/letter/send",method = RequestMethod.POST)
@ResponseBody
public String sendLetter(String toName, String content) {
User target = userService.queryUserByName(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);
}
$(function(){
$("#sendBtn").click(send_letter);
$(".close").click(delete_msg);
});
function send_letter() {
$("#sendModal").modal("hide");
var toName = $("#recipient-name").val();
var content = $("#message-text").val();
$.post(
CONTEXT_PATH + "/letter/send",
{"toName":toName,"content":content},
function(data) {
data = $.parseJSON(data);
if(data.code == 0) {
$("#hintBody").text("发送成功!");
} else {
$("#hintBody").text(data.msg);
}
$("#hintModal").modal("show");
setTimeout(function(){
$("#hintModal").modal("hide");
location.reload();
}, 2000);
}
);
}
function delete_msg() {
// TODO 删除数据
$(this).parents(".media").remove();
}
//获取当前用户的私信中该用户属于接收方并且私信状态为0的帖子
private List getLetterIds(List letterList) {
List 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;
}
// 设置已读
List ids = getLetterIds(letterList);
if (!ids.isEmpty()) {
messageService.updateStatus(ids,1);
}