-- ============================================
-- UUID迁移脚本
-- 将系统中所有bigint类型的ID字段改为CHAR(36)类型的UUID
-- ============================================

-- 注意：此脚本需要分阶段执行，建议在测试环境充分测试后再在生产环境执行
-- 执行前请务必备份数据库！

-- ============================================
-- 第一阶段：创建UUID映射表（用于数据迁移）
-- ============================================

-- 创建临时映射表，用于存储旧ID和新UUID的对应关系
CREATE TABLE IF NOT EXISTS `uuid_mapping` (
    `table_name` VARCHAR(64) NOT NULL COMMENT '表名',
    `old_id` BIGINT NOT NULL COMMENT '旧ID',
    `new_uuid` CHAR(36) NOT NULL COMMENT '新UUID',
    PRIMARY KEY (`table_name`, `old_id`),
    KEY `idx_new_uuid` (`new_uuid`)
) ENGINE = InnoDB DEFAULT CHARSET = utf8mb4 COLLATE = utf8mb4_general_ci COMMENT = 'UUID映射表';

-- ============================================
-- 第二阶段：为所有表生成UUID映射
-- ============================================

-- 为user表生成UUID映射
INSERT INTO
    `uuid_mapping` (
        `table_name`,
        `old_id`,
        `new_uuid`
    )
SELECT 'user', `id`, UUID()
FROM `user`
WHERE
    `del_state` = 0;

-- 为agent表生成UUID映射
INSERT INTO
    `uuid_mapping` (
        `table_name`,
        `old_id`,
        `new_uuid`
    )
SELECT 'agent', `id`, UUID()
FROM `agent`
WHERE
    `del_state` = 0;

-- 为product表生成UUID映射
INSERT INTO
    `uuid_mapping` (
        `table_name`,
        `old_id`,
        `new_uuid`
    )
SELECT 'product', `id`, UUID()
FROM `product`
WHERE
    `del_state` = 0;

-- 为order表生成UUID映射
INSERT INTO
    `uuid_mapping` (
        `table_name`,
        `old_id`,
        `new_uuid`
    )
SELECT 'order', `id`, UUID()
FROM `order`
WHERE
    `del_state` = 0;

-- 为query表生成UUID映射
INSERT INTO
    `uuid_mapping` (
        `table_name`,
        `old_id`,
        `new_uuid`
    )
SELECT 'query', `id`, UUID()
FROM `query`
WHERE
    `del_state` = 0;

-- 为user_auth表生成UUID映射
INSERT INTO
    `uuid_mapping` (
        `table_name`,
        `old_id`,
        `new_uuid`
    )
SELECT 'user_auth', `id`, UUID()
FROM `user_auth`
WHERE
    `del_state` = 0;

-- 为agent_commission表生成UUID映射
INSERT INTO
    `uuid_mapping` (
        `table_name`,
        `old_id`,
        `new_uuid`
    )
SELECT 'agent_commission', `id`, UUID()
FROM `agent_commission`
WHERE
    `del_state` = 0;

-- 为agent_invite_code表生成UUID映射
INSERT INTO
    `uuid_mapping` (
        `table_name`,
        `old_id`,
        `new_uuid`
    )
SELECT 'agent_invite_code', `id`, UUID()
FROM `agent_invite_code`
WHERE
    `del_state` = 0;

-- 为agent_link表生成UUID映射
INSERT INTO
    `uuid_mapping` (
        `table_name`,
        `old_id`,
        `new_uuid`
    )
SELECT 'agent_link', `id`, UUID()
FROM `agent_link`
WHERE
    `del_state` = 0;

-- 为agent_order表生成UUID映射
INSERT INTO
    `uuid_mapping` (
        `table_name`,
        `old_id`,
        `new_uuid`
    )
SELECT 'agent_order', `id`, UUID()
FROM `agent_order`
WHERE
    `del_state` = 0;

-- 为agent_wallet表生成UUID映射
INSERT INTO
    `uuid_mapping` (
        `table_name`,
        `old_id`,
        `new_uuid`
    )
SELECT 'agent_wallet', `id`, UUID()
FROM `agent_wallet`
WHERE
    `del_state` = 0;

-- 为agent_withdrawal表生成UUID映射
INSERT INTO
    `uuid_mapping` (
        `table_name`,
        `old_id`,
        `new_uuid`
    )
SELECT 'agent_withdrawal', `id`, UUID()
FROM `agent_withdrawal`
WHERE
    `del_state` = 0;

-- 为agent_withdrawal_tax表生成UUID映射
INSERT INTO
    `uuid_mapping` (
        `table_name`,
        `old_id`,
        `new_uuid`
    )
SELECT 'agent_withdrawal_tax', `id`, UUID()
FROM `agent_withdrawal_tax`
WHERE
    `del_state` = 0;

-- 为agent_rebate表生成UUID映射
INSERT INTO
    `uuid_mapping` (
        `table_name`,
        `old_id`,
        `new_uuid`
    )
SELECT 'agent_rebate', `id`, UUID()
FROM `agent_rebate`
WHERE
    `del_state` = 0;

-- 为agent_relation表生成UUID映射
INSERT INTO
    `uuid_mapping` (
        `table_name`,
        `old_id`,
        `new_uuid`
    )
SELECT 'agent_relation', `id`, UUID()
FROM `agent_relation`
WHERE
    `del_state` = 0;

-- 为agent_upgrade表生成UUID映射
INSERT INTO
    `uuid_mapping` (
        `table_name`,
        `old_id`,
        `new_uuid`
    )
SELECT 'agent_upgrade', `id`, UUID()
FROM `agent_upgrade`
WHERE
    `del_state` = 0;

-- 为agent_real_name表生成UUID映射
INSERT INTO
    `uuid_mapping` (
        `table_name`,
        `old_id`,
        `new_uuid`
    )
SELECT 'agent_real_name', `id`, UUID()
FROM `agent_real_name`
WHERE
    `del_state` = 0;

-- 为agent_config表生成UUID映射
INSERT INTO
    `uuid_mapping` (
        `table_name`,
        `old_id`,
        `new_uuid`
    )
SELECT 'agent_config', `id`, UUID()
FROM `agent_config`
WHERE
    `del_state` = 0;

-- 为agent_product_config表生成UUID映射
INSERT INTO
    `uuid_mapping` (
        `table_name`,
        `old_id`,
        `new_uuid`
    )
SELECT 'agent_product_config', `id`, UUID()
FROM `agent_product_config`
WHERE
    `del_state` = 0;

-- 为agent_short_link表生成UUID映射
INSERT INTO
    `uuid_mapping` (
        `table_name`,
        `old_id`,
        `new_uuid`
    )
SELECT 'agent_short_link', `id`, UUID()
FROM `agent_short_link`
WHERE
    `del_state` = 0;

-- 为agent_invite_code_usage表生成UUID映射
INSERT INTO
    `uuid_mapping` (
        `table_name`,
        `old_id`,
        `new_uuid`
    )
SELECT 'agent_invite_code_usage', `id`, UUID()
FROM `agent_invite_code_usage`
WHERE
    `del_state` = 0;

-- 为agent_freeze_task表生成UUID映射
INSERT INTO
    `uuid_mapping` (
        `table_name`,
        `old_id`,
        `new_uuid`
    )
SELECT 'agent_freeze_task', `id`, UUID()
FROM `agent_freeze_task`
WHERE
    `del_state` = 0;

-- 为order_refund表生成UUID映射
INSERT INTO
    `uuid_mapping` (
        `table_name`,
        `old_id`,
        `new_uuid`
    )
SELECT 'order_refund', `id`, UUID()
FROM `order_refund`
WHERE
    `del_state` = 0;

-- 为query_cleanup_log表生成UUID映射
INSERT INTO
    `uuid_mapping` (
        `table_name`,
        `old_id`,
        `new_uuid`
    )
SELECT 'query_cleanup_log', `id`, UUID()
FROM `query_cleanup_log`
WHERE
    `del_state` = 0;

-- 为query_cleanup_detail表生成UUID映射
INSERT INTO
    `uuid_mapping` (
        `table_name`,
        `old_id`,
        `new_uuid`
    )
SELECT 'query_cleanup_detail', `id`, UUID()
FROM `query_cleanup_detail`
WHERE
    `del_state` = 0;

-- 为query_cleanup_config表生成UUID映射
INSERT INTO
    `uuid_mapping` (
        `table_name`,
        `old_id`,
        `new_uuid`
    )
SELECT 'query_cleanup_config', `id`, UUID()
FROM `query_cleanup_config`
WHERE
    `del_state` = 0;

-- 为product_feature表生成UUID映射
INSERT INTO
    `uuid_mapping` (
        `table_name`,
        `old_id`,
        `new_uuid`
    )
SELECT 'product_feature', `id`, UUID()
FROM `product_feature`
WHERE
    `del_state` = 0;

