All checks were successful
Build and Deploy / deploy (push) Successful in 1m19s
合并微信/邮件通知日志,支持 H5 双通道订阅、系统发信与操作日志摘要;上线前执行 sql/migrate_order_notify_all_20261121.sql。 Co-authored-by: Cursor <cursoragent@cursor.com>
176 lines
8.2 KiB
SQL
176 lines
8.2 KiB
SQL
-- =============================================================================
|
||
-- 订单通知(微信 + 邮件)一次升级脚本
|
||
-- 前提:已有 t_wechat_notify_template / t_customer_wechat_notify_pref / t_wechat_notify_log
|
||
-- 可重复执行
|
||
-- =============================================================================
|
||
|
||
SET NAMES utf8mb4;
|
||
SET @db := DATABASE();
|
||
|
||
-- ---------------------------------------------------------------------------
|
||
-- 1) 模板表:邮件通道字段
|
||
-- ---------------------------------------------------------------------------
|
||
SET @sql := (
|
||
SELECT IF(
|
||
EXISTS(
|
||
SELECT 1 FROM information_schema.COLUMNS
|
||
WHERE TABLE_SCHEMA = @db
|
||
AND TABLE_NAME = 't_wechat_notify_template'
|
||
AND COLUMN_NAME = 'EmailEnabled'
|
||
),
|
||
'SELECT ''EmailEnabled already exists'' AS info',
|
||
'ALTER TABLE `t_wechat_notify_template`
|
||
ADD COLUMN `EmailEnabled` TINYINT NOT NULL DEFAULT 0 COMMENT ''是否启用邮件通知:0-否,1-是'' AFTER `IsEnabled`,
|
||
ADD COLUMN `EmailTemplateName` VARCHAR(128) NULL COMMENT ''batch-email 模板名称'' AFTER `EmailEnabled`,
|
||
ADD COLUMN `EmailSmtpOwnerUserId` INT NULL COMMENT ''发信 SMTP 所属用户;0 表示系统发信账号'' AFTER `EmailTemplateName`'
|
||
)
|
||
);
|
||
PREPARE stmt FROM @sql; EXECUTE stmt; DEALLOCATE PREPARE stmt;
|
||
|
||
-- ---------------------------------------------------------------------------
|
||
-- 2) 模板表:模板归属用户(与 SMTP 解耦)
|
||
-- ---------------------------------------------------------------------------
|
||
SET @sql := (
|
||
SELECT IF(
|
||
EXISTS(
|
||
SELECT 1 FROM information_schema.COLUMNS
|
||
WHERE TABLE_SCHEMA = @db
|
||
AND TABLE_NAME = 't_wechat_notify_template'
|
||
AND COLUMN_NAME = 'EmailTemplateOwnerUserId'
|
||
),
|
||
'SELECT ''EmailTemplateOwnerUserId already exists'' AS info',
|
||
'ALTER TABLE `t_wechat_notify_template`
|
||
ADD COLUMN `EmailTemplateOwnerUserId` INT NULL COMMENT ''batch-email 模板所属用户'' AFTER `EmailTemplateName`'
|
||
)
|
||
);
|
||
PREPARE stmt FROM @sql; EXECUTE stmt; DEALLOCATE PREPARE stmt;
|
||
|
||
UPDATE `t_wechat_notify_template`
|
||
SET `EmailTemplateOwnerUserId` = `EmailSmtpOwnerUserId`
|
||
WHERE `EmailTemplateOwnerUserId` IS NULL
|
||
AND `EmailSmtpOwnerUserId` IS NOT NULL
|
||
AND `EmailSmtpOwnerUserId` > 0;
|
||
|
||
-- ---------------------------------------------------------------------------
|
||
-- 3) 客户偏好:邮件订阅
|
||
-- ---------------------------------------------------------------------------
|
||
SET @sql := (
|
||
SELECT IF(
|
||
EXISTS(
|
||
SELECT 1 FROM information_schema.COLUMNS
|
||
WHERE TABLE_SCHEMA = @db
|
||
AND TABLE_NAME = 't_customer_wechat_notify_pref'
|
||
AND COLUMN_NAME = 'IsEmailSubscribed'
|
||
),
|
||
'SELECT ''IsEmailSubscribed already exists'' AS info',
|
||
'ALTER TABLE `t_customer_wechat_notify_pref`
|
||
ADD COLUMN `IsEmailSubscribed` TINYINT NOT NULL DEFAULT 0 COMMENT ''是否订阅邮件:0-否,1-是'' AFTER `IsSubscribed`'
|
||
)
|
||
);
|
||
PREPARE stmt FROM @sql; EXECUTE stmt; DEALLOCATE PREPARE stmt;
|
||
|
||
-- ---------------------------------------------------------------------------
|
||
-- 4) 统一通知日志表 t_order_notify_log(微信 / 邮件)
|
||
-- ---------------------------------------------------------------------------
|
||
SET @sql := (
|
||
SELECT IF(
|
||
EXISTS(
|
||
SELECT 1 FROM information_schema.TABLES
|
||
WHERE TABLE_SCHEMA = @db AND TABLE_NAME = 't_order_notify_log'
|
||
),
|
||
'SELECT ''t_order_notify_log already exists'' AS info',
|
||
'CREATE TABLE `t_order_notify_log` (
|
||
`Id` INT NOT NULL AUTO_INCREMENT COMMENT ''主键,自增,步长 1'',
|
||
`CustomerId` INT NOT NULL DEFAULT 0 COMMENT ''客户 Id'',
|
||
`OrderId` INT NOT NULL DEFAULT 0 COMMENT ''订单 Id'',
|
||
`Channel` VARCHAR(16) NOT NULL COMMENT ''wechat/email'',
|
||
`EventCode` VARCHAR(32) NOT NULL COMMENT ''事件码'',
|
||
`Recipient` VARCHAR(256) NULL COMMENT ''OpenId 或收件邮箱'',
|
||
`TemplateRef` VARCHAR(128) NULL COMMENT ''微信 TemplateId 或邮件模板名'',
|
||
`Subject` VARCHAR(512) NULL COMMENT ''邮件主题'',
|
||
`Status` VARCHAR(16) NOT NULL DEFAULT ''skipped'' COMMENT ''sent/failed/skipped'',
|
||
`ProviderMsgId` VARCHAR(64) NULL COMMENT ''通道回执 msgid'',
|
||
`ErrCode` INT NULL COMMENT ''错误码'',
|
||
`ErrMsg` VARCHAR(512) NULL COMMENT ''错误或跳过原因'',
|
||
`SmtpUsername` VARCHAR(256) NULL COMMENT ''发件 SMTP'',
|
||
`PayloadJson` TEXT NULL COMMENT ''发送快照'',
|
||
`IsDeleted` TINYINT NOT NULL DEFAULT 0 COMMENT ''软删除:0-否,1-是'',
|
||
`ConcurrencyStamp` CHAR(36) NOT NULL DEFAULT (UUID()) COMMENT ''并发戳'',
|
||
`CreateUserId` INT NOT NULL DEFAULT 0 COMMENT ''创建人'',
|
||
`CreateTime` DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP COMMENT ''创建时间'',
|
||
`UpdateUserId` INT NULL COMMENT ''更新人'',
|
||
`UpdateTime` DATETIME NULL COMMENT ''更新时间'',
|
||
PRIMARY KEY (`Id`),
|
||
KEY `ix_order_notify_log_order_ch_event` (`OrderId`, `Channel`, `EventCode`, `CreateTime`),
|
||
KEY `ix_order_notify_log_cus_time` (`CustomerId`, `CreateTime`),
|
||
KEY `ix_order_notify_log_status` (`Status`, `Id`)
|
||
) COMMENT=''订单通知发送日志(微信/邮件)'''
|
||
)
|
||
);
|
||
PREPARE stmt FROM @sql; EXECUTE stmt; DEALLOCATE PREPARE stmt;
|
||
|
||
-- ---------------------------------------------------------------------------
|
||
-- 5) 若线上已有旧微信日志,灌入统一表(无则跳过;可重复)
|
||
-- ---------------------------------------------------------------------------
|
||
SET @has_wx := (
|
||
SELECT COUNT(*) FROM information_schema.TABLES
|
||
WHERE TABLE_SCHEMA = @db AND TABLE_NAME = 't_wechat_notify_log'
|
||
);
|
||
SET @sql := IF(
|
||
@has_wx > 0,
|
||
'INSERT INTO `t_order_notify_log` (
|
||
`CustomerId`, `OrderId`, `Channel`, `EventCode`, `Recipient`, `TemplateRef`, `Subject`,
|
||
`Status`, `ProviderMsgId`, `ErrCode`, `ErrMsg`, `SmtpUsername`, `PayloadJson`,
|
||
`IsDeleted`, `ConcurrencyStamp`, `CreateUserId`, `CreateTime`, `UpdateUserId`, `UpdateTime`
|
||
)
|
||
SELECT
|
||
w.`CustomerId`, w.`OrderId`, ''wechat'', w.`EventCode`, w.`OpenId`, w.`TemplateId`, NULL,
|
||
w.`Status`, w.`WxMsgId`, w.`ErrCode`, w.`ErrMsg`, NULL, w.`PayloadJson`,
|
||
w.`IsDeleted`, UUID(), w.`CreateUserId`, w.`CreateTime`, w.`UpdateUserId`, w.`UpdateTime`
|
||
FROM `t_wechat_notify_log` w
|
||
WHERE NOT EXISTS (
|
||
SELECT 1 FROM `t_order_notify_log` n
|
||
WHERE n.`Channel` = ''wechat''
|
||
AND n.`OrderId` = w.`OrderId`
|
||
AND n.`EventCode` = w.`EventCode`
|
||
AND n.`CreateTime` = w.`CreateTime`
|
||
AND IFNULL(n.`Status`, '''') = IFNULL(w.`Status`, '''')
|
||
AND IFNULL(n.`Recipient`, '''') = IFNULL(w.`OpenId`, '''')
|
||
)',
|
||
'SELECT ''skip wechat migrate'' AS info'
|
||
);
|
||
PREPARE stmt FROM @sql; EXECUTE stmt; DEALLOCATE PREPARE stmt;
|
||
|
||
-- 若曾建过邮件专用日志表,一并灌入(多数环境无此表)
|
||
SET @has_mail := (
|
||
SELECT COUNT(*) FROM information_schema.TABLES
|
||
WHERE TABLE_SCHEMA = @db AND TABLE_NAME = 't_order_notify_email_log'
|
||
);
|
||
SET @sql := IF(
|
||
@has_mail > 0,
|
||
'INSERT INTO `t_order_notify_log` (
|
||
`CustomerId`, `OrderId`, `Channel`, `EventCode`, `Recipient`, `TemplateRef`, `Subject`,
|
||
`Status`, `ProviderMsgId`, `ErrCode`, `ErrMsg`, `SmtpUsername`, `PayloadJson`,
|
||
`IsDeleted`, `ConcurrencyStamp`, `CreateUserId`, `CreateTime`, `UpdateUserId`, `UpdateTime`
|
||
)
|
||
SELECT
|
||
e.`CustomerId`, e.`OrderId`, ''email'', e.`EventCode`, e.`ToEmail`, e.`TemplateName`, e.`Subject`,
|
||
e.`Status`, NULL, NULL, e.`ErrMsg`, e.`SmtpUsername`, e.`PayloadJson`,
|
||
e.`IsDeleted`, UUID(), e.`CreateUserId`, e.`CreateTime`, e.`UpdateUserId`, e.`UpdateTime`
|
||
FROM `t_order_notify_email_log` e
|
||
WHERE NOT EXISTS (
|
||
SELECT 1 FROM `t_order_notify_log` n
|
||
WHERE n.`Channel` = ''email''
|
||
AND n.`OrderId` = e.`OrderId`
|
||
AND n.`EventCode` = e.`EventCode`
|
||
AND n.`CreateTime` = e.`CreateTime`
|
||
AND IFNULL(n.`Status`, '''') = IFNULL(e.`Status`, '''')
|
||
AND IFNULL(n.`Recipient`, '''') = IFNULL(e.`ToEmail`, '''')
|
||
)',
|
||
'SELECT ''skip email migrate'' AS info'
|
||
);
|
||
PREPARE stmt FROM @sql; EXECUTE stmt; DEALLOCATE PREPARE stmt;
|
||
|
||
-- 旧表 t_wechat_notify_log / t_order_notify_email_log 可保留只读;确认无流量后再手工 DROP
|
||
SELECT 'migrate_order_notify_all_20261121 done' AS info;
|