-- V52: 补齐缺失的审批流模板
-- 用途: 添加用户冻结/解冻、敏感导出、活动更新/删除等审批场景模板
-- 注意: 此迁移可重复执行（幂等）
-- 修复: V52原版本使用了错误的表名sys_approval_flows（应为sys_approval_flow）

-- 1. 用户冻结审批模板
INSERT INTO sys_approval_flow (flow_code, flow_name, trigger_event, conditions, nodes, timeout_hours, timeout_action, status, created_at)
SELECT 'USER_FREEZE', '用户冻结审批', 'USER_FREEZE', NULL,
'[{"nodeIndex":1,"nodeName":"风控审核","approverType":"ROLE","approverCodes":["risk_manager"]}]',
24, 'ESCALATE', 'ENABLED', CURRENT_TIMESTAMP
WHERE NOT EXISTS (SELECT 1 FROM sys_approval_flow WHERE trigger_event = 'USER_FREEZE');

-- 2. 用户解冻审批模板
INSERT INTO sys_approval_flow (flow_code, flow_name, trigger_event, conditions, nodes, timeout_hours, timeout_action, status, created_at)
SELECT 'USER_UNFREEZE', '用户解冻审批', 'USER_UNFREEZE', NULL,
'[{"nodeIndex":1,"nodeName":"运营经理审核","approverType":"ROLE","approverCodes":["operation_manager"]}]',
24, 'ESCALATE', 'ENABLED', CURRENT_TIMESTAMP
WHERE NOT EXISTS (SELECT 1 FROM sys_approval_flow WHERE trigger_event = 'USER_UNFREEZE');

-- 3. 敏感数据导出审批模板
INSERT INTO sys_approval_flow (flow_code, flow_name, trigger_event, conditions, nodes, timeout_hours, timeout_action, status, created_at)
SELECT 'SENSITIVE_EXPORT', '敏感数据导出审批', 'SENSITIVE_EXPORT', NULL,
'[{"nodeIndex":1,"nodeName":"安全主管审核","approverType":"ROLE","approverCodes":["security_leader"]}]',
24, 'ESCALATE', 'ENABLED', CURRENT_TIMESTAMP
WHERE NOT EXISTS (SELECT 1 FROM sys_approval_flow WHERE trigger_event = 'SENSITIVE_EXPORT');

-- 4. 活动更新审批模板
INSERT INTO sys_approval_flow (flow_code, flow_name, trigger_event, conditions, nodes, timeout_hours, timeout_action, status, created_at)
SELECT 'ACTIVITY_UPDATE', '活动更新审批', 'ACTIVITY_UPDATE', NULL,
'[{"nodeIndex":1,"nodeName":"运营经理审核","approverType":"ROLE","approverCodes":["operation_manager"]}]',
24, 'ESCALATE', 'ENABLED', CURRENT_TIMESTAMP
WHERE NOT EXISTS (SELECT 1 FROM sys_approval_flow WHERE trigger_event = 'ACTIVITY_UPDATE');

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