-- 为feature表生成UUID映射
INSERT INTO
    `uuid_mapping` (
        `table_name`,
        `old_id`,
        `new_uuid`
    )
SELECT 'feature', `id`, UUID()
FROM `feature`
WHERE
    `del_state` = 0;

-- 为authorization_document表生成UUID映射
INSERT INTO
    `uuid_mapping` (
        `table_name`,
        `old_id`,
        `new_uuid`
    )
SELECT 'authorization_document', `id`, UUID()
FROM `authorization_document`
WHERE
    `del_state` = 0;

-- 为global_notifications表生成UUID映射
INSERT INTO
    `uuid_mapping` (
        `table_name`,
        `old_id`,
        `new_uuid`
    )
SELECT 'global_notifications', `id`, UUID()
FROM `global_notifications`
WHERE
    `del_state` = 0;

-- 为admin_user表生成UUID映射
INSERT INTO
    `uuid_mapping` (
        `table_name`,
        `old_id`,
        `new_uuid`
    )
SELECT 'admin_user', `id`, UUID()
FROM `admin_user`
WHERE
    `del_state` = 0;

-- 为admin_role表生成UUID映射
INSERT INTO
    `uuid_mapping` (
        `table_name`,
        `old_id`,
        `new_uuid`
    )
SELECT 'admin_role', `id`, UUID()
FROM `admin_role`
WHERE
    `del_state` = 0;

-- 为admin_menu表生成UUID映射
INSERT INTO
    `uuid_mapping` (
        `table_name`,
        `old_id`,
        `new_uuid`
    )
SELECT 'admin_menu', `id`, UUID()
FROM `admin_menu`
WHERE
    `del_state` = 0;

-- 为admin_api表生成UUID映射
INSERT INTO
    `uuid_mapping` (
        `table_name`,
        `old_id`,
        `new_uuid`
    )
SELECT 'admin_api', `id`, UUID()
FROM `admin_api`
WHERE
    `del_state` = 0;

-- 为admin_dict_type表生成UUID映射
INSERT INTO
    `uuid_mapping` (
        `table_name`,
        `old_id`,
        `new_uuid`
    )
SELECT 'admin_dict_type', `id`, UUID()
FROM `admin_dict_type`
WHERE
    `del_state` = 0;

-- 为admin_dict_data表生成UUID映射
INSERT INTO
    `uuid_mapping` (
        `table_name`,
        `old_id`,
        `new_uuid`
    )
SELECT 'admin_dict_data', `id`, UUID()
FROM `admin_dict_data`
WHERE
    `del_state` = 0;

-- 为admin_user_role表生成UUID映射
INSERT INTO
    `uuid_mapping` (
        `table_name`,
        `old_id`,
        `new_uuid`
    )
SELECT 'admin_user_role', `id`, UUID()
FROM `admin_user_role`
WHERE
    `del_state` = 0;

-- 为admin_role_menu表生成UUID映射
INSERT INTO
    `uuid_mapping` (
        `table_name`,
        `old_id`,
        `new_uuid`
    )
SELECT 'admin_role_menu', `id`, UUID()
FROM `admin_role_menu`
WHERE
    `del_state` = 0;

-- 为admin_role_api表生成UUID映射
INSERT INTO
    `uuid_mapping` (
        `table_name`,
        `old_id`,
        `new_uuid`
    )
SELECT 'admin_role_api', `id`, UUID()
FROM `admin_role_api`
WHERE
    `del_state` = 0;

-- ============================================
-- 第三阶段：修改表结构（添加UUID字段，保留原ID字段）
-- ============================================

-- 注意：此阶段先添加新字段，不删除旧字段，以便回滚

-- user表：添加UUID主键字段
ALTER TABLE `user`
ADD COLUMN `id_uuid` CHAR(36) NULL COMMENT 'UUID主键' AFTER `id`,
ADD INDEX `idx_id_uuid` (`id_uuid`);

-- agent表：添加UUID主键字段
ALTER TABLE `agent`
ADD COLUMN `id_uuid` CHAR(36) NULL COMMENT 'UUID主键' AFTER `id`,
ADD COLUMN `user_id_uuid` CHAR(36) NULL COMMENT '用户UUID' AFTER `user_id`,
ADD COLUMN `team_leader_id_uuid` CHAR(36) NULL COMMENT '团队首领UUID' AFTER `team_leader_id`,
ADD INDEX `idx_id_uuid` (`id_uuid`),
ADD INDEX `idx_user_id_uuid` (`user_id_uuid`),
ADD INDEX `idx_team_leader_id_uuid` (`team_leader_id_uuid`);

-- product表：添加UUID主键字段
ALTER TABLE `product`
ADD COLUMN `id_uuid` CHAR(36) NULL COMMENT 'UUID主键' AFTER `id`,
ADD INDEX `idx_id_uuid` (`id_uuid`);

-- order表：添加UUID主键字段和外键字段
ALTER TABLE `order`
ADD COLUMN `id_uuid` CHAR(36) NULL COMMENT 'UUID主键' AFTER `id`,
ADD COLUMN `user_id_uuid` CHAR(36) NULL COMMENT '用户UUID' AFTER `user_id`,
ADD COLUMN `product_id_uuid` CHAR(36) NULL COMMENT '产品UUID' AFTER `product_id`,
ADD INDEX `idx_id_uuid` (`id_uuid`),
ADD INDEX `idx_user_id_uuid` (`user_id_uuid`),
ADD INDEX `idx_product_id_uuid` (`product_id_uuid`);

-- query表：添加UUID主键字段和外键字段
ALTER TABLE `query`
ADD COLUMN `id_uuid` CHAR(36) NULL COMMENT 'UUID主键' AFTER `id`,
ADD COLUMN `order_id_uuid` CHAR(36) NULL COMMENT '订单UUID' AFTER `order_id`,
ADD COLUMN `user_id_uuid` CHAR(36) NULL COMMENT '用户UUID' AFTER `user_id`,
ADD COLUMN `product_id_uuid` CHAR(36) NULL COMMENT '产品UUID' AFTER `product_id`,
ADD INDEX `idx_id_uuid` (`id_uuid`),
ADD INDEX `idx_order_id_uuid` (`order_id_uuid`),
ADD INDEX `idx_user_id_uuid` (`user_id_uuid`),
ADD INDEX `idx_product_id_uuid` (`product_id_uuid`);

-- user_auth表：添加UUID主键字段和外键字段
ALTER TABLE `user_auth`
ADD COLUMN `id_uuid` CHAR(36) NULL COMMENT 'UUID主键' AFTER `id`,
ADD COLUMN `user_id_uuid` CHAR(36) NULL COMMENT '用户UUID' AFTER `user_id`,
ADD INDEX `idx_id_uuid` (`id_uuid`),
ADD INDEX `idx_user_id_uuid` (`user_id_uuid`);

-- agent_commission表：添加UUID主键字段和外键字段
ALTER TABLE `agent_commission`
ADD COLUMN `id_uuid` CHAR(36) NULL COMMENT 'UUID主键' AFTER `id`,
ADD COLUMN `agent_id_uuid` CHAR(36) NULL COMMENT '代理UUID' AFTER `agent_id`,
ADD COLUMN `order_id_uuid` CHAR(36) NULL COMMENT '订单UUID' AFTER `order_id`,
ADD COLUMN `product_id_uuid` CHAR(36) NULL COMMENT '产品UUID' AFTER `product_id`,
ADD INDEX `idx_id_uuid` (`id_uuid`),
ADD INDEX `idx_agent_id_uuid` (`agent_id_uuid`),
ADD INDEX `idx_order_id_uuid` (`order_id_uuid`),
ADD INDEX `idx_product_id_uuid` (`product_id_uuid`);

-- agent_invite_code表：添加UUID主键字段和外键字段
ALTER TABLE `agent_invite_code`
ADD COLUMN `id_uuid` CHAR(36) NULL COMMENT 'UUID主键' AFTER `id`,
ADD COLUMN `agent_id_uuid` CHAR(36) NULL COMMENT '代理UUID' AFTER `agent_id`,
ADD COLUMN `used_user_id_uuid` CHAR(36) NULL COMMENT '使用用户UUID' AFTER `used_user_id`,
ADD COLUMN `used_agent_id_uuid` CHAR(36) NULL COMMENT '使用代理UUID' AFTER `used_agent_id`,
ADD INDEX `idx_id_uuid` (`id_uuid`),
ADD INDEX `idx_agent_id_uuid` (`agent_id_uuid`),
ADD INDEX `idx_used_user_id_uuid` (`used_user_id_uuid`),
ADD INDEX `idx_used_agent_id_uuid` (`used_agent_id_uuid`);

