-- V75: 添加API Key管理权限 (system.api-key.manage)
-- 用于 useApiKey 和 validateApiKey 接口
-- ID从613开始

-- API Key管理权限
INSERT INTO sys_permission (id, permission_code, permission_name, module_code, resource_code, operation_code, data_scope, description, status, created_at)
SELECT 613, 'system.api-key.manage', '管理API Key', 'system', 'api-key', 'manage', 'ALL', '管理API Key使用和验证', 'ENABLED', CURRENT_TIMESTAMP
WHERE NOT EXISTS (SELECT 1 FROM sys_permission WHERE permission_code = 'system.api-key.manage');

-- 为超级管理员角色添加管理权限
INSERT INTO sys_role_permission (role_id, permission_id)
SELECT r.id, p.id
FROM sys_role r
CROSS JOIN sys_permission p
WHERE r.role_code = 'super_admin'
AND p.permission_code = 'system.api-key.manage'
AND NOT EXISTS (
    SELECT 1 FROM sys_role_permission rp
    WHERE rp.role_id = r.id AND rp.permission_id = p.id
);

-- 为系统管理员角色添加管理权限
INSERT INTO sys_role_permission (role_id, permission_id)
SELECT r.id, p.id
FROM sys_role r
CROSS JOIN sys_permission p
WHERE r.role_code = 'system_admin'
AND p.permission_code = 'system.api-key.manage'
AND NOT EXISTS (
    SELECT 1 FROM sys_role_permission rp
    WHERE rp.role_id = r.id AND rp.permission_id = p.id
);
