-- V63: 修复风控专员权限分配错误
-- 问题: V62将风控权限错误分配给了role_id=12(财务专员)，应为role_id=13(风控专员)
-- 修复: 将错误分配给finance_specialist的权限移除，正确分配给risk_specialist

-- 1. 移除风控权限从财务专员(role_id=12)
DELETE FROM sys_role_permission
WHERE role_id = 12 AND permission_id = (
    SELECT id FROM sys_permission WHERE permission_code = 'risk.index.view.ALL'
);

-- 2. 为风控专员(role_id=13)分配风控只读权限
INSERT INTO sys_role_permission (role_id, permission_id)
SELECT 13, id FROM sys_permission WHERE permission_code = 'risk.index.view.ALL'
AND NOT EXISTS (
    SELECT 1 FROM sys_role_permission WHERE role_id = 13 AND permission_id = sys_permission.id
);

-- 3. 为风控专员(role_id=13)分配风控规则权限(如果风控经理有这些权限)
INSERT INTO sys_role_permission (role_id, permission_id)
SELECT 13, id FROM sys_permission WHERE permission_code IN (
    'risk.rule.create.ALL', 'risk.rule.edit.ALL', 'risk.rule.delete.ALL', 'risk.rule.enable.ALL'
) AND NOT EXISTS (
    SELECT 1 FROM sys_role_permission WHERE role_id = 13 AND permission_id = sys_permission.id
);

-- 4. 确保风控经理(role_id=8)拥有完整的风控规则权限
INSERT INTO sys_role_permission (role_id, permission_id)
SELECT 8, id FROM sys_permission WHERE permission_code IN (
    'risk.rule.create.ALL', 'risk.rule.edit.ALL', 'risk.rule.delete.ALL', 'risk.rule.enable.ALL'
) AND NOT EXISTS (
    SELECT 1 FROM sys_role_permission WHERE role_id = 8 AND permission_id = sys_permission.id
);
