-- CRM商机测试数据生成脚本
-- 使用方法: docker exec -i hzhub-mysql mysql -uroot -phzhub123 hzhub < test_opportunity_data.sql

-- ========================================
-- 1. 先创建经销商测试数据（如果需要）
-- ========================================

-- 使用INSERT IGNORE避免重复插入
INSERT IGNORE INTO crm_dealer (dealer_id, tenant_id, dealer_name, dealer_code, contact_name, mobile, province, city, level, lifecycle, owner_user_id, create_by, create_time, del_flag)
VALUES
(1001, '000000', '华东商贸有限公司', 'DL001', '张经理', '13800001001', '上海', '上海', 'A', 'active', 1, 1, NOW(), 0),
(1002, '000000', '华南供应链集团', 'DL002', '李总监', '13800001002', '广东', '广州', 'A', 'active', 1, 1, NOW(), 0),
(1003, '000000', '北方商贸股份', 'DL003', '王总', '13800001003', '北京', '北京', 'B', 'active', 2, 1, NOW(), 0),
(1004, '000000', '西南贸易公司', 'DL004', '陈经理', '13800001004', '四川', '成都', 'B', 'active', 2, 1, NOW(), 0),
(1005, '000000', '东北经销中心', 'DL005', '刘主管', '13800001005', '辽宁', '沈阳', 'C', 'active', 3, 1, NOW(), 0),
(1006, '000000', '中部物流商贸', 'DL006', '赵经理', '13800001006', '湖北', '武汉', 'B', 'active', 1, 1, NOW(), 0),
(1007, '000000', '沿海贸易集团', 'DL007', '孙总', '13800001007', '浙江', '杭州', 'A', 'active', 2, 1, NOW(), 0),
(1008, '000000', '西部商贸公司', 'DL008', '周经理', '13800001008', '陕西', '西安', 'C', 'active', 3, 1, NOW(), 0),
(1009, '000000', '南方快销商贸', 'DL009', '吴主管', '13800001009', '福建', '厦门', 'B', 'active', 1, 1, NOW(), 0),
(1010, '000000', '京津冀经销站', 'DL010', '郑经理', '13800001010', '河北', '石家庄', 'C', 'active', 2, 1, NOW(), 0);

-- ========================================
-- 2. 创建50条商机测试数据
-- ========================================

