-- V55: 补齐剩余审批流模板
-- 用途: 添加用户删除、系统配置、权限变更等审批场景模板
-- 注意: 此迁移可重复执行（幂等）
-- 对应PRD: 管理后台PRD-v1.0.md 7.1 关键审批场景

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

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

-- 3. 权限变更审批模板
INSERT INTO sys_approval_flow (flow_code, flow_name, trigger_event, conditions, nodes, timeout_hours, timeout_action, status, created_at)
SELECT 'PERMISSION_CHANGE', '权限变更审批', 'PERMISSION_CHANGE', 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_CHANGE');

-- 4. 角色分配审批模板
INSERT INTO sys_approval_flow (flow_code, flow_name, trigger_event, conditions, nodes, timeout_hours, timeout_action, status, created_at)
SELECT 'ROLE_ASSIGN', '角色分配审批', 'ROLE_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 = 'ROLE_ASSIGN');

-- 5. 部门变更审批模板
INSERT INTO sys_approval_flow (flow_code, flow_name, trigger_event, conditions, nodes, timeout_hours, timeout_action, status, created_at)
SELECT 'DEPARTMENT_CHANGE', '部门变更审批', 'DEPARTMENT_CHANGE', NULL,
'[{"nodeIndex":1,"nodeName":"运营总监审核","approverType":"ROLE","roleCode":"operation_director"}]',
24, 'ESCALATE', 'ENABLED', CURRENT_TIMESTAMP
WHERE NOT EXISTS (SELECT 1 FROM sys_approval_flow WHERE trigger_event = 'DEPARTMENT_CHANGE');

-- 6. 大额奖励审批模板（金额>10000）
INSERT INTO sys_approval_flow (flow_code, flow_name, trigger_event, conditions, nodes, timeout_hours, timeout_action, status, created_at)
SELECT 'HIGH_VALUE_REWARD', '大额奖励审批', 'HIGH_VALUE_REWARD', '{"amount":10000}',
'[{"nodeIndex":1,"nodeName":"运营总监审核","approverType":"ROLE","roleCode":"operation_director"},{"nodeIndex":2,"nodeName":"财务经理审核","approverType":"ROLE","roleCode":"finance_manager"}]',
48, 'AUTO_REJECT', 'ENABLED', CURRENT_TIMESTAMP
WHERE NOT EXISTS (SELECT 1 FROM sys_approval_flow WHERE trigger_event = 'HIGH_VALUE_REWARD');

-- 7. 风控规则审批模板
INSERT INTO sys_approval_flow (flow_code, flow_name, trigger_event, conditions, nodes, timeout_hours, timeout_action, status, created_at)
SELECT 'RISK_RULE', '风控规则审批', 'RISK_RULE', NULL,
'[{"nodeIndex":1,"nodeName":"风控经理审核","approverType":"ROLE","roleCode":"risk_manager"},{"nodeIndex":2,"nodeName":"运营总监审核","approverType":"ROLE","roleCode":"operation_director"}]',
48, 'ESCALATE', 'ENABLED', CURRENT_TIMESTAMP
WHERE NOT EXISTS (SELECT 1 FROM sys_approval_flow WHERE trigger_event = 'RISK_RULE');

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