-- ============================================
-- PROD 数据库: BIGINT → UUID 迁移 (保留数据)
-- 数据库: mokeegateway
-- 执行前务必备份！
-- ============================================

-- 步骤1: 所有表添加 UUID 列
ALTER TABLE sys_system       ADD COLUMN uuid_id VARCHAR(64) NOT NULL DEFAULT '' AFTER id;
ALTER TABLE sys_user         ADD COLUMN uuid_id VARCHAR(64) NOT NULL DEFAULT '' AFTER id;
ALTER TABLE sys_role         ADD COLUMN uuid_id VARCHAR(64) NOT NULL DEFAULT '' AFTER id;
ALTER TABLE sys_menu         ADD COLUMN uuid_id VARCHAR(64) NOT NULL DEFAULT '' AFTER id;
ALTER TABLE sys_api          ADD COLUMN uuid_id VARCHAR(64) NOT NULL DEFAULT '' AFTER id;
ALTER TABLE sys_dict         ADD COLUMN uuid_id VARCHAR(64) NOT NULL DEFAULT '' AFTER id;
ALTER TABLE sys_file         ADD COLUMN uuid_id VARCHAR(64) NOT NULL DEFAULT '' AFTER id;
ALTER TABLE sys_user_system  ADD COLUMN uuid_id VARCHAR(64) NOT NULL DEFAULT '' AFTER id;
ALTER TABLE sys_user_role    ADD COLUMN uuid_id VARCHAR(64) NOT NULL DEFAULT '' AFTER id;
ALTER TABLE sys_role_menu    ADD COLUMN uuid_id VARCHAR(64) NOT NULL DEFAULT '' AFTER id;
ALTER TABLE sys_login_log    ADD COLUMN uuid_id VARCHAR(64) NOT NULL DEFAULT '' AFTER id;
ALTER TABLE sys_operation_log ADD COLUMN uuid_id VARCHAR(64) NOT NULL DEFAULT '' AFTER id;

-- 步骤2: 生成 UUID
UPDATE sys_system       SET uuid_id = REPLACE(UUID(),'-','');
UPDATE sys_user         SET uuid_id = REPLACE(UUID(),'-','');
UPDATE sys_role         SET uuid_id = REPLACE(UUID(),'-','');
UPDATE sys_menu         SET uuid_id = REPLACE(UUID(),'-','');
UPDATE sys_api          SET uuid_id = REPLACE(UUID(),'-','');
UPDATE sys_dict         SET uuid_id = REPLACE(UUID(),'-','');
UPDATE sys_file         SET uuid_id = REPLACE(UUID(),'-','');
UPDATE sys_user_system  SET uuid_id = REPLACE(UUID(),'-','');
UPDATE sys_user_role    SET uuid_id = REPLACE(UUID(),'-','');
UPDATE sys_role_menu    SET uuid_id = REPLACE(UUID(),'-','');
UPDATE sys_login_log    SET uuid_id = REPLACE(UUID(),'-','');
UPDATE sys_operation_log SET uuid_id = REPLACE(UUID(),'-','');

-- 步骤3: 更新所有外键引用
UPDATE sys_api          a JOIN sys_system       s ON a.system_id = s.id SET a.system_id = s.uuid_id;
UPDATE sys_role         r JOIN sys_system       s ON r.system_id = s.id SET r.system_id = s.uuid_id;
UPDATE sys_menu         m JOIN sys_system       s ON m.system_id = s.id SET m.system_id = s.uuid_id;
UPDATE sys_user_system us JOIN sys_system       s ON us.system_id = s.id SET us.system_id = s.uuid_id;
UPDATE sys_user_system us JOIN sys_user         u ON us.user_id = u.id     SET us.user_id = u.uuid_id;
UPDATE sys_user_role   ur JOIN sys_user         u ON ur.user_id = u.id     SET ur.user_id = u.uuid_id;
UPDATE sys_user_role   ur JOIN sys_role         r ON ur.role_id = r.id     SET ur.role_id = r.uuid_id;
UPDATE sys_role_menu   rm JOIN sys_role         r ON rm.role_id = r.id     SET rm.role_id = r.uuid_id;
UPDATE sys_role_menu   rm JOIN sys_menu         m ON rm.menu_id = m.id     SET rm.menu_id = m.uuid_id;
UPDATE sys_login_log    l JOIN sys_user         u ON l.user_id = u.id      SET l.user_id = u.uuid_id;
UPDATE sys_login_log    l JOIN sys_system       s ON l.system_id = s.id    SET l.system_id = s.uuid_id;
UPDATE sys_operation_log o JOIN sys_user        u ON o.user_id = u.id      SET o.user_id = u.uuid_id;
UPDATE sys_operation_log o JOIN sys_system      s ON o.system_id = s.id    SET o.system_id = s.uuid_id;
-- menu parent_id 自引用
UPDATE sys_menu child JOIN sys_menu parent ON child.parent_id = parent.id SET child.parent_id = parent.uuid_id WHERE child.parent_id != '0';