-- lead阶段 - 15条（线索阶段，新商机）
INSERT IGNORE INTO crm_opportunity (opportunity_id, tenant_id, dealer_id, opportunity_name, stage, amount, probability, expected_close_date, owner_user_id, product_name, description, status, create_by, create_time, update_by, update_time, del_flag)
VALUES
(2001, '000000', 1001, '首批进货合作意向', 'lead', 50000.00, 10, DATE_ADD(NOW(), INTERVAL 90 DAY), 1, '智能货架系统', '客户对智能货架产品有兴趣，初步接触阶段', 'active', 1, DATE_ADD(NOW(), INTERVAL -5 DAY), 1, NOW(), 0),
(2002, '000000', 1002, '季度采购计划', 'lead', 120000.00, 10, DATE_ADD(NOW(), INTERVAL 85 DAY), 2, '仓储管理软件', '大型经销商季度采购需求', 'active', 1, DATE_ADD(NOW(), INTERVAL -3 DAY), 1, NOW(), 0),
(2003, '000000', 1003, '新品推广合作', 'lead', 35000.00, 10, DATE_ADD(NOW(), INTERVAL 80 DAY), 1, '营销推广服务', '新品上市推广合作意向', 'active', 2, NOW(), 2, NOW(), 0),
(2004, '000000', 1004, '门店升级项目', 'lead', 85000.00, 10, DATE_ADD(NOW(), INTERVAL 75 DAY), 2, '门店装修方案', '门店形象升级需求', 'active', 2, DATE_ADD(NOW(), INTERVAL -2 DAY), 2, NOW(), 0),
(2005, '000000', 1005, '区域代理申请', 'lead', 20000.00, 10, DATE_ADD(NOW(), INTERVAL 70 DAY), 3, '区域代理权', '申请区域代理资格', 'active', 3, NOW(), 3, NOW(), 0),
(2006, '000000', 1006, '物流系统对接', 'lead', 150000.00, 10, DATE_ADD(NOW(), INTERVAL 95 DAY), 1, '物流追踪系统', '物流管理系统对接需求', 'active', 1, DATE_ADD(NOW(), INTERVAL -7 DAY), 1, NOW(), 0),
(2007, '000000', 1007, '年度框架协议', 'lead', 300000.00, 10, DATE_ADD(NOW(), INTERVAL 100 DAY), 2, '综合服务包', '年度合作协议框架', 'active', 2, DATE_ADD(NOW(), INTERVAL -4 DAY), 2, NOW(), 0),
(2008, '000000', 1008, '本地化服务需求', 'lead', 45000.00, 10, DATE_ADD(NOW(), INTERVAL 65 DAY), 3, '本地运营支持', '本地化运营服务需求', 'active', 3, NOW(), 3, NOW(), 0),
(2009, '000000', 1009, '电商渠道拓展', 'lead', 75000.00, 10, DATE_ADD(NOW(), INTERVAL 60 DAY), 1, '电商平台对接', '电商销售渠道拓展', 'active', 1, DATE_ADD(NOW(), INTERVAL -1 DAY), 1, NOW(), 0),
(2010, '000000', 1010, '冷链仓储合作', 'lead', 180000.00, 10, DATE_ADD(NOW(), INTERVAL 88 DAY), 2, '冷链解决方案', '冷链仓储需求', 'active', 2, NOW(), 2, NOW(), 0),
(2011, '000000', 1001, '二次采购意向', 'lead', 60000.00, 10, DATE_ADD(NOW(), INTERVAL 92 DAY), 1, '货架配件', '已有合作客户追加采购', 'active', 1, NOW(), 1, NOW(), 0),
(2012, '000000', 1003, '促销活动支持', 'lead', 25000.00, 10, DATE_ADD(NOW(), INTERVAL 55 DAY), 2, '促销策划服务', '促销活动策划需求', 'active', 2, DATE_ADD(NOW(), INTERVAL -6 DAY), 2, NOW(), 0),
(2013, '000000', 1005, '培训服务需求', 'lead', 15000.00, 10, DATE_ADD(NOW(), INTERVAL 50 DAY), 3, '员工培训课程', '员工技能培训需求', 'active', 3, NOW(), 3, NOW(), 0),
(2014, '000000', 1007, '数据分析服务', 'lead', 40000.00, 10, DATE_ADD(NOW(), INTERVAL 45 DAY), 1, 'BI数据分析', '销售数据分析服务', 'active', 1, DATE_ADD(NOW(), INTERVAL -8 DAY), 1, NOW(), 0),
(2015, '000000', 1009, '客户管理系统', 'lead', 95000.00, 10, DATE_ADD(NOW(), INTERVAL 58 DAY), 2, 'CRM系统定制', '客户管理需求', 'active', 2, NOW(), 2, NOW(), 0);