-- agent_link表：添加UUID主键字段和外键字段
ALTER TABLE `agent_link`
ADD COLUMN `id_uuid` CHAR(36) NULL COMMENT 'UUID主键' AFTER `id`,
ADD COLUMN `agent_id_uuid` CHAR(36) NULL COMMENT '代理UUID' AFTER `agent_id`,
ADD COLUMN `user_id_uuid` CHAR(36) NULL COMMENT '用户UUID' AFTER `user_id`,
ADD COLUMN `product_id_uuid` CHAR(36) NULL COMMENT '产品UUID' AFTER `product_id`,
ADD INDEX `idx_id_uuid` (`id_uuid`),
ADD INDEX `idx_agent_id_uuid` (`agent_id_uuid`),
ADD INDEX `idx_user_id_uuid` (`user_id_uuid`),
ADD INDEX `idx_product_id_uuid` (`product_id_uuid`);

-- agent_order表：添加UUID主键字段和外键字段
ALTER TABLE `agent_order`
ADD COLUMN `id_uuid` CHAR(36) NULL COMMENT 'UUID主键' AFTER `id`,
ADD COLUMN `agent_id_uuid` CHAR(36) NULL COMMENT '代理UUID' AFTER `agent_id`,
ADD COLUMN `order_id_uuid` CHAR(36) NULL COMMENT '订单UUID' AFTER `order_id`,
ADD COLUMN `product_id_uuid` CHAR(36) NULL COMMENT '产品UUID' AFTER `product_id`,
ADD INDEX `idx_id_uuid` (`id_uuid`),
ADD INDEX `idx_agent_id_uuid` (`agent_id_uuid`),
ADD INDEX `idx_order_id_uuid` (`order_id_uuid`),
ADD INDEX `idx_product_id_uuid` (`product_id_uuid`);

-- agent_wallet表：添加UUID主键字段和外键字段
ALTER TABLE `agent_wallet`
ADD COLUMN `id_uuid` CHAR(36) NULL COMMENT 'UUID主键' AFTER `id`,
ADD COLUMN `agent_id_uuid` CHAR(36) NULL COMMENT '代理UUID' AFTER `agent_id`,
ADD INDEX `idx_id_uuid` (`id_uuid`),
ADD INDEX `idx_agent_id_uuid` (`agent_id_uuid`);

-- agent_withdrawal表：添加UUID主键字段和外键字段
ALTER TABLE `agent_withdrawal`
ADD COLUMN `id_uuid` CHAR(36) NULL COMMENT 'UUID主键' AFTER `id`,
ADD COLUMN `agent_id_uuid` CHAR(36) NULL COMMENT '代理UUID' AFTER `agent_id`,
ADD COLUMN `wallet_id_uuid` CHAR(36) NULL COMMENT '钱包UUID' AFTER `wallet_id`,
ADD INDEX `idx_id_uuid` (`id_uuid`),
ADD INDEX `idx_agent_id_uuid` (`agent_id_uuid`),
ADD INDEX `idx_wallet_id_uuid` (`wallet_id_uuid`);

-- agent_withdrawal_tax表：添加UUID主键字段和外键字段
ALTER TABLE `agent_withdrawal_tax`
ADD COLUMN `id_uuid` CHAR(36) NULL COMMENT 'UUID主键' AFTER `id`,
ADD COLUMN `withdrawal_id_uuid` CHAR(36) NULL COMMENT '提现UUID' AFTER `withdrawal_id`,
ADD INDEX `idx_id_uuid` (`id_uuid`),
ADD INDEX `idx_withdrawal_id_uuid` (`withdrawal_id_uuid`);

-- agent_rebate表：添加UUID主键字段和外键字段
ALTER TABLE `agent_rebate`
ADD COLUMN `id_uuid` CHAR(36) NULL COMMENT 'UUID主键' AFTER `id`,
ADD COLUMN `agent_id_uuid` CHAR(36) NULL COMMENT '代理UUID' AFTER `agent_id`,
ADD COLUMN `order_id_uuid` CHAR(36) NULL COMMENT '订单UUID' AFTER `order_id`,
ADD COLUMN `product_id_uuid` CHAR(36) NULL COMMENT '产品UUID' AFTER `product_id`,
ADD INDEX `idx_id_uuid` (`id_uuid`),
ADD INDEX `idx_agent_id_uuid` (`agent_id_uuid`),
ADD INDEX `idx_order_id_uuid` (`order_id_uuid`),
ADD INDEX `idx_product_id_uuid` (`product_id_uuid`);

-- agent_relation表：添加UUID主键字段和外键字段
ALTER TABLE `agent_relation`
ADD COLUMN `id_uuid` CHAR(36) NULL COMMENT 'UUID主键' AFTER `id`,
ADD COLUMN `agent_id_uuid` CHAR(36) NULL COMMENT '代理UUID' AFTER `agent_id`,
ADD COLUMN `parent_agent_id_uuid` CHAR(36) NULL COMMENT '上级代理UUID' AFTER `parent_agent_id`,
ADD INDEX `idx_id_uuid` (`id_uuid`),
ADD INDEX `idx_agent_id_uuid` (`agent_id_uuid`),
ADD INDEX `idx_parent_agent_id_uuid` (`parent_agent_id_uuid`);

-- agent_upgrade表：添加UUID主键字段和外键字段
ALTER TABLE `agent_upgrade`
ADD COLUMN `id_uuid` CHAR(36) NULL COMMENT 'UUID主键' AFTER `id`,
ADD COLUMN `agent_id_uuid` CHAR(36) NULL COMMENT '代理UUID' AFTER `agent_id`,
ADD INDEX `idx_id_uuid` (`id_uuid`),
ADD INDEX `idx_agent_id_uuid` (`agent_id_uuid`);

-- agent_real_name表：添加UUID主键字段和外键字段
ALTER TABLE `agent_real_name`
ADD COLUMN `id_uuid` CHAR(36) NULL COMMENT 'UUID主键' AFTER `id`,
ADD COLUMN `agent_id_uuid` CHAR(36) NULL COMMENT '代理UUID' AFTER `agent_id`,
ADD INDEX `idx_id_uuid` (`id_uuid`),
ADD INDEX `idx_agent_id_uuid` (`agent_id_uuid`);

-- agent_config表：添加UUID主键字段
ALTER TABLE `agent_config`
ADD COLUMN `id_uuid` CHAR(36) NULL COMMENT 'UUID主键' AFTER `id`,
ADD INDEX `idx_id_uuid` (`id_uuid`);

-- agent_product_config表：添加UUID主键字段和外键字段
ALTER TABLE `agent_product_config`
ADD COLUMN `id_uuid` CHAR(36) NULL COMMENT 'UUID主键' AFTER `id`,
ADD COLUMN `product_id_uuid` CHAR(36) NULL COMMENT '产品UUID' AFTER `product_id`,
ADD INDEX `idx_id_uuid` (`id_uuid`),
ADD INDEX `idx_product_id_uuid` (`product_id_uuid`);

-- agent_short_link表：添加UUID主键字段和外键字段
ALTER TABLE `agent_short_link`
ADD COLUMN `id_uuid` CHAR(36) NULL COMMENT 'UUID主键' AFTER `id`,
ADD COLUMN `invite_code_id_uuid` CHAR(36) NULL COMMENT '邀请码UUID' AFTER `invite_code_id`,
ADD COLUMN `link_id_uuid` CHAR(36) NULL COMMENT '链接UUID' AFTER `link_id`,
ADD INDEX `idx_id_uuid` (`id_uuid`),
ADD INDEX `idx_invite_code_id_uuid` (`invite_code_id_uuid`),
ADD INDEX `idx_link_id_uuid` (`link_id_uuid`);

-- agent_invite_code_usage表：添加UUID主键字段和外键字段
ALTER TABLE `agent_invite_code_usage`
ADD COLUMN `id_uuid` CHAR(36) NULL COMMENT 'UUID主键' AFTER `id`,
ADD COLUMN `invite_code_id_uuid` CHAR(36) NULL COMMENT '邀请码UUID' AFTER `invite_code_id`,
ADD COLUMN `user_id_uuid` CHAR(36) NULL COMMENT '用户UUID' AFTER `user_id`,
ADD COLUMN `agent_id_uuid` CHAR(36) NULL COMMENT '代理UUID' AFTER `agent_id`,
ADD INDEX `idx_id_uuid` (`id_uuid`),
ADD INDEX `idx_invite_code_id_uuid` (`invite_code_id_uuid`),
ADD INDEX `idx_user_id_uuid` (`user_id_uuid`),
ADD INDEX `idx_agent_id_uuid` (`agent_id_uuid`);

