#!/usr/bin/env python3
"""
测试采购信息是否正确显示
"""
import os
import requests
import json

# 禁用代理
os.environ['NO_PROXY'] = 'localhost,127.0.0.1'
os.environ.pop('http_proxy', None)
os.environ.pop('https_proxy', None)
os.environ.pop('all_proxy', None)


def test_procurement_info():
    """测试采购信息是否正确返回"""

    url = "http://localhost:8000/api/v1/new-shipping/pending-orders"
    headers = {"Content-Type": "application/json"}
    data = {"limit": 5}

    try:
        response = requests.post(url, json=data, headers=headers, timeout=10)

        if response.status_code == 200:
            result = response.json()
            print(f"✅ API请求成功")
            print(f"总订单数: {result.get('total', 0)}")

            if result.get('orders'):
                print("\n订单采购信息展示:")
                print("=" * 60)

                for i, order in enumerate(result['orders'][:5], 1):
                    print(f"\n订单 {i}:")
                    print(f"  子订单编号: {order.get('子订单编号')}")
                    print(f"  商品标题: {order.get('商品标题')[:40]}...")
                    print(f"  网店名称: {order.get('网店名称')}")
                    print(f"  procurement_order_id: {order.get('procurement_order_id')}")
                    print(f"  match_status: {order.get('match_status')}")

                    if order.get('procurement_info'):
                        info = order['procurement_info']
                        print(f"  ✅ 采购信息:")
                        print(f"    品牌: {info.get('品牌', '未知')}")
                        print(f"    颜色: {info.get('颜色', '未知')}")
                        print(f"    尺码: {info.get('尺码', '未知')}")
                        print(f"    货号: {info.get('货号', '未知')}")
                        print(f"    采购方式: {info.get('采购方式', '未知')}")
                        print(f"    图片: {'有' if info.get('图片') else '无'}")
                    else:
                        print(f"  ❌ 无采购信息")

                # 统计
                print("\n" + "=" * 60)
                matched_count = sum(1 for o in result['orders'] if o.get('procurement_order_id'))
                with_info_count = sum(1 for o in result['orders'] if o.get('procurement_info'))

                print(f"统计:")
                print(f"  已匹配采购订单: {matched_count}/{len(result['orders'])}")
                print(f"  有采购信息: {with_info_count}/{len(result['orders'])}")

                if matched_count != with_info_count:
                    print(f"  ⚠️ 注意: 有{matched_count - with_info_count}个订单已匹配但无采购信息")
            else:
                print("⚠️ 没有返回订单数据")

        else:
            print(f"❌ API请求失败，状态码: {response.status_code}")
            print(f"响应内容: {response.text}")

    except requests.exceptions.RequestException as e:
        print(f"❌ 网络请求错误: {e}")
    except json.JSONDecodeError as e:
        print(f"❌ JSON解析错误: {e}")
    except Exception as e:
        print(f"❌ 未知错误: {e}")


if __name__ == "__main__":
    test_procurement_info()