-- V64: 修复权限码一致性 - 添加后端独有的权限码
-- 问题: 后端使用user.role.view.ALL，但数据库中不存在

INSERT INTO sys_permission (id, permission_code, permission_name, module_code, resource_code, operation_code, data_scope, description, status, created_at)
SELECT 520, 'user.role.view.ALL', '查看用户角色', 'user', 'role', 'view', 'ALL', '查看用户角色分配情况', 'ENABLED', CURRENT_TIMESTAMP
WHERE NOT EXISTS (SELECT 1 FROM sys_permission WHERE permission_code = 'user.role.view.ALL');

-- 为超级管理员分配该权限
INSERT INTO sys_role_permission (role_id, permission_id)
SELECT 1, id FROM sys_permission WHERE permission_code = 'user.role.view.ALL'
AND NOT EXISTS (
    SELECT 1 FROM sys_role_permission WHERE role_id = 1 AND permission_id = (SELECT id FROM sys_permission WHERE permission_code = 'user.role.view.ALL')
);

-- 为系统管理员分配该权限
INSERT INTO sys_role_permission (role_id, permission_id)
SELECT 2, id FROM sys_permission WHERE permission_code = 'user.role.view.ALL'
AND NOT EXISTS (
    SELECT 1 FROM sys_role_permission WHERE role_id = 2 AND permission_id = (SELECT id FROM sys_permission WHERE permission_code = 'user.role.view.ALL')
);
