-- ============================================================
-- 代理系统简化迁移脚本
-- 日期: 2025-01-XX
-- 说明: 移除代理邀请、团队关系、提现功能，简化为纯推广模式
-- ============================================================

-- 注意：执行前请备份数据库！

-- ============================================================
-- 第一部分：删除不需要的字段
-- ============================================================

-- 1. agent 表 - 删除等级、上级代理、邀请码相关字段
-- 注意：MySQL DROP COLUMN 不支持 IF EXISTS，如果字段不存在会报错
-- 请先检查字段是否存在再执行
ALTER TABLE `agent` DROP COLUMN `level`;

ALTER TABLE `agent` DROP COLUMN `team_leader_id`;

ALTER TABLE `agent` DROP COLUMN `invite_code_id`;

-- 注意：由于这是生产环境，暂时注释掉删除语句
-- 如确认执行，请取消注释以上语句

-- 2. agent_wallet 表 - 删除冻结余额、提现金额字段
ALTER TABLE `agent_wallet` DROP COLUMN `frozen_balance`;

ALTER TABLE `agent_wallet` DROP COLUMN `withdrawn_amount`;

-- 注意：由于这是生产环境，暂时注释掉删除语句
-- 如确认执行，请取消注释以上语句

-- ============================================================
-- 第二部分：简化佣金状态
-- ============================================================

-- 更新佣金状态：
-- 原状态：1=已发放，2=已冻结，3=已取消
-- 新状态：1=已发放，2=已取消
--
-- 将状态3（已取消）保持不变
-- 将状态2（原已冻结）改为状态1（已发放）
UPDATE `agent_commission` SET `status` = 1 WHERE `status` = 2;

-- 注意：由于这是生产环境，暂时注释掉更新语句
-- 如确认执行，请取消注释以上语句

-- ============================================================
-- 第三部分：清理不需要的表（谨慎操作！）
-- ============================================================
--
-- 以下表包含重要历史数据，暂时不删除
-- 如确认删除，请取消注释并仔细检查
--
-- 邀请码相关表
DROP TABLE IF EXISTS `agent_invite_code`;

DROP TABLE IF EXISTS `agent_invite_code_usage`;

-- 团队关系表
DROP TABLE IF EXISTS `agent_relation`;

-- 返佣相关表
DROP TABLE IF EXISTS `agent_rebate`;

-- 提现相关表
DROP TABLE IF EXISTS `agent_withdrawal`;

DROP TABLE IF EXISTS `agent_withdrawal_tax`;

-- 升级相关表
DROP TABLE IF EXISTS `agent_upgrade`;

-- 实名认证相关表
DROP TABLE IF EXISTS `agent_real_name`;

-- ============================================================
-- 第四部分：验证脚本（执行前先运行此脚本查看影响）
-- ============================================================

-- 查看 agent 表结构和数据量
SELECT
    'agent' as table_name,
    COUNT(*) as total_records,
    COUNT(
        CASE
            WHEN level > 0 THEN 1
        END
    ) as has_level,
    COUNT(
        CASE
            WHEN team_leader_id IS NOT NULL
            AND team_leader_id != '' THEN 1
        END
    ) as has_team_leader,
    COUNT(
        CASE
            WHEN invite_code_id IS NOT NULL
            AND invite_code_id != '' THEN 1
        END
    ) as has_invite_code
FROM `agent`
WHERE
    `del_state` = 0;

-- 查看 agent_wallet 表结构和数据量
SELECT
    'agent_wallet' as table_name,
    COUNT(*) as total_records,
    SUM(frozen_balance) as total_frozen_balance,
    SUM(withdrawn_amount) as total_withdrawn_amount
FROM `agent_wallet`
WHERE
    `del_state` = 0;

-- 查看 agent_commission 状态分布
SELECT status, COUNT(*) as count, SUM(amount) as total_amount
FROM `agent_commission`
WHERE
    `del_state` = 0
GROUP BY
    status;

-- 查看需要删除的表的数据量
SELECT 'agent_invite_code' as table_name, COUNT(*) as count
FROM `agent_invite_code`
WHERE
    `del_state` = 0
UNION ALL
SELECT 'agent_relation' as table_name, COUNT(*) as count
FROM `agent_relation`
WHERE
    `del_state` = 0
UNION ALL
SELECT 'agent_rebate' as table_name, COUNT(*) as count
FROM `agent_rebate`
WHERE
    `del_state` = 0
UNION ALL
SELECT 'agent_withdrawal' as table_name, COUNT(*) as count
FROM `agent_withdrawal`
WHERE
    `del_state` = 0
UNION ALL
SELECT 'agent_upgrade' as table_name, COUNT(*) as count
FROM `agent_upgrade`
WHERE
    `del_state` = 0
UNION ALL
SELECT 'agent_real_name' as table_name, COUNT(*) as count
FROM `agent_real_name`
WHERE
    `del_state` = 0;

-- ============================================================
-- 执行说明
-- ============================================================
--
-- 1. 首先运行验证脚本（第四部分），查看影响范围
-- 2. 确认无误后，取消注释第一部分和第二部分的SQL语句
-- 3. 逐步执行，不要一次性执行所有语句
-- 4. 每执行一部分后，验证应用程序是否正常
-- 5. 第三部分的表删除操作需要额外谨慎，建议先备份数据
--
-- ============================================================