-- =============================================================================
-- 企微IT智能服务台 — 快速规则初始化数据
-- =============================================================================
-- 说明：将硬编码的快速规则迁移到数据库
-- 适用：首次部署 / 从硬编码迁移到数据库
-- 日期：2026-07-27
-- =============================================================================

-- 清空现有数据（仅首次部署时使用）
-- TRUNCATE TABLE quick_rules RESTART IDENTITY;

-- --------------------------------------------------------------------------
-- 1. 打招呼关键词（来自 ai_handler.py）
-- --------------------------------------------------------------------------
INSERT INTO quick_rules (rule_type, keyword, priority, is_active) VALUES
('greeting', '你好', 10, true),
('greeting', '您好', 9, true),
('greeting', 'hi', 8, true),
('greeting', 'hello', 7, true),
('greeting', '嗨', 6, true),
('greeting', '在吗', 5, true),
('greeting', '在不在', 5, true),
('greeting', '哈喽', 4, true),
('greeting', '早', 3, true),
('greeting', '早上好', 3, true),
('greeting', '下午好', 3, true),
('greeting', '晚上好', 3, true)
ON CONFLICT (rule_type, keyword) DO NOTHING;

-- --------------------------------------------------------------------------
-- 2. 业务路由预过滤关键词（来自 routing_service.py）
-- --------------------------------------------------------------------------
INSERT INTO quick_rules (rule_type, category, keyword, priority, is_active) VALUES
-- 行政
('routing_prefilter', '行政', '复印机', 5, true),
('routing_prefilter', '行政', '扫描仪', 5, true),
('routing_prefilter', '行政', '保洁', 5, true),
('routing_prefilter', '行政', '名片印刷', 5, true),
-- IT服务
('routing_prefilter', 'IT服务', '打印机', 10, true),
('routing_prefilter', 'IT服务', '打印', 9, true),
('routing_prefilter', 'IT服务', '电脑', 8, true),
('routing_prefilter', 'IT服务', '网络', 8, true),
('routing_prefilter', 'IT服务', '软件', 7, true),
('routing_prefilter', 'IT服务', '重装', 7, true),
('routing_prefilter', 'IT服务', '装机', 7, true),
('routing_prefilter', 'IT服务', '显示器', 6, true),
('routing_prefilter', 'IT服务', '键盘', 5, true),
('routing_prefilter', 'IT服务', '鼠标', 5, true),
('routing_prefilter', 'IT服务', '耳机', 5, true),
('routing_prefilter', 'IT服务', '投影仪', 5, true),
('routing_prefilter', 'IT服务', '会议设备', 5, true),
-- 人力资源
('routing_prefilter', '人力资源', '工牌', 8, true),
('routing_prefilter', '人力资源', '考勤', 8, true),
('routing_prefilter', '人力资源', '入职', 7, true),
('routing_prefilter', '人力资源', '离职', 7, true),
('routing_prefilter', '人力资源', '社保', 6, true),
('routing_prefilter', '人力资源', '公积金', 6, true),
('routing_prefilter', '人力资源', '云盘', 5, true),
-- 财务
('routing_prefilter', '财务', '报销', 10, true),
('routing_prefilter', '财务', '发票', 9, true),
('routing_prefilter', '财务', '借款', 8, true),
('routing_prefilter', '财务', '工资条', 7, true),
-- 法务
('routing_prefilter', '法务', '合同', 10, true),
('routing_prefilter', '法务', '法务', 10, true),
('routing_prefilter', '法务', '知识产权', 8, true),
-- 行政-物业
('routing_prefilter', '行政-物业', '空调', 7, true),
('routing_prefilter', '行政-物业', '电梯', 7, true),
('routing_prefilter', '行政-物业', '门禁', 7, true),
('routing_prefilter', '行政-物业', '停车', 7, true)
ON CONFLICT (rule_type, keyword) DO NOTHING;

-- --------------------------------------------------------------------------
-- 3. 路由目标配置（来自 routing_service.py ROUTING_TARGETS）
-- --------------------------------------------------------------------------
INSERT INTO quick_rules (rule_type, category, keyword, extra_data, priority, is_active) VALUES
('routing_target', '行政', '机票酒店前台',
 '{"service_name": "机票酒店前台", "description": "受理和咨询机票、酒店、快递、访客、失物招领及行政办公相关事宜", "url": "https://work.weixin.qq.com/nl/innerkfid/ikfCtcYBwAAV00zNJGfmuA0aG1c2qCDnQ"}',
 0, true),
('routing_target', '人力资源', '人力资源共享服务咨询',
 '{"service_name": "人力资源共享服务咨询", "description": "咨询和处理人力资源相关问题（工牌/考勤/入离职/社保公积金等）", "url": "https://work.weixin.qq.com/nl/innerkfid/ikfCtcYBwAAvSRL5i5b_Xia8vCmFc2gRw"}',
 0, true),
('routing_target', '财务', '总部报销服务台',
 '{"service_name": "总部报销服务台", "description": "咨询和处理总部报销系统相关问题", "url": "https://work.weixin.qq.com/nl/innerkfid/ikfCtcYBwAAG4HC1zWJtPuALPKl2X6jcw"}',
 0, true),
('routing_target', '法务', '行政法务团队',
 '{"service_name": "行政法务团队", "description": "可咨询合同、合规、劳动争议、法律案件纠纷、外部检查、其他法律问题", "url": "https://work.weixin.qq.com/nl/innerkfid/ikfCtcYBwAABF746pGAY5WZ0mOTP6kGKA"}',
 0, true),
('routing_target', '行政-物业', '物业服务',
 '{"service_name": "物业服务", "description": "咨询和处理物业相关问题（空调/照明/门禁卡/车位/维修等）", "url": "https://work.weixin.qq.com/nl/innerkfid/ikfCtcYBwAAUtkMyOToCZqe42ZBDupVEQ"}',
 0, true),
('routing_target', 'IT服务', 'IT人工坐席',
 '{"service_name": "IT人工坐席", "description": "IT支持组人工服务（打印机/电脑/网络/软件安装等）", "url": "https://work.weixin.qq.com/nl/innerkfid/ikfCtcYBwAA人工坐席KFID", "is_agent": true}',
 0, true)
ON CONFLICT (rule_type, keyword) DO NOTHING;

-- --------------------------------------------------------------------------
-- 验证数据
-- --------------------------------------------------------------------------
SELECT
    rule_type,
    COUNT(*) as count,
    COUNT(CASE WHEN is_active THEN 1 END) as active_count
FROM quick_rules
GROUP BY rule_type
ORDER BY rule_type;