-- negotiation阶段 - 15条（谈判中）
INSERT IGNORE INTO crm_opportunity (opportunity_id, tenant_id, dealer_id, opportunity_name, stage, amount, probability, expected_close_date, owner_user_id, product_name, description, status, create_by, create_time, update_by, update_time, del_flag)
VALUES
(2016, '000000', 1001, '大批量采购谈判', 'negotiation', 280000.00, 30, DATE_ADD(NOW(), INTERVAL 60 DAY), 1, '智能货架批量', '批量采购价格谈判', 'active', 1, DATE_ADD(NOW(), INTERVAL -20 DAY), 1, NOW(), 0),
(2017, '000000', 1002, '系统集成方案', 'negotiation', 450000.00, 30, DATE_ADD(NOW(), INTERVAL 55 DAY), 2, '全系统集成', '系统集成方案谈判', 'active', 2, DATE_ADD(NOW(), INTERVAL -18 DAY), 2, NOW(), 0),
(2018, '000000', 1003, '品牌合作推广', 'negotiation', 120000.00, 30, DATE_ADD(NOW(), INTERVAL 50 DAY), 1, '品牌联合推广', '品牌推广合作谈判', 'active', 1, DATE_ADD(NOW(), INTERVAL -15 DAY), 1, NOW(), 0),
(2019, '000000', 1004, '区域扩展计划', 'negotiation', 200000.00, 30, DATE_ADD(NOW(), INTERVAL 48 DAY), 2, '区域扩张服务', '区域扩展方案谈判', 'active', 2, DATE_ADD(NOW(), INTERVAL -12 DAY), 2, NOW(), 0),
(2020, '000000', 1005, '年度供货协议', 'negotiation', 350000.00, 30, DATE_ADD(NOW(), INTERVAL 45 DAY), 3, '年度供货合同', '年度协议谈判', 'active', 3, DATE_ADD(NOW(), INTERVAL -10 DAY), 3, NOW(), 0),
(2021, '000000', 1006, '智慧门店方案', 'negotiation', 180000.00, 30, DATE_ADD(NOW(), INTERVAL 42 DAY), 1, '智慧门店系统', '智慧门店方案谈判', 'active', 1, DATE_ADD(NOW(), INTERVAL -25 DAY), 1, NOW(), 0),
(2022, '000000', 1007, '营销自动化', 'negotiation', 95000.00, 30, DATE_ADD(NOW(), INTERVAL 40 DAY), 2, '营销自动化工具', '营销工具采购谈判', 'active', 2, DATE_ADD(NOW(), INTERVAL -22 DAY), 2, NOW(), 0),
(2023, '000000', 1008, '供应链优化', 'negotiation', 250000.00, 30, DATE_ADD(NOW(), INTERVAL 38 DAY), 3, '供应链优化方案', '供应链优化谈判', 'active', 3, DATE_ADD(NOW(), INTERVAL -19 DAY), 3, NOW(), 0),
(2024, '000000', 1009, '电商运营合作', 'negotiation', 150000.00, 30, DATE_ADD(NOW(), INTERVAL 35 DAY), 1, '电商运营服务', '电商运营合作谈判', 'active', 1, DATE_ADD(NOW(), INTERVAL -16 DAY), 1, NOW(), 0),
(2025, '000000', 1010, '仓储物流升级', 'negotiation', 320000.00, 30, DATE_ADD(NOW(), INTERVAL 33 DAY), 2, '仓储升级方案', '仓储升级谈判', 'active', 2, DATE_ADD(NOW(), INTERVAL -14 DAY), 2, NOW(), 0),
(2026, '000000', 1002, '增值服务采购', 'negotiation', 80000.00, 30, DATE_ADD(NOW(), INTERVAL 30 DAY), 2, '增值服务包', '增值服务采购谈判', 'active', 2, DATE_ADD(NOW(), INTERVAL -28 DAY), 2, NOW(), 0),
(2027, '000000', 1004, '定制开发需求', 'negotiation', 220000.00, 30, DATE_ADD(NOW(), INTERVAL 28 DAY), 1, '定制开发服务', '定制开发方案谈判', 'active', 1, DATE_ADD(NOW(), INTERVAL -21 DAY), 1, NOW(), 0),
(2028, '000000', 1006, '设备租赁合作', 'negotiation', 65000.00, 30, DATE_ADD(NOW(), INTERVAL 26 DAY), 3, '设备租赁方案', '设备租赁谈判', 'active', 3, DATE_ADD(NOW(), INTERVAL -17 DAY), 3, NOW(), 0),
(2029, '000000', 1008, '数据服务合作', 'negotiation', 100000.00, 30, DATE_ADD(NOW(), INTERVAL 24 DAY), 2, '数据分析服务', '数据服务谈判', 'active', 2, DATE_ADD(NOW(), INTERVAL -11 DAY), 2, NOW(), 0),
(2030, '000000', 1010, '技术支持服务', 'negotiation', 55000.00, 30, DATE_ADD(NOW(), INTERVAL 22 DAY), 1, '技术支持包', '技术支持谈判', 'active', 1, DATE_ADD(NOW(), INTERVAL -9 DAY), 1, NOW(), 0);

