admin/sql/migrate_agent_module_20261029.sql
zzh 1d987cde6e
All checks were successful
Build and Deploy / deploy (push) Successful in 1m13s
feat: 新增渠道代理注册归因与返佣明细
独立 Agent 主体与 loginType=2,注册绑定/下单快照,派送成功失败计提明细,不含提现入账。

Co-authored-by: Cursor <cursoragent@cursor.com>
2026-10-29 17:18:07 +08:00

79 lines
3.5 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。
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,
PRIMARY KEY (`Id`),
KEY `ix_agent_code` (`AgentCode`),
KEY `ix_agent_userid` (`UserId`)
) COMMENT='渠道代理主数据';
-- ---------- 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,
PRIMARY KEY (`Id`),
KEY `ix_agent_comm_agent` (`AgentId`),
KEY `ix_agent_comm_order` (`OrderId`),
KEY `ix_agent_comm_status` (`Status`)
) COMMENT='代理返佣明细(首版无余额入账)';
-- ---------- 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;