#!/usr/bin/env python3
"""
测试修改后的parse_resume_with_qwen函数的字段映射功能
"""

import os
import sys
import logging
from pathlib import Path

# 添加项目根目录到Python路径
project_root = Path(__file__).parent
sys.path.insert(0, str(project_root))

# 配置日志
logging.basicConfig(
    level=logging.INFO,
    format='%(asctime)s - %(levelname)s - %(message)s'
)

def test_field_mapping():
    """测试字段映射功能"""
    try:
        # 导入函数
        from app.core.data_parse.parse_resume import parse_resume_with_qwen
        
        # 测试文件路径
        test_file = "方全.pdf"
        
        # 检查文件是否存在
        if not os.path.exists(test_file):
            print(f"错误: 测试文件 {test_file} 不存在")
            return False
        
        print(f"开始测试字段映射功能...")
        print(f"测试文件: {test_file}")
        print(f"文件大小: {os.path.getsize(test_file)} 字节")
        
        # 调用函数
        result = parse_resume_with_qwen(test_file)
        
        # 输出映射结果
        print("\n=== 字段映射结果 ===")
        print(f"姓名(中文): {result.get('name_zh', 'N/A')}")
        print(f"姓名(英文): {result.get('name_en', 'N/A')}")
        print(f"职位(中文): {result.get('title_zh', 'N/A')}")
        print(f"职位(英文): {result.get('title_en', 'N/A')}")
        print(f"公司(中文): {result.get('hotel_zh', 'N/A')}")
        print(f"公司(英文): {result.get('hotel_en', 'N/A')}")
        print(f"手机号码: {result.get('mobile', 'N/A')}")
        print(f"固定电话: {result.get('phone', 'N/A')}")
        print(f"电子邮箱: {result.get('email', 'N/A')}")
        print(f"地址(中文): {result.get('address_zh', 'N/A')}")
        print(f"地址(英文): {result.get('address_en', 'N/A')}")
        print(f"生日: {result.get('birthday', 'N/A')}")
        print(f"年龄: {result.get('age', 'N/A')}")
        print(f"籍贯: {result.get('native_place', 'N/A')}")
        print(f"居住地: {result.get('residence', 'N/A')}")
        print(f"品牌组合: {result.get('brand_group', 'N/A')}")
        
        # 输出职业轨迹
        career_path = result.get('career_path', [])
        if career_path:
            print(f"\n职业轨迹 (共{len(career_path)}条):")
            for i, career in enumerate(career_path, 1):
                print(f"  {i}. 日期: {career.get('date', 'N/A')}")
                print(f"     公司: {career.get('hotel_zh', 'N/A')} / {career.get('hotel_en', 'N/A')}")
                print(f"     职位: {career.get('title_zh', 'N/A')} / {career.get('title_en', 'N/A')}")
        else:
            print("\n职业轨迹: 无")
        
        # 输出隶属关系
        affiliation = result.get('affiliation', [])
        if affiliation:
            print(f"\n隶属关系 (共{len(affiliation)}条):")
            for i, aff in enumerate(affiliation, 1):
                print(f"  {i}. 公司: {aff.get('company', 'N/A')}")
                print(f"     集团: {aff.get('group', 'N/A')}")
        else:
            print("\n隶属关系: 无")
        
        # 验证字段映射是否正确
        print("\n=== 字段映射验证 ===")
        mapping_verification = {
            'name_zh': '中文姓名',
            'name_en': '英文姓名', 
            'title_zh': '中文头衔',
            'title_en': '英文头衔',
            'hotel_zh': '中文酒店',
            'hotel_en': '英文酒店',
            'mobile': '手机号',
            'email': '邮箱',
            'address_zh': '中文工作地址',
            'address_en': '英文工作地址',
            'birthday': '生日',
            'age': '年龄',
            'native_place': '籍贯',
            'residence': '居住地',
            'brand_group': '品牌组合'
        }
        
        for english_field, chinese_field in mapping_verification.items():
            value = result.get(english_field, '')
            if value:
                print(f"✓ {chinese_field} -> {english_field}: {value}")
            else:
                print(f"✗ {chinese_field} -> {english_field}: 空值")
        
        print("\n=== 测试完成 ===")
        return True
        
    except Exception as e:
        print(f"测试失败: {str(e)}")
        import traceback
        traceback.print_exc()
        return False

if __name__ == "__main__":
    print("=" * 50)
    print("测试字段映射功能")
    print("=" * 50)
    
    # 测试字段映射
    success = test_field_mapping()
    
    print("\n" + "=" * 50)
    print("测试总结:")
    print(f"字段映射测试: {'✓ 成功' if success else '✗ 失败'}")
    print("=" * 50) 