-- proposal阶段 - 12条（方案阶段）
INSERT IGNORE INTO crm_opportunity (opportunity_id, tenant_id, dealer_id, opportunity_name, stage, amount, probability, expected_close_date, owner_user_id, product_name, description, status, create_by, create_time, update_by, update_time, del_flag)
VALUES
(2031, '000000', 1001, '大型项目方案', 'proposal', 580000.00, 50, DATE_ADD(NOW(), INTERVAL 30 DAY), 1, '全链条解决方案', '已提交方案等待审批', 'active', 1, DATE_ADD(NOW(), INTERVAL -35 DAY), 1, NOW(), 0),
(2032, '000000', 1002, '年度战略合作', 'proposal', 850000.00, 50, DATE_ADD(NOW(), INTERVAL 28 DAY), 2, '战略合作方案', '方案已提交等待确认', 'active', 2, DATE_ADD(NOW(), INTERVAL -32 DAY), 2, NOW(), 0),
(2033, '000000', 1003, '智慧零售方案', 'proposal', 380000.00, 50, DATE_ADD(NOW(), INTERVAL 25 DAY), 1, '智慧零售系统', '方案评审中', 'active', 1, DATE_ADD(NOW(), INTERVAL -30 DAY), 1, NOW(), 0),
(2034, '000000', 1004, '区域代理方案', 'proposal', 250000.00, 50, DATE_ADD(NOW(), INTERVAL 23 DAY), 2, '代理合作方案', '代理方案已提交', 'active', 2, DATE_ADD(NOW(), INTERVAL -27 DAY), 2, NOW(), 0),
(2035, '000000', 1005, '仓储改造方案', 'proposal', 420000.00, 50, DATE_ADD(NOW(), INTERVAL 20 DAY), 3, '仓储改造计划', '改造方案等待审批', 'active', 3, DATE_ADD(NOW(), INTERVAL -25 DAY), 3, NOW(), 0),
(2036, '000000', 1006, '品牌升级方案', 'proposal', 180000.00, 50, DATE_ADD(NOW(), INTERVAL 18 DAY), 1, '品牌升级服务', '品牌方案评审中', 'active', 1, DATE_ADD(NOW(), INTERVAL -40 DAY), 1, NOW(), 0),
(2037, '000000', 1007, '营销整合方案', 'proposal', 320000.00, 50, DATE_ADD(NOW(), INTERVAL 15 DAY), 2, '营销整合服务', '营销方案已提交', 'active', 2, DATE_ADD(NOW(), INTERVAL -38 DAY), 2, NOW(), 0),
(2038, '000000', 1008, '供应链方案', 'proposal', 450000.00, 50, DATE_ADD(NOW(), INTERVAL 12 DAY), 3, '供应链整合', '供应链方案等待审批', 'active', 3, DATE_ADD(NOW(), INTERVAL -36 DAY), 3, NOW(), 0),
(2039, '000000', 1009, '电商平台方案', 'proposal', 280000.00, 50, DATE_ADD(NOW(), INTERVAL 10 DAY), 1, '电商全案', '电商方案评审中', 'active', 1, DATE_ADD(NOW(), INTERVAL -33 DAY), 1, NOW(), 0),
(2040, '000000', 1010, '物流优化方案', 'proposal', 350000.00, 50, DATE_ADD(NOW(), INTERVAL 8 DAY), 2, '物流优化服务', '物流方案已提交', 'active', 2, DATE_ADD(NOW(), INTERVAL -29 DAY), 2, NOW(), 0),
(2041, '000000', 1003, '培训体系建设', 'proposal', 120000.00, 50, DATE_ADD(NOW(), INTERVAL 15 DAY), 1, '培训体系建设', '培训方案等待审批', 'active', 1, DATE_ADD(NOW(), INTERVAL -42 DAY), 1, NOW(), 0),
(2042, '000000', 1007, '客户运营方案', 'proposal', 200000.00, 50, DATE_ADD(NOW(), INTERVAL 12 DAY), 2, '客户运营服务', '运营方案评审中', 'active', 2, DATE_ADD(NOW(), INTERVAL -44 DAY), 2, NOW(), 0);

