-- 企业微信获客助手:客户归因、累计收消息数与回调重试审计 CREATE TABLE IF NOT EXISTS `zyt_qywx_customer_acquisition_customer` ( `id` bigint unsigned NOT NULL AUTO_INCREMENT, `promotion_link_id` int unsigned NOT NULL DEFAULT 0 COMMENT '本地 qywx_promotion_link.id', `link_id` varchar(128) NOT NULL DEFAULT '' COMMENT '企业微信获客链接 ID', `external_userid` varchar(128) NOT NULL DEFAULT '', `userid` varchar(128) NOT NULL DEFAULT '' COMMENT '接待成员企业微信 userid', `owner_admin_id` int unsigned NOT NULL DEFAULT 0 COMMENT 'userid 对应后台管理员 ID,0 为未映射', `dept_id` int unsigned NOT NULL DEFAULT 0, `state` varchar(255) NOT NULL DEFAULT '', `chat_status` tinyint unsigned NOT NULL DEFAULT 0, `recv_msg_cnt` int unsigned NOT NULL DEFAULT 0 COMMENT 'get_chat_info 返回的累计值,只覆盖/max,不累加', `message_count_known` tinyint unsigned NOT NULL DEFAULT 0 COMMENT '1已在 ChatKey 有效期内取得精确累计数,0仅有聊天状态', `first_acquired_time` int unsigned NOT NULL DEFAULT 0, `last_chat_time` int unsigned NOT NULL DEFAULT 0, `last_sync_time` int unsigned NOT NULL DEFAULT 0, `raw_snapshot` mediumtext NULL, `create_time` int unsigned NOT NULL DEFAULT 0, `update_time` int unsigned NOT NULL DEFAULT 0, PRIMARY KEY (`id`), UNIQUE KEY `uk_link_external_user` (`link_id`,`external_userid`,`userid`), KEY `idx_owner_chat` (`owner_admin_id`,`chat_status`), KEY `idx_dept` (`dept_id`), KEY `idx_local_link` (`promotion_link_id`), KEY `idx_last_chat` (`last_chat_time`) ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COMMENT='企微获客客户归因与消息统计'; CREATE TABLE IF NOT EXISTS `zyt_qywx_customer_acquisition_event` ( `id` bigint unsigned NOT NULL AUTO_INCREMENT, `event_key` char(64) NOT NULL COMMENT '回调稳定字段 SHA-256 幂等键', `change_type` varchar(64) NOT NULL DEFAULT '', `chat_key` varchar(512) NOT NULL DEFAULT '', `link_id` varchar(128) NOT NULL DEFAULT '', `external_userid` varchar(128) NOT NULL DEFAULT '', `userid` varchar(128) NOT NULL DEFAULT '', `status` tinyint unsigned NOT NULL DEFAULT 0 COMMENT '0处理中 1成功 2待重试 3终态失败(过期或无效)', `attempts` smallint unsigned NOT NULL DEFAULT 0, `event_time` int unsigned NOT NULL DEFAULT 0, `expire_time` int unsigned NOT NULL DEFAULT 0 COMMENT 'ChatKey 硬截止时间', `next_retry` int unsigned NOT NULL DEFAULT 0, `error_message` varchar(1000) NOT NULL DEFAULT '', `raw_json` mediumtext NULL, `create_time` int unsigned NOT NULL DEFAULT 0, `update_time` int unsigned NOT NULL DEFAULT 0, PRIMARY KEY (`id`), UNIQUE KEY `uk_event_key` (`event_key`), KEY `idx_retry` (`status`,`next_retry`,`expire_time`), KEY `idx_chat_key` (`chat_key`(191)), KEY `idx_event_time` (`event_time`) ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COMMENT='企微获客回调幂等与失败重试审计'; -- 依赖系统层每分钟执行 php think crontab;本任务自身也按每分钟调度,确保不浪费 ChatKey 30 分钟窗口。 INSERT INTO `zyt_dev_crontab` ( `name`, `type`, `system`, `remark`, `command`, `params`, `status`, `expression`, `create_time`, `update_time` ) SELECT '重试企微获客会话回调', 1, 0, '重试 customer_start_chat/message_from_customer 的 get_chat_info;30 分钟后明确 failed_expired', 'qywx:retry-customer-acquisition-events', '', 1, '* * * * *', UNIX_TIMESTAMP(), UNIX_TIMESTAMP() FROM DUAL WHERE NOT EXISTS ( SELECT 1 FROM `zyt_dev_crontab` WHERE `command` = 'qywx:retry-customer-acquisition-events' AND `delete_time` IS NULL );