-- 步骤4: 删除旧主键, 切换到 UUID 主键
ALTER TABLE sys_system       DROP PRIMARY KEY, DROP COLUMN id, CHANGE uuid_id id VARCHAR(64) NOT NULL, ADD PRIMARY KEY (id);
ALTER TABLE sys_user         DROP PRIMARY KEY, DROP COLUMN id, CHANGE uuid_id id VARCHAR(64) NOT NULL, ADD PRIMARY KEY (id);
ALTER TABLE sys_role         DROP PRIMARY KEY, DROP COLUMN id, CHANGE uuid_id id VARCHAR(64) NOT NULL, ADD PRIMARY KEY (id);
ALTER TABLE sys_menu         DROP PRIMARY KEY, DROP COLUMN id, CHANGE uuid_id id VARCHAR(64) NOT NULL, ADD PRIMARY KEY (id);
ALTER TABLE sys_api          DROP PRIMARY KEY, DROP COLUMN id, CHANGE uuid_id id VARCHAR(64) NOT NULL, ADD PRIMARY KEY (id);
ALTER TABLE sys_dict         DROP PRIMARY KEY, DROP COLUMN id, CHANGE uuid_id id VARCHAR(64) NOT NULL, ADD PRIMARY KEY (id);
ALTER TABLE sys_file         DROP PRIMARY KEY, DROP COLUMN id, CHANGE uuid_id id VARCHAR(64) NOT NULL, ADD PRIMARY KEY (id);
ALTER TABLE sys_user_system  DROP PRIMARY KEY, DROP COLUMN id, CHANGE uuid_id id VARCHAR(64) NOT NULL, ADD PRIMARY KEY (id);
ALTER TABLE sys_user_role    DROP PRIMARY KEY, DROP COLUMN id, CHANGE uuid_id id VARCHAR(64) NOT NULL, ADD PRIMARY KEY (id);
ALTER TABLE sys_role_menu    DROP PRIMARY KEY, DROP COLUMN id, CHANGE uuid_id id VARCHAR(64) NOT NULL, ADD PRIMARY KEY (id);
ALTER TABLE sys_login_log    DROP PRIMARY KEY, DROP COLUMN id, CHANGE uuid_id id VARCHAR(64) NOT NULL, ADD PRIMARY KEY (id);
ALTER TABLE sys_operation_log DROP PRIMARY KEY, DROP COLUMN id, CHANGE uuid_id id VARCHAR(64) NOT NULL, ADD PRIMARY KEY (id);

-- 步骤5: 调整外键类型为 VARCHAR(64)
ALTER TABLE sys_api          MODIFY system_id VARCHAR(64) NOT NULL;
ALTER TABLE sys_role         MODIFY system_id VARCHAR(64) NOT NULL;
ALTER TABLE sys_menu         MODIFY system_id VARCHAR(64) NOT NULL, MODIFY parent_id VARCHAR(64) NOT NULL DEFAULT '0';
ALTER TABLE sys_user_system  MODIFY user_id VARCHAR(64) NOT NULL, MODIFY system_id VARCHAR(64) NOT NULL;
ALTER TABLE sys_user_role    MODIFY user_id VARCHAR(64) NOT NULL, MODIFY role_id VARCHAR(64) NOT NULL;
ALTER TABLE sys_role_menu    MODIFY role_id VARCHAR(64) NOT NULL, MODIFY menu_id VARCHAR(64) NOT NULL;
ALTER TABLE sys_login_log    MODIFY user_id VARCHAR(64), MODIFY system_id VARCHAR(64);
ALTER TABLE sys_operation_log MODIFY user_id VARCHAR(64), MODIFY system_id VARCHAR(64);
ALTER TABLE sys_file         MODIFY upload_by VARCHAR(64);
ALTER TABLE sys_system       MODIFY created_by VARCHAR(64), MODIFY updated_by VARCHAR(64);
ALTER TABLE sys_role         MODIFY created_by VARCHAR(64), MODIFY updated_by VARCHAR(64);

-- 步骤6: 重建唯一索引 (FK列类型变更后需要重建)
ALTER TABLE sys_api          DROP INDEX uk_system_path_method, ADD UNIQUE KEY uk_system_path_method (system_id, api_path, api_method);
ALTER TABLE sys_role         DROP INDEX uk_system_role_code,   ADD UNIQUE KEY uk_system_role_code (system_id, role_code);

-- 完成
