#!/usr/bin/env python3
"""
直接调用API检查返回的数据结构
"""
import requests
import json

def test_api():
    """测试API返回的数据"""
    try:
        # 测试API端点
        url = "http://localhost:8000/api/v1/products-master/products"
        params = {
            "procurement_method": "LA",
            "page": 1,
            "page_size": 3
        }
        
        print("🔍 调用API:", url)
        print("📋 参数:", params)
        
        response = requests.get(url, params=params)
        print(f"📊 HTTP状态码: {response.status_code}")
        
        if response.status_code == 200:
            data = response.json()
            print("✅ API调用成功!")
            print("\n=== API响应结构 ===")
            print("响应顶层keys:", list(data.keys()))
            
            if data.get('success') and data.get('data') and data.get('data', {}).get('items'):
                items = data['data']['items']
                print(f"\n✅ 找到 {len(items)} 个产品项目")
                
                # 显示第一个产品的完整结构
                if items:
                    first_item = items[0]
                    print(f"\n=== 第一个产品的所有字段 ===")
                    for key, value in first_item.items():
                        if isinstance(value, str) and len(value) > 60:
                            value = value[:60] + "..."
                        print(f"{key}: {repr(value)}")
                    
                    # 查找Tods产品
                    tods_item = None
                    for item in items:
                        if 'Tods' in str(item.get('product_name', '')):
                            tods_item = item
                            break
                    
                    if tods_item:
                        print(f"\n=== 找到Tods产品 ===")
                        print(f"product_name: {repr(tods_item.get('product_name'))}")
                        print(f"brand: {repr(tods_item.get('brand'))}")
                        print(f"procurement_method: {repr(tods_item.get('procurement_method'))}")
                        
                        # 检查所有可能的字段
                        for key, value in tods_item.items():
                            if 'brand' in key.lower() or 'code' in key.lower() or 'attr' in key.lower():
                                print(f"{key}: {repr(value)}")
                    else:
                        print("\n❌ 当前页没有Tods产品")
            else:
                print("❌ API响应格式不正确")
                print("完整响应:", json.dumps(data, ensure_ascii=False, indent=2))
        else:
            print(f"❌ API调用失败: {response.status_code}")
            print("响应内容:", response.text)
            
    except Exception as e:
        print(f"❌ 调用API时出错: {e}")

if __name__ == "__main__":
    test_api()