verify_buyer_nature_source.py 7.36 KB
#!/usr/bin/env python3
"""验证 buyer_nature 字典信息的来源"""

import os
import pymysql
import sys
sys.path.append(os.path.dirname(os.path.abspath(__file__)))
from config.offline_config import DB_CONFIG

def verify_buyer_nature_source():
    """验证 buyer_nature 字典来源"""
    try:
        connection = pymysql.connect(
            host=DB_CONFIG['host'],
            port=int(DB_CONFIG['port']),
            user=DB_CONFIG['username'],
            password=DB_CONFIG['password'],
            database=DB_CONFIG['database'],
            charset='utf8mb4'
        )

        with connection.cursor() as cursor:
            print("=" * 120)
            print(" 验证 buyer_nature 字典信息的来源")
            print("=" * 120)
            
            # 1. 查询 pub_dict_category 表,确认 buyer_nature 类别存在
            print("\n【步骤1】查询 pub_dict_category 表,确认 buyer_nature 字典类别")
            print("=" * 120)
            
            sql = """
            SELECT code, name, id
            FROM pub_dict_category
            WHERE code = 'buyer_nature'
            """
            cursor.execute(sql)
            category_result = cursor.fetchall()
            
            if category_result:
                for row in category_result:
                    print(f"\n✓ 找到字典类别:")
                    print(f"  - ID: {row[2]}")
                    print(f"  - code: {row[0]}")
                    print(f"  - name: {row[1]}")
            else:
                print("\n✗ 未找到 buyer_nature 字典类别")
                return

            # 2. 查询 pub_dict_item 表,获取 buyer_nature 的所有字典项
            print("\n" + "=" * 120)
            print("【步骤2】查询 pub_dict_item 表,获取 buyer_nature 的所有字典项")
            print("=" * 120)
            
            sql = """
            SELECT id, code, name, sort_order, is_enable, is_delete
            FROM pub_dict_item
            WHERE category_code = 'buyer_nature'
            ORDER BY sort_order, code
            """
            cursor.execute(sql)
            items = cursor.fetchall()
            
            if items:
                print(f"\n✓ 找到 {len(items)} 个字典项:\n")
                print(f"{'ID':<10} {'code':<10} {'name':<40} {'sort_order':<15} {'is_enable':<10} {'is_delete':<10}")
                print("-" * 120)
                
                for item in items:
                    item_id = item[0]
                    code = str(item[1]) if item[1] else 'NULL'
                    name = item[2] if item[2] else 'NULL'
                    sort_order = item[3] if item[3] else 'NULL'
                    is_enable = item[4] if item[4] is not None else 'NULL'
                    is_delete = item[5] if item[5] is not None else 'NULL'
                    
                    print(f"{item_id:<10} {code:<10} {name:<40} {sort_order:<15} {is_enable:<10} {is_delete:<10}")
            else:
                print("\n✗ 未找到字典项")

            # 3. 查询 pub_dict_item_lang 表,获取多语言翻译
            print("\n" + "=" * 120)
            print("【步骤3】查询 pub_dict_item_lang 表,获取多语言翻译")
            print("=" * 120)
            
            sql = """
            SELECT i.id, i.code, i.name as zh_name,
                   il.lang, il.name as lang_name
            FROM pub_dict_item i
            LEFT JOIN pub_dict_item_lang il ON i.id = il.dict_item_id
            WHERE i.category_code = 'buyer_nature'
            ORDER BY i.sort_order, i.code, il.lang
            """
            cursor.execute(sql)
            lang_items = cursor.fetchall()
            
            if lang_items:
                print(f"\n✓ 找到 {len(lang_items)} 条翻译记录:\n")
                print(f"{'ID':<8} {'code':<8} {'中文名称':<25} {'lang':<8} {'翻译名称'}")
                print("-" * 120)
                
                current_code = None
                for item in lang_items:
                    item_id = item[0]
                    code = str(item[1]) if item[1] else 'NULL'
                    zh_name = item[2] if item[2] else 'NULL'
                    lang = item[3] if item[3] else 'NULL'
                    lang_name = item[4] if item[4] else ''
                    
                    if code != current_code:
                        if current_code is not None:
                            print()  # 空行分隔
                        current_code = code
                    
                    lang_display = f"{lang}:" if lang else ''
                    print(f"{item_id:<8} {code:<8} {zh_name:<25} {lang_display:<8} {lang_name}")
            else:
                print("\n✗ 未找到翻译记录")

            # 4. 总结信息来源
            print("\n" + "=" * 120)
            print("【总结】buyer_nature 字典信息的来源")
            print("=" * 120)
            
            print("""
这些信息来源于数据库中的字典表系统:

1️⃣ pub_dict_category(字典类别表)
   - 存储字典类别定义
   - buyer_nature 是其中一个类别,表示"公司性质"

2️⃣ pub_dict_item(字典项表)
   - 存储具体的字典项
   - 包含字段:id, category_code, code, name, sort_order 等
   - buyer_nature 的字典项包括:
     • code=1, name="贸易公司"
     • code=2, name="零售商"
     • code=3, name="批发商"
     • code=4, name="分销商"
     • code=5, name="代理商"
     • code=6, name="自有品牌"
     • code=7, name="其他"

3️⃣ pub_dict_item_lang(字典项多语言表)
   - 存储字典项的多语言翻译
   - 包含字段:dict_item_id, lang, name 等
   - 支持 zh(中文), en(英文), ru(俄语) 等多种语言

💡 数据库结构:
   pub_dict_category (1) ←→ (N) pub_dict_item (1) ←→ (N) pub_dict_item_lang
          ↓                                ↓                        ↓
    字典类别定义                      具体字典项                多语言翻译

⚠️ 重要发现:
   - buyer_nature 字典在数据库中已定义
   - 但 buy_buyer 表中并没有使用这个字段
   - 这是一个"已定义但未使用"的字典
            """)

            # 5. 验证表结构
            print("\n" + "=" * 120)
            print("【验证】显示相关表结构")
            print("=" * 120)
            
            print("\n1️⃣ pub_dict_category 表结构(关键字段):")
            cursor.execute("DESCRIBE pub_dict_category")
            cols = cursor.fetchall()
            for col in cols[:10]:
                print(f"   - {col[0]:<20} {col[1]:<20}")
            
            print("\n2️⃣ pub_dict_item 表结构(关键字段):")
            cursor.execute("DESCRIBE pub_dict_item")
            cols = cursor.fetchall()
            for col in cols[:15]:
                print(f"   - {col[0]:<30} {col[1]:<20}")
            
            print("\n3️⃣ pub_dict_item_lang 表结构(关键字段):")
            cursor.execute("DESCRIBE pub_dict_item_lang")
            cols = cursor.fetchall()
            for col in cols[:10]:
                print(f"   - {col[0]:<30} {col[1]:<20}")

    except Exception as e:
        print(f"查询失败: {e}")
        import traceback
        traceback.print_exc()
    finally:
        if 'connection' in locals():
            connection.close()

if __name__ == "__main__":
    verify_buyer_nature_source()