-- 套餐与佣金模型重构 - 回滚
-- 注意：此回滚会丢失新增字段的数据

-- ============================================
-- 7. ShopSeriesAllocation 表恢复废弃字段
-- ============================================

-- 移除新增的字段
ALTER TABLE tb_shop_series_allocation
DROP COLUMN IF EXISTS one_time_commission_amount;

-- 恢复一次性佣金完整配置字段
ALTER TABLE tb_shop_series_allocation
ADD COLUMN IF NOT EXISTS enable_one_time_commission BOOLEAN DEFAULT false NOT NULL,
ADD COLUMN IF NOT EXISTS one_time_commission_type VARCHAR(20),
ADD COLUMN IF NOT EXISTS one_time_commission_trigger VARCHAR(30),
ADD COLUMN IF NOT EXISTS one_time_commission_threshold BIGINT DEFAULT 0,
ADD COLUMN IF NOT EXISTS one_time_commission_mode VARCHAR(20),
ADD COLUMN IF NOT EXISTS one_time_commission_value BIGINT DEFAULT 0,
ADD COLUMN IF NOT EXISTS enable_force_recharge BOOLEAN DEFAULT false,
ADD COLUMN IF NOT EXISTS force_recharge_amount BIGINT DEFAULT 0,
ADD COLUMN IF NOT EXISTS force_recharge_trigger_type INT DEFAULT 2;

-- ============================================
-- 6. ShopSeriesOneTimeCommissionTier 表移除统计范围
-- ============================================

DO $$
BEGIN
    IF EXISTS (
        SELECT 1 FROM information_schema.tables 
        WHERE table_name = 'tb_shop_series_one_time_commission_tier'
    ) THEN
        ALTER TABLE tb_shop_series_one_time_commission_tier
        DROP COLUMN IF EXISTS stat_scope;
    END IF;
END $$;

-- ============================================
-- 5. PackageSeries 表移除一次性佣金规则配置
-- ============================================

ALTER TABLE tb_package_series
DROP COLUMN IF EXISTS one_time_commission_config;

-- ============================================
-- 4. Device 表移除追踪字段
-- ============================================

ALTER TABLE tb_device
DROP COLUMN IF EXISTS accumulated_recharge_by_series,
DROP COLUMN IF EXISTS first_recharge_triggered_by_series;

-- ============================================
-- 3. IoTCard 表移除追踪字段
-- ============================================

ALTER TABLE tb_iot_card
DROP COLUMN IF EXISTS accumulated_recharge_by_series,
DROP COLUMN IF EXISTS first_recharge_triggered_by_series;

-- ============================================
-- 2. ShopPackageAllocation 表移除字段
-- ============================================

ALTER TABLE tb_shop_package_allocation
DROP COLUMN IF EXISTS one_time_commission_amount;

-- ============================================
-- 1. Package 表恢复废弃字段
-- ============================================

-- 移除虚流量开关字段
ALTER TABLE tb_package
DROP COLUMN IF EXISTS enable_virtual_data;

-- 恢复 cost_price 为 suggested_cost_price
DO $$
BEGIN
    IF EXISTS (
        SELECT 1 FROM information_schema.columns 
        WHERE table_name = 'tb_package' AND column_name = 'cost_price'
    ) AND NOT EXISTS (
        SELECT 1 FROM information_schema.columns 
        WHERE table_name = 'tb_package' AND column_name = 'suggested_cost_price'
    ) THEN
        ALTER TABLE tb_package RENAME COLUMN cost_price TO suggested_cost_price;
    END IF;
END $$;

-- 恢复废弃字段
ALTER TABLE tb_package
ADD COLUMN IF NOT EXISTS price BIGINT DEFAULT 0 NOT NULL,
ADD COLUMN IF NOT EXISTS data_type VARCHAR(20),
ADD COLUMN IF NOT EXISTS data_amount_mb BIGINT DEFAULT 0;
