-- V76: 规范化权限码到Canonical四段式
-- 问题: 数据库中存在三段式权限码（如 dashboard.chart.realtime），需要统一到四段式格式（.ALL）
-- 注意: ID从700开始

-- 1. 仪表盘监控权限规范化（三段式 -> 四段式）
UPDATE sys_permission SET permission_code = 'dashboard.chart.realtime.ALL', operation_code = 'realtime' WHERE permission_code = 'dashboard.chart.realtime';
UPDATE sys_permission SET permission_code = 'dashboard.chart.history.ALL', operation_code = 'history' WHERE permission_code = 'dashboard.chart.history';
UPDATE sys_permission SET permission_code = 'dashboard.kpi.config.ALL', operation_code = 'config' WHERE permission_code = 'dashboard.kpi.config';

-- 2. 风险拦截权限规范化
UPDATE sys_permission SET permission_code = 'risk.block.execute.ALL', operation_code = 'execute' WHERE permission_code = 'risk.block.execute';
UPDATE sys_permission SET permission_code = 'risk.block.release.ALL', operation_code = 'release' WHERE permission_code = 'risk.block.release';

-- 3. 用户标签权限规范化
UPDATE sys_permission SET permission_code = 'user.tag.view.ALL', operation_code = 'view' WHERE permission_code = 'user.tag.view';
UPDATE sys_permission SET permission_code = 'user.tag.add.ALL', operation_code = 'add' WHERE permission_code = 'user.tag.add';

-- 4. API Key权限规范化
UPDATE sys_permission SET permission_code = 'system.api-key.view.ALL', operation_code = 'view' WHERE permission_code = 'system.api-key.view';
UPDATE sys_permission SET permission_code = 'system.api-key.create.ALL', operation_code = 'create' WHERE permission_code = 'system.api-key.create';
UPDATE sys_permission SET permission_code = 'system.api-key.enable.ALL', operation_code = 'enable' WHERE permission_code = 'system.api-key.enable';
UPDATE sys_permission SET permission_code = 'system.api-key.disable.ALL', operation_code = 'disable' WHERE permission_code = 'system.api-key.disable';
UPDATE sys_permission SET permission_code = 'system.api-key.delete.ALL', operation_code = 'delete' WHERE permission_code = 'system.api-key.delete';
UPDATE sys_permission SET permission_code = 'system.api-key.reset.ALL', operation_code = 'reset' WHERE permission_code = 'system.api-key.reset';
UPDATE sys_permission SET permission_code = 'system.api-key.manage.ALL', operation_code = 'manage' WHERE permission_code = 'system.api-key.manage';

-- 5. 更新角色权限关联表中的权限码引用
UPDATE sys_role_permission rp
SET permission_id = (
    SELECT p.id FROM sys_permission p
    WHERE p.permission_code = REPLACE(
        (SELECT permission_code FROM sys_permission WHERE id = rp.permission_id),
        '.ALL', '.ALL'
    )
)
WHERE rp.permission_id IN (
    SELECT id FROM sys_permission WHERE permission_code IN (
        'dashboard.chart.realtime', 'dashboard.chart.history', 'dashboard.kpi.config',
        'risk.block.execute', 'risk.block.release',
        'user.tag.view', 'user.tag.add',
        'system.api-key.view', 'system.api-key.create', 'system.api-key.enable',
        'system.api-key.disable', 'system.api-key.delete', 'system.api-key.reset',
        'system.api-key.manage'
    )
);

-- 注意: 如果上面的UPDATE失败（因为子查询），使用以下替代方案
-- 重新插入规范化后的权限（如果不存在）
INSERT INTO sys_permission (id, permission_code, permission_name, module_code, resource_code, operation_code, data_scope, description, status, created_at)
SELECT 700, 'dashboard.chart.realtime.ALL', '实时图表', 'dashboard', 'chart', 'realtime', 'ALL', '查看实时监控图表', 'ENABLED', CURRENT_TIMESTAMP
WHERE NOT EXISTS (SELECT 1 FROM sys_permission WHERE permission_code = 'dashboard.chart.realtime.ALL');

INSERT INTO sys_permission (id, permission_code, permission_name, module_code, resource_code, operation_code, data_scope, description, status, created_at)
SELECT 701, 'dashboard.chart.history.ALL', '历史图表', 'dashboard', 'chart', 'history', 'ALL', '查看历史数据图表', 'ENABLED', CURRENT_TIMESTAMP
WHERE NOT EXISTS (SELECT 1 FROM sys_permission WHERE permission_code = 'dashboard.chart.history.ALL');

INSERT INTO sys_permission (id, permission_code, permission_name, module_code, resource_code, operation_code, data_scope, description, status, created_at)
SELECT 702, 'dashboard.kpi.config.ALL', 'KPI配置', 'dashboard', 'kpi', 'config', 'ALL', '配置KPI阈值和告警规则', 'ENABLED', CURRENT_TIMESTAMP
WHERE NOT EXISTS (SELECT 1 FROM sys_permission WHERE permission_code = 'dashboard.kpi.config.ALL');

