CREATE TABLE `chat_room` (
                             `id` bigint(20) NOT NULL AUTO_INCREMENT  COMMENT '主键id',
                             `user_one_email` varchar(32) COLLATE utf8mb4_bin NOT NULL COMMENT '聊天室用户1(普通用户)',
                             `user_two_email` varchar(32) COLLATE utf8mb4_bin NOT NULL COMMENT '聊天室用户2(客服)',
                             `last_user_send_time` datetime NOT NULL DEFAULT CURRENT_TIMESTAMP COMMENT '用户最后发送时间',
                             `last_customer_send_time` datetime NOT NULL DEFAULT CURRENT_TIMESTAMP COMMENT '客服最后发送时间',
                             `flag` tinyint(1) NOT NULL DEFAULT '0' COMMENT '重要聊天室标记 0 正常 1重要',
                             `create_time` datetime NOT NULL DEFAULT CURRENT_TIMESTAMP COMMENT '创建时间',
                             `update_time` datetime NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP COMMENT '修改时间',
                             `del` tinyint(1) unsigned zerofill NOT NULL DEFAULT '0' COMMENT '逻辑删除字段',
                             PRIMARY KEY (`id`),
                             UNIQUE KEY `usertwo_userone` (`user_two_email`,`user_one_email`) USING BTREE COMMENT '聊天室快速查找索引'
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4  AUTO_INCREMENT=1 COLLATE=utf8mb4_bin COMMENT='聊天服务---聊天室';



CREATE TABLE `chat_message` (
                                `id` bigint(20) NOT NULL AUTO_INCREMENT COMMENT '主键id',
                                `send_email` varchar(32) COLLATE utf8mb4_bin NOT NULL COMMENT '发送者id',
                                `content` varchar(255) COLLATE utf8mb4_bin NOT NULL COMMENT '消息内容',
                                `room_id` bigint(20) NOT NULL COMMENT '外键-聊天室id',
                                `type` tinyint(1) NOT NULL  DEFAULT '0' COMMENT '消息类型 0 文本 1图片',
                                `read` tinyint(1) NOT NULL DEFAULT '0' COMMENT '0已读  1未读',
                                `create_time` datetime NOT NULL DEFAULT CURRENT_TIMESTAMP COMMENT '创建时间',
                                `update_time` datetime NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP COMMENT '修改时间',
                                PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 AUTO_INCREMENT=1 COLLATE=utf8mb4_bin COMMENT='聊天消息存储表';


CREATE TABLE `chat_message_history` (
                                        `id` bigint(20) NOT NULL AUTO_INCREMENT COMMENT '主键id',
                                        `send_email` varchar(32) COLLATE utf8mb4_bin NOT NULL COMMENT '发送者邮箱',
                                        `content` varchar(255) COLLATE utf8mb4_bin NOT NULL COMMENT '消息内容',
                                        `room_id` bigint(20) NOT NULL COMMENT '外键-聊天室id',
                                        `type` tinyint(1) NOT NULL  DEFAULT '0' COMMENT '消息类型 0 文本 1图片',
                                        `read` tinyint(1) NOT NULL DEFAULT '0' COMMENT '0已读  1未读',
                                        `create_time` datetime NOT NULL DEFAULT CURRENT_TIMESTAMP COMMENT '创建时间',
                                        `update_time` datetime NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP COMMENT '修改时间',
                                        PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 AUTO_INCREMENT=1 COLLATE=utf8mb4_bin COMMENT='聊天消息存储表';



alter table chat_room MODIFY COLUMN  `last_user_send_time` datetime NOT NULL DEFAULT CURRENT_TIMESTAMP COMMENT '用户最后发送时间';
alter table chat_room MODIFY COLUMN  `last_customer_send_time` datetime NOT NULL DEFAULT CURRENT_TIMESTAMP COMMENT '客服最后发送时间';
ALTER TABLE chat_message ALTER COLUMN room_id SET DEFAULT '0';
ALTER TABLE chat_message_history ALTER COLUMN room_id SET DEFAULT '0';
ALTER TABLE chat_room ALTER COLUMN user_one_email SET DEFAULT '';
ALTER TABLE chat_room ALTER COLUMN user_two_email SET DEFAULT '';




ALTER TABLE chat_message_history
    CHANGE COLUMN `read` is_read tinyint(1) NOT NULL DEFAULT '0' COMMENT '0已读  1未读';

ALTER TABLE chat_message
    CHANGE COLUMN `read` is_read tinyint(1) NOT NULL DEFAULT '0' COMMENT '0已读  1未读';


-- 2025-4-28 9:50
ALTER TABLE chat_message DROP COLUMN `is_read`;
ALTER TABLE chat_message_history DROP COLUMN `is_read`;
ALTER TABLE chat_room ADD COLUMN service_read_num bigint(4) NOT NULL DEFAULT '0'  COMMENT '客服未读消息数量';
ALTER TABLE chat_room ADD COLUMN client_read_num bigint(4) NOT NULL DEFAULT '0'  COMMENT '客户未读消息数量';

ALTER TABLE chat_room CHANGE COLUMN content longtext COLLATE utf8mb4_bin NOT NULL COMMENT '消息内容',
-- 存储图片的base64编码文件
ALTER TABLE chat_message CHANGE COLUMN content LONGTEXT NOT NULL COMMENT '消息内容';
