#!/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_dict_meanings(): """查询字典表中的代码含义""" 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) # 查看字典表结构 for table in ['pub_dict_item', 'pub_dict_item_lang']: print(f"\n【{table} 表结构】") sql = f"DESCRIBE {table}" cursor.execute(sql) result = cursor.fetchall() for row in result: print(f" {row[0]:<30} {row[1]:<20}") # 查询 pub_dict_category print("\n" + "=" * 100) print("【pub_dict_category 表内容】") sql = "SELECT * FROM pub_dict_category LIMIT 20" cursor.execute(sql) result = cursor.fetchall() cursor.execute("DESCRIBE pub_dict_category") columns = [row[0] for row in cursor.fetchall()] print(f"{' | '.join(columns)}") print("-" * 100) for row in result: print(" | ".join([str(v) if v is not None else 'NULL' for v in row])) # 查询 pub_dict_item 中与客户类型相关的条目 print("\n" + "=" * 100) print("【pub_dict_item 表内容 - 查找客户类型相关的字典】") sql = """ SELECT * FROM pub_dict_item WHERE dict_code LIKE '%customer%' OR dict_code LIKE '%buyer%' OR dict_code LIKE '%type%' OR dict_code LIKE '%nature%' OR dict_code LIKE '%from%' LIMIT 20 """ cursor.execute(sql) result = cursor.fetchall() if result: cursor.execute("DESCRIBE pub_dict_item") columns = [row[0] for row in cursor.fetchall()] print(f"\n找到 {len(result)} 条相关字典") print(f"{' | '.join(columns)}") print("-" * 140) for row in result: print(" | ".join([str(v) if v is not None else 'NULL' for v in row])) else: print("未找到相关字典,显示所有字典类别:") sql = "SELECT DISTINCT dict_code FROM pub_dict_item LIMIT 30" cursor.execute(sql) all_dicts = cursor.fetchall() for d in all_dicts: print(f" - {d[0]}") # 查询 pub_dict_item_lang 获取具体值 print("\n" + "=" * 100) print("【pub_dict_item_lang 表内容 - 客户类型值查询】") # 尝试查询几个常见的字典代码 possible_codes = ['customer_type', 'buyer_from', 'nature_of_company', 'cooperate_type'] for dict_code in possible_codes: print(f"\n--- 查询字典: {dict_code} ---") sql = """ SELECT i.*, il.item_name, il.lang FROM pub_dict_item i LEFT JOIN pub_dict_item_lang il ON i.id = il.dict_item_id WHERE i.dict_code = %s ORDER BY i.item_value """ cursor.execute(sql, (dict_code,)) result = cursor.fetchall() if result: print(f"{'item_value':<15} {'item_code':<20} {'item_name':<30} {'lang':<10}") print("-" * 80) for row in result: item_value = str(row[3]) if row[3] is not None else 'NULL' item_code = str(row[2]) if row[2] is not None else 'NULL' item_name = row[7] if row[7] is not None else 'NULL' lang = row[8] if row[8] is not None else 'NULL' print(f"{item_value:<15} {item_code:<20} {item_name:<30} {lang:<10}") else: print(" (未找到)") # 如果上述都没找到,显示所有字典代码 print("\n" + "=" * 100) print("【所有可用的字典代码】") sql = """ SELECT DISTINCT dict_code FROM pub_dict_item ORDER BY dict_code """ cursor.execute(sql) all_dict_codes = cursor.fetchall() print(f"总共 {len(all_dict_codes)} 个字典:") for i, d in enumerate(all_dict_codes, 1): print(f" {i:3}. {d[0]}") except Exception as e: print(f"查询失败: {e}") import traceback traceback.print_exc() finally: if 'connection' in locals(): connection.close() if __name__ == "__main__": query_dict_meanings()