This commit is contained in:
Your Name
2026-04-21 09:49:44 +08:00
parent c9e0419895
commit c15c348b7a
454 changed files with 5391 additions and 25651 deletions
@@ -0,0 +1,105 @@
-- 企业微信 员工↔客户 消息收发相关表
-- 1) 会话索引(员工 userid × 客户 external_userid × 群 roomid 三元组,用于前端会话列表)
-- 2) 消息归档(会话存档 SDK 拉取落库;msgid 唯一,支持撤回更新)
-- 3) 消息归档附件(大型媒体独立存储,按需下载到本地/对象存储)
-- 4) 发消息任务(企业群发 add_msg_template 一次投递记录 + 每客户明细)
CREATE TABLE IF NOT EXISTS `zyt_qywx_msg_session` (
`id` bigint(20) unsigned NOT NULL AUTO_INCREMENT,
`staff_userid` varchar(64) NOT NULL DEFAULT '' COMMENT '企微员工 userid',
`external_userid` varchar(64) NOT NULL DEFAULT '' COMMENT '客户 external_userid;群聊时为 roomid 对应的主联系客户,可留空',
`roomid` varchar(64) NOT NULL DEFAULT '' COMMENT '企微群聊 roomid;单聊为空',
`session_type` tinyint(1) unsigned NOT NULL DEFAULT 1 COMMENT '1=单聊 2=群聊',
`last_msg_id` varchar(64) NOT NULL DEFAULT '' COMMENT '最近一条归档消息 msgid(来自会话存档)',
`last_msg_seq` bigint(20) unsigned NOT NULL DEFAULT 0 COMMENT '最近一条归档消息 seq',
`last_msg_time` int(11) unsigned NOT NULL DEFAULT 0 COMMENT '最近一条归档消息 Unix 秒',
`last_msg_type` varchar(32) NOT NULL DEFAULT '' COMMENT '最近一条消息类型',
`last_msg_summary` varchar(512) NOT NULL DEFAULT '' COMMENT '最近一条消息摘要(纯文本,用于列表)',
`unread_staff` int(11) unsigned NOT NULL DEFAULT 0 COMMENT '员工侧未读(后台管理员打开会话后清零)',
`create_time` int(11) unsigned NOT NULL DEFAULT 0,
`update_time` int(11) unsigned NOT NULL DEFAULT 0,
PRIMARY KEY (`id`),
UNIQUE KEY `uk_session` (`staff_userid`,`external_userid`,`roomid`),
KEY `idx_staff_time` (`staff_userid`,`last_msg_time`),
KEY `idx_ext_time` (`external_userid`,`last_msg_time`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COMMENT='企微消息会话索引';
CREATE TABLE IF NOT EXISTS `zyt_qywx_msg_archive` (
`id` bigint(20) unsigned NOT NULL AUTO_INCREMENT,
`msgid` varchar(64) NOT NULL DEFAULT '' COMMENT '企微会话存档 msgid,官方唯一键',
`seq` bigint(20) unsigned NOT NULL DEFAULT 0 COMMENT '拉取 seq(单调递增,断点续拉)',
`action` varchar(16) NOT NULL DEFAULT 'send' COMMENT 'send/recall/switch',
`from_user` varchar(64) NOT NULL DEFAULT '' COMMENT '发送方 userid / external_userid',
`from_type` tinyint(1) unsigned NOT NULL DEFAULT 0 COMMENT '0=未知 1=员工 2=客户',
`to_list` text COMMENT '接收方列表 JSON(单聊通常仅 1 个)',
`roomid` varchar(64) NOT NULL DEFAULT '' COMMENT '群 roomid,单聊为空',
`msgtype` varchar(32) NOT NULL DEFAULT '' COMMENT 'text/image/voice/video/file/link/location/emotion/chatrecord/markdown/news/meeting/calendar/mixed/weapp/voip_doc_share/...',
`content` mediumtext COMMENT '文本/卡片摘要(mixed/chatrecord 会序列化 JSON',
`media_id` varchar(128) NOT NULL DEFAULT '' COMMENT 'sdkfileid:作为 GetMediaData 入参',
`md5sum` varchar(64) NOT NULL DEFAULT '' COMMENT '企微下发的 md5sum',
`file_name` varchar(512) NOT NULL DEFAULT '' COMMENT '原始文件名',
`file_size` bigint(20) unsigned NOT NULL DEFAULT 0 COMMENT '字节',
`file_ext` varchar(32) NOT NULL DEFAULT '' COMMENT '扩展名',
`play_length` int(11) unsigned NOT NULL DEFAULT 0 COMMENT '语音/视频时长 秒',
`raw` mediumtext COMMENT '解密后的原始 JSON,兜底',
`send_time` int(11) unsigned NOT NULL DEFAULT 0 COMMENT '消息时间 Unix 秒',
`create_time` int(11) unsigned NOT NULL DEFAULT 0,
`update_time` int(11) unsigned NOT NULL DEFAULT 0,
PRIMARY KEY (`id`),
UNIQUE KEY `uk_msgid` (`msgid`),
KEY `idx_seq` (`seq`),
KEY `idx_room_time` (`roomid`,`send_time`),
KEY `idx_from_time` (`from_user`,`send_time`),
KEY `idx_send_time` (`send_time`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COMMENT='企微会话存档消息';
CREATE TABLE IF NOT EXISTS `zyt_qywx_msg_archive_media` (
`id` bigint(20) unsigned NOT NULL AUTO_INCREMENT,
`msgid` varchar(64) NOT NULL DEFAULT '' COMMENT '关联 zyt_qywx_msg_archive.msgid',
`sdkfileid` varchar(128) NOT NULL DEFAULT '',
`status` tinyint(1) unsigned NOT NULL DEFAULT 0 COMMENT '0=待下载 1=成功 2=失败',
`retry_count` int(11) unsigned NOT NULL DEFAULT 0,
`file_path` varchar(1024) NOT NULL DEFAULT '' COMMENT '本地/对象存储相对路径',
`file_url` varchar(1024) NOT NULL DEFAULT '' COMMENT '可公网访问的地址(如已迁 CDN',
`file_size` bigint(20) unsigned NOT NULL DEFAULT 0,
`md5sum` varchar(64) NOT NULL DEFAULT '',
`error` varchar(512) NOT NULL DEFAULT '',
`create_time` int(11) unsigned NOT NULL DEFAULT 0,
`update_time` int(11) unsigned NOT NULL DEFAULT 0,
PRIMARY KEY (`id`),
UNIQUE KEY `uk_msg_sdk` (`msgid`,`sdkfileid`),
KEY `idx_status` (`status`,`retry_count`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COMMENT='企微会话存档消息媒介文件';
CREATE TABLE IF NOT EXISTS `zyt_qywx_msg_send_task` (
`id` bigint(20) unsigned NOT NULL AUTO_INCREMENT,
`admin_id` int(11) unsigned NOT NULL DEFAULT 0 COMMENT '后台发起人 admin id',
`sender_userid` varchar(64) NOT NULL DEFAULT '' COMMENT '代发员工 useridadd_msg_template 的 sender',
`external_userids` text COMMENT '目标客户 external_userid JSON 数组',
`chat_type` tinyint(1) unsigned NOT NULL DEFAULT 1 COMMENT '1=single 2=group(对应企微 chat_type',
`msg_payload` mediumtext COMMENT '消息体 JSON(含 text/image/video/file/link/miniprogram 任意组合)',
`msg_template_id` varchar(64) NOT NULL DEFAULT '' COMMENT '企微返回 msgid(提醒员工确认发送的 ID)',
`fail_list` text COMMENT '企微返回的 fail_list',
`status` tinyint(1) unsigned NOT NULL DEFAULT 0 COMMENT '0=待提交 1=已提交等待员工确认 2=已确认发送 3=失败',
`error` varchar(512) NOT NULL DEFAULT '',
`create_time` int(11) unsigned NOT NULL DEFAULT 0,
`update_time` int(11) unsigned NOT NULL DEFAULT 0,
PRIMARY KEY (`id`),
KEY `idx_sender_time` (`sender_userid`,`create_time`),
KEY `idx_template` (`msg_template_id`),
KEY `idx_status` (`status`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COMMENT='企微员工代发消息任务';
-- 断点续拉游标(seq 持久化;一行一条,按 staff_userid 维度)
CREATE TABLE IF NOT EXISTS `zyt_qywx_msg_archive_cursor` (
`id` int(11) unsigned NOT NULL AUTO_INCREMENT,
`cursor_key` varchar(64) NOT NULL DEFAULT '' COMMENT '默认 global;如需按员工分片则存 staff_userid',
`seq` bigint(20) unsigned NOT NULL DEFAULT 0,
`last_pull_time` int(11) unsigned NOT NULL DEFAULT 0,
`last_pull_count` int(11) unsigned NOT NULL DEFAULT 0,
`last_error` varchar(512) NOT NULL DEFAULT '',
`create_time` int(11) unsigned NOT NULL DEFAULT 0,
`update_time` int(11) unsigned NOT NULL DEFAULT 0,
PRIMARY KEY (`id`),
UNIQUE KEY `uk_cursor_key` (`cursor_key`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COMMENT='企微会话存档拉取游标';