-- 为模板市场添加假数据
-- 注意：需要先有一个用户，这里假设用户ID为第一个用户的ID

-- 获取第一个用户ID（如果不存在，需要先创建用户）
SET @user_id = (SELECT id FROM users LIMIT 1);

-- 如果用户不存在，提示错误
SELECT IF(@user_id IS NULL, 'ERROR: 请先创建用户', 'OK: 找到用户') AS status;

-- 插入模板数据
INSERT INTO workflow_templates (
    id, name, description, category, tags, nodes, edges, 
    is_public, is_featured, view_count, use_count, rating_count, rating_avg, 
    user_id, created_at, updated_at
) VALUES
(
    UUID(),
    '智能客服工作流',
    '一个完整的智能客服工作流，支持自动回复、问题分类和转人工功能。',
    'llm',
    '["客服", "AI", "自动化"]',
    '[{"id": "start", "type": "start", "position": {"x": 100, "y": 100}, "data": {"label": "开始"}}, {"id": "input", "type": "input", "position": {"x": 300, "y": 100}, "data": {"label": "接收用户消息"}}, {"id": "llm", "type": "llm", "position": {"x": 500, "y": 100}, "data": {"label": "AI回复", "model": "gpt-3.5-turbo"}}, {"id": "end", "type": "end", "position": {"x": 700, "y": 100}, "data": {"label": "结束"}}]',
    '[{"id": "e1", "source": "start", "target": "input"}, {"id": "e2", "source": "input", "target": "llm"}, {"id": "e3", "source": "llm", "target": "end"}]',
    TRUE,
    TRUE,
    FLOOR(RAND() * 1000),
    FLOOR(RAND() * 100),
    FLOOR(RAND() * 50),
    ROUND(3.5 + RAND() * 1.5, 1),
    @user_id,
    NOW() - INTERVAL FLOOR(RAND() * 30) DAY,
    NOW() - INTERVAL FLOOR(RAND() * 7) DAY
),
(
    UUID(),
    '数据清洗工作流',
    '自动清洗和转换数据，支持多种数据源格式。',
    'data_processing',
    '["数据处理", "ETL", "数据清洗"]',
    '[{"id": "start", "type": "start", "position": {"x": 100, "y": 100}, "data": {"label": "开始"}}, {"id": "input", "type": "input", "position": {"x": 300, "y": 100}, "data": {"label": "读取数据"}}, {"id": "transform", "type": "transform", "position": {"x": 500, "y": 100}, "data": {"label": "数据清洗"}}, {"id": "end", "type": "end", "position": {"x": 700, "y": 100}, "data": {"label": "结束"}}]',
    '[{"id": "e1", "source": "start", "target": "input"}, {"id": "e2", "source": "input", "target": "transform"}, {"id": "e3", "source": "transform", "target": "end"}]',
    TRUE,
    FALSE,
    FLOOR(RAND() * 1000),
    FLOOR(RAND() * 100),
    FLOOR(RAND() * 50),
    ROUND(3.5 + RAND() * 1.5, 1),
    @user_id,
    NOW() - INTERVAL FLOOR(RAND() * 30) DAY,
    NOW() - INTERVAL FLOOR(RAND() * 7) DAY
),
(
    UUID(),
    '定时数据同步',
    '定时从数据库同步数据到另一个系统，支持增量同步。',
    'automation',
    '["定时任务", "数据同步", "自动化"]',
    '[{"id": "start", "type": "start", "position": {"x": 100, "y": 100}, "data": {"label": "开始"}}, {"id": "schedule", "type": "schedule", "position": {"x": 300, "y": 100}, "data": {"label": "定时触发"}}, {"id": "db", "type": "database", "position": {"x": 500, "y": 100}, "data": {"label": "同步数据"}}, {"id": "end", "type": "end", "position": {"x": 700, "y": 100}, "data": {"label": "结束"}}]',
    '[{"id": "e1", "source": "start", "target": "schedule"}, {"id": "e2", "source": "schedule", "target": "db"}, {"id": "e3", "source": "db", "target": "end"}]',
    TRUE,
    TRUE,
    FLOOR(RAND() * 1000),
    FLOOR(RAND() * 100),
    FLOOR(RAND() * 50),
    ROUND(3.5 + RAND() * 1.5, 1),
    @user_id,
    NOW() - INTERVAL FLOOR(RAND() * 30) DAY,
    NOW() - INTERVAL FLOOR(RAND() * 7) DAY
),
(
    UUID(),
    'API数据聚合',
    '从多个API获取数据并聚合处理，支持错误重试。',
    'integration',
    '["API", "数据聚合", "集成"]',
    '[{"id": "start", "type": "start", "position": {"x": 100, "y": 100}, "data": {"label": "开始"}}, {"id": "http1", "type": "http", "position": {"x": 300, "y": 50}, "data": {"label": "API 1"}}, {"id": "http2", "type": "http", "position": {"x": 300, "y": 150}, "data": {"label": "API 2"}}, {"id": "transform", "type": "transform", "position": {"x": 500, "y": 100}, "data": {"label": "数据聚合"}}, {"id": "end", "type": "end", "position": {"x": 700, "y": 100}, "data": {"label": "结束"}}]',
    '[{"id": "e1", "source": "start", "target": "http1"}, {"id": "e2", "source": "start", "target": "http2"}, {"id": "e3", "source": "http1", "target": "transform"}, {"id": "e4", "source": "http2", "target": "transform"}, {"id": "e5", "source": "transform", "target": "end"}]',
    TRUE,
    FALSE,
    FLOOR(RAND() * 1000),
    FLOOR(RAND() * 100),
    FLOOR(RAND() * 50),
    ROUND(3.5 + RAND() * 1.5, 1),
    @user_id,
    NOW() - INTERVAL FLOOR(RAND() * 30) DAY,
    NOW() - INTERVAL FLOOR(RAND() * 7) DAY
),
(
    UUID(),
    '邮件通知工作流',
    '根据条件发送邮件通知，支持HTML格式和附件。',
    'automation',
    '["邮件", "通知", "自动化"]',
    '[{"id": "start", "type": "start", "position": {"x": 100, "y": 100}, "data": {"label": "开始"}}, {"id": "input", "type": "input", "position": {"x": 300, "y": 100}, "data": {"label": "接收事件"}}, {"id": "condition", "type": "condition", "position": {"x": 500, "y": 100}, "data": {"label": "判断是否需要通知"}}, {"id": "email", "type": "email", "position": {"x": 700, "y": 100}, "data": {"label": "发送邮件"}}, {"id": "end", "type": "end", "position": {"x": 900, "y": 100}, "data": {"label": "结束"}}]',
    '[{"id": "e1", "source": "start", "target": "input"}, {"id": "e2", "source": "input", "target": "condition"}, {"id": "e3", "source": "condition", "target": "email"}, {"id": "e4", "source": "email", "target": "end"}]',
    TRUE,
    TRUE,
    FLOOR(RAND() * 1000),
    FLOOR(RAND() * 100),
    FLOOR(RAND() * 50),
    ROUND(3.5 + RAND() * 1.5, 1),
    @user_id,
    NOW() - INTERVAL FLOOR(RAND() * 30) DAY,
    NOW() - INTERVAL FLOOR(RAND() * 7) DAY
),
(
    UUID(),
    '内容生成工作流',
    '使用AI生成文章、摘要等内容，支持多种格式输出。',
    'llm',
    '["AI", "内容生成", "LLM"]',
    '[{"id": "start", "type": "start", "position": {"x": 100, "y": 100}, "data": {"label": "开始"}}, {"id": "input", "type": "input", "position": {"x": 300, "y": 100}, "data": {"label": "输入主题"}}, {"id": "llm1", "type": "llm", "position": {"x": 500, "y": 100}, "data": {"label": "生成大纲"}}, {"id": "llm2", "type": "llm", "position": {"x": 700, "y": 100}, "data": {"label": "生成内容"}}, {"id": "end", "type": "end", "position": {"x": 900, "y": 100}, "data": {"label": "结束"}}]',
    '[{"id": "e1", "source": "start", "target": "input"}, {"id": "e2", "source": "input", "target": "llm1"}, {"id": "e3", "source": "llm1", "target": "llm2"}, {"id": "e4", "source": "llm2", "target": "end"}]',
    TRUE,
    FALSE,
    FLOOR(RAND() * 1000),
    FLOOR(RAND() * 100),
    FLOOR(RAND() * 50),
    ROUND(3.5 + RAND() * 1.5, 1),
    @user_id,
    NOW() - INTERVAL FLOOR(RAND() * 30) DAY,
    NOW() - INTERVAL FLOOR(RAND() * 7) DAY
),
(
    UUID(),
    '文件处理工作流',
    '批量处理文件，支持上传、下载、格式转换等功能。',
    'data_processing',
    '["文件处理", "批量操作"]',
    '[{"id": "start", "type": "start", "position": {"x": 100, "y": 100}, "data": {"label": "开始"}}, {"id": "loop", "type": "loop", "position": {"x": 300, "y": 100}, "data": {"label": "遍历文件列表"}}, {"id": "file_read", "type": "file_operation", "position": {"x": 500, "y": 100}, "data": {"label": "读取文件"}}, {"id": "file_write", "type": "file_operation", "position": {"x": 700, "y": 100}, "data": {"label": "保存文件"}}, {"id": "end", "type": "end", "position": {"x": 900, "y": 100}, "data": {"label": "结束"}}]',
    '[{"id": "e1", "source": "start", "target": "loop"}, {"id": "e2", "source": "loop", "target": "file_read"}, {"id": "e3", "source": "file_read", "target": "file_write"}, {"id": "e4", "source": "file_write", "target": "end"}]',
    TRUE,
    FALSE,
    FLOOR(RAND() * 1000),
    FLOOR(RAND() * 100),
    FLOOR(RAND() * 50),
    ROUND(3.5 + RAND() * 1.5, 1),
    @user_id,
    NOW() - INTERVAL FLOOR(RAND() * 30) DAY,
    NOW() - INTERVAL FLOOR(RAND() * 7) DAY
),
(
    UUID(),
    'Webhook触发器',
    '接收外部Webhook请求并触发工作流，支持多种认证方式。',
    'integration',
    '["Webhook", "触发器", "集成"]',
    '[{"id": "start", "type": "start", "position": {"x": 100, "y": 100}, "data": {"label": "开始"}}, {"id": "webhook", "type": "webhook", "position": {"x": 300, "y": 100}, "data": {"label": "接收Webhook"}}, {"id": "transform", "type": "transform", "position": {"x": 500, "y": 100}, "data": {"label": "处理数据"}}, {"id": "http", "type": "http", "position": {"x": 700, "y": 100}, "data": {"label": "调用外部API"}}, {"id": "end", "type": "end", "position": {"x": 900, "y": 100}, "data": {"label": "结束"}}]',
    '[{"id": "e1", "source": "start", "target": "webhook"}, {"id": "e2", "source": "webhook", "target": "transform"}, {"id": "e3", "source": "transform", "target": "http"}, {"id": "e4", "source": "http", "target": "end"}]',
    TRUE,
    TRUE,
    FLOOR(RAND() * 1000),
    FLOOR(RAND() * 100),
    FLOOR(RAND() * 50),
    ROUND(3.5 + RAND() * 1.5, 1),
    @user_id,
    NOW() - INTERVAL FLOOR(RAND() * 30) DAY,
    NOW() - INTERVAL FLOOR(RAND() * 7) DAY
)
ON DUPLICATE KEY UPDATE name=name;

-- 显示插入结果
SELECT COUNT(*) AS '模板数量' FROM workflow_templates WHERE user_id = @user_id;
