-- V39: 补充审批中心缺失的权限码（Controller需要的闭环）
-- 修复审批流程管理的权限码缺失问题

-- 1. 添加审批流程管理权限
INSERT INTO sys_permission (id, permission_code, permission_name, module_code, resource_code, operation_code, data_scope, description, sort_order, status, created_at)
SELECT 401, 'approval.flow.manage.ALL', '管理审批流程', 'approval', 'flow', 'manage', 'ALL', '创建/更新/删除/启用禁用审批流程', 401, 'ENABLED', CURRENT_TIMESTAMP
WHERE NOT EXISTS (SELECT 1 FROM sys_permission WHERE permission_code = 'approval.flow.manage.ALL');

-- 2. 添加提交审批权限
INSERT INTO sys_permission (id, permission_code, permission_name, module_code, resource_code, operation_code, data_scope, description, sort_order, status, created_at)
SELECT 402, 'approval.index.submit.ALL', '提交审批', 'approval', 'index', 'submit', 'ALL', '提交审批申请', 402, 'ENABLED', CURRENT_TIMESTAMP
WHERE NOT EXISTS (SELECT 1 FROM sys_permission WHERE permission_code = 'approval.index.submit.ALL');

-- 3. 添加取消审批权限
INSERT INTO sys_permission (id, permission_code, permission_name, module_code, resource_code, operation_code, data_scope, description, sort_order, status, created_at)
SELECT 403, 'approval.index.cancel.ALL', '取消审批', 'approval', 'index', 'cancel', 'ALL', '取消自己提交的审批申请', 403, 'ENABLED', CURRENT_TIMESTAMP
WHERE NOT EXISTS (SELECT 1 FROM sys_permission WHERE permission_code = 'approval.index.cancel.ALL');

-- 4. 为角色分配审批流程管理权限 (仅系统层角色)
INSERT INTO sys_role_permission (role_id, permission_id)
SELECT 1, id FROM sys_permission WHERE permission_code = 'approval.flow.manage.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.flow.manage.ALL'));

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

-- 5. 为角色分配提交审批权限 (运营/市场/财务/风控管理层)
INSERT INTO sys_role_permission (role_id, permission_id)
SELECT 3, id FROM sys_permission WHERE permission_code = 'approval.index.submit.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.submit.ALL'));

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

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

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

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

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

-- 6. 为角色分配取消审批权限 (所有有提交审批权限的角色)
INSERT INTO sys_role_permission (role_id, permission_id)
SELECT 3, id FROM sys_permission WHERE permission_code = 'approval.index.cancel.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.cancel.ALL'));

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

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

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

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

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

-- 7. 为执行层添加提交/取消审批权限 (需审批时)
INSERT INTO sys_role_permission (role_id, permission_id)
SELECT 10, id FROM sys_permission WHERE permission_code = 'approval.index.submit.ALL'  -- 运营专员
AND NOT EXISTS (SELECT 1 FROM sys_role_permission WHERE role_id = 10 AND permission_id = (SELECT id FROM sys_permission WHERE permission_code = 'approval.index.submit.ALL'));

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

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

INSERT INTO sys_role_permission (role_id, permission_id)
SELECT 13, id FROM sys_permission WHERE permission_code = 'approval.index.submit.ALL'  -- 风控专员
AND NOT EXISTS (SELECT 1 FROM sys_role_permission WHERE role_id = 13 AND permission_id = (SELECT id FROM sys_permission WHERE permission_code = 'approval.index.submit.ALL'));
