-- ============================================================
-- 删除 users.referred_by 冗余字段
-- 优化绑定关系存储，只使用 referral_bindings 表
-- ============================================================

-- 使用数据库
USE soul_miniprogram;

-- ============================================================
-- 步骤1: 备份 referred_by 数据
-- ============================================================
DROP TABLE IF EXISTS users_referred_by_backup;

CREATE TABLE users_referred_by_backup AS
SELECT id, referred_by, created_at
FROM users
WHERE referred_by IS NOT NULL;

-- 查看备份统计
SELECT 
    COUNT(*) as '备份记录数',
    COUNT(DISTINCT referred_by) as '唯一推荐人数'
FROM users_referred_by_backup;

-- ============================================================
-- 步骤2: 验证 referral_bindings 数据完整性
-- ============================================================

-- 检查绑定表统计
SELECT 
    COUNT(*) as '总绑定记录',
    COUNT(DISTINCT referee_id) as '唯一被推荐人',
    SUM(CASE WHEN status = 'active' THEN 1 ELSE 0 END) as '当前活跃绑定'
FROM referral_bindings;

-- 检查数据一致性（找出在users中有referred_by但在bindings中没有记录的用户）
SELECT COUNT(*) as '不一致记录数' FROM users u
WHERE u.referred_by IS NOT NULL
  AND NOT EXISTS (
    SELECT 1 FROM referral_bindings rb
    WHERE rb.referee_id = u.id
  );

-- ============================================================
-- 步骤3: 删除 referred_by 索引
-- ============================================================

-- 检查索引是否存在
SELECT 
    INDEX_NAME,
    COLUMN_NAME,
    SEQ_IN_INDEX
FROM information_schema.statistics
WHERE table_schema = 'soul_miniprogram'
  AND table_name = 'users'
  AND index_name = 'idx_referred_by';

-- 删除索引（如果存在）
ALTER TABLE users DROP INDEX IF EXISTS idx_referred_by;

-- ============================================================
-- 步骤4: 删除 referred_by 字段
-- ============================================================

-- 检查字段是否存在
SELECT 
    COLUMN_NAME,
    COLUMN_TYPE,
    IS_NULLABLE
FROM information_schema.columns
WHERE table_schema = 'soul_miniprogram'
  AND table_name = 'users'
  AND column_name = 'referred_by';

-- 删除字段
ALTER TABLE users DROP COLUMN referred_by;

-- ============================================================
-- 步骤5: 验证删除结果
-- ============================================================

-- 验证字段已删除
SELECT COUNT(*) as '字段是否仍存在（应为0）'
FROM information_schema.columns
WHERE table_schema = 'soul_miniprogram'
  AND table_name = 'users'
  AND column_name = 'referred_by';

-- 验证备份表
SELECT COUNT(*) as '备份记录数' FROM users_referred_by_backup;

-- ============================================================
-- 完成！
-- ============================================================

-- 查看users表当前结构
SHOW COLUMNS FROM users;

-- 查看referral_bindings表当前结构
SHOW COLUMNS FROM referral_bindings;

-- ============================================================
-- 备注：
-- 1. 备份表 users_referred_by_backup 可保留一段时间
-- 2. 确认无误后，可执行: DROP TABLE users_referred_by_backup;
-- 3. 代码已修改为只使用 referral_bindings 表
-- 4. 部署后请测试绑定和佣金功能
-- ============================================================
