-- 回滚: 账号与佣金管理模块数据模型变更
-- 变更ID: add-commission-model-changes

-- 1. 恢复 owner_type 枚举值 (shop -> agent)
UPDATE tb_iot_card SET owner_type = 'agent' WHERE owner_type = 'shop';
UPDATE tb_device SET owner_type = 'agent' WHERE owner_type = 'shop';

-- 2. 删除新增的表
DROP TABLE IF EXISTS tb_asset_allocation_record;
DROP TABLE IF EXISTS tb_enterprise_card_authorization;

-- 3. 删除 tb_device.shop_id 字段
DROP INDEX IF EXISTS idx_device_shop_id;
ALTER TABLE tb_device DROP COLUMN IF EXISTS shop_id;

-- 4. 删除 tb_iot_card.shop_id 字段
DROP INDEX IF EXISTS idx_iot_card_shop_id;
ALTER TABLE tb_iot_card DROP COLUMN IF EXISTS shop_id;

-- 5. 删除 tb_commission_withdrawal_setting.daily_withdrawal_limit 字段
ALTER TABLE tb_commission_withdrawal_setting DROP COLUMN IF EXISTS daily_withdrawal_limit;

-- 6. 删除 tb_commission_record 新增字段
DROP INDEX IF EXISTS idx_commission_record_shop_id;
ALTER TABLE tb_commission_record 
DROP COLUMN IF EXISTS shop_id,
DROP COLUMN IF EXISTS balance_after;

-- 7. 删除 tb_account.is_primary 字段
ALTER TABLE tb_account DROP COLUMN IF EXISTS is_primary;

-- 8. 删除 tb_commission_withdrawal_request 新增字段
DROP INDEX IF EXISTS uk_commission_withdrawal_no;
ALTER TABLE tb_commission_withdrawal_request
DROP COLUMN IF EXISTS withdrawal_no,
DROP COLUMN IF EXISTS applicant_id,
DROP COLUMN IF EXISTS shop_id,
DROP COLUMN IF EXISTS fee_rate,
DROP COLUMN IF EXISTS payment_type,
DROP COLUMN IF EXISTS processor_id,
DROP COLUMN IF EXISTS processed_at,
DROP COLUMN IF EXISTS remark;
