query_buyer_dict.py 4.46 KB
#!/usr/bin/env python3
"""查询客户类型字典的准确含义"""

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

def query_buyer_type_dict():
    """查询客户类型字典"""
    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("=" * 100)
            print("查询客户类型相关的字典")
            print("=" * 100)

            # 1. 查找所有与客户类型相关的字典类别
            print("\n【1. 查找客户类型相关的字典类别】")
            sql = """
            SELECT code, name
            FROM pub_dict_category
            WHERE code LIKE '%buyer%'
               OR code LIKE '%customer%'
               OR code LIKE '%nature%'
               OR code LIKE '%company%'
               OR code LIKE '%cooperate%'
            ORDER BY code
            """
            cursor.execute(sql)
            categories = cursor.fetchall()

            if categories:
                print(f"\n找到 {len(categories)} 个相关字典类别:")
                for cat in categories:
                    print(f"  - {cat[0]:<30} {cat[1]}")
            else:
                print("\n未找到完全匹配的字典,显示所有类别:")
                sql = "SELECT code, name FROM pub_dict_category ORDER BY sort_order LIMIT 50"
                cursor.execute(sql)
                all_cats = cursor.fetchall()
                for cat in all_cats:
                    print(f"  - {cat[0]:<30} {cat[1]}")

            # 2. 针对可能相关的字典类别查询具体值
            print("\n" + "=" * 100)
            print("【2. 查询可能相关的字典值】")

            possible_codes = [
                'supplier_nature',      # 公司性质
                'supplier_type',        # 供应商类别
                'customer_type',        # 客户类型
                'buyer_from',           # 买家来源
                'cooperate_type'        # 合作类型
            ]

            for dict_code in possible_codes:
                print(f"\n--- 字典: {dict_code} ---")
                
                # 查询该类别下的所有条目
                sql = """
                SELECT i.id, i.code, i.name, i.sort_order, 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 = %s
                ORDER BY i.sort_order, i.id
                """
                cursor.execute(sql, (dict_code,))
                items = cursor.fetchall()

                if items:
                    print(f"{'code':<15} {'name':<30} {'sort':<10} {'lang':<10} {'翻译名称'}")
                    print("-" * 100)
                    for item in items:
                        code = item[1] if item[1] else 'NULL'
                        name = item[2] if item[2] else 'NULL'
                        sort = item[3] if item[3] else 'NULL'
                        lang = item[4] if item[4] else 'NULL'
                        lang_name = item[5] if item[5] else ''
                        
                        # 只显示主要语言和翻译
                        if lang in ['zh', 'en', 'ru', None]:
                            print(f"{code:<15} {name:<30} {sort:<10} {lang if lang else 'NULL':<10} {lang_name}")
                else:
                    print("  (该字典不存在或无数据)")

            # 3. 查询所有字典类别
            print("\n" + "=" * 100)
            print("【3. 所有可用的字典类别(前50个)】")
            sql = """
            SELECT code, name
            FROM pub_dict_category
            ORDER BY sort_order
            LIMIT 50
            """
            cursor.execute(sql)
            all_cats = cursor.fetchall()

            print(f"{'代码':<30} {'名称'}")
            print("-" * 60)
            for cat in all_cats:
                print(f"{cat[0]:<30} {cat[1]}")

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

if __name__ == "__main__":
    query_buyer_type_dict()