假如后台某个任务比较耗时,这时就需要任务完成时,通知一下用户,如下图,实现站内信的效果
两张表即可实现
t_message_content内容表
CREATE TABLE `t_message_content` (
`c_id` int(11) NOT NULL AUTO_INCREMENT COMMENT '消息的id',
`send_id` int(11) DEFAULT NULL COMMENT '消息发送者的id',
`content` varchar(255) DEFAULT NULL COMMENT '消息的内容',
`type` int(11) DEFAULT NULL COMMENT '消息的类型',
`create_time` timestamp NULL DEFAULT CURRENT_TIMESTAMP COMMENT '消息发送的时间',
PRIMARY KEY (`c_id`)
) ENGINE=InnoDB AUTO_INCREMENT=6 DEFAULT CHARSET=utf8;
其中type消息类型分为私信和公告,0是私信、1为公告。发送时间默认为当前时间
t_message_record记录表
CREATE TABLE `t_message_record` (
`r_id` int(11) NOT NULL AUTO_INCREMENT COMMENT '阅读记录的id',
`rec_id` int(11) DEFAULT NULL COMMENT '消息接收者的id',
`c_id` int(11) DEFAULT NULL COMMENT '对应消息的id',
`status` int(11) DEFAULT '0' COMMENT '阅读记录的状态',
PRIMARY KEY (`r_id`)
) ENGINE=InnoDB AUTO_INCREMENT=7 DEFAULT CHARSET=utf8;
status表示阅读记录的状态,0表示未读,1已读,2删除。
一对一:用户admin 发送私信给 用户WH2306186
1、在内容表里插入私信内容,并返回该内容的自增主键c_id = 5
INSERT INTO t_message_content (`send_id`,`content`,`type`) VALUES ("admin","这是admin发送私信给WH2306186",0)
2、往记录表里插入私信接收方未读的记录
INSERT INTO t_message_record (`rec_id`,`c_id`) VALUES ('WH2306186',5)
私信接收方接收消息
一、用户WH2306186 登录时获取全部私信消息
SELECT c.*,r.status
FROM t_message_content c
LEFT JOIN t_message_record r
ON c.c_id = r.c_id
WHERE r.rec_id = "WH2306186"
AND c.type = 0
AND r.`status` != 2
二、用户WH2306186 点击阅读时
UPDATE t_message_record SET status = 1 WHERE c_id = 5 AND rec_id = "WH2306186"
三、当用户WH2306186 点击删除私信时
UPDATE t_message_record SET status = 2 WHERE c_id = 5 AND rec_id = "WH2306186"
一对多:用户admin 发布公告
INSERT INTO `t_message_content` (`send_id`,`content`,`type`) VALUES ("admin","这是公告1的内容",1)
INSERT INTO `t_message_content` (`send_id`,`content`,`type`) VALUES ("admin","这是公告2的内容",1)
用户 接收公告
一、用户WH2306186 登陆时获取全部公告消息
SELECT c.*,IFNULL(r.status,0) AS status
FROM t_message_content c
LEFT JOIN t_message_record r
ON (c.c_id = r.c_id AND r.rec_id = "WH2306186")
WHERE c.type = 1
二、公告1设为已读
INSERT INTO t_message_record (`rec_id`,`c_id`,`status`) VALUES ("WH2306186",7,1)
三、删除公告1
UPDATE t_message_record SET status = 2 WHERE rec_id = "WH2306186" AND c_id = 7
参考blog:https://cloud.tencent.com/developer/article/1596184