-- V85: 新增PRD细粒度权限点（第一批）
-- 问题: 部分PRD按钮级权限未显式建模，被合并到更粗权限码
-- 新增: activity.participant.view.ALL, risk.detail.view.ALL, risk.alert.handle.ALL

-- 1. 添加活动参与者查看权限
INSERT INTO sys_permission (id, permission_code, permission_name, module_code, resource_code, operation_code, data_scope, description, status, created_at)
SELECT 800, 'activity.participant.view.ALL', '查看活动参与者', 'activity', 'participant', 'view', 'ALL', '查看活动参与者列表和详情', 'ENABLED', CURRENT_TIMESTAMP
WHERE NOT EXISTS (SELECT 1 FROM sys_permission WHERE permission_code = 'activity.participant.view.ALL');

-- 2. 添加风险详情查看权限
INSERT INTO sys_permission (id, permission_code, permission_name, module_code, resource_code, operation_code, data_scope, description, status, created_at)
SELECT 801, 'risk.detail.view.ALL', '查看风险详情', 'risk', 'detail', 'view', 'ALL', '查看风险告警详情', 'ENABLED', CURRENT_TIMESTAMP
WHERE NOT EXISTS (SELECT 1 FROM sys_permission WHERE permission_code = 'risk.detail.view.ALL');

-- 3. 添加风险告警处理权限
INSERT INTO sys_permission (id, permission_code, permission_name, module_code, resource_code, operation_code, data_scope, description, status, created_at)
SELECT 802, 'risk.alert.handle.ALL', '处理风险告警', 'risk', 'alert', 'handle', 'ALL', '处理和响应风险告警', 'ENABLED', CURRENT_TIMESTAMP
WHERE NOT EXISTS (SELECT 1 FROM sys_permission WHERE permission_code = 'risk.alert.handle.ALL');

-- 4. 为超级管理员分配新权限
INSERT INTO sys_role_permission (role_id, permission_id)
SELECT 1, id FROM sys_permission WHERE permission_code = 'activity.participant.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 = 'activity.participant.view.ALL'));

INSERT INTO sys_role_permission (role_id, permission_id)
SELECT 1, id FROM sys_permission WHERE permission_code = 'risk.detail.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 = 'risk.detail.view.ALL'));

INSERT INTO sys_role_permission (role_id, permission_id)
SELECT 1, id FROM sys_permission WHERE permission_code = 'risk.alert.handle.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 = 'risk.alert.handle.ALL'));

-- 5. 为系统管理员分配新权限
INSERT INTO sys_role_permission (role_id, permission_id)
SELECT 2, id FROM sys_permission WHERE permission_code = 'activity.participant.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 = 'activity.participant.view.ALL'));

INSERT INTO sys_role_permission (role_id, permission_id)
SELECT 2, id FROM sys_permission WHERE permission_code = 'risk.detail.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 = 'risk.detail.view.ALL'));

INSERT INTO sys_role_permission (role_id, permission_id)
SELECT 2, id FROM sys_permission WHERE permission_code = 'risk.alert.handle.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 = 'risk.alert.handle.ALL'));

-- 6. 为风控经理和风控专员分配风险详情和告警处理权限
INSERT INTO sys_role_permission (role_id, permission_id)
SELECT 11, id FROM sys_permission WHERE permission_code = 'risk.detail.view.ALL'  -- 风控经理
AND NOT EXISTS (SELECT 1 FROM sys_role_permission WHERE role_id = 11 AND permission_id = (SELECT id FROM sys_permission WHERE permission_code = 'risk.detail.view.ALL'));

INSERT INTO sys_role_permission (role_id, permission_id)
SELECT 11, id FROM sys_permission WHERE permission_code = 'risk.alert.handle.ALL'  -- 风控经理
AND NOT EXISTS (SELECT 1 FROM sys_role_permission WHERE role_id = 11 AND permission_id = (SELECT id FROM sys_permission WHERE permission_code = 'risk.alert.handle.ALL'));

INSERT INTO sys_role_permission (role_id, permission_id)
SELECT 12, id FROM sys_permission WHERE permission_code = 'risk.detail.view.ALL'  -- 风控专员
AND NOT EXISTS (SELECT 1 FROM sys_role_permission WHERE role_id = 12 AND permission_id = (SELECT id FROM sys_permission WHERE permission_code = 'risk.detail.view.ALL'));

INSERT INTO sys_role_permission (role_id, permission_id)
SELECT 12, id FROM sys_permission WHERE permission_code = 'risk.alert.handle.ALL'  -- 风控专员
AND NOT EXISTS (SELECT 1 FROM sys_role_permission WHERE role_id = 12 AND permission_id = (SELECT id FROM sys_permission WHERE permission_code = 'risk.alert.handle.ALL'));

-- 7. 为运营经理和市场经理分配活动参与者查看权限
INSERT INTO sys_role_permission (role_id, permission_id)
SELECT 4, id FROM sys_permission WHERE permission_code = 'activity.participant.view.ALL'  -- 运营经理
AND NOT EXISTS (SELECT 1 FROM sys_role_permission WHERE role_id = 4 AND permission_id = (SELECT id FROM sys_permission WHERE permission_code = 'activity.participant.view.ALL'));

INSERT INTO sys_role_permission (role_id, permission_id)
SELECT 5, id FROM sys_permission WHERE permission_code = 'activity.participant.view.ALL'  -- 市场经理
AND NOT EXISTS (SELECT 1 FROM sys_role_permission WHERE role_id = 5 AND permission_id = (SELECT id FROM sys_permission WHERE permission_code = 'activity.participant.view.ALL'));

INSERT INTO sys_role_permission (role_id, permission_id)
SELECT 6, id FROM sys_permission WHERE permission_code = 'activity.participant.view.ALL'  -- 运营总监
AND NOT EXISTS (SELECT 1 FROM sys_role_permission WHERE role_id = 6 AND permission_id = (SELECT id FROM sys_permission WHERE permission_code = 'activity.participant.view.ALL'));

INSERT INTO sys_role_permission (role_id, permission_id)
SELECT 7, id FROM sys_permission WHERE permission_code = 'activity.participant.view.ALL'  -- 市场总监
AND NOT EXISTS (SELECT 1 FROM sys_role_permission WHERE role_id = 7 AND permission_id = (SELECT id FROM sys_permission WHERE permission_code = 'activity.participant.view.ALL'));