﻿-- ============================================================================
-- 旅游销冠全流程工作流 — 破冰→询需→推荐→促单→签约→关怀→旅途→回访→长期运营
-- ============================================================================

-- 1. 插入模板头
INSERT INTO company_workflow_lobster (company_id, template_code, template_name, industry_type, description, status, version, del_flag, canvas_data, create_time, update_time)
SELECT 0, 'TRAVEL_TOP_SALES_001', '旅游销冠全流程', '旅游',
       '销冠级全生命周期：直播加微→破冰问候→出行需求采集→线路推荐→促单成交→出行前关怀(分急缓)→旅途中关怀→回访调研→长期问候营销',
       1, 1, 0, '{}', NOW(), NOW()
FROM DUAL
WHERE NOT EXISTS (SELECT 1 FROM company_workflow_lobster WHERE template_code='TRAVEL_TOP_SALES_001' AND del_flag=0);

SET @wf_id = (SELECT id FROM company_workflow_lobster WHERE template_code='TRAVEL_TOP_SALES_001' AND del_flag=0 ORDER BY id DESC LIMIT 1);

-- ============================================================================
-- 第一阶段：破冰 & 需求采集
-- ============================================================================
INSERT IGNORE INTO company_workflow_lobster_node
(workflow_id, node_code, node_name, node_type, sort_no, next_node_code, message_template, condition_expr, node_config, scene_code, model_name, send_time, create_time)
VALUES
(@wf_id, 'START',              '客户加微信触发',          1,  1,  'GREET',    NULL, NULL, NULL, 'sale', 'gpt-4o-mini', NULL, NOW()),
(@wf_id, 'GREET',              '销冠破冰问候',            2,  2,  'ASK_DEST',
'您好呀~我是{公司简称}的金牌旅行顾问{客服名}，很高兴认识你！👋
我看到您是通过直播关注我们的，一定是热爱旅行的朋友吧~我自己也超爱出去玩，去过很多地方了！您有想去的目的地想去看看的吗？先了解下您的想法，我才能给您最专业的建议哦😊', NULL, '{"sendChannel":"im","delaySec":3}', 'sale', 'gpt-4o-mini', NULL, NOW()),
(@wf_id, 'ASK_DEST',           '询问目的地类型',          2,  3,  'JUDGE_DEST_REPLY',
'方便告诉我您想去的类型吗？比如：
🌴 海岛度假  🏔 山水风光  🕌 人文古城  🎢 亲子乐园
或者您心里已经有目标城市啦？随便聊聊就行~', NULL, '{"sendChannel":"im"}', 'sale', 'gpt-4o-mini', NULL, NOW()),
(@wf_id, 'JUDGE_DEST_REPLY',   '判断：客户回复目的地',    3,  4,  'ASK_PEOPLE', NULL,
'{"branches":[{"nextNode":"ASK_PEOPLE","condition":"回复包含目的地/线路/城市/国家关键词","desc":"已回复目的地"},{"nextNode":"NUDGE_NO_REPLY","condition":"超时或未回复","desc":"未回复"}]}',
'{"timeoutSec":3600}', 'sale', NULL, NULL, NOW()),
(@wf_id, 'NUDGE_NO_REPLY',     '再次暖场',               2,  5,  'JUDGE_DEST_REPLY',
'哎呀是不是有点忙呀~不着急的，我随时在~要是还没想好也没关系，我可以等您的😊', NULL, '{"sendChannel":"im","delaySec":1800}', 'sale', 'gpt-4o-mini', NULL, NOW()),
(@wf_id, 'ASK_PEOPLE',         '询问出行人数与人员',      2,  6,  'ASK_HEALTH',
'好的收到！那这次准备几个人一起去呀？是跟朋友、家人还是一个人自由行呢？方便的话也告诉下年龄阶段哈（比如有老人小孩的话我帮您推荐轻松的线路）', NULL, '{"sendChannel":"im"}', 'sale', 'gpt-4o-mini', NULL, NOW()),
(@wf_id, 'ASK_HEALTH',         '询问年龄身体与预算',      2,  7,  'TAG_PROFILE',
'感谢告知~还想问一下，同行人里有没有需要特别照顾的情况呀？比如腿脚不便、心脏病史之类，这样我好避开高海拔或者暴走的行程。另外预算和时间（计划什么时候出发、几天），也方便说一下吗？我帮您控制范围内最优方案 👌', NULL, '{"sendChannel":"im"}', 'sale', 'gpt-4o-mini', NULL, NOW()),
(@wf_id, 'TAG_PROFILE',        '打标签：客户画像',       41,  8,  'SEARCH_ROUTE', NULL, NULL,
'{"extractFields":["destination","peopleCount","ages","healthStatus","budget","departDate","duration","travelType"],"model":"gpt-4o-mini","sceneCode":"sale"}', 'sale', 'gpt-4o-mini', NULL, NOW());

-- ============================================================================
-- 第二阶段：线路推荐
-- ============================================================================
INSERT IGNORE INTO company_workflow_lobster_node
(workflow_id, node_code, node_name, node_type, sort_no, next_node_code, message_template, condition_expr, node_config, scene_code, model_name, send_time, create_time)
VALUES
(@wf_id, 'SEARCH_ROUTE',       '知识库检索：匹配线路',   23,  9,  'RECOMMEND', NULL, NULL,
'{"topK":3,"useVectorSearch":true,"filterTags":["destination","budget","peopleCount","ages"]}', 'sale', NULL, NULL, NOW()),
(@wf_id, 'RECOMMEND',          '推荐线路',               2, 10,  'JUDGE_INTEREST',
'根据您的情况，我帮您筛选了最合适的线路👇
{匹配线路列表，含行程亮点/天数/价格对比}
这几条您觉得怎么样？有什么想改的或要详细信息告诉我就好~', NULL, '{"sendChannel":"im","includeDocs":true}', 'sale', 'gpt-4o-mini', NULL, NOW()),
(@wf_id, 'JUDGE_INTEREST',     '判断：客户感兴趣',       3, 11,  'COUPON', NULL,
'{"branches":[{"nextNode":"COUPON","condition":"回复积极、询问价格、表达意向","desc":"感兴趣"},{"nextNode":"RETRY_RECOMMEND","condition":"不感兴趣或犹豫","desc":"不感兴趣"},{"nextNode":"NUDGE_URGE","condition":"超时未回复","desc":"未回复"}]}',
'{"timeoutSec":7200}', 'sale', NULL, NULL, NOW()),
(@wf_id, 'RETRY_RECOMMEND',    '补充推荐/询因',          2, 12,  'JUDGE_INTEREST',
'嗯嗯了解~方便说一下不太满意的地方吗？是预算、时间还是线路内容不感兴趣？我帮您重新匹配更合适的方案 😊', NULL, '{"sendChannel":"im"}', 'sale', 'gpt-4o-mini', NULL, NOW());

-- ============================================================================
-- 第三阶段：促单成交
-- ============================================================================
INSERT IGNORE INTO company_workflow_lobster_node
(workflow_id, node_code, node_name, node_type, sort_no, next_node_code, message_template, condition_expr, node_config, scene_code, model_name, send_time, create_time)
VALUES
(@wf_id, 'COUPON',             '发放专属优惠券',          8, 13,  'URGE_TO_CLOSE', NULL, NULL,
'{"couponType":"lineDiscount","amount":"custom","desc":"销冠专属优惠"}', 'sale', NULL, NULL, NOW()),
(@wf_id, 'URGE_TO_CLOSE',      '限时促单话术',            2, 14,  'JUDGE_DEAL',
'亲~我帮您争取到了一个专属折扣优惠，这个只有我们销冠才有权限发放的！而且这条线路最近咨询的人特别多，如果确定的话我建议今天就锁定名额，不然到时候涨价就可惜了~ 😊 要不要我直接帮您下单？', NULL, '{"sendChannel":"im"}', 'sale', 'gpt-4o-mini', NULL, NOW()),
(@wf_id, 'NUDGE_URGE',         '再次试探兴趣',            2, 15,  'JUDGE_INTEREST',
'亲~还在吗？不用有压力，就是真诚地建议而已。如果暂时不考虑也没关系的，有什么疑问我随时解答 🙌', NULL, '{"sendChannel":"im","delaySec":3600}', 'sale', 'gpt-4o-mini', NULL, NOW()),
(@wf_id, 'JUDGE_DEAL',         '判断：是否成交',          3, 16,  'TAG_ORDER', NULL,
'{"branches":[{"nextNode":"TAG_ORDER","condition":"客户确认下单、付款、签单","desc":"成交"},{"nextNode":"HUMAN_HANDOFF","condition":"明确拒绝或转人工请求","desc":"不成交"}]}',
'{"timeoutSec":86400}', 'sale', NULL, NULL, NOW()),
(@wf_id, 'HUMAN_HANDOFF',      '转人工跟进',             21, 17,  'END', NULL, NULL,
'{"reason":"客户未成交","action":"assignToHuman"}', 'sale', NULL, NULL, NOW());

-- ============================================================================
-- 第四阶段：签约后 — 确认出发日期
-- ============================================================================
INSERT IGNORE INTO company_workflow_lobster_node
(workflow_id, node_code, node_name, node_type, sort_no, next_node_code, message_template, condition_expr, node_config, scene_code, model_name, send_time, create_time)
VALUES
(@wf_id, 'TAG_ORDER',          '打标签：订单信息',       41, 18,  'ASK_DEPART_DATE', NULL, NULL,
'{"extractFields":["orderId","lineName","departDate","returnDate","days","totalPrice","paymentStatus"],"model":"gpt-4o-mini"}', 'sale', 'gpt-4o-mini', NULL, NOW()),
(@wf_id, 'ASK_DEPART_DATE',    '确认出发日期',           2, 19,  'JUDGE_URGENT',
'恭喜您下单成功！🎉 您的{线路名}已经预定好了~方便确认一下您计划的出发日期吗？我这边提前帮您安排好所有出行准备 😊', NULL, '{"sendChannel":"im"}', 'sale', 'gpt-4o-mini', NULL, NOW());

-- ============================================================================
-- 第五阶段：出行前关怀 — 分支：急(<5天) vs 缓(>5天) → 汇合→注意事项
-- ============================================================================
INSERT IGNORE INTO company_workflow_lobster_node
(workflow_id, node_code, node_name, node_type, sort_no, next_node_code, message_template, condition_expr, node_config, scene_code, model_name, send_time, create_time)
VALUES
(@wf_id, 'JUDGE_URGENT',       '判断：出发<5天？',        3, 20,  'CARE_URGENT', NULL,
'{"branches":[{"nextNode":"CARE_URGENT","condition":"${departureDays} <= 5","desc":"紧急(<5天)"},{"nextNode":"WAIT_DAILY","condition":"${departureDays} > 5","desc":"宽裕(>5天)"}]}',
'{"varCalc":"departureDays = daysUntil(departDate)"}', 'sale', NULL, NULL, NOW()),

-- 紧急(<5天)：直接发关怀+注意事项
(@wf_id, 'CARE_URGENT',        '紧急关怀消息',           2, 21,  'CARE_NOTES',
'亲爱的{客户名}~您的{线路名}过几天就要出发了，有点激动吧！😄
这几天请注意身体，同时我帮您整理了一份出行清单：
✅ 证件：身份证/护照 请确认在有效期内
✅ 防护：防晒霜、驱蚊液、常用药
有任何疑问随时找我，我 24h 在线~ 🫶', NULL, '{"sendChannel":"im"}', 'sale', 'gpt-4o-mini', NULL, NOW()),

-- 宽裕(>5天)：每日差异化关怀循环
(@wf_id, 'WAIT_DAILY',         '等待：每日一次',          4, 22,  'CARE_DAILY', NULL, NULL,
'{"waitType":"daily","intervalHours":24,"maxLoops":60}', NULL, NULL, '08:00:00', NOW()),
(@wf_id, 'CARE_DAILY',         '每日差异化关怀',          2, 23,  'CHECK_URGENT_LOOP',
'{{每日AI动态生成不同主题的出行预热内容，从10个主题轮换}}', NULL,
'{"aiGenerate":true,"promptCode":"daily_care","maxTurn":60,"diffStrategy":"rotate","themes":["旅行憧憬","拍照攻略","当地美食","行李箱清单","健康提醒","防晒护肤","必备APP","路线预习","亲子互动","心情准备"]}',
'sale', 'gpt-4o-mini', '08:00:00', NOW()),
(@wf_id, 'CHECK_URGENT_LOOP', '回到紧急判断',            3, 24,  'CARE_URGENT', NULL,
'{"branches":[{"nextNode":"CARE_URGENT","condition":"${departureDays} <= 5","desc":"进入5天内"},{"nextNode":"WAIT_DAILY","condition":"${departureDays} > 5","desc":"继续每日关怀"}]}',
'{"varCalc":"departureDays = daysUntil(departDate)"}', 'sale', NULL, NULL, NOW()),

-- 汇合节点：出行前注意事项
(@wf_id, 'CARE_NOTES',         '出行前注意事项',         2, 25,  'WAIT_DEPART',
'出发前最后给您几个贴心小提醒 ✨
🌤 目的地天气：请关注{目的地的天气预报}
👕 穿衣建议：{根据天气+年龄段给出建议}
⚠️ 特别提醒：{根据身体情况给出个性化提醒}
📞 紧急联系：{当地紧急电话+客服热线}
祝您一路顺风！出发那天我再问候您~', NULL, '{"sendChannel":"im","queryWeatherApi":true}', 'sale', 'gpt-4o-mini', NULL, NOW());

-- ============================================================================
-- 第六阶段：旅途中
-- ============================================================================
INSERT IGNORE INTO company_workflow_lobster_node
(workflow_id, node_code, node_name, node_type, sort_no, next_node_code, message_template, condition_expr, node_config, scene_code, model_name, send_time, create_time)
VALUES
(@wf_id, 'WAIT_DEPART',        '等待：到出发日',          4, 26,  'GREET_DEPART', NULL, NULL,
'{"waitType":"tillDate","dateField":"departDate","sendTime":"08:00:00"}', NULL, NULL, '08:00:00', NOW()),
(@wf_id, 'GREET_DEPART',       '出发日祝福',              2, 27,  'WAIT_MID_TRIP',
'亲爱的{客户名}~今天出发啦！🎊 祝您旅途愉快，一路平安！有任何突发情况或者需要帮助，随时打我电话{客服电话}，为您24h待命！📱', NULL, '{"sendChannel":"im"}', 'sale', 'gpt-4o-mini', NULL, NOW()),
(@wf_id, 'WAIT_MID_TRIP',      '等待：旅途中',            4, 28,  'CARE_MID_TRIP', NULL, NULL,
'{"waitType":"fixed","intervalHours":48}', NULL, NULL, NULL, NOW()),
(@wf_id, 'CARE_MID_TRIP',      '旅途中关怀',              2, 29,  'WAIT_RETURN',
'嗨~玩得还好吗？这几天有遇到什么问题需要帮忙不？无论是改签、酒店沟通还是当地攻略，都可以找我哈~希望你玩得开心！😊', NULL, '{"sendChannel":"im"}', 'sale', 'gpt-4o-mini', NULL, NOW());

-- ============================================================================
-- 第七阶段：旅行结束回访
-- ============================================================================
INSERT IGNORE INTO company_workflow_lobster_node
(workflow_id, node_code, node_name, node_type, sort_no, next_node_code, message_template, condition_expr, node_config, scene_code, model_name, send_time, create_time)
VALUES
(@wf_id, 'WAIT_RETURN',        '等待：旅行结束日',        4, 30,  'GREET_RETURN', NULL, NULL,
'{"waitType":"tillDate","dateField":"returnDate","sendTime":"18:00:00"}', NULL, NULL, '18:00:00', NOW()),
(@wf_id, 'GREET_RETURN',       '欢迎回家',                2, 31,  'SURVEY',
'亲爱的{客户名}~欢迎回家！😊 旅行结束啦，不知道这次的体验怎么样？想听听您的真实感受~', NULL, '{"sendChannel":"im"}', 'sale', 'gpt-4o-mini', NULL, NOW()),
(@wf_id, 'SURVEY',             '满意度调研',              2, 32,  'TAG_SATISFY',
'真诚邀请您花1分钟给我们打个分 🌟
✅ 线路安排满意度(1-5星)
✅ 酒店体验满意度(1-5星)
✅ 客服服务满意度(1-5星)
✅ 会不会推荐给朋友
✅ 下次想去哪里旅行
您的反馈会让我们变得更好！', NULL, '{"sendChannel":"im"}', 'sale', 'gpt-4o-mini', NULL, NOW()),
(@wf_id, 'TAG_SATISFY',        '打标签：满意度',         41, 33,  'WAIT_MONTHLY', NULL, NULL,
'{"extractFields":["satisfactionScore","lineRating","hotelRating","serviceRating","wouldRecommend","nextDestination","feedback"],"model":"gpt-4o-mini"}', 'sale', 'gpt-4o-mini', NULL, NOW());

-- ============================================================================
-- 第八阶段：长期运营 — 月度→节日→大节前30天→营销→有需求回到推荐
-- ============================================================================
INSERT IGNORE INTO company_workflow_lobster_node
(workflow_id, node_code, node_name, node_type, sort_no, next_node_code, message_template, condition_expr, node_config, scene_code, model_name, send_time, create_time)
VALUES
(@wf_id, 'WAIT_MONTHLY',       '等待：每月触发',          4, 34,  'GREET_MONTHLY', NULL, NULL,
'{"waitType":"monthly","dayOfMonth":1,"sendTime":"09:00:00","maxLoops":120}', NULL, NULL, '09:00:00', NOW()),
(@wf_id, 'GREET_MONTHLY',      '月度问候',                2, 35,  'WAIT_HOLIDAY',
'{月度温馨关怀，含当月旅行推荐/游记分享/健康提醒}', NULL,
'{"aiGenerate":true,"promptCode":"monthly_greeting","includeTravelTips":true}', 'sale', 'gpt-4o-mini', NULL, NOW()),
(@wf_id, 'WAIT_HOLIDAY',       '等待：节假日触发',        4, 36,  'GREET_HOLIDAY', NULL, NULL,
'{"waitType":"holiday","holidayList":"[\"春节\",\"元宵节\",\"清明节\",\"劳动节\",\"端午节\",\"中秋节\",\"国庆节\",\"元旦\"]","sendTime":"09:00:00"}', NULL, NULL, '09:00:00', NOW()),
(@wf_id, 'GREET_HOLIDAY',      '节日祝福+优惠',           2, 37,  'WAIT_PRE_BIG_HOLIDAY',
'亲~今天是{节日名称}呀！祝您和家人节日快乐，阖家幸福！🎉 {AI动态生成个性化节日祝福}', NULL,
'{"aiGenerate":true,"promptCode":"holiday_blessing","plusCoupon":true}', 'sale', 'gpt-4o-mini', NULL, NOW()),
(@wf_id, 'WAIT_PRE_BIG_HOLIDAY','等待：大节前30天',       4, 38,  'MARKETING_PUSH', NULL, NULL,
'{"waitType":"daysBeforeHoliday","daysBefore":30,"majorHolidays":"[\"春节\",\"清明节\",\"劳动节\",\"端午节\",\"中秋节\",\"国庆节\",\"元旦\"]","sendTime":"10:00:00"}', NULL, NULL, '10:00:00', NOW()),
(@wf_id, 'MARKETING_PUSH',     '营销触达',               2, 39,  'JUDGE_NEW_NEED',
'亲~{大节名称}快到了，有没有计划趁着假期出去玩一玩啊？🚗
我这边刚上线了几条限量的{节日特惠/季节限定线路}，都是老客户优先预定的~
要不要看看有没有感兴趣的？提前订肯定比到时候便宜！🔥', NULL, '{"sendChannel":"im"}', 'sale', 'gpt-4o-mini', NULL, NOW()),
(@wf_id, 'JUDGE_NEW_NEED',     '判断：有新需求？',       3, 40,  'LOOP_BACK', NULL,
'{"branches":[{"nextNode":"LOOP_BACK","condition":"客户表达旅行需求、询问线路、感兴趣","desc":"有新需求→回到推荐流程"},{"nextNode":"WAIT_MONTHLY","condition":"暂时没有或回复否定","desc":"无需求→继续月度循环"}]}',
'{"timeoutSec":86400}', 'sale', NULL, NULL, NOW()),
(@wf_id, 'LOOP_BACK',          '重新激活销冠推荐',        2, 41,  'TAG_PROFILE',
'太好了亲！这次有什么新想法吗？最近又上了不少好线路~您这次想去哪方面的？时间预算有什么新变化没？我马上帮您安排 💪', NULL, '{"sendChannel":"im"}', 'sale', 'gpt-4o-mini', NULL, NOW());

-- ============================================================================
-- 结束节点
-- ============================================================================
INSERT IGNORE INTO company_workflow_lobster_node
(workflow_id, node_code, node_name, node_type, sort_no, next_node_code, message_template, condition_expr, node_config, scene_code, model_name, send_time, create_time)
VALUES
(@wf_id, 'END',                '结束（仅转人工时退出）',   5, 99,  NULL, NULL, NULL, NULL, NULL, NULL, NULL, NOW());
