Files
ycc-proxy-server/deploy/sql/add_invite_code_usage_table.sql
2025-12-02 19:57:10 +08:00

46 lines
2.7 KiB
SQL
Raw Permalink 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.

-- ============================================
-- 邀请码使用历史表
-- 说明:记录每次邀请码的使用情况,支持统计和查询
--
-- 功能说明:
-- 1. 记录每个邀请码每次使用的详细信息
-- 2. 支持统计每个邀请码邀请了多少代理
-- 3. 支持查询某个代理是通过哪个邀请码成为代理的
-- 4. 保留完整的使用历史记录
--
-- 执行步骤:
-- 1. 执行此 SQL 创建表和添加字段
-- 2. 使用 goctl 生成 Modelagent_invite_code_usage -> AgentInviteCodeUsageModel
-- 3. 在 servicecontext.go 中添加 AgentInviteCodeUsageModel
-- ============================================
CREATE TABLE `agent_invite_code_usage` (
`id` bigint NOT NULL AUTO_INCREMENT COMMENT '主键ID',
`invite_code_id` bigint NOT NULL COMMENT '邀请码ID关联agent_invite_code表',
`code` varchar(50) NOT NULL COMMENT '邀请码(冗余字段,便于查询)',
`user_id` bigint NOT NULL COMMENT '使用用户ID',
`agent_id` bigint NOT NULL COMMENT '成为的代理ID',
`agent_level` tinyint NOT NULL COMMENT '代理等级1=普通2=黄金3=钻石',
`used_time` datetime NOT NULL DEFAULT CURRENT_TIMESTAMP COMMENT '使用时间',
`create_time` datetime NOT NULL DEFAULT CURRENT_TIMESTAMP COMMENT '创建时间',
`update_time` datetime NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP COMMENT '更新时间',
`delete_time` datetime DEFAULT NULL COMMENT '删除时间',
`del_state` tinyint NOT NULL DEFAULT 0 COMMENT '删除状态0=未删除1=已删除',
`version` bigint NOT NULL DEFAULT 0 COMMENT '版本号(乐观锁)',
PRIMARY KEY (`id`),
KEY `idx_invite_code_id` (`invite_code_id`) COMMENT '关联邀请码ID',
KEY `idx_code` (`code`) COMMENT '邀请码索引',
KEY `idx_user_id` (`user_id`) COMMENT '用户ID索引',
KEY `idx_agent_id` (`agent_id`) COMMENT '代理ID索引',
KEY `idx_used_time` (`used_time`) COMMENT '使用时间索引',
KEY `idx_create_time` (`create_time`) COMMENT '创建时间索引',
KEY `idx_code_time` (`code`, `used_time`) COMMENT '邀请码和使用时间复合索引'
) ENGINE = InnoDB DEFAULT CHARSET = utf8mb4 COLLATE = utf8mb4_general_ci COMMENT = '邀请码使用历史表';
-- ============================================
-- 在agent表中添加invite_code_id字段可选便于直接查询代理是通过哪个邀请码成为的
-- ============================================
ALTER TABLE `agent`
ADD COLUMN `invite_code_id` bigint DEFAULT NULL COMMENT '通过哪个邀请码成为代理关联agent_invite_code表' AFTER `team_leader_id`;
ALTER TABLE `agent`
ADD KEY `idx_invite_code_id` (`invite_code_id`) COMMENT '邀请码ID索引';