-- V60: 修正审批模板与触发事件
-- 用途: 修正审批模板节点与PRD一致，补充缺失的触发事件
-- 对应PRD: 管理后台PRD-v1.0.md 7.1 关键审批场景

-- 1. 修正用户删除审批模板（客服主管→运营总监）
UPDATE sys_approval_flow SET
    flow_name = '用户删除审批',
    nodes = '[{"nodeIndex":1,"nodeName":"客服主管审核","approverType":"ROLE","roleCode":"customer_service_leader"},{"nodeIndex":2,"nodeName":"运营总监审核","approverType":"ROLE","roleCode":"operation_director"}]'
WHERE trigger_event = 'USER_DELETE';

-- 2. 修正系统配置审批模板（系统管理员→超级管理员）
UPDATE sys_approval_flow SET
    flow_name = '系统配置审批',
    nodes = '[{"nodeIndex":1,"nodeName":"系统管理员审核","approverType":"ROLE","roleCode":"system_admin"},{"nodeIndex":2,"nodeName":"超级管理员审核","approverType":"ROLE","roleCode":"super_admin"}]'
WHERE trigger_event = 'SYSTEM_CONFIG';

-- 3. 修正角色变更审批模板（超级管理员审批）
UPDATE sys_approval_flow SET
    flow_name = '角色变更审批',
    nodes = '[{"nodeIndex":1,"nodeName":"超级管理员审核","approverType":"ROLE","roleCode":"super_admin"}]'
WHERE trigger_event = 'ROLE_CHANGE';

-- 4. 新增角色创建审批模板
INSERT INTO sys_approval_flow (flow_code, flow_name, trigger_event, conditions, nodes, timeout_hours, timeout_action, status, created_at)
SELECT 'ROLE_CREATE', '角色创建审批', 'ROLE_CREATE', 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 = 'ROLE_CREATE');

-- 5. 新增角色更新审批模板
INSERT INTO sys_approval_flow (flow_code, flow_name, trigger_event, conditions, nodes, timeout_hours, timeout_action, status, created_at)
SELECT 'ROLE_UPDATE', '角色更新审批', 'ROLE_UPDATE', 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 = 'ROLE_UPDATE');

-- 6. 新增角色删除审批模板
INSERT INTO sys_approval_flow (flow_code, flow_name, trigger_event, conditions, nodes, timeout_hours, timeout_action, status, created_at)
SELECT 'ROLE_DELETE', '角色删除审批', 'ROLE_DELETE', 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 = 'ROLE_DELETE');

-- 7. 新增风控规则创建审批模板
INSERT INTO sys_approval_flow (flow_code, flow_name, trigger_event, conditions, nodes, timeout_hours, timeout_action, status, created_at)
SELECT 'RISK_RULE_CREATE', '风控规则创建审批', 'RISK_RULE_CREATE', 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_CREATE');

-- 8. 新增风控规则更新审批模板
INSERT INTO sys_approval_flow (flow_code, flow_name, trigger_event, conditions, nodes, timeout_hours, timeout_action, status, created_at)
SELECT 'RISK_RULE_UPDATE', '风控规则更新审批', 'RISK_RULE_UPDATE', 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_UPDATE');

-- 9. 新增风控规则删除审批模板
INSERT INTO sys_approval_flow (flow_code, flow_name, trigger_event, conditions, nodes, timeout_hours, timeout_action, status, created_at)
SELECT 'RISK_RULE_DELETE', '风控规则删除审批', 'RISK_RULE_DELETE', 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_DELETE');

-- 10. 修复ACTIVITY_UPDATE flow_code条件不命中问题
UPDATE sys_approval_flow SET
    trigger_event = 'ACTIVITY_UPDATE'
WHERE flow_code = 'ACTIVITY_EDIT';