-- closing阶段 - 8条（赢单阶段，本月和上月分布）
INSERT IGNORE INTO crm_opportunity (opportunity_id, tenant_id, dealer_id, opportunity_name, stage, amount, probability, expected_close_date, owner_user_id, product_name, description, status, create_by, create_time, update_by, update_time, del_flag)
VALUES
(2043, '000000', 1001, '首批进货合同', 'closing', 680000.00, 90, DATE_ADD(NOW(), INTERVAL 5 DAY), 1, '智能货架系统', '合同已签订，准备执行', 'active', 1, DATE_ADD(NOW(), INTERVAL -45 DAY), 1, NOW(), 0),
(2044, '000000', 1002, '年度框架合同', 'closing', 1200000.00, 90, NOW(), 2, '综合服务包', '年度合同已签订', 'active', 2, DATE_ADD(NOW(), INTERVAL -50 DAY), 2, NOW(), 0),
(2045, '000000', 1003, '区域代理合同', 'closing', 350000.00, 90, DATE_ADD(NOW(), INTERVAL -3 DAY), 1, '区域代理权', '代理合同已签订', 'active', 1, DATE_ADD(NOW(), INTERVAL -48 DAY), 1, NOW(), 0),
(2046, '000000', 1004, '仓储改造合同', 'closing', 520000.00, 90, DATE_ADD(NOW(), INTERVAL -7 DAY), 2, '仓储改造计划', '改造合同已签订', 'active', 2, DATE_ADD(NOW(), INTERVAL -52 DAY), 2, NOW(), 0),
-- 上月赢单（用于计算环比）
(2047, '000000', 1005, '上月签约项目A', 'closing', 450000.00, 90, DATE_ADD(NOW(), INTERVAL -35 DAY), 3, '物流方案', '上月赢单案例', 'active', 3, DATE_ADD(NOW(), INTERVAL -55 DAY), 3, NOW(), 0),
(2048, '000000', 1006, '上月签约项目B', 'closing', 280000.00, 90, DATE_ADD(NOW(), INTERVAL -38 DAY), 1, '营销服务', '上月赢单案例', 'active', 1, DATE_ADD(NOW(), INTERVAL -58 DAY), 1, NOW(), 0),
(2049, '000000', 1007, '上月签约项目C', 'closing', 380000.00, 90, DATE_ADD(NOW(), INTERVAL -40 DAY), 2, '数据服务', '上月赢单案例', 'active', 2, DATE_ADD(NOW(), INTERVAL -62 DAY), 2, NOW(), 0),
(2050, '000000', 1008, '上月签约项目D', 'closing', 150000.00, 90, DATE_ADD(NOW(), INTERVAL -42 DAY), 3, '培训服务', '上月赢单案例', 'active', 3, DATE_ADD(NOW(), INTERVAL -65 DAY), 3, NOW(), 0);

-- ========================================
-- 3. 验证数据插入结果
-- ========================================
SELECT '=== 各阶段商机统计 ===' as title;
SELECT
    stage,
    COUNT(*) as count,
    SUM(amount) as total_amount,
    ROUND(AVG(amount), 2) as avg_amount
FROM crm_opportunity
WHERE del_flag = 0
GROUP BY stage
ORDER BY FIELD(stage, 'lead', 'negotiation', 'proposal', 'closing');

SELECT '=== 赢单月度统计 ===' as title;
SELECT
    CASE
        WHEN MONTH(create_time) = MONTH(NOW()) AND YEAR(create_time) = YEAR(NOW()) THEN '本月'
        WHEN MONTH(create_time) = MONTH(DATE_SUB(NOW(), INTERVAL 1 MONTH)) AND YEAR(create_time) = YEAR(DATE_SUB(NOW(), INTERVAL 1 MONTH)) THEN '上月'
        ELSE '其他'
    END as month_period,
    COUNT(*) as win_count,
    SUM(amount) as win_amount
FROM crm_opportunity
WHERE stage = 'closing' AND del_flag = 0
GROUP BY month_period;

SELECT '=== 经销商数据确认 ===' as title;
SELECT dealer_id, dealer_name, dealer_code FROM crm_dealer WHERE del_flag = 0 LIMIT 10;