admin/sql/migrate_agent_module_20261029.sql
zzh 0c89ec6ce5
All checks were successful
Build and Deploy / deploy (push) Successful in 1m12s
fix: 代理迁移脚本补充 ConcurrencyStamp,避免 MySQL 1674
Co-authored-by: Cursor <cursoragent@cursor.com>
2026-10-29 18:38:18 +08:00

106 lines
4.7 KiB
SQL
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

-- 渠道代理与返佣(首版轻量)。可重复执行。
-- 含:t_agent、t_agent_commission_detail;t_customer / t_order 扩展 AgentId。
-- AggregateRoot 需要 ConcurrencyStamp。
SET @db := DATABASE();
-- ---------- t_agent ----------
CREATE TABLE IF NOT EXISTS `t_agent` (
`Id` INT NOT NULL AUTO_INCREMENT,
`UserId` INT NOT NULL COMMENT 'RBAC user.userid / JWT mapId',
`AgentCode` VARCHAR(32) NOT NULL COMMENT '登录账号+注册链接编码',
`AgentName` VARCHAR(64) NOT NULL COMMENT '显示名',
`Phone` VARCHAR(32) NULL,
`Email` VARCHAR(128) NULL,
`Password` VARCHAR(64) NOT NULL COMMENT 'MD5 密码',
`Status` TINYINT NOT NULL DEFAULT 2 COMMENT '1停用 2启用',
`BaseType` VARCHAR(32) NOT NULL DEFAULT 'freight' COMMENT 'freight/receivable',
`CommissionRate` DECIMAL(18,6) NOT NULL DEFAULT 0 COMMENT '返佣比例',
`SalesProductIds` VARCHAR(512) NULL COMMENT '可选销售产品Id列表 JSON/[1,2] 或逗号;空=不限',
`Remark` VARCHAR(512) NULL,
`IsDeleted` TINYINT(1) NOT NULL DEFAULT 0,
`CreateUserId` INT NOT NULL DEFAULT 0,
`CreateTime` DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP,
`UpdateUserId` INT NOT NULL DEFAULT 0,
`UpdateTime` DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
-- 不用 DEFAULT(UUID()):statement 复制下会报 1674;运行时由 ORM 写入
`ConcurrencyStamp` CHAR(36) NOT NULL DEFAULT '' COMMENT '并发戳',
PRIMARY KEY (`Id`),
KEY `ix_agent_code` (`AgentCode`),
KEY `ix_agent_userid` (`UserId`)
) COMMENT='渠道代理主数据';
-- 已建表补 ConcurrencyStamp(可重复)
SET @exists := (
SELECT COUNT(1) FROM information_schema.COLUMNS
WHERE TABLE_SCHEMA = @db AND TABLE_NAME = 't_agent' AND COLUMN_NAME = 'ConcurrencyStamp'
);
SET @sql := IF(
@exists = 0,
'ALTER TABLE `t_agent` ADD COLUMN `ConcurrencyStamp` CHAR(36) NOT NULL DEFAULT '''' COMMENT ''并发戳'' AFTER `UpdateTime`',
'SELECT ''t_agent.ConcurrencyStamp already exists'' AS info'
);
PREPARE stmt FROM @sql; EXECUTE stmt; DEALLOCATE PREPARE stmt;
-- ---------- t_agent_commission_detail ----------
CREATE TABLE IF NOT EXISTS `t_agent_commission_detail` (
`Id` INT NOT NULL AUTO_INCREMENT,
`AgentId` INT NOT NULL,
`OrderId` INT NOT NULL,
`CustomerId` INT NOT NULL,
`SalesProductId` INT NULL,
`BaseType` VARCHAR(32) NOT NULL,
`BaseAmount` DECIMAL(18,4) NOT NULL DEFAULT 0,
`CommissionRate` DECIMAL(18,6) NOT NULL DEFAULT 0,
`CommissionAmount` DECIMAL(18,4) NOT NULL DEFAULT 0,
`Status` VARCHAR(16) NOT NULL DEFAULT 'pending' COMMENT 'pending/paid',
`TriggerOrderStatus` VARCHAR(32) NOT NULL,
`PaidTime` DATETIME NULL,
`Remark` VARCHAR(512) NULL,
`IsDeleted` TINYINT(1) NOT NULL DEFAULT 0,
`CreateUserId` INT NOT NULL DEFAULT 0,
`CreateTime` DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP,
`UpdateUserId` INT NOT NULL DEFAULT 0,
`UpdateTime` DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
`ConcurrencyStamp` CHAR(36) NOT NULL DEFAULT '' COMMENT '并发戳',
PRIMARY KEY (`Id`),
KEY `ix_agent_comm_agent` (`AgentId`),
KEY `ix_agent_comm_order` (`OrderId`),
KEY `ix_agent_comm_status` (`Status`)
) COMMENT='代理返佣明细(首版无余额入账)';
SET @exists := (
SELECT COUNT(1) FROM information_schema.COLUMNS
WHERE TABLE_SCHEMA = @db AND TABLE_NAME = 't_agent_commission_detail' AND COLUMN_NAME = 'ConcurrencyStamp'
);
SET @sql := IF(
@exists = 0,
'ALTER TABLE `t_agent_commission_detail` ADD COLUMN `ConcurrencyStamp` CHAR(36) NOT NULL DEFAULT '''' COMMENT ''并发戳'' AFTER `UpdateTime`',
'SELECT ''t_agent_commission_detail.ConcurrencyStamp already exists'' AS info'
);
PREPARE stmt FROM @sql; EXECUTE stmt; DEALLOCATE PREPARE stmt;
-- ---------- t_customer.AgentId ----------
SET @exists := (
SELECT COUNT(1) FROM information_schema.COLUMNS
WHERE TABLE_SCHEMA = @db AND TABLE_NAME = 't_customer' AND COLUMN_NAME = 'AgentId'
);
SET @sql := IF(
@exists = 0,
'ALTER TABLE `t_customer` ADD COLUMN `AgentId` INT NULL COMMENT ''所属渠道代理'' AFTER `CommissionBalance`, ADD COLUMN `AgentBindTime` DATETIME NULL COMMENT ''代理绑定时间'' AFTER `AgentId`, ADD KEY `ix_customer_agent` (`AgentId`)',
'SELECT ''t_customer.AgentId already exists'' AS info'
);
PREPARE stmt FROM @sql; EXECUTE stmt; DEALLOCATE PREPARE stmt;
-- ---------- t_order.AgentId ----------
SET @exists := (
SELECT COUNT(1) FROM information_schema.COLUMNS
WHERE TABLE_SCHEMA = @db AND TABLE_NAME = 't_order' AND COLUMN_NAME = 'AgentId'
);
SET @sql := IF(
@exists = 0,
'ALTER TABLE `t_order` ADD COLUMN `AgentId` INT NULL COMMENT ''下单时代理快照'' AFTER `CustomerId`, ADD KEY `ix_order_agent` (`AgentId`)',
'SELECT ''t_order.AgentId already exists'' AS info'
);
PREPARE stmt FROM @sql; EXECUTE stmt; DEALLOCATE PREPARE stmt;