-- agent_freeze_task表：添加UUID主键字段和外键字段
ALTER TABLE `agent_freeze_task`
ADD COLUMN `id_uuid` CHAR(36) NULL COMMENT 'UUID主键' AFTER `id`,
ADD COLUMN `agent_id_uuid` CHAR(36) NULL COMMENT '代理UUID' AFTER `agent_id`,
ADD COLUMN `order_id_uuid` CHAR(36) NULL COMMENT '订单UUID' AFTER `order_id`,
ADD COLUMN `commission_id_uuid` CHAR(36) NULL COMMENT '佣金UUID' AFTER `commission_id`,
ADD INDEX `idx_id_uuid` (`id_uuid`),
ADD INDEX `idx_agent_id_uuid` (`agent_id_uuid`),
ADD INDEX `idx_order_id_uuid` (`order_id_uuid`),
ADD INDEX `idx_commission_id_uuid` (`commission_id_uuid`);

-- order_refund表：添加UUID主键字段和外键字段
ALTER TABLE `order_refund`
ADD COLUMN `id_uuid` CHAR(36) NULL COMMENT 'UUID主键' AFTER `id`,
ADD COLUMN `order_id_uuid` CHAR(36) NULL COMMENT '订单UUID' AFTER `order_id`,
ADD COLUMN `user_id_uuid` CHAR(36) NULL COMMENT '用户UUID' AFTER `user_id`,
ADD COLUMN `product_id_uuid` CHAR(36) NULL COMMENT '产品UUID' AFTER `product_id`,
ADD INDEX `idx_id_uuid` (`id_uuid`),
ADD INDEX `idx_order_id_uuid` (`order_id_uuid`),
ADD INDEX `idx_user_id_uuid` (`user_id_uuid`),
ADD INDEX `idx_product_id_uuid` (`product_id_uuid`);

-- query_cleanup_log表：添加UUID主键字段
ALTER TABLE `query_cleanup_log`
ADD COLUMN `id_uuid` CHAR(36) NULL COMMENT 'UUID主键' AFTER `id`,
ADD INDEX `idx_id_uuid` (`id_uuid`);

-- query_cleanup_detail表：添加UUID主键字段和外键字段
ALTER TABLE `query_cleanup_detail`
ADD COLUMN `id_uuid` CHAR(36) NULL COMMENT 'UUID主键' AFTER `id`,
ADD COLUMN `cleanup_log_id_uuid` CHAR(36) NULL COMMENT '清理日志UUID' AFTER `cleanup_log_id`,
ADD COLUMN `query_id_uuid` CHAR(36) NULL COMMENT '查询UUID' AFTER `query_id`,
ADD COLUMN `order_id_uuid` CHAR(36) NULL COMMENT '订单UUID' AFTER `order_id`,
ADD COLUMN `user_id_uuid` CHAR(36) NULL COMMENT '用户UUID' AFTER `user_id`,
ADD COLUMN `product_id_uuid` CHAR(36) NULL COMMENT '产品UUID' AFTER `product_id`,
ADD INDEX `idx_id_uuid` (`id_uuid`),
ADD INDEX `idx_cleanup_log_id_uuid` (`cleanup_log_id_uuid`),
ADD INDEX `idx_query_id_uuid` (`query_id_uuid`),
ADD INDEX `idx_order_id_uuid` (`order_id_uuid`),
ADD INDEX `idx_user_id_uuid` (`user_id_uuid`),
ADD INDEX `idx_product_id_uuid` (`product_id_uuid`);

-- query_cleanup_config表：添加UUID主键字段
ALTER TABLE `query_cleanup_config`
ADD COLUMN `id_uuid` CHAR(36) NULL COMMENT 'UUID主键' AFTER `id`,
ADD INDEX `idx_id_uuid` (`id_uuid`);

-- product_feature表：添加UUID主键字段和外键字段
ALTER TABLE `product_feature`
ADD COLUMN `id_uuid` CHAR(36) NULL COMMENT 'UUID主键' AFTER `id`,
ADD COLUMN `product_id_uuid` CHAR(36) NULL COMMENT '产品UUID' AFTER `product_id`,
ADD COLUMN `feature_id_uuid` CHAR(36) NULL COMMENT '功能UUID' AFTER `feature_id`,
ADD INDEX `idx_id_uuid` (`id_uuid`),
ADD INDEX `idx_product_id_uuid` (`product_id_uuid`),
ADD INDEX `idx_feature_id_uuid` (`feature_id_uuid`);

-- feature表：添加UUID主键字段
ALTER TABLE `feature`
ADD COLUMN `id_uuid` CHAR(36) NULL COMMENT 'UUID主键' AFTER `id`,
ADD INDEX `idx_id_uuid` (`id_uuid`);

-- authorization_document表：添加UUID主键字段和外键字段
ALTER TABLE `authorization_document`
ADD COLUMN `id_uuid` CHAR(36) NULL COMMENT 'UUID主键' AFTER `id`,
ADD COLUMN `user_id_uuid` CHAR(36) NULL COMMENT '用户UUID' AFTER `user_id`,
ADD COLUMN `order_id_uuid` CHAR(36) NULL COMMENT '订单UUID' AFTER `order_id`,
ADD INDEX `idx_id_uuid` (`id_uuid`),
ADD INDEX `idx_user_id_uuid` (`user_id_uuid`),
ADD INDEX `idx_order_id_uuid` (`order_id_uuid`);

-- global_notifications表：添加UUID主键字段
ALTER TABLE `global_notifications`
ADD COLUMN `id_uuid` CHAR(36) NULL COMMENT 'UUID主键' AFTER `id`,
ADD INDEX `idx_id_uuid` (`id_uuid`);

-- admin_user表：添加UUID主键字段
ALTER TABLE `admin_user`
ADD COLUMN `id_uuid` CHAR(36) NULL COMMENT 'UUID主键' AFTER `id`,
ADD INDEX `idx_id_uuid` (`id_uuid`);

-- admin_role表：添加UUID主键字段
ALTER TABLE `admin_role`
ADD COLUMN `id_uuid` CHAR(36) NULL COMMENT 'UUID主键' AFTER `id`,
ADD INDEX `idx_id_uuid` (`id_uuid`);

-- admin_menu表：添加UUID主键字段和外键字段
ALTER TABLE `admin_menu`
ADD COLUMN `id_uuid` CHAR(36) NULL COMMENT 'UUID主键' AFTER `id`,
ADD COLUMN `parent_id_uuid` CHAR(36) NULL COMMENT '父菜单UUID' AFTER `parent_id`,
ADD INDEX `idx_id_uuid` (`id_uuid`),
ADD INDEX `idx_parent_id_uuid` (`parent_id_uuid`);

-- admin_api表：添加UUID主键字段
ALTER TABLE `admin_api`
ADD COLUMN `id_uuid` CHAR(36) NULL COMMENT 'UUID主键' AFTER `id`,
ADD INDEX `idx_id_uuid` (`id_uuid`);

-- admin_dict_type表：添加UUID主键字段
ALTER TABLE `admin_dict_type`
ADD COLUMN `id_uuid` CHAR(36) NULL COMMENT 'UUID主键' AFTER `id`,
ADD INDEX `idx_id_uuid` (`id_uuid`);

-- admin_dict_data表：添加UUID主键字段和外键字段
ALTER TABLE `admin_dict_data`
ADD COLUMN `id_uuid` CHAR(36) NULL COMMENT 'UUID主键' AFTER `id`,
ADD COLUMN `dict_type_id_uuid` CHAR(36) NULL COMMENT '字典类型UUID' AFTER `dict_type_id`,
ADD INDEX `idx_id_uuid` (`id_uuid`),
ADD INDEX `idx_dict_type_id_uuid` (`dict_type_id_uuid`);

-- admin_user_role表：添加UUID主键字段和外键字段
ALTER TABLE `admin_user_role`
ADD COLUMN `id_uuid` CHAR(36) NULL COMMENT 'UUID主键' AFTER `id`,
ADD COLUMN `user_id_uuid` CHAR(36) NULL COMMENT '用户UUID' AFTER `user_id`,
ADD COLUMN `role_id_uuid` CHAR(36) NULL COMMENT '角色UUID' AFTER `role_id`,
ADD INDEX `idx_id_uuid` (`id_uuid`),
ADD INDEX `idx_user_id_uuid` (`user_id_uuid`),
ADD INDEX `idx_role_id_uuid` (`role_id_uuid`);

-- admin_role_menu表：添加UUID主键字段和外键字段
ALTER TABLE `admin_role_menu`
ADD COLUMN `id_uuid` CHAR(36) NULL COMMENT 'UUID主键' AFTER `id`,
ADD COLUMN `role_id_uuid` CHAR(36) NULL COMMENT '角色UUID' AFTER `role_id`,
ADD COLUMN `menu_id_uuid` CHAR(36) NULL COMMENT '菜单UUID' AFTER `menu_id`,
ADD INDEX `idx_id_uuid` (`id_uuid`),
ADD INDEX `idx_role_id_uuid` (`role_id_uuid`),
ADD INDEX `idx_menu_id_uuid` (`menu_id_uuid`);