INSERT INTO sys_permission (id, permission_code, permission_name, module_code, resource_code, operation_code, data_scope, description, status, created_at)
SELECT 703, 'risk.block.execute.ALL', '执行拦截', 'risk', 'block', 'execute', 'ALL', '执行风险拦截操作', 'ENABLED', CURRENT_TIMESTAMP
WHERE NOT EXISTS (SELECT 1 FROM sys_permission WHERE permission_code = 'risk.block.execute.ALL');

INSERT INTO sys_permission (id, permission_code, permission_name, module_code, resource_code, operation_code, data_scope, description, status, created_at)
SELECT 704, 'risk.block.release.ALL', '解除拦截', 'risk', 'block', 'release', 'ALL', '解除风险拦截', 'ENABLED', CURRENT_TIMESTAMP
WHERE NOT EXISTS (SELECT 1 FROM sys_permission WHERE permission_code = 'risk.block.release.ALL');

INSERT INTO sys_permission (id, permission_code, permission_name, module_code, resource_code, operation_code, data_scope, description, status, created_at)
SELECT 705, 'user.tag.view.ALL', '查看标签', 'user', 'tag', 'view', 'ALL', '查看用户标签', 'ENABLED', CURRENT_TIMESTAMP
WHERE NOT EXISTS (SELECT 1 FROM sys_permission WHERE permission_code = 'user.tag.view.ALL');

INSERT INTO sys_permission (id, permission_code, permission_name, module_code, resource_code, operation_code, data_scope, description, status, created_at)
SELECT 706, 'user.tag.add.ALL', '添加标签', 'user', 'tag', 'add', 'ALL', '为用户添加标签', 'ENABLED', CURRENT_TIMESTAMP
WHERE NOT EXISTS (SELECT 1 FROM sys_permission WHERE permission_code = 'user.tag.add.ALL');

INSERT INTO sys_permission (id, permission_code, permission_name, module_code, resource_code, operation_code, data_scope, description, status, created_at)
SELECT 707, 'system.api-key.view.ALL', '查看API Key', 'system', 'api-key', 'view', 'ALL', '查看API Key列表', 'ENABLED', CURRENT_TIMESTAMP
WHERE NOT EXISTS (SELECT 1 FROM sys_permission WHERE permission_code = 'system.api-key.view.ALL');

INSERT INTO sys_permission (id, permission_code, permission_name, module_code, resource_code, operation_code, data_scope, description, status, created_at)
SELECT 708, 'system.api-key.create.ALL', '创建API Key', 'system', 'api-key', 'create', 'ALL', '创建新的API Key', 'ENABLED', CURRENT_TIMESTAMP
WHERE NOT EXISTS (SELECT 1 FROM sys_permission WHERE permission_code = 'system.api-key.create.ALL');

INSERT INTO sys_permission (id, permission_code, permission_name, module_code, resource_code, operation_code, data_scope, description, status, created_at)
SELECT 709, 'system.api-key.enable.ALL', '启用API Key', 'system', 'api-key', 'enable', 'ALL', '启用API Key', 'ENABLED', CURRENT_TIMESTAMP
WHERE NOT EXISTS (SELECT 1 FROM sys_permission WHERE permission_code = 'system.api-key.enable.ALL');

INSERT INTO sys_permission (id, permission_code, permission_name, module_code, resource_code, operation_code, data_scope, description, status, created_at)
SELECT 710, 'system.api-key.disable.ALL', '禁用API Key', 'system', 'api-key', 'disable', 'ALL', '禁用API Key', 'ENABLED', CURRENT_TIMESTAMP
WHERE NOT EXISTS (SELECT 1 FROM sys_permission WHERE permission_code = 'system.api-key.disable.ALL');

INSERT INTO sys_permission (id, permission_code, permission_name, module_code, resource_code, operation_code, data_scope, description, status, created_at)
SELECT 711, 'system.api-key.delete.ALL', '删除API Key', 'system', 'api-key', 'delete', 'ALL', '删除API Key', 'ENABLED', CURRENT_TIMESTAMP
WHERE NOT EXISTS (SELECT 1 FROM sys_permission WHERE permission_code = 'system.api-key.delete.ALL');

INSERT INTO sys_permission (id, permission_code, permission_name, module_code, resource_code, operation_code, data_scope, description, status, created_at)
SELECT 712, 'system.api-key.reset.ALL', '重置API Key', 'system', 'api-key', 'reset', 'ALL', '重置API Key密钥', 'ENABLED', CURRENT_TIMESTAMP
WHERE NOT EXISTS (SELECT 1 FROM sys_permission WHERE permission_code = 'system.api-key.reset.ALL');

INSERT INTO sys_permission (id, permission_code, permission_name, module_code, resource_code, operation_code, data_scope, description, status, created_at)
SELECT 713, 'system.api-key.manage.ALL', '管理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.ALL');
