#!/usr/bin/env python3
"""
测试多采购方式导出功能
"""
import requests
import json
import time

API_BASE = "http://localhost:8000/api/v1"

def test_multi_methods():
    print("=" * 80)
    print("测试多采购方式导出")
    print("=" * 80)
    
    # 准备导出数据 - 模拟前端选择了NY + MC
    export_data = {
        "selected_products": [{
            "product_name": "小王国Polo Ralph Lauren 大童 LOGO马标加绒棉质拉链连帽衫32354",
            "product_code": "32354",
            "brand": "Polo Ralph Lauren",
            "image_url": "",
            "skus": [
                {
                    "color": "DARK SPORT HEATHER[323547626002]",
                    "size": "L[胸围约98CM]",
                    "sales_attr": "",
                    "quantity": 1,
                    "avg_price": 419,
                    "image_url": ""
                }
            ]
        }],
        "procurement_method": "NY",  # 向后兼容
        "procurement_methods": ["NY", "MC"]  # 新增：多个采购方式
    }
    
    print("\n1. 导出PDF（包含NY和MC采购方式）...")
    export_response = requests.post(
        f"{API_BASE}/procurement/v2/export/pdf",
        json=export_data
    )
    
    if export_response.status_code == 200:
        print(f"✓ PDF导出成功")
    else:
        print(f"✗ 导出失败: {export_response.text}")
        return
    
    # 等待更新
    time.sleep(2)
    
    # 检查推送状态
    print("\n2. 检查推送状态...")
    orders_response = requests.get(
        f"{API_BASE}/procurement/orders",
        params={
            "page_size": 100
        }
    )
    
    if orders_response.status_code == 200:
        orders = orders_response.json().get('orders', [])
        
        # 找到测试的订单
        polo_orders = [o for o in orders 
                      if '32354' in o.get('线上宝贝名称', '')
                      and '加绒' in o.get('线上宝贝名称', '')]
        
        # 按采购方式分组
        ny_orders = [o for o in polo_orders if o.get('procurement_method') == 'NY']
        mc_orders = [o for o in polo_orders if o.get('procurement_method') == 'MC']
        
        ny_pushed = sum(1 for o in ny_orders if o.get('已推送') == '是')
        mc_pushed = sum(1 for o in mc_orders if o.get('已推送') == '是')
        
        print(f"\n统计结果：")
        print(f"  NY订单: {len(ny_orders)}条，已推送: {ny_pushed}条")
        print(f"  MC订单: {len(mc_orders)}条，已推送: {mc_pushed}条")
        
        # 验证
        if mc_pushed > 0:
            print("\n✅ 成功！MC订单也被更新了，多采购方式支持生效")
        else:
            print("\n❌ 失败！MC订单没有被更新，多采购方式支持未生效")
    
    print("\n" + "=" * 80)

if __name__ == "__main__":
    test_multi_methods()