-- admin_role_api表：添加UUID主键字段和外键字段
ALTER TABLE `admin_role_api`
ADD COLUMN `id_uuid` CHAR(36) NULL COMMENT 'UUID主键' AFTER `id`,
ADD COLUMN `role_id_uuid` CHAR(36) NULL COMMENT '角色UUID' AFTER `role_id`,
ADD COLUMN `api_id_uuid` CHAR(36) NULL COMMENT 'API UUID' AFTER `api_id`,
ADD INDEX `idx_id_uuid` (`id_uuid`),
ADD INDEX `idx_role_id_uuid` (`role_id_uuid`),
ADD INDEX `idx_api_id_uuid` (`api_id_uuid`);

-- ============================================
-- 第四阶段：填充UUID字段数据（根据映射表）
-- ============================================

-- user表：填充UUID
UPDATE `user` u
INNER JOIN `uuid_mapping` m ON m.table_name = 'user'
AND m.old_id = u.id
SET
    u.id_uuid = m.new_uuid
WHERE
    u.del_state = 0;

-- agent表：填充UUID
UPDATE `agent` a
INNER JOIN `uuid_mapping` m ON m.table_name = 'agent'
AND m.old_id = a.id
SET
    a.id_uuid = m.new_uuid
WHERE
    a.del_state = 0;

UPDATE `agent` a
INNER JOIN `uuid_mapping` m ON m.table_name = 'user'
AND m.old_id = a.user_id
SET
    a.user_id_uuid = m.new_uuid
WHERE
    a.del_state = 0
    AND a.user_id IS NOT NULL;

UPDATE `agent` a
INNER JOIN `uuid_mapping` m ON m.table_name = 'agent'
AND m.old_id = a.team_leader_id
SET
    a.team_leader_id_uuid = m.new_uuid
WHERE
    a.del_state = 0
    AND a.team_leader_id IS NOT NULL;

-- product表：填充UUID
UPDATE `product` p
INNER JOIN `uuid_mapping` m ON m.table_name = 'product'
AND m.old_id = p.id
SET
    p.id_uuid = m.new_uuid
WHERE
    p.del_state = 0;

-- order表：填充UUID
UPDATE `order` o
INNER JOIN `uuid_mapping` m ON m.table_name = 'order'
AND m.old_id = o.id
SET
    o.id_uuid = m.new_uuid
WHERE
    o.del_state = 0;

UPDATE `order` o
INNER JOIN `uuid_mapping` m ON m.table_name = 'user'
AND m.old_id = o.user_id
SET
    o.user_id_uuid = m.new_uuid
WHERE
    o.del_state = 0
    AND o.user_id IS NOT NULL;

UPDATE `order` o
INNER JOIN `uuid_mapping` m ON m.table_name = 'product'
AND m.old_id = o.product_id
SET
    o.product_id_uuid = m.new_uuid
WHERE
    o.del_state = 0
    AND o.product_id IS NOT NULL;

-- query表：填充UUID
UPDATE `query` q
INNER JOIN `uuid_mapping` m ON m.table_name = 'query'
AND m.old_id = q.id
SET
    q.id_uuid = m.new_uuid
WHERE
    q.del_state = 0;

UPDATE `query` q
INNER JOIN `uuid_mapping` m ON m.table_name = 'order'
AND m.old_id = q.order_id
SET
    q.order_id_uuid = m.new_uuid
WHERE
    q.del_state = 0
    AND q.order_id IS NOT NULL;

UPDATE `query` q
INNER JOIN `uuid_mapping` m ON m.table_name = 'user'
AND m.old_id = q.user_id
SET
    q.user_id_uuid = m.new_uuid
WHERE
    q.del_state = 0
    AND q.user_id IS NOT NULL;

UPDATE `query` q
INNER JOIN `uuid_mapping` m ON m.table_name = 'product'
AND m.old_id = q.product_id
SET
    q.product_id_uuid = m.new_uuid
WHERE
    q.del_state = 0
    AND q.product_id IS NOT NULL;

-- user_auth表：填充UUID
UPDATE `user_auth` ua
INNER JOIN `uuid_mapping` m ON m.table_name = 'user_auth'
AND m.old_id = ua.id
SET
    ua.id_uuid = m.new_uuid
WHERE
    ua.del_state = 0;

UPDATE `user_auth` ua
INNER JOIN `uuid_mapping` m ON m.table_name = 'user'
AND m.old_id = ua.user_id
SET
    ua.user_id_uuid = m.new_uuid
WHERE
    ua.del_state = 0
    AND ua.user_id IS NOT NULL;

-- agent_commission表：填充UUID
UPDATE `agent_commission` ac
INNER JOIN `uuid_mapping` m ON m.table_name = 'agent_commission'
AND m.old_id = ac.id
SET
    ac.id_uuid = m.new_uuid
WHERE
    ac.del_state = 0;

UPDATE `agent_commission` ac
INNER JOIN `uuid_mapping` m ON m.table_name = 'agent'
AND m.old_id = ac.agent_id
SET
    ac.agent_id_uuid = m.new_uuid
WHERE
    ac.del_state = 0
    AND ac.agent_id IS NOT NULL;

UPDATE `agent_commission` ac
INNER JOIN `uuid_mapping` m ON m.table_name = 'order'
AND m.old_id = ac.order_id
SET
    ac.order_id_uuid = m.new_uuid
WHERE
    ac.del_state = 0
    AND ac.order_id IS NOT NULL;

UPDATE `agent_commission` ac
INNER JOIN `uuid_mapping` m ON m.table_name = 'product'
AND m.old_id = ac.product_id
SET
    ac.product_id_uuid = m.new_uuid
WHERE
    ac.del_state = 0
    AND ac.product_id IS NOT NULL;

-- agent_invite_code表：填充UUID
UPDATE `agent_invite_code` aic
INNER JOIN `uuid_mapping` m ON m.table_name = 'agent_invite_code'
AND m.old_id = aic.id
SET
    aic.id_uuid = m.new_uuid
WHERE
    aic.del_state = 0;

UPDATE `agent_invite_code` aic
INNER JOIN `uuid_mapping` m ON m.table_name = 'agent'
AND m.old_id = aic.agent_id
SET
    aic.agent_id_uuid = m.new_uuid
WHERE
    aic.del_state = 0
    AND aic.agent_id IS NOT NULL;

UPDATE `agent_invite_code` aic
INNER JOIN `uuid_mapping` m ON m.table_name = 'user'
AND m.old_id = aic.used_user_id
SET
    aic.used_user_id_uuid = m.new_uuid
WHERE
    aic.del_state = 0
    AND aic.used_user_id IS NOT NULL;

UPDATE `agent_invite_code` aic
INNER JOIN `uuid_mapping` m ON m.table_name = 'agent'
AND m.old_id = aic.used_agent_id
SET
    aic.used_agent_id_uuid = m.new_uuid
WHERE
    aic.del_state = 0
    AND aic.used_agent_id IS NOT NULL;

-- agent_link表：填充UUID
UPDATE `agent_link` al
INNER JOIN `uuid_mapping` m ON m.table_name = 'agent_link'
AND m.old_id = al.id
SET
    al.id_uuid = m.new_uuid
WHERE
    al.del_state = 0;

UPDATE `agent_link` al
INNER JOIN `uuid_mapping` m ON m.table_name = 'agent'
AND m.old_id = al.agent_id
SET
    al.agent_id_uuid = m.new_uuid
WHERE
    al.del_state = 0
    AND al.agent_id IS NOT NULL;

UPDATE `agent_link` al
INNER JOIN `uuid_mapping` m ON m.table_name = 'user'
AND m.old_id = al.user_id
SET
    al.user_id_uuid = m.new_uuid
WHERE
    al.del_state = 0
    AND al.user_id IS NOT NULL;

UPDATE `agent_link` al
INNER JOIN `uuid_mapping` m ON m.table_name = 'product'
AND m.old_id = al.product_id
SET
    al.product_id_uuid = m.new_uuid
WHERE
    al.del_state = 0
    AND al.product_id IS NOT NULL;

-- agent_order表：填充UUID
UPDATE `agent_order` ao
INNER JOIN `uuid_mapping` m ON m.table_name = 'agent_order'
AND m.old_id = ao.id
SET
    ao.id_uuid = m.new_uuid
WHERE
    ao.del_state = 0;

UPDATE `agent_order` ao
INNER JOIN `uuid_mapping` m ON m.table_name = 'agent'
AND m.old_id = ao.agent_id
SET
    ao.agent_id_uuid = m.new_uuid
WHERE
    ao.del_state = 0
    AND ao.agent_id IS NOT NULL;

UPDATE `agent_order` ao
INNER JOIN `uuid_mapping` m ON m.table_name = 'order'
AND m.old_id = ao.order_id
SET
    ao.order_id_uuid = m.new_uuid
WHERE
    ao.del_state = 0
    AND ao.order_id IS NOT NULL;

UPDATE `agent_order` ao
INNER JOIN `uuid_mapping` m ON m.table_name = 'product'
AND m.old_id = ao.product_id
SET
    ao.product_id_uuid = m.new_uuid
WHERE
    ao.del_state = 0
    AND ao.product_id IS NOT NULL;

-- agent_wallet表：填充UUID
UPDATE `agent_wallet` aw
INNER JOIN `uuid_mapping` m ON m.table_name = 'agent_wallet'
AND m.old_id = aw.id
SET
    aw.id_uuid = m.new_uuid
WHERE
    aw.del_state = 0;

UPDATE `agent_wallet` aw
INNER JOIN `uuid_mapping` m ON m.table_name = 'agent'
AND m.old_id = aw.agent_id
SET
    aw.agent_id_uuid = m.new_uuid
WHERE
    aw.del_state = 0
    AND aw.agent_id IS NOT NULL;

-- agent_withdrawal表：填充UUID
UPDATE `agent_withdrawal` aw
INNER JOIN `uuid_mapping` m ON m.table_name = 'agent_withdrawal'
AND m.old_id = aw.id
SET
    aw.id_uuid = m.new_uuid
WHERE
    aw.del_state = 0;

UPDATE `agent_withdrawal` aw
INNER JOIN `uuid_mapping` m ON m.table_name = 'agent'
AND m.old_id = aw.agent_id
SET
    aw.agent_id_uuid = m.new_uuid
WHERE
    aw.del_state = 0
    AND aw.agent_id IS NOT NULL;

UPDATE `agent_withdrawal` aw
INNER JOIN `uuid_mapping` m ON m.table_name = 'agent_wallet'
AND m.old_id = aw.wallet_id
SET
    aw.wallet_id_uuid = m.new_uuid
WHERE
    aw.del_state = 0
    AND aw.wallet_id IS NOT NULL;

-- agent_withdrawal_tax表：填充UUID
UPDATE `agent_withdrawal_tax` awt
INNER JOIN `uuid_mapping` m ON m.table_name = 'agent_withdrawal_tax'
AND m.old_id = awt.id
SET
    awt.id_uuid = m.new_uuid
WHERE
    awt.del_state = 0;

UPDATE `agent_withdrawal_tax` awt
INNER JOIN `uuid_mapping` m ON m.table_name = 'agent_withdrawal'
AND m.old_id = awt.withdrawal_id
SET
    awt.withdrawal_id_uuid = m.new_uuid
WHERE
    awt.del_state = 0
    AND awt.withdrawal_id IS NOT NULL;

-- agent_rebate表：填充UUID
UPDATE `agent_rebate` ar
INNER JOIN `uuid_mapping` m ON m.table_name = 'agent_rebate'
AND m.old_id = ar.id
SET
    ar.id_uuid = m.new_uuid
WHERE
    ar.del_state = 0;

UPDATE `agent_rebate` ar
INNER JOIN `uuid_mapping` m ON m.table_name = 'agent'
AND m.old_id = ar.agent_id
SET
    ar.agent_id_uuid = m.new_uuid
WHERE
    ar.del_state = 0
    AND ar.agent_id IS NOT NULL;

UPDATE `agent_rebate` ar
INNER JOIN `uuid_mapping` m ON m.table_name = 'order'
AND m.old_id = ar.order_id
SET
    ar.order_id_uuid = m.new_uuid
WHERE
    ar.del_state = 0
    AND ar.order_id IS NOT NULL;

UPDATE `agent_rebate` ar
INNER JOIN `uuid_mapping` m ON m.table_name = 'product'
AND m.old_id = ar.product_id
SET
    ar.product_id_uuid = m.new_uuid
WHERE
    ar.del_state = 0
    AND ar.product_id IS NOT NULL;

-- agent_relation表：填充UUID
UPDATE `agent_relation` ar
INNER JOIN `uuid_mapping` m ON m.table_name = 'agent_relation'
AND m.old_id = ar.id
SET
    ar.id_uuid = m.new_uuid
WHERE
    ar.del_state = 0;

UPDATE `agent_relation` ar
INNER JOIN `uuid_mapping` m ON m.table_name = 'agent'
AND m.old_id = ar.agent_id
SET
    ar.agent_id_uuid = m.new_uuid
WHERE
    ar.del_state = 0
    AND ar.agent_id IS NOT NULL;

UPDATE `agent_relation` ar
INNER JOIN `uuid_mapping` m ON m.table_name = 'agent'
AND m.old_id = ar.parent_agent_id
SET
    ar.parent_agent_id_uuid = m.new_uuid
WHERE
    ar.del_state = 0
    AND ar.parent_agent_id IS NOT NULL;

-- agent_upgrade表：填充UUID
UPDATE `agent_upgrade` au
INNER JOIN `uuid_mapping` m ON m.table_name = 'agent_upgrade'
AND m.old_id = au.id
SET
    au.id_uuid = m.new_uuid
WHERE
    au.del_state = 0;

UPDATE `agent_upgrade` au
INNER JOIN `uuid_mapping` m ON m.table_name = 'agent'
AND m.old_id = au.agent_id
SET
    au.agent_id_uuid = m.new_uuid
WHERE
    au.del_state = 0
    AND au.agent_id IS NOT NULL;

-- agent_real_name表：填充UUID
UPDATE `agent_real_name` arn
INNER JOIN `uuid_mapping` m ON m.table_name = 'agent_real_name'
AND m.old_id = arn.id
SET
    arn.id_uuid = m.new_uuid
WHERE
    arn.del_state = 0;

UPDATE `agent_real_name` arn
INNER JOIN `uuid_mapping` m ON m.table_name = 'agent'
AND m.old_id = arn.agent_id
SET
    arn.agent_id_uuid = m.new_uuid
WHERE
    arn.del_state = 0
    AND arn.agent_id IS NOT NULL;

-- agent_config表：填充UUID
UPDATE `agent_config` ac
INNER JOIN `uuid_mapping` m ON m.table_name = 'agent_config'
AND m.old_id = ac.id
SET
    ac.id_uuid = m.new_uuid
WHERE
    ac.del_state = 0;

-- agent_product_config表：填充UUID
UPDATE `agent_product_config` apc
INNER JOIN `uuid_mapping` m ON m.table_name = 'agent_product_config'
AND m.old_id = apc.id
SET
    apc.id_uuid = m.new_uuid
WHERE
    apc.del_state = 0;

UPDATE `agent_product_config` apc
INNER JOIN `uuid_mapping` m ON m.table_name = 'product'
AND m.old_id = apc.product_id
SET
    apc.product_id_uuid = m.new_uuid
WHERE
    apc.del_state = 0
    AND apc.product_id IS NOT NULL;

-- agent_short_link表：填充UUID
UPDATE `agent_short_link` asl
INNER JOIN `uuid_mapping` m ON m.table_name = 'agent_short_link'
AND m.old_id = asl.id
SET
    asl.id_uuid = m.new_uuid
WHERE
    asl.del_state = 0;

UPDATE `agent_short_link` asl
INNER JOIN `uuid_mapping` m ON m.table_name = 'agent_invite_code'
AND m.old_id = asl.invite_code_id
SET
    asl.invite_code_id_uuid = m.new_uuid
WHERE
    asl.del_state = 0
    AND asl.invite_code_id IS NOT NULL;

UPDATE `agent_short_link` asl
INNER JOIN `uuid_mapping` m ON m.table_name = 'agent_link'
AND m.old_id = asl.link_id
SET
    asl.link_id_uuid = m.new_uuid
WHERE
    asl.del_state = 0
    AND asl.link_id IS NOT NULL;

-- agent_invite_code_usage表：填充UUID
UPDATE `agent_invite_code_usage` aicu
INNER JOIN `uuid_mapping` m ON m.table_name = 'agent_invite_code_usage'
AND m.old_id = aicu.id
SET
    aicu.id_uuid = m.new_uuid
WHERE
    aicu.del_state = 0;

UPDATE `agent_invite_code_usage` aicu
INNER JOIN `uuid_mapping` m ON m.table_name = 'agent_invite_code'
AND m.old_id = aicu.invite_code_id
SET
    aicu.invite_code_id_uuid = m.new_uuid
WHERE
    aicu.del_state = 0
    AND aicu.invite_code_id IS NOT NULL;

UPDATE `agent_invite_code_usage` aicu
INNER JOIN `uuid_mapping` m ON m.table_name = 'user'
AND m.old_id = aicu.user_id
SET
    aicu.user_id_uuid = m.new_uuid
WHERE
    aicu.del_state = 0
    AND aicu.user_id IS NOT NULL;

UPDATE `agent_invite_code_usage` aicu
INNER JOIN `uuid_mapping` m ON m.table_name = 'agent'
AND m.old_id = aicu.agent_id
SET
    aicu.agent_id_uuid = m.new_uuid
WHERE
    aicu.del_state = 0
    AND aicu.agent_id IS NOT NULL;

-- agent_freeze_task表：填充UUID
UPDATE `agent_freeze_task` aft
INNER JOIN `uuid_mapping` m ON m.table_name = 'agent_freeze_task'
AND m.old_id = aft.id
SET
    aft.id_uuid = m.new_uuid
WHERE
    aft.del_state = 0;

UPDATE `agent_freeze_task` aft
INNER JOIN `uuid_mapping` m ON m.table_name = 'agent'
AND m.old_id = aft.agent_id
SET
    aft.agent_id_uuid = m.new_uuid
WHERE
    aft.del_state = 0
    AND aft.agent_id IS NOT NULL;

UPDATE `agent_freeze_task` aft
INNER JOIN `uuid_mapping` m ON m.table_name = 'order'
AND m.old_id = aft.order_id
SET
    aft.order_id_uuid = m.new_uuid
WHERE
    aft.del_state = 0
    AND aft.order_id IS NOT NULL;

UPDATE `agent_freeze_task` aft
INNER JOIN `uuid_mapping` m ON m.table_name = 'agent_commission'
AND m.old_id = aft.commission_id
SET
    aft.commission_id_uuid = m.new_uuid
WHERE
    aft.del_state = 0
    AND aft.commission_id IS NOT NULL;

-- order_refund表：填充UUID
UPDATE `order_refund` orf
INNER JOIN `uuid_mapping` m ON m.table_name = 'order_refund'
AND m.old_id = orf.id
SET
    orf.id_uuid = m.new_uuid
WHERE
    orf.del_state = 0;

UPDATE `order_refund` orf
INNER JOIN `uuid_mapping` m ON m.table_name = 'order'
AND m.old_id = orf.order_id
SET
    orf.order_id_uuid = m.new_uuid
WHERE
    orf.del_state = 0
    AND orf.order_id IS NOT NULL;

UPDATE `order_refund` orf
INNER JOIN `uuid_mapping` m ON m.table_name = 'user'
AND m.old_id = orf.user_id
SET
    orf.user_id_uuid = m.new_uuid
WHERE
    orf.del_state = 0
    AND orf.user_id IS NOT NULL;

UPDATE `order_refund` orf
INNER JOIN `uuid_mapping` m ON m.table_name = 'product'
AND m.old_id = orf.product_id
SET
    orf.product_id_uuid = m.new_uuid
WHERE
    orf.del_state = 0
    AND orf.product_id IS NOT NULL;

-- query_cleanup_log表：填充UUID
UPDATE `query_cleanup_log` qcl
INNER JOIN `uuid_mapping` m ON m.table_name = 'query_cleanup_log'
AND m.old_id = qcl.id
SET
    qcl.id_uuid = m.new_uuid
WHERE
    qcl.del_state = 0;

-- query_cleanup_detail表：填充UUID
UPDATE `query_cleanup_detail` qcd
INNER JOIN `uuid_mapping` m ON m.table_name = 'query_cleanup_detail'
AND m.old_id = qcd.id
SET
    qcd.id_uuid = m.new_uuid
WHERE
    qcd.del_state = 0;

UPDATE `query_cleanup_detail` qcd
INNER JOIN `uuid_mapping` m ON m.table_name = 'query_cleanup_log'
AND m.old_id = qcd.cleanup_log_id
SET
    qcd.cleanup_log_id_uuid = m.new_uuid
WHERE
    qcd.del_state = 0
    AND qcd.cleanup_log_id IS NOT NULL;

UPDATE `query_cleanup_detail` qcd
INNER JOIN `uuid_mapping` m ON m.table_name = 'query'
AND m.old_id = qcd.query_id
SET
    qcd.query_id_uuid = m.new_uuid
WHERE
    qcd.del_state = 0
    AND qcd.query_id IS NOT NULL;

UPDATE `query_cleanup_detail` qcd
INNER JOIN `uuid_mapping` m ON m.table_name = 'order'
AND m.old_id = qcd.order_id
SET
    qcd.order_id_uuid = m.new_uuid
WHERE
    qcd.del_state = 0
    AND qcd.order_id IS NOT NULL;

UPDATE `query_cleanup_detail` qcd
INNER JOIN `uuid_mapping` m ON m.table_name = 'user'
AND m.old_id = qcd.user_id
SET
    qcd.user_id_uuid = m.new_uuid
WHERE
    qcd.del_state = 0
    AND qcd.user_id IS NOT NULL;

UPDATE `query_cleanup_detail` qcd
INNER JOIN `uuid_mapping` m ON m.table_name = 'product'
AND m.old_id = qcd.product_id
SET
    qcd.product_id_uuid = m.new_uuid
WHERE
    qcd.del_state = 0
    AND qcd.product_id IS NOT NULL;

-- query_cleanup_config表：填充UUID
UPDATE `query_cleanup_config` qcc
INNER JOIN `uuid_mapping` m ON m.table_name = 'query_cleanup_config'
AND m.old_id = qcc.id
SET
    qcc.id_uuid = m.new_uuid
WHERE
    qcc.del_state = 0;

-- product_feature表：填充UUID
UPDATE `product_feature` pf
INNER JOIN `uuid_mapping` m ON m.table_name = 'product_feature'
AND m.old_id = pf.id
SET
    pf.id_uuid = m.new_uuid
WHERE
    pf.del_state = 0;

UPDATE `product_feature` pf
INNER JOIN `uuid_mapping` m ON m.table_name = 'product'
AND m.old_id = pf.product_id
SET
    pf.product_id_uuid = m.new_uuid
WHERE
    pf.del_state = 0
    AND pf.product_id IS NOT NULL;

UPDATE `product_feature` pf
INNER JOIN `uuid_mapping` m ON m.table_name = 'feature'
AND m.old_id = pf.feature_id
SET
    pf.feature_id_uuid = m.new_uuid
WHERE
    pf.del_state = 0
    AND pf.feature_id IS NOT NULL;

-- feature表：填充UUID
UPDATE `feature` f
INNER JOIN `uuid_mapping` m ON m.table_name = 'feature'
AND m.old_id = f.id
SET
    f.id_uuid = m.new_uuid
WHERE
    f.del_state = 0;

-- authorization_document表：填充UUID
UPDATE `authorization_document` ad
INNER JOIN `uuid_mapping` m ON m.table_name = 'authorization_document'
AND m.old_id = ad.id
SET
    ad.id_uuid = m.new_uuid
WHERE
    ad.del_state = 0;

UPDATE `authorization_document` ad
INNER JOIN `uuid_mapping` m ON m.table_name = 'user'
AND m.old_id = ad.user_id
SET
    ad.user_id_uuid = m.new_uuid
WHERE
    ad.del_state = 0
    AND ad.user_id IS NOT NULL;

UPDATE `authorization_document` ad
INNER JOIN `uuid_mapping` m ON m.table_name = 'order'
AND m.old_id = ad.order_id
SET
    ad.order_id_uuid = m.new_uuid
WHERE
    ad.del_state = 0
    AND ad.order_id IS NOT NULL;

-- global_notifications表：填充UUID
UPDATE `global_notifications` gn
INNER JOIN `uuid_mapping` m ON m.table_name = 'global_notifications'
AND m.old_id = gn.id
SET
    gn.id_uuid = m.new_uuid
WHERE
    gn.del_state = 0;

-- admin_user表：填充UUID
UPDATE `admin_user` au
INNER JOIN `uuid_mapping` m ON m.table_name = 'admin_user'
AND m.old_id = au.id
SET
    au.id_uuid = m.new_uuid
WHERE
    au.del_state = 0;

-- admin_role表：填充UUID
UPDATE `admin_role` ar
INNER JOIN `uuid_mapping` m ON m.table_name = 'admin_role'
AND m.old_id = ar.id
SET
    ar.id_uuid = m.new_uuid
WHERE
    ar.del_state = 0;

-- admin_menu表：填充UUID
UPDATE `admin_menu` am
INNER JOIN `uuid_mapping` m ON m.table_name = 'admin_menu'
AND m.old_id = am.id
SET
    am.id_uuid = m.new_uuid
WHERE
    am.del_state = 0;

UPDATE `admin_menu` am
INNER JOIN `uuid_mapping` m ON m.table_name = 'admin_menu'
AND m.old_id = am.parent_id
SET
    am.parent_id_uuid = m.new_uuid
WHERE
    am.del_state = 0
    AND am.parent_id IS NOT NULL;

-- admin_api表：填充UUID
UPDATE `admin_api` aa
INNER JOIN `uuid_mapping` m ON m.table_name = 'admin_api'
AND m.old_id = aa.id
SET
    aa.id_uuid = m.new_uuid
WHERE
    aa.del_state = 0;

-- admin_dict_type表：填充UUID
UPDATE `admin_dict_type` adt
INNER JOIN `uuid_mapping` m ON m.table_name = 'admin_dict_type'
AND m.old_id = adt.id
SET
    adt.id_uuid = m.new_uuid
WHERE
    adt.del_state = 0;

-- admin_dict_data表：填充UUID
UPDATE `admin_dict_data` add
INNER JOIN `uuid_mapping` m ON m.table_name = 'admin_dict_data' AND m.old_id = add.id
SET add.id_uuid = m.new_uuid
WHERE add.del_state = 0;

UPDATE `admin_dict_data` add
INNER JOIN `uuid_mapping` m ON m.table_name = 'admin_dict_type' AND m.old_id = add.dict_type_id
SET add.dict_type_id_uuid = m.new_uuid
WHERE add.del_state = 0 AND add.dict_type_id IS NOT NULL;

-- admin_user_role表：填充UUID
UPDATE `admin_user_role` aur
INNER JOIN `uuid_mapping` m ON m.table_name = 'admin_user_role'
AND m.old_id = aur.id
SET
    aur.id_uuid = m.new_uuid
WHERE
    aur.del_state = 0;

UPDATE `admin_user_role` aur
INNER JOIN `uuid_mapping` m ON m.table_name = 'admin_user'
AND m.old_id = aur.user_id
SET
    aur.user_id_uuid = m.new_uuid
WHERE
    aur.del_state = 0
    AND aur.user_id IS NOT NULL;

UPDATE `admin_user_role` aur
INNER JOIN `uuid_mapping` m ON m.table_name = 'admin_role'
AND m.old_id = aur.role_id
SET
    aur.role_id_uuid = m.new_uuid
WHERE
    aur.del_state = 0
    AND aur.role_id IS NOT NULL;

-- admin_role_menu表：填充UUID
UPDATE `admin_role_menu` arm
INNER JOIN `uuid_mapping` m ON m.table_name = 'admin_role_menu'
AND m.old_id = arm.id
SET
    arm.id_uuid = m.new_uuid
WHERE
    arm.del_state = 0;

UPDATE `admin_role_menu` arm
INNER JOIN `uuid_mapping` m ON m.table_name = 'admin_role'
AND m.old_id = arm.role_id
SET
    arm.role_id_uuid = m.new_uuid
WHERE
    arm.del_state = 0
    AND arm.role_id IS NOT NULL;

UPDATE `admin_role_menu` arm
INNER JOIN `uuid_mapping` m ON m.table_name = 'admin_menu'
AND m.old_id = arm.menu_id
SET
    arm.menu_id_uuid = m.new_uuid
WHERE
    arm.del_state = 0
    AND arm.menu_id IS NOT NULL;

-- admin_role_api表：填充UUID
UPDATE `admin_role_api` ara
INNER JOIN `uuid_mapping` m ON m.table_name = 'admin_role_api'
AND m.old_id = ara.id
SET
    ara.id_uuid = m.new_uuid
WHERE
    ara.del_state = 0;

UPDATE `admin_role_api` ara
INNER JOIN `uuid_mapping` m ON m.table_name = 'admin_role'
AND m.old_id = ara.role_id
SET
    ara.role_id_uuid = m.new_uuid
WHERE
    ara.del_state = 0
    AND ara.role_id IS NOT NULL;

UPDATE `admin_role_api` ara
INNER JOIN `uuid_mapping` m ON m.table_name = 'admin_api'
AND m.old_id = ara.api_id
SET
    ara.api_id_uuid = m.new_uuid
WHERE
    ara.del_state = 0
    AND ara.api_id IS NOT NULL;

-- ============================================
-- 第五阶段：验证数据完整性
-- ============================================

-- 检查是否有NULL的UUID字段（不应该有）
SELECT 'user' as table_name, COUNT(*) as null_count
FROM `user`
WHERE
    `id_uuid` IS NULL
    AND `del_state` = 0
UNION ALL
SELECT 'agent', COUNT(*)
FROM `agent`
WHERE
    `id_uuid` IS NULL
    AND `del_state` = 0
UNION ALL
SELECT 'order', COUNT(*)
FROM `order`
WHERE
    `id_uuid` IS NULL
    AND `del_state` = 0
UNION ALL
SELECT 'query', COUNT(*)
FROM `query`
WHERE
    `id_uuid` IS NULL
    AND `del_state` = 0;

-- ============================================
-- 第六阶段：切换主键（需要停止服务）
-- ============================================

-- 注意：此阶段需要停止服务，因为会修改主键和索引
-- 建议在维护窗口期执行

-- 删除旧主键，将UUID字段设为主键
-- 注意：需要先删除所有外键约束（如果有的话）

-- user表：切换主键
ALTER TABLE `user`
DROP PRIMARY KEY,
MODIFY COLUMN `id` BIGINT NULL COMMENT '旧ID（保留用于回滚）',
MODIFY COLUMN `id_uuid` CHAR(36) NOT NULL COMMENT 'UUID主键',
ADD PRIMARY KEY (`id_uuid`);

-- agent表：切换主键
ALTER TABLE `agent`
DROP PRIMARY KEY,
MODIFY COLUMN `id` BIGINT NULL COMMENT '旧ID（保留用于回滚）',
MODIFY COLUMN `id_uuid` CHAR(36) NOT NULL COMMENT 'UUID主键',
ADD PRIMARY KEY (`id_uuid`);

-- product表：切换主键
ALTER TABLE `product`
DROP PRIMARY KEY,
MODIFY COLUMN `id` BIGINT NULL COMMENT '旧ID（保留用于回滚）',
MODIFY COLUMN `id_uuid` CHAR(36) NOT NULL COMMENT 'UUID主键',
ADD PRIMARY KEY (`id_uuid`);

-- order表：切换主键
ALTER TABLE `order`
DROP PRIMARY KEY,
MODIFY COLUMN `id` BIGINT NULL COMMENT '旧ID（保留用于回滚）',
MODIFY COLUMN `id_uuid` CHAR(36) NOT NULL COMMENT 'UUID主键',
ADD PRIMARY KEY (`id_uuid`);

-- query表：切换主键
ALTER TABLE `query`
DROP PRIMARY KEY,
MODIFY COLUMN `id` BIGINT NULL COMMENT '旧ID（保留用于回滚）',
MODIFY COLUMN `id_uuid` CHAR(36) NOT NULL COMMENT 'UUID主键',
ADD PRIMARY KEY (`id_uuid`);

-- user_auth表：切换主键
ALTER TABLE `user_auth`
DROP PRIMARY KEY,
MODIFY COLUMN `id` BIGINT NULL COMMENT '旧ID（保留用于回滚）',
MODIFY COLUMN `id_uuid` CHAR(36) NOT NULL COMMENT 'UUID主键',
ADD PRIMARY KEY (`id_uuid`);

-- 其他表类似处理...
-- （为节省篇幅，这里省略其他表的切换，实际执行时需要为所有表执行类似操作）

-- ============================================
-- 第七阶段：重命名字段（将_uuid后缀去掉）
-- ============================================

-- user表：重命名字段
ALTER TABLE `user`
CHANGE COLUMN `id_uuid` `id` CHAR(36) NOT NULL COMMENT 'UUID主键',
DROP COLUMN `id`;
-- 删除旧ID字段（确认无误后执行）

-- 注意：其他表也需要类似处理，但需要先确保所有代码都已更新为使用UUID

-- ============================================
-- 第八阶段：更新唯一索引和约束
-- ============================================

-- user表：更新mobile唯一索引（如果存在）
-- ALTER TABLE `user` ADD UNIQUE INDEX `uk_mobile` (`mobile`);

-- user_auth表：更新(auth_type, auth_key)唯一索引
ALTER TABLE `user_auth`
ADD UNIQUE INDEX `uk_auth_type_key` (`auth_type`, `auth_key`);

-- agent表：更新user_id唯一索引
ALTER TABLE `agent` ADD UNIQUE INDEX `uk_user_id` (`user_id_uuid`);

-- ============================================
-- 第九阶段：清理临时表
-- ============================================

-- 确认所有数据迁移无误后，删除映射表
-- DROP TABLE IF EXISTS `uuid_mapping`;

-- ============================================
-- 注意事项
-- ============================================
-- 1. 此脚本需要分阶段执行，每阶段执行后需要验证数据完整性
-- 2. 第六阶段（切换主键）需要停止服务
-- 3. 执行前务必备份数据库
-- 4. 建议先在测试环境完整执行一遍
-- 5. 代码层面需要同步修改：
--    - 所有Model的ID字段类型从int64改为string
--    - 所有插入操作需要生成UUID（使用uuid.NewString()）
--    - 所有查询操作需要使用UUID
-- 6. 删除旧ID字段前，确保所有代码都已更新完成