-- V81: 添加权限分配/撤销审批模板
-- 用途: 修复PermissionController调用的PERMISSION_ASSIGN/PERMISSION_REVOKE事件缺少对应模板问题
-- 触发问题: PermissionController.assignUserPermissions使用TRIGGER_PERMISSION_ASSIGN，
--           PermissionController.revokeUserPermissions使用TRIGGER_PERMISSION_REVOKE，
--           但原V55只添加了PERMISSION_CHANGE模板，导致submitApprovalByEvent抛异常
-- 注意: 此迁移可重复执行（幂等）
-- 对应PRD: 管理后台PRD-v1.0.md 7.1.10 权限变更审批

-- 1. 权限分配审批模板
INSERT INTO sys_approval_flow (flow_code, flow_name, trigger_event, conditions, nodes, timeout_hours, timeout_action, status, created_at)
SELECT 'PERMISSION_ASSIGN', '权限分配审批', 'PERMISSION_ASSIGN', NULL,
'[{"nodeIndex":1,"nodeName":"系统管理员审核","approverType":"ROLE","roleCode":"system_admin"}]',
24, 'ESCALATE', 'ENABLED', CURRENT_TIMESTAMP
WHERE NOT EXISTS (SELECT 1 FROM sys_approval_flow WHERE trigger_event = 'PERMISSION_ASSIGN');

-- 2. 权限撤销审批模板
INSERT INTO sys_approval_flow (flow_code, flow_name, trigger_event, conditions, nodes, timeout_hours, timeout_action, status, created_at)
SELECT 'PERMISSION_REVOKE', '权限撤销审批', 'PERMISSION_REVOKE', NULL,
'[{"nodeIndex":1,"nodeName":"系统管理员审核","approverType":"ROLE","roleCode":"system_admin"}]',
24, 'ESCALATE', 'ENABLED', CURRENT_TIMESTAMP
WHERE NOT EXISTS (SELECT 1 FROM sys_approval_flow WHERE trigger_event = 'PERMISSION_REVOKE');
