-- V86: 新增审批意见独立权限
-- 问题: 审批意见添加能力被合并到更粗的审批处理权限中
-- 新增: approval.comment.add.ALL

-- 1. 添加审批意见添加权限
INSERT INTO sys_permission (id, permission_code, permission_name, module_code, resource_code, operation_code, data_scope, description, status, created_at)
SELECT 803, 'approval.comment.add.ALL', '添加审批意见', 'approval', 'comment', 'add', 'ALL', '在审批流程中添加审批意见', 'ENABLED', CURRENT_TIMESTAMP
WHERE NOT EXISTS (SELECT 1 FROM sys_permission WHERE permission_code = 'approval.comment.add.ALL');

-- 2. 为超级管理员分配审批意见权限
INSERT INTO sys_role_permission (role_id, permission_id)
SELECT 1, id FROM sys_permission WHERE permission_code = 'approval.comment.add.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.comment.add.ALL'));

-- 3. 为系统管理员分配审批意见权限
INSERT INTO sys_role_permission (role_id, permission_id)
SELECT 2, id FROM sys_permission WHERE permission_code = 'approval.comment.add.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.comment.add.ALL'));

-- 4. 为管理层（总监/经理）分配审批意见权限
INSERT INTO sys_role_permission (role_id, permission_id)
SELECT 3, id FROM sys_permission WHERE permission_code = 'approval.comment.add.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.comment.add.ALL'));

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

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

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

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

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

INSERT INTO sys_role_permission (role_id, permission_id)
SELECT 9, id FROM sys_permission WHERE permission_code = 'approval.comment.add.ALL'  -- 客服主管
AND NOT EXISTS (SELECT 1 FROM sys_role_permission WHERE role_id = 9 AND permission_id = (SELECT id FROM sys_permission WHERE permission_code = 'approval.comment.add.ALL'));