-- V54: 补齐批量审批权限码
-- 修复审批中心批量处理权限缺失问题

-- 1. 添加批量审批处理权限
INSERT INTO sys_permission (id, permission_code, permission_name, module_code, resource_code, operation_code, data_scope, description, status, created_at)
SELECT 404, 'approval.index.batch.handle.ALL', '批量处理审批', 'approval', 'index', 'batch_handle', 'ALL', '批量审批通过/拒绝', 'ENABLED', CURRENT_TIMESTAMP
WHERE NOT EXISTS (SELECT 1 FROM sys_permission WHERE permission_code = 'approval.index.batch.handle.ALL');

-- 2. 添加批量转交审批权限
INSERT INTO sys_permission (id, permission_code, permission_name, module_code, resource_code, operation_code, data_scope, description, status, created_at)
SELECT 405, 'approval.index.batch.transfer.ALL', '批量转交审批', 'approval', 'index', 'batch_transfer', 'ALL', '批量转交审批', 'ENABLED', CURRENT_TIMESTAMP
WHERE NOT EXISTS (SELECT 1 FROM sys_permission WHERE permission_code = 'approval.index.batch.transfer.ALL');

-- 3. 为超级管理员分配批量处理权限
INSERT INTO sys_role_permission (role_id, permission_id)
SELECT 1, id FROM sys_permission WHERE permission_code = 'approval.index.batch.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 = 'approval.index.batch.handle.ALL'));

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

-- 4. 为系统管理员分配批量处理权限
INSERT INTO sys_role_permission (role_id, permission_id)
SELECT 2, id FROM sys_permission WHERE permission_code = 'approval.index.batch.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 = 'approval.index.batch.handle.ALL'));

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

-- 5. 为管理层（总监/经理）分配批量处理权限
INSERT INTO sys_role_permission (role_id, permission_id)
SELECT 3, id FROM sys_permission WHERE permission_code = 'approval.index.batch.handle.ALL'  -- 运营总监
AND NOT EXISTS (SELECT 1 FROM sys_role_permission WHERE role_id = 3 AND permission_id = (SELECT id FROM sys_permission WHERE permission_code = 'approval.index.batch.handle.ALL'));

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

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

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

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

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

-- 6. 添加审计报告查看权限
INSERT INTO sys_permission (id, permission_code, permission_name, module_code, resource_code, operation_code, data_scope, description, status, created_at)
SELECT 406, 'audit.report.view.ALL', '查看审计报告', 'audit', 'report', 'view', 'ALL', '查看系统审计报告', 'ENABLED', CURRENT_TIMESTAMP
WHERE NOT EXISTS (SELECT 1 FROM sys_permission WHERE permission_code = 'audit.report.view.ALL');

-- 7. 为超级管理员/系统管理员/审计员分配审计报告权限
INSERT INTO sys_role_permission (role_id, permission_id)
SELECT 1, id FROM sys_permission WHERE permission_code = 'audit.report.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 = 'audit.report.view.ALL'));

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

INSERT INTO sys_role_permission (role_id, permission_id)
SELECT 15, id FROM sys_permission WHERE permission_code = 'audit.report.view.ALL'  -- 审计员
AND NOT EXISTS (SELECT 1 FROM sys_role_permission WHERE role_id = 15 AND permission_id = (SELECT id FROM sys_permission WHERE permission_code = 'audit.report.view.ALL'));
