Commit 0f35b7e12b55832a031fd5cf89d5cabba0aa987b
1 parent
d63934f5
tests
Showing
18 changed files
with
2550 additions
and
0 deletions
Show diff stats
| @@ -0,0 +1,157 @@ | @@ -0,0 +1,157 @@ | ||
| 1 | +#!/usr/bin/env python3 | ||
| 2 | +"""总结 buy_buyer 表的客户类型字段情况""" | ||
| 3 | + | ||
| 4 | +import os | ||
| 5 | +import pymysql | ||
| 6 | +import sys | ||
| 7 | +sys.path.append(os.path.dirname(os.path.abspath(__file__))) | ||
| 8 | +from config.offline_config import DB_CONFIG | ||
| 9 | + | ||
| 10 | +def summarize_buyer_type(): | ||
| 11 | + """总结客户类型字段""" | ||
| 12 | + try: | ||
| 13 | + connection = pymysql.connect( | ||
| 14 | + host=DB_CONFIG['host'], | ||
| 15 | + port=int(DB_CONFIG['port']), | ||
| 16 | + user=DB_CONFIG['username'], | ||
| 17 | + password=DB_CONFIG['password'], | ||
| 18 | + database=DB_CONFIG['database'], | ||
| 19 | + charset='utf8mb4' | ||
| 20 | + ) | ||
| 21 | + | ||
| 22 | + with connection.cursor() as cursor: | ||
| 23 | + print("=" * 120) | ||
| 24 | + print(" buy_buyer 表客户类型字段总结") | ||
| 25 | + print("=" * 120) | ||
| 26 | + | ||
| 27 | + print("\n【问题】哪个字段表示客户类型(商超、品牌、批发商等)?") | ||
| 28 | + print("\n【答案】") | ||
| 29 | + | ||
| 30 | + print("\n" + "=" * 120) | ||
| 31 | + print("1. 字典表中定义的客户类型(buyer_nature)") | ||
| 32 | + print("=" * 120) | ||
| 33 | + print("在 pub_dict_item 中有 buyer_nature 字典,包含以下客户类型:") | ||
| 34 | + print(" code | name") | ||
| 35 | + print("-------|----------------------------------") | ||
| 36 | + print(" 1 | 贸易公司") | ||
| 37 | + print(" 2 | 零售商(包含商超)") | ||
| 38 | + print(" 3 | 批发商") | ||
| 39 | + print(" 4 | 分销商") | ||
| 40 | + print(" 5 | 代理商") | ||
| 41 | + print(" 6 | 自有品牌") | ||
| 42 | + print(" 7 | 其他") | ||
| 43 | + | ||
| 44 | + print("\n" + "=" * 120) | ||
| 45 | + print("2. buy_buyer 表实际存在的字段") | ||
| 46 | + print("=" * 120) | ||
| 47 | + print("⚠️ 注意:buy_buyer 表中并没有 buyer_nature 字段!") | ||
| 48 | + print("\n实际存在的相关字段:") | ||
| 49 | + | ||
| 50 | + # customer_type | ||
| 51 | + print("\n 1) customer_type - 客户类型") | ||
| 52 | + sql = """ | ||
| 53 | + SELECT customer_type, COUNT(*) as count | ||
| 54 | + FROM buy_buyer | ||
| 55 | + WHERE is_delete = 0 | ||
| 56 | + GROUP BY customer_type | ||
| 57 | + ORDER BY customer_type | ||
| 58 | + """ | ||
| 59 | + cursor.execute(sql) | ||
| 60 | + result = cursor.fetchall() | ||
| 61 | + | ||
| 62 | + print(" 值分布:") | ||
| 63 | + for row in result: | ||
| 64 | + val = row[0] if row[0] is not None else 'NULL' | ||
| 65 | + count = row[1] | ||
| 66 | + print(f" - {val}: {count:,} 条") | ||
| 67 | + | ||
| 68 | + print(" ⚠️ 数据稀疏,仅5%的买家有值") | ||
| 69 | + | ||
| 70 | + # nature_of_company | ||
| 71 | + print("\n 2) nature_of_company - 公司类型") | ||
| 72 | + sql = """ | ||
| 73 | + SELECT nature_of_company, COUNT(*) as count | ||
| 74 | + FROM buy_buyer | ||
| 75 | + WHERE is_delete = 0 | ||
| 76 | + GROUP BY nature_of_company | ||
| 77 | + ORDER BY nature_of_company | ||
| 78 | + """ | ||
| 79 | + cursor.execute(sql) | ||
| 80 | + result = cursor.fetchall() | ||
| 81 | + | ||
| 82 | + print(" 值分布:") | ||
| 83 | + for row in result: | ||
| 84 | + val = row[0] if row[0] is not None else 'NULL' | ||
| 85 | + count = row[1] | ||
| 86 | + print(f" - {val}: {count:,} 条") | ||
| 87 | + | ||
| 88 | + print(" ⚠️ 仅有51, 52两个值,字典中无对应定义") | ||
| 89 | + | ||
| 90 | + # buyer_from | ||
| 91 | + print("\n 3) buyer_from - 采购商来源(不是客户类型)") | ||
| 92 | + sql = """ | ||
| 93 | + SELECT buyer_from, COUNT(*) as count | ||
| 94 | + FROM buy_buyer | ||
| 95 | + WHERE is_delete = 0 | ||
| 96 | + GROUP BY buyer_from | ||
| 97 | + ORDER BY buyer_from | ||
| 98 | + """ | ||
| 99 | + cursor.execute(sql) | ||
| 100 | + result = cursor.fetchall() | ||
| 101 | + | ||
| 102 | + print(" 值分布:") | ||
| 103 | + for row in result: | ||
| 104 | + val = row[0] if row[0] is not None else 'NULL' | ||
| 105 | + count = row[1] | ||
| 106 | + print(f" - {val}: {count:,} 条") | ||
| 107 | + | ||
| 108 | + print("\n 字典含义:") | ||
| 109 | + print(" - 1: 展会收集") | ||
| 110 | + print(" - 2: 网络收集") | ||
| 111 | + print(" - 3: 渠道搜集") | ||
| 112 | + print(" - 4: 客户介绍") | ||
| 113 | + print(" - 5: 其他/导入") | ||
| 114 | + print(" - 6: 自行注册") | ||
| 115 | + | ||
| 116 | + # cooperate_type | ||
| 117 | + print("\n 4) cooperate_type - 合作模式") | ||
| 118 | + sql = """ | ||
| 119 | + SELECT cooperate_type, COUNT(*) as count | ||
| 120 | + FROM buy_buyer | ||
| 121 | + WHERE is_delete = 0 | ||
| 122 | + GROUP BY cooperate_type | ||
| 123 | + ORDER BY cooperate_type | ||
| 124 | + """ | ||
| 125 | + cursor.execute(sql) | ||
| 126 | + result = cursor.fetchall() | ||
| 127 | + | ||
| 128 | + print(" 值分布:") | ||
| 129 | + for row in result: | ||
| 130 | + val = row[0] if row[0] is not None else 'NULL' | ||
| 131 | + count = row[1] | ||
| 132 | + print(f" - {val}: {count:,} 条") | ||
| 133 | + | ||
| 134 | + print("\n" + "=" * 120) | ||
| 135 | + print("3. 结论与建议") | ||
| 136 | + print("=" * 120) | ||
| 137 | + print("❌ buy_buyer 表目前没有直接存储「商超、品牌、批发商」这类客户类型") | ||
| 138 | + print("\n✅ 可用的替代方案:") | ||
| 139 | + print(" 1. customer_type - 但数据稀疏,仅5%有值") | ||
| 140 | + print(" 2. nature_of_company - 数据较完整(57%),但只有51/52两个值,含义不明") | ||
| 141 | + print(" 3. buyer_from - 采购商来源,不是客户类型") | ||
| 142 | + print(" 4. cooperate_type - 合作模式,不是客户类型") | ||
| 143 | + print("\n💡 建议:") | ||
| 144 | + print(" 1. 如果需要客户类型分类,可能需要基于其他字段(如采购量、采购频率等)推算") | ||
| 145 | + print(" 2. 或者联系业务方确认 customer_type 和 nature_of_company 的真实含义") | ||
| 146 | + print(" 3. 考虑引入 buyer_nature 字段(字典已定义,表中未使用)") | ||
| 147 | + | ||
| 148 | + except Exception as e: | ||
| 149 | + print(f"查询失败: {e}") | ||
| 150 | + import traceback | ||
| 151 | + traceback.print_exc() | ||
| 152 | + finally: | ||
| 153 | + if 'connection' in locals(): | ||
| 154 | + connection.close() | ||
| 155 | + | ||
| 156 | +if __name__ == "__main__": | ||
| 157 | + summarize_buyer_type() |
| @@ -0,0 +1,124 @@ | @@ -0,0 +1,124 @@ | ||
| 1 | +#!/usr/bin/env python3 | ||
| 2 | +"""检查 buy_buyer 表是否有 buyer_nature 字段""" | ||
| 3 | + | ||
| 4 | +import os | ||
| 5 | +import pymysql | ||
| 6 | +import sys | ||
| 7 | +sys.path.append(os.path.dirname(os.path.abspath(__file__))) | ||
| 8 | +from config.offline_config import DB_CONFIG | ||
| 9 | + | ||
| 10 | +def check_buyer_nature(): | ||
| 11 | + """检查 buyer_nature 字段""" | ||
| 12 | + try: | ||
| 13 | + connection = pymysql.connect( | ||
| 14 | + host=DB_CONFIG['host'], | ||
| 15 | + port=int(DB_CONFIG['port']), | ||
| 16 | + user=DB_CONFIG['username'], | ||
| 17 | + password=DB_CONFIG['password'], | ||
| 18 | + database=DB_CONFIG['database'], | ||
| 19 | + charset='utf8mb4' | ||
| 20 | + ) | ||
| 21 | + | ||
| 22 | + with connection.cursor() as cursor: | ||
| 23 | + # 1. 检查表结构中是否有 buyer_nature 字段 | ||
| 24 | + print("=" * 100) | ||
| 25 | + print("【检查 buy_buyer 表是否有 buyer_nature 相关字段】") | ||
| 26 | + print("=" * 100) | ||
| 27 | + | ||
| 28 | + sql = "DESCRIBE buy_buyer" | ||
| 29 | + cursor.execute(sql) | ||
| 30 | + result = cursor.fetchall() | ||
| 31 | + | ||
| 32 | + print("\n包含 'nature' 或 'type' 的字段:") | ||
| 33 | + for row in result: | ||
| 34 | + field_name = row[0] | ||
| 35 | + if 'nature' in field_name.lower() or 'type' in field_name.lower(): | ||
| 36 | + print(f" - {field_name}") | ||
| 37 | + | ||
| 38 | + # 2. 查询是否有以 nature_ 开头的字段 | ||
| 39 | + sql = """ | ||
| 40 | + SELECT id, name, buyer_nature, nature_of_company, customer_type | ||
| 41 | + FROM buy_buyer | ||
| 42 | + WHERE is_delete = 0 | ||
| 43 | + LIMIT 10 | ||
| 44 | + """ | ||
| 45 | + | ||
| 46 | + print("\n" + "=" * 100) | ||
| 47 | + print("【尝试查询 buyer_nature 字段】") | ||
| 48 | + print("=" * 100) | ||
| 49 | + | ||
| 50 | + try: | ||
| 51 | + cursor.execute(sql) | ||
| 52 | + result = cursor.fetchall() | ||
| 53 | + | ||
| 54 | + print(f"{'ID':<10} {'name':<30} {'buyer_nature':<15} {'nature_of_company':<20} {'customer_type':<15}") | ||
| 55 | + print("-" * 100) | ||
| 56 | + | ||
| 57 | + for row in result: | ||
| 58 | + id_val = str(row[0]) | ||
| 59 | + name = row[1][:27] if row[1] else 'NULL' | ||
| 60 | + buyer_nature = str(row[2]) if row[2] is not None else 'NULL' | ||
| 61 | + nature_of_company = str(row[3]) if row[3] is not None else 'NULL' | ||
| 62 | + customer_type = str(row[4]) if row[4] is not None else 'NULL' | ||
| 63 | + | ||
| 64 | + print(f"{id_val:<10} {name:<30} {buyer_nature:<15} {nature_of_company:<20} {customer_type:<15}") | ||
| 65 | + | ||
| 66 | + except Exception as e: | ||
| 67 | + print(f"查询失败: {e}") | ||
| 68 | + | ||
| 69 | + # 3. 如果有 buyer_nature 字段,查询其分布 | ||
| 70 | + print("\n" + "=" * 100) | ||
| 71 | + print("【buyer_nature 字段值分布(如果存在)】") | ||
| 72 | + print("=" * 100) | ||
| 73 | + | ||
| 74 | + sql_distinct = """ | ||
| 75 | + SELECT buyer_nature, COUNT(*) as count | ||
| 76 | + FROM buy_buyer | ||
| 77 | + WHERE is_delete = 0 AND buyer_nature IS NOT NULL | ||
| 78 | + GROUP BY buyer_nature | ||
| 79 | + ORDER BY count DESC | ||
| 80 | + """ | ||
| 81 | + | ||
| 82 | + try: | ||
| 83 | + cursor.execute(sql_distinct) | ||
| 84 | + result = cursor.fetchall() | ||
| 85 | + | ||
| 86 | + if result: | ||
| 87 | + print(f"{'buyer_nature':<15} {'数量':>10}") | ||
| 88 | + print("-" * 30) | ||
| 89 | + | ||
| 90 | + for row in result: | ||
| 91 | + val = str(row[0]) | ||
| 92 | + count = row[1] | ||
| 93 | + print(f"{val:<15} {count:>10,}") | ||
| 94 | + | ||
| 95 | + # 查询示例 | ||
| 96 | + sql_sample = """ | ||
| 97 | + SELECT id, name, countries | ||
| 98 | + FROM buy_buyer | ||
| 99 | + WHERE buyer_nature = %s AND is_delete = 0 | ||
| 100 | + LIMIT 3 | ||
| 101 | + """ | ||
| 102 | + cursor.execute(sql_sample, (row[0],)) | ||
| 103 | + samples = cursor.fetchall() | ||
| 104 | + | ||
| 105 | + if samples: | ||
| 106 | + print(f" 示例:") | ||
| 107 | + for s in samples: | ||
| 108 | + print(f" - [{s[0]}] {s[1][:30]} (国家:{s[2]})") | ||
| 109 | + else: | ||
| 110 | + print(" buyer_nature 字段不存在或全部为NULL") | ||
| 111 | + | ||
| 112 | + except Exception as e: | ||
| 113 | + print(f"查询失败: {e}") | ||
| 114 | + | ||
| 115 | + except Exception as e: | ||
| 116 | + print(f"查询失败: {e}") | ||
| 117 | + import traceback | ||
| 118 | + traceback.print_exc() | ||
| 119 | + finally: | ||
| 120 | + if 'connection' in locals(): | ||
| 121 | + connection.close() | ||
| 122 | + | ||
| 123 | +if __name__ == "__main__": | ||
| 124 | + check_buyer_nature() |
| @@ -0,0 +1,77 @@ | @@ -0,0 +1,77 @@ | ||
| 1 | +""" | ||
| 2 | +检查 sensors_events 表是否有国家相关字段 | ||
| 3 | +""" | ||
| 4 | +import sys | ||
| 5 | +import os | ||
| 6 | +sys.path.append(os.path.dirname(os.path.dirname(os.path.abspath(__file__)))) | ||
| 7 | + | ||
| 8 | +from db_service import create_db_connection | ||
| 9 | +from offline_tasks.config.offline_config import DB_CONFIG | ||
| 10 | +import pandas as pd | ||
| 11 | + | ||
| 12 | +# 创建数据库连接 | ||
| 13 | +print("连接数据库...") | ||
| 14 | +engine = create_db_connection( | ||
| 15 | + DB_CONFIG['host'], | ||
| 16 | + DB_CONFIG['port'], | ||
| 17 | + DB_CONFIG['database'], | ||
| 18 | + DB_CONFIG['username'], | ||
| 19 | + DB_CONFIG['password'] | ||
| 20 | +) | ||
| 21 | + | ||
| 22 | +print("\n" + "="*80) | ||
| 23 | +print("检查 sensors_events 表的所有字段") | ||
| 24 | +print("="*80) | ||
| 25 | + | ||
| 26 | +# 查看表结构 | ||
| 27 | +try: | ||
| 28 | + df = pd.read_sql("DESCRIBE sensors_events", engine) | ||
| 29 | + print("\n表所有字段列表:") | ||
| 30 | + print(df.to_string()) | ||
| 31 | + | ||
| 32 | + # 查找可能包含国家信息的字段 | ||
| 33 | + country_keywords = ['country', '国家', 'geo', 'nation', '地区', 'region', 'location'] | ||
| 34 | + print("\n" + "="*80) | ||
| 35 | + print("可能包含国家/地区信息的字段:") | ||
| 36 | + print("="*80) | ||
| 37 | + | ||
| 38 | + for field in df['Field'].values: | ||
| 39 | + field_lower = field.lower() | ||
| 40 | + for keyword in country_keywords: | ||
| 41 | + if keyword in field_lower: | ||
| 42 | + print(f" - {field}") | ||
| 43 | + | ||
| 44 | + # 特别检查一些可能的字段 | ||
| 45 | + potential_fields = ['country', 'geo_country', 'user_country', 'ip_country', | ||
| 46 | + 'location_country', 'browser_country', 'device_country'] | ||
| 47 | + | ||
| 48 | + print("\n" + "="*80) | ||
| 49 | + print("检查常见国家字段名称:") | ||
| 50 | + print("="*80) | ||
| 51 | + | ||
| 52 | + all_fields = df['Field'].values.tolist() | ||
| 53 | + for field in potential_fields: | ||
| 54 | + if field in all_fields: | ||
| 55 | + print(f" ✓ 找到字段: {field}") | ||
| 56 | + # 查询该字段的样本数据 | ||
| 57 | + try: | ||
| 58 | + sample_df = pd.read_sql(f"SELECT DISTINCT {field} FROM sensors_events WHERE {field} IS NOT NULL LIMIT 20", engine) | ||
| 59 | + print(f" 样本值: {sample_df[field].tolist()}") | ||
| 60 | + except: | ||
| 61 | + pass | ||
| 62 | + else: | ||
| 63 | + print(f" ✗ 未找到: {field}") | ||
| 64 | + | ||
| 65 | + # 查看所有字段名称的列表(方便手动检查) | ||
| 66 | + print("\n" + "="*80) | ||
| 67 | + print("所有字段名称列表(用于手动检查):") | ||
| 68 | + print("="*80) | ||
| 69 | + for i, field in enumerate(all_fields, 1): | ||
| 70 | + print(f"{i:3d}. {field}") | ||
| 71 | + | ||
| 72 | +except Exception as e: | ||
| 73 | + print(f"无法获取表结构: {e}") | ||
| 74 | + | ||
| 75 | +print("\n" + "="*80) | ||
| 76 | +print("检查完成") | ||
| 77 | +print("="*80) |
| @@ -0,0 +1,144 @@ | @@ -0,0 +1,144 @@ | ||
| 1 | +""" | ||
| 2 | +检查 sensors_events 表可能的地理位置信息 | ||
| 3 | +""" | ||
| 4 | +import sys | ||
| 5 | +import os | ||
| 6 | +sys.path.append(os.path.dirname(os.path.dirname(os.path.abspath(__file__)))) | ||
| 7 | + | ||
| 8 | +from db_service import create_db_connection | ||
| 9 | +from offline_tasks.config.offline_config import DB_CONFIG | ||
| 10 | +import pandas as pd | ||
| 11 | +import json | ||
| 12 | + | ||
| 13 | +# 创建数据库连接 | ||
| 14 | +print("连接数据库...") | ||
| 15 | +engine = create_db_connection( | ||
| 16 | + DB_CONFIG['host'], | ||
| 17 | + DB_CONFIG['port'], | ||
| 18 | + DB_CONFIG['database'], | ||
| 19 | + DB_CONFIG['username'], | ||
| 20 | + DB_CONFIG['password'] | ||
| 21 | +) | ||
| 22 | + | ||
| 23 | +print("\n" + "="*80) | ||
| 24 | +print("检查地理位置相关信息") | ||
| 25 | +print("="*80) | ||
| 26 | + | ||
| 27 | +# 1. 检查浏览器语言 | ||
| 28 | +print("\n1. 浏览器语言 (__browser_language):") | ||
| 29 | +try: | ||
| 30 | + df_lang = pd.read_sql(""" | ||
| 31 | + SELECT __browser_language, COUNT(*) as count | ||
| 32 | + FROM sensors_events | ||
| 33 | + WHERE __browser_language IS NOT NULL | ||
| 34 | + GROUP BY __browser_language | ||
| 35 | + ORDER BY count DESC | ||
| 36 | + LIMIT 20 | ||
| 37 | + """, engine) | ||
| 38 | + print(df_lang.to_string(index=False)) | ||
| 39 | +except Exception as e: | ||
| 40 | + print(f"查询失败: {e}") | ||
| 41 | + | ||
| 42 | +# 2. 检查 IP 地址 | ||
| 43 | +print("\n2. IP地址样本 (ip):") | ||
| 44 | +try: | ||
| 45 | + df_ip = pd.read_sql(""" | ||
| 46 | + SELECT DISTINCT ip | ||
| 47 | + FROM sensors_events | ||
| 48 | + WHERE ip IS NOT NULL | ||
| 49 | + LIMIT 10 | ||
| 50 | + """, engine) | ||
| 51 | + print(df_ip['ip'].tolist()) | ||
| 52 | +except Exception as e: | ||
| 53 | + print(f"查询失败: {e}") | ||
| 54 | + | ||
| 55 | +# 3. 检查 __properties JSON字段中是否包含地理位置信息 | ||
| 56 | +print("\n3. 检查 __properties 字段(可能包含地理位置):") | ||
| 57 | +try: | ||
| 58 | + df_props = pd.read_sql(""" | ||
| 59 | + SELECT __properties | ||
| 60 | + FROM sensors_events | ||
| 61 | + WHERE __properties IS NOT NULL | ||
| 62 | + LIMIT 5 | ||
| 63 | + """, engine) | ||
| 64 | + | ||
| 65 | + print("\n样本数据(前5条):") | ||
| 66 | + for i, prop in enumerate(df_props['__properties'].tolist(), 1): | ||
| 67 | + print(f"\n第{i}条:") | ||
| 68 | + if isinstance(prop, str): | ||
| 69 | + try: | ||
| 70 | + prop_json = json.loads(prop) | ||
| 71 | + print(json.dumps(prop_json, indent=2, ensure_ascii=False)[:500]) | ||
| 72 | + except: | ||
| 73 | + print(prop[:200]) | ||
| 74 | + else: | ||
| 75 | + print(prop) | ||
| 76 | +except Exception as e: | ||
| 77 | + print(f"查询失败: {e}") | ||
| 78 | + | ||
| 79 | +# 4. 检查 __properties 中的所有键 | ||
| 80 | +print("\n\n4. __properties 中的所有键名(查找地理位置相关):") | ||
| 81 | +try: | ||
| 82 | + df_props = pd.read_sql(""" | ||
| 83 | + SELECT __properties | ||
| 84 | + FROM sensors_events | ||
| 85 | + WHERE __properties IS NOT NULL | ||
| 86 | + LIMIT 100 | ||
| 87 | + """, engine) | ||
| 88 | + | ||
| 89 | + # 收集所有键 | ||
| 90 | + all_keys = set() | ||
| 91 | + for prop in df_props['__properties'].tolist(): | ||
| 92 | + if isinstance(prop, str): | ||
| 93 | + try: | ||
| 94 | + prop_json = json.loads(prop) | ||
| 95 | + if isinstance(prop_json, dict): | ||
| 96 | + all_keys.update(prop_json.keys()) | ||
| 97 | + except: | ||
| 98 | + pass | ||
| 99 | + | ||
| 100 | + print(f"\n找到 {len(all_keys)} 个不同的键:") | ||
| 101 | + geo_keywords = ['country', 'geo', 'nation', '地区', '区域', 'city', 'province', 'state', 'location', 'region'] | ||
| 102 | + | ||
| 103 | + for key in sorted(all_keys): | ||
| 104 | + is_geo = False | ||
| 105 | + for keyword in geo_keywords: | ||
| 106 | + if keyword.lower() in key.lower(): | ||
| 107 | + is_geo = True | ||
| 108 | + break | ||
| 109 | + | ||
| 110 | + if is_geo: | ||
| 111 | + print(f" ★ {key} (可能是地理位置)") | ||
| 112 | + else: | ||
| 113 | + print(f" {key}") | ||
| 114 | + | ||
| 115 | +except Exception as e: | ||
| 116 | + print(f"查询失败: {e}") | ||
| 117 | + | ||
| 118 | +# 5. 检查是否有其他可能的地理位置字段 | ||
| 119 | +print("\n\n5. 检查其他可能包含地理位置的字段:") | ||
| 120 | +try: | ||
| 121 | + # 检查 channel_source 和 channel_type 是否包含地理位置信息 | ||
| 122 | + df_geo = pd.read_sql(""" | ||
| 123 | + SELECT channel_type, channel_source, COUNT(*) as count | ||
| 124 | + FROM sensors_events | ||
| 125 | + WHERE channel_type LIKE '%国家%' | ||
| 126 | + OR channel_type LIKE '%地区%' | ||
| 127 | + OR channel_source LIKE '%国家%' | ||
| 128 | + OR channel_source LIKE '%地区%' | ||
| 129 | + GROUP BY channel_type, channel_source | ||
| 130 | + ORDER BY count DESC | ||
| 131 | + LIMIT 20 | ||
| 132 | + """, engine) | ||
| 133 | + | ||
| 134 | + if len(df_geo) > 0: | ||
| 135 | + print(df_geo.to_string(index=False)) | ||
| 136 | + else: | ||
| 137 | + print("未找到包含国家/地区关键词的 channel_type 或 channel_source") | ||
| 138 | + | ||
| 139 | +except Exception as e: | ||
| 140 | + print(f"查询失败: {e}") | ||
| 141 | + | ||
| 142 | +print("\n" + "="*80) | ||
| 143 | +print("检查完成") | ||
| 144 | +print("="*80) |
| @@ -0,0 +1,166 @@ | @@ -0,0 +1,166 @@ | ||
| 1 | +""" | ||
| 2 | +查询 __browser_language 字段的详细数据 | ||
| 3 | +""" | ||
| 4 | +import sys | ||
| 5 | +import os | ||
| 6 | +sys.path.append(os.path.dirname(os.path.dirname(os.path.abspath(__file__)))) | ||
| 7 | + | ||
| 8 | +from db_service import create_db_connection | ||
| 9 | +from offline_tasks.config.offline_config import DB_CONFIG | ||
| 10 | +import pandas as pd | ||
| 11 | + | ||
| 12 | +# 创建数据库连接 | ||
| 13 | +print("连接数据库...") | ||
| 14 | +engine = create_db_connection( | ||
| 15 | + DB_CONFIG['host'], | ||
| 16 | + DB_CONFIG['port'], | ||
| 17 | + DB_CONFIG['database'], | ||
| 18 | + DB_CONFIG['username'], | ||
| 19 | + DB_CONFIG['password'] | ||
| 20 | +) | ||
| 21 | + | ||
| 22 | +print("\n" + "="*80) | ||
| 23 | +print("查询 __browser_language 字段数据") | ||
| 24 | +print("="*80) | ||
| 25 | + | ||
| 26 | +# 1. 查看所有不同的浏览器语言及其数量 | ||
| 27 | +print("\n1. 所有浏览器语言及其数量(按数量排序):") | ||
| 28 | +try: | ||
| 29 | + df = pd.read_sql(""" | ||
| 30 | + SELECT | ||
| 31 | + __browser_language, | ||
| 32 | + COUNT(*) as count, | ||
| 33 | + ROUND(COUNT(*) * 100.0 / (SELECT COUNT(*) FROM sensors_events WHERE __browser_language IS NOT NULL), 2) as percentage | ||
| 34 | + FROM sensors_events | ||
| 35 | + WHERE __browser_language IS NOT NULL | ||
| 36 | + GROUP BY __browser_language | ||
| 37 | + ORDER BY count DESC | ||
| 38 | + """, engine) | ||
| 39 | + | ||
| 40 | + print(f"\n总共找到 {len(df)} 种不同的浏览器语言") | ||
| 41 | + print(f"总记录数: {df['count'].sum():,}") | ||
| 42 | + print("\n前50种语言:") | ||
| 43 | + print(df.head(50).to_string(index=False)) | ||
| 44 | + | ||
| 45 | +except Exception as e: | ||
| 46 | + print(f"查询失败: {e}") | ||
| 47 | + | ||
| 48 | +# 2. 查看样本数据(包含其他字段) | ||
| 49 | +print("\n\n2. 样本数据(包含相关字段):") | ||
| 50 | +try: | ||
| 51 | + df_sample = pd.read_sql(""" | ||
| 52 | + SELECT | ||
| 53 | + __browser_language, | ||
| 54 | + __os, | ||
| 55 | + __browser, | ||
| 56 | + client_platform, | ||
| 57 | + business_platform, | ||
| 58 | + create_time | ||
| 59 | + FROM sensors_events | ||
| 60 | + WHERE __browser_language IS NOT NULL | ||
| 61 | + ORDER BY create_time DESC | ||
| 62 | + LIMIT 30 | ||
| 63 | + """, engine) | ||
| 64 | + | ||
| 65 | + print(df_sample.to_string(index=False)) | ||
| 66 | + | ||
| 67 | +except Exception as e: | ||
| 68 | + print(f"查询失败: {e}") | ||
| 69 | + | ||
| 70 | +# 3. 按语言和平台分组统计 | ||
| 71 | +print("\n\n3. 按语言和平台分组统计(Top 30):") | ||
| 72 | +try: | ||
| 73 | + df_platform = pd.read_sql(""" | ||
| 74 | + SELECT | ||
| 75 | + __browser_language, | ||
| 76 | + client_platform, | ||
| 77 | + COUNT(*) as count | ||
| 78 | + FROM sensors_events | ||
| 79 | + WHERE __browser_language IS NOT NULL | ||
| 80 | + GROUP BY __browser_language, client_platform | ||
| 81 | + ORDER BY count DESC | ||
| 82 | + LIMIT 30 | ||
| 83 | + """, engine) | ||
| 84 | + | ||
| 85 | + print(df_platform.to_string(index=False)) | ||
| 86 | + | ||
| 87 | +except Exception as e: | ||
| 88 | + print(f"查询失败: {e}") | ||
| 89 | + | ||
| 90 | +# 4. 查看一些特殊或罕见的语言 | ||
| 91 | +print("\n\n4. 罕见或特殊的浏览器语言(数量<1000):") | ||
| 92 | +try: | ||
| 93 | + df_rare = pd.read_sql(""" | ||
| 94 | + SELECT | ||
| 95 | + __browser_language, | ||
| 96 | + COUNT(*) as count | ||
| 97 | + FROM sensors_events | ||
| 98 | + WHERE __browser_language IS NOT NULL | ||
| 99 | + GROUP BY __browser_language | ||
| 100 | + HAVING count < 1000 | ||
| 101 | + ORDER BY count DESC | ||
| 102 | + LIMIT 30 | ||
| 103 | + """, engine) | ||
| 104 | + | ||
| 105 | + print(df_rare.to_string(index=False)) | ||
| 106 | + | ||
| 107 | +except Exception as e: | ||
| 108 | + print(f"查询失败: {e}") | ||
| 109 | + | ||
| 110 | +# 5. 查看包含特殊字符的语言代码 | ||
| 111 | +print("\n\n5. 包含特殊字符的语言代码:") | ||
| 112 | +try: | ||
| 113 | + df_special = pd.read_sql(""" | ||
| 114 | + SELECT | ||
| 115 | + __browser_language, | ||
| 116 | + COUNT(*) as count | ||
| 117 | + FROM sensors_events | ||
| 118 | + WHERE __browser_language IS NOT NULL | ||
| 119 | + AND (__browser_language LIKE '%-%' | ||
| 120 | + OR __browser_language LIKE '%;%' | ||
| 121 | + OR LENGTH(__browser_language) > 10) | ||
| 122 | + GROUP BY __browser_language | ||
| 123 | + ORDER BY count DESC | ||
| 124 | + LIMIT 20 | ||
| 125 | + """, engine) | ||
| 126 | + | ||
| 127 | + print(df_special.to_string(index=False)) | ||
| 128 | + | ||
| 129 | +except Exception as e: | ||
| 130 | + print(f"查询失败: {e}") | ||
| 131 | + | ||
| 132 | +# 6. 语言代码解析示例 | ||
| 133 | +print("\n\n6. 主要语言代码分布(按语言家族分类):") | ||
| 134 | +try: | ||
| 135 | + # 简单分类 | ||
| 136 | + df_family = pd.read_sql(""" | ||
| 137 | + SELECT | ||
| 138 | + CASE | ||
| 139 | + WHEN __browser_language LIKE 'zh%' THEN '中文 (Chinese)' | ||
| 140 | + WHEN __browser_language LIKE 'en%' THEN '英语 (English)' | ||
| 141 | + WHEN __browser_language LIKE 'ru%' THEN '俄语 (Russian)' | ||
| 142 | + WHEN __browser_language LIKE 'es%' THEN '西班牙语 (Spanish)' | ||
| 143 | + WHEN __browser_language LIKE 'fr%' THEN '法语 (French)' | ||
| 144 | + WHEN __browser_language LIKE 'tr%' THEN '土耳其语 (Turkish)' | ||
| 145 | + WHEN __browser_language LIKE 'bg%' THEN '保加利亚语 (Bulgarian)' | ||
| 146 | + WHEN __browser_language LIKE 'it%' THEN '意大利语 (Italian)' | ||
| 147 | + WHEN __browser_language LIKE 'ar%' THEN '阿拉伯语 (Arabic)' | ||
| 148 | + WHEN __browser_language LIKE 'hr%' THEN '克罗地亚语 (Croatian)' | ||
| 149 | + ELSE '其他语言' | ||
| 150 | + END as language_family, | ||
| 151 | + COUNT(*) as count, | ||
| 152 | + ROUND(COUNT(*) * 100.0 / (SELECT COUNT(*) FROM sensors_events WHERE __browser_language IS NOT NULL), 2) as percentage | ||
| 153 | + FROM sensors_events | ||
| 154 | + WHERE __browser_language IS NOT NULL | ||
| 155 | + GROUP BY language_family | ||
| 156 | + ORDER BY count DESC | ||
| 157 | + """, engine) | ||
| 158 | + | ||
| 159 | + print(df_family.to_string(index=False)) | ||
| 160 | + | ||
| 161 | +except Exception as e: | ||
| 162 | + print(f"查询失败: {e}") | ||
| 163 | + | ||
| 164 | +print("\n" + "="*80) | ||
| 165 | +print("查询完成") | ||
| 166 | +print("="*80) |
| @@ -0,0 +1,90 @@ | @@ -0,0 +1,90 @@ | ||
| 1 | +#!/usr/bin/env python3 | ||
| 2 | +"""查询 buy_buyer 表的示例数据""" | ||
| 3 | + | ||
| 4 | +import os | ||
| 5 | +import pymysql | ||
| 6 | +import sys | ||
| 7 | +sys.path.append(os.path.dirname(os.path.abspath(__file__))) | ||
| 8 | +from config.offline_config import DB_CONFIG | ||
| 9 | + | ||
| 10 | +def query_sample_data(): | ||
| 11 | + """查询示例数据""" | ||
| 12 | + try: | ||
| 13 | + # 连接数据库 | ||
| 14 | + connection = pymysql.connect( | ||
| 15 | + host=DB_CONFIG['host'], | ||
| 16 | + port=int(DB_CONFIG['port']), | ||
| 17 | + user=DB_CONFIG['username'], | ||
| 18 | + password=DB_CONFIG['password'], | ||
| 19 | + database=DB_CONFIG['database'], | ||
| 20 | + charset='utf8mb4' | ||
| 21 | + ) | ||
| 22 | + | ||
| 23 | + with connection.cursor() as cursor: | ||
| 24 | + # 查询指定字段的示例数据 | ||
| 25 | + sql = """ | ||
| 26 | + SELECT id, lang, customer_brand, countries, province, city, area, city_code | ||
| 27 | + FROM buy_buyer | ||
| 28 | + WHERE is_delete = 0 | ||
| 29 | + LIMIT 20 | ||
| 30 | + """ | ||
| 31 | + cursor.execute(sql) | ||
| 32 | + result = cursor.fetchall() | ||
| 33 | + | ||
| 34 | + # 获取列名 | ||
| 35 | + columns = [desc[0] for desc in cursor.description] | ||
| 36 | + | ||
| 37 | + print("=" * 140) | ||
| 38 | + print(f"{'ID':<15} {'语言':<10} {'客户品牌':<20} {'国家':<15} {'省份':<20} {'城市':<20} {'区域':<15} {'城市代码':<10}") | ||
| 39 | + print("=" * 140) | ||
| 40 | + | ||
| 41 | + for row in result: | ||
| 42 | + id_val = row[0] if row[0] is not None else '' | ||
| 43 | + lang = row[1] if row[1] is not None else '' | ||
| 44 | + brand = row[2] if row[2] is not None else '' | ||
| 45 | + countries = row[3] if row[3] is not None else '' | ||
| 46 | + province = row[4] if row[4] is not None else '' | ||
| 47 | + city = row[5] if row[5] is not None else '' | ||
| 48 | + area = row[6] if row[6] is not None else '' | ||
| 49 | + city_code = row[7] if row[7] is not None else '' | ||
| 50 | + | ||
| 51 | + # 截断过长的字符串 | ||
| 52 | + brand = (brand[:18] + '..') if len(brand) > 20 else brand | ||
| 53 | + province = (province[:18] + '..') if len(province) > 20 else province | ||
| 54 | + city = (city[:18] + '..') if len(city) > 20 else city | ||
| 55 | + | ||
| 56 | + print(f"{id_val:<15} {lang:<10} {brand:<20} {countries:<15} {province:<20} {city:<20} {area:<15} {city_code:<10}") | ||
| 57 | + | ||
| 58 | + print("=" * 140) | ||
| 59 | + print(f"\n总计: {len(result)} 条记录") | ||
| 60 | + | ||
| 61 | + # 查询字段值分布统计 | ||
| 62 | + print("\n" + "=" * 80) | ||
| 63 | + print("字段值分布统计:") | ||
| 64 | + print("=" * 80) | ||
| 65 | + | ||
| 66 | + stats_queries = [ | ||
| 67 | + ("语言 (lang)", "SELECT lang, COUNT(*) as cnt FROM buy_buyer WHERE is_delete = 0 GROUP BY lang ORDER BY cnt DESC LIMIT 10"), | ||
| 68 | + ("国家 (countries)", "SELECT countries, COUNT(*) as cnt FROM buy_buyer WHERE is_delete = 0 GROUP BY countries ORDER BY cnt DESC LIMIT 10"), | ||
| 69 | + ("省份 (province)", "SELECT province, COUNT(*) as cnt FROM buy_buyer WHERE is_delete = 0 GROUP BY province ORDER BY cnt DESC LIMIT 10"), | ||
| 70 | + ("城市 (city)", "SELECT city, COUNT(*) as cnt FROM buy_buyer WHERE is_delete = 0 GROUP BY city ORDER BY cnt DESC LIMIT 10"), | ||
| 71 | + ("客户品牌 (customer_brand)", "SELECT customer_brand, COUNT(*) as cnt FROM buy_buyer WHERE is_delete = 0 GROUP BY customer_brand ORDER BY cnt DESC LIMIT 10"), | ||
| 72 | + ] | ||
| 73 | + | ||
| 74 | + for title, query in stats_queries: | ||
| 75 | + print(f"\n【{title}】") | ||
| 76 | + cursor.execute(query) | ||
| 77 | + stats = cursor.fetchall() | ||
| 78 | + for row in stats: | ||
| 79 | + value = row[0] if row[0] is not None else '(NULL)' | ||
| 80 | + count = row[1] | ||
| 81 | + print(f" {value:<30} {count:>10} 条") | ||
| 82 | + | ||
| 83 | + except Exception as e: | ||
| 84 | + print(f"查询失败: {e}") | ||
| 85 | + finally: | ||
| 86 | + if 'connection' in locals(): | ||
| 87 | + connection.close() | ||
| 88 | + | ||
| 89 | +if __name__ == "__main__": | ||
| 90 | + query_sample_data() |
| @@ -0,0 +1,53 @@ | @@ -0,0 +1,53 @@ | ||
| 1 | +#!/usr/bin/env python3 | ||
| 2 | +"""查询 buy_buyer 表的字段信息""" | ||
| 3 | + | ||
| 4 | +import os | ||
| 5 | +import pymysql | ||
| 6 | +import sys | ||
| 7 | +sys.path.append(os.path.dirname(os.path.abspath(__file__))) | ||
| 8 | +from config.offline_config import DB_CONFIG | ||
| 9 | + | ||
| 10 | +def query_table_fields(): | ||
| 11 | + """查询表结构""" | ||
| 12 | + try: | ||
| 13 | + # 连接数据库 | ||
| 14 | + connection = pymysql.connect( | ||
| 15 | + host=DB_CONFIG['host'], | ||
| 16 | + port=int(DB_CONFIG['port']), | ||
| 17 | + user=DB_CONFIG['username'], | ||
| 18 | + password=DB_CONFIG['password'], | ||
| 19 | + database=DB_CONFIG['database'], | ||
| 20 | + charset='utf8mb4' | ||
| 21 | + ) | ||
| 22 | + | ||
| 23 | + with connection.cursor() as cursor: | ||
| 24 | + # 查询表结构 | ||
| 25 | + sql = "DESCRIBE buy_buyer" | ||
| 26 | + cursor.execute(sql) | ||
| 27 | + result = cursor.fetchall() | ||
| 28 | + | ||
| 29 | + print("=" * 100) | ||
| 30 | + print(f"{'字段名':<30} {'类型':<20} {'允许NULL':<10} {'键':<10} {'默认值':<15} {'额外信息'}") | ||
| 31 | + print("=" * 100) | ||
| 32 | + | ||
| 33 | + for row in result: | ||
| 34 | + field = row[0] | ||
| 35 | + type_info = row[1] | ||
| 36 | + null = row[2] | ||
| 37 | + key = row[3] | ||
| 38 | + default = row[4] | ||
| 39 | + extra = row[5] | ||
| 40 | + | ||
| 41 | + print(f"{field:<30} {type_info:<20} {null:<10} {key:<10} {str(default):<15} {extra}") | ||
| 42 | + | ||
| 43 | + print("=" * 100) | ||
| 44 | + print(f"\n总计: {len(result)} 个字段") | ||
| 45 | + | ||
| 46 | + except Exception as e: | ||
| 47 | + print(f"查询失败: {e}") | ||
| 48 | + finally: | ||
| 49 | + if 'connection' in locals(): | ||
| 50 | + connection.close() | ||
| 51 | + | ||
| 52 | +if __name__ == "__main__": | ||
| 53 | + query_table_fields() |
| @@ -0,0 +1,122 @@ | @@ -0,0 +1,122 @@ | ||
| 1 | +#!/usr/bin/env python3 | ||
| 2 | +"""查询客户类型字典的准确含义""" | ||
| 3 | + | ||
| 4 | +import os | ||
| 5 | +import pymysql | ||
| 6 | +import sys | ||
| 7 | +sys.path.append(os.path.dirname(os.path.abspath(__file__))) | ||
| 8 | +from config.offline_config import DB_CONFIG | ||
| 9 | + | ||
| 10 | +def query_buyer_type_dict(): | ||
| 11 | + """查询客户类型字典""" | ||
| 12 | + try: | ||
| 13 | + connection = pymysql.connect( | ||
| 14 | + host=DB_CONFIG['host'], | ||
| 15 | + port=int(DB_CONFIG['port']), | ||
| 16 | + user=DB_CONFIG['username'], | ||
| 17 | + password=DB_CONFIG['password'], | ||
| 18 | + database=DB_CONFIG['database'], | ||
| 19 | + charset='utf8mb4' | ||
| 20 | + ) | ||
| 21 | + | ||
| 22 | + with connection.cursor() as cursor: | ||
| 23 | + print("=" * 100) | ||
| 24 | + print("查询客户类型相关的字典") | ||
| 25 | + print("=" * 100) | ||
| 26 | + | ||
| 27 | + # 1. 查找所有与客户类型相关的字典类别 | ||
| 28 | + print("\n【1. 查找客户类型相关的字典类别】") | ||
| 29 | + sql = """ | ||
| 30 | + SELECT code, name | ||
| 31 | + FROM pub_dict_category | ||
| 32 | + WHERE code LIKE '%buyer%' | ||
| 33 | + OR code LIKE '%customer%' | ||
| 34 | + OR code LIKE '%nature%' | ||
| 35 | + OR code LIKE '%company%' | ||
| 36 | + OR code LIKE '%cooperate%' | ||
| 37 | + ORDER BY code | ||
| 38 | + """ | ||
| 39 | + cursor.execute(sql) | ||
| 40 | + categories = cursor.fetchall() | ||
| 41 | + | ||
| 42 | + if categories: | ||
| 43 | + print(f"\n找到 {len(categories)} 个相关字典类别:") | ||
| 44 | + for cat in categories: | ||
| 45 | + print(f" - {cat[0]:<30} {cat[1]}") | ||
| 46 | + else: | ||
| 47 | + print("\n未找到完全匹配的字典,显示所有类别:") | ||
| 48 | + sql = "SELECT code, name FROM pub_dict_category ORDER BY sort_order LIMIT 50" | ||
| 49 | + cursor.execute(sql) | ||
| 50 | + all_cats = cursor.fetchall() | ||
| 51 | + for cat in all_cats: | ||
| 52 | + print(f" - {cat[0]:<30} {cat[1]}") | ||
| 53 | + | ||
| 54 | + # 2. 针对可能相关的字典类别查询具体值 | ||
| 55 | + print("\n" + "=" * 100) | ||
| 56 | + print("【2. 查询可能相关的字典值】") | ||
| 57 | + | ||
| 58 | + possible_codes = [ | ||
| 59 | + 'supplier_nature', # 公司性质 | ||
| 60 | + 'supplier_type', # 供应商类别 | ||
| 61 | + 'customer_type', # 客户类型 | ||
| 62 | + 'buyer_from', # 买家来源 | ||
| 63 | + 'cooperate_type' # 合作类型 | ||
| 64 | + ] | ||
| 65 | + | ||
| 66 | + for dict_code in possible_codes: | ||
| 67 | + print(f"\n--- 字典: {dict_code} ---") | ||
| 68 | + | ||
| 69 | + # 查询该类别下的所有条目 | ||
| 70 | + sql = """ | ||
| 71 | + SELECT i.id, i.code, i.name, i.sort_order, il.lang, il.name as lang_name | ||
| 72 | + FROM pub_dict_item i | ||
| 73 | + LEFT JOIN pub_dict_item_lang il ON i.id = il.dict_item_id | ||
| 74 | + WHERE i.category_code = %s | ||
| 75 | + ORDER BY i.sort_order, i.id | ||
| 76 | + """ | ||
| 77 | + cursor.execute(sql, (dict_code,)) | ||
| 78 | + items = cursor.fetchall() | ||
| 79 | + | ||
| 80 | + if items: | ||
| 81 | + print(f"{'code':<15} {'name':<30} {'sort':<10} {'lang':<10} {'翻译名称'}") | ||
| 82 | + print("-" * 100) | ||
| 83 | + for item in items: | ||
| 84 | + code = item[1] if item[1] else 'NULL' | ||
| 85 | + name = item[2] if item[2] else 'NULL' | ||
| 86 | + sort = item[3] if item[3] else 'NULL' | ||
| 87 | + lang = item[4] if item[4] else 'NULL' | ||
| 88 | + lang_name = item[5] if item[5] else '' | ||
| 89 | + | ||
| 90 | + # 只显示主要语言和翻译 | ||
| 91 | + if lang in ['zh', 'en', 'ru', None]: | ||
| 92 | + print(f"{code:<15} {name:<30} {sort:<10} {lang if lang else 'NULL':<10} {lang_name}") | ||
| 93 | + else: | ||
| 94 | + print(" (该字典不存在或无数据)") | ||
| 95 | + | ||
| 96 | + # 3. 查询所有字典类别 | ||
| 97 | + print("\n" + "=" * 100) | ||
| 98 | + print("【3. 所有可用的字典类别(前50个)】") | ||
| 99 | + sql = """ | ||
| 100 | + SELECT code, name | ||
| 101 | + FROM pub_dict_category | ||
| 102 | + ORDER BY sort_order | ||
| 103 | + LIMIT 50 | ||
| 104 | + """ | ||
| 105 | + cursor.execute(sql) | ||
| 106 | + all_cats = cursor.fetchall() | ||
| 107 | + | ||
| 108 | + print(f"{'代码':<30} {'名称'}") | ||
| 109 | + print("-" * 60) | ||
| 110 | + for cat in all_cats: | ||
| 111 | + print(f"{cat[0]:<30} {cat[1]}") | ||
| 112 | + | ||
| 113 | + except Exception as e: | ||
| 114 | + print(f"查询失败: {e}") | ||
| 115 | + import traceback | ||
| 116 | + traceback.print_exc() | ||
| 117 | + finally: | ||
| 118 | + if 'connection' in locals(): | ||
| 119 | + connection.close() | ||
| 120 | + | ||
| 121 | +if __name__ == "__main__": | ||
| 122 | + query_buyer_type_dict() |
| @@ -0,0 +1,186 @@ | @@ -0,0 +1,186 @@ | ||
| 1 | +#!/usr/bin/env python3 | ||
| 2 | +"""查询 buy_buyer 表客户类型字段的详细信息""" | ||
| 3 | + | ||
| 4 | +import os | ||
| 5 | +import pymysql | ||
| 6 | +import sys | ||
| 7 | +sys.path.append(os.path.dirname(os.path.abspath(__file__))) | ||
| 8 | +from config.offline_config import DB_CONFIG | ||
| 9 | + | ||
| 10 | +def query_buyer_type_details(): | ||
| 11 | + """查询客户类型详细信息""" | ||
| 12 | + try: | ||
| 13 | + connection = pymysql.connect( | ||
| 14 | + host=DB_CONFIG['host'], | ||
| 15 | + port=int(DB_CONFIG['port']), | ||
| 16 | + user=DB_CONFIG['username'], | ||
| 17 | + password=DB_CONFIG['password'], | ||
| 18 | + database=DB_CONFIG['database'], | ||
| 19 | + charset='utf8mb4' | ||
| 20 | + ) | ||
| 21 | + | ||
| 22 | + with connection.cursor() as cursor: | ||
| 23 | + print("=" * 100) | ||
| 24 | + print("客户类型字段详细分析") | ||
| 25 | + print("=" * 100) | ||
| 26 | + | ||
| 27 | + # 1. customer_type 字段 | ||
| 28 | + print("\n【1. customer_type 字段】") | ||
| 29 | + sql = """ | ||
| 30 | + SELECT customer_type, COUNT(*) as count | ||
| 31 | + FROM buy_buyer | ||
| 32 | + WHERE is_delete = 0 | ||
| 33 | + GROUP BY customer_type | ||
| 34 | + ORDER BY customer_type | ||
| 35 | + """ | ||
| 36 | + cursor.execute(sql) | ||
| 37 | + result = cursor.fetchall() | ||
| 38 | + | ||
| 39 | + print(f"{'customer_type':<20} {'买家数量':>15}") | ||
| 40 | + print("-" * 50) | ||
| 41 | + for row in result: | ||
| 42 | + val = row[0] if row[0] is not None else 'NULL' | ||
| 43 | + count = row[1] | ||
| 44 | + print(f"{val:<20} {count:>15,}") | ||
| 45 | + | ||
| 46 | + # 查询不同 customer_type 的示例 | ||
| 47 | + print("\n示例买家名称:") | ||
| 48 | + for ctype in [1, 2]: | ||
| 49 | + sql = """ | ||
| 50 | + SELECT id, name, customer_type, countries | ||
| 51 | + FROM buy_buyer | ||
| 52 | + WHERE customer_type = %s AND is_delete = 0 | ||
| 53 | + LIMIT 5 | ||
| 54 | + """ | ||
| 55 | + cursor.execute(sql, (ctype,)) | ||
| 56 | + samples = cursor.fetchall() | ||
| 57 | + if samples: | ||
| 58 | + print(f"\n customer_type={ctype} 的示例:") | ||
| 59 | + for s in samples: | ||
| 60 | + print(f" - [{s[0]}] {s[1]} (国家:{s[3]})") | ||
| 61 | + | ||
| 62 | + # 2. buyer_from 字段 | ||
| 63 | + print("\n" + "=" * 100) | ||
| 64 | + print("【2. buyer_from 字段】") | ||
| 65 | + sql = """ | ||
| 66 | + SELECT buyer_from, COUNT(*) as count | ||
| 67 | + FROM buy_buyer | ||
| 68 | + WHERE is_delete = 0 | ||
| 69 | + GROUP BY buyer_from | ||
| 70 | + ORDER BY buyer_from | ||
| 71 | + """ | ||
| 72 | + cursor.execute(sql) | ||
| 73 | + result = cursor.fetchall() | ||
| 74 | + | ||
| 75 | + print(f"{'buyer_from':<20} {'买家数量':>15}") | ||
| 76 | + print("-" * 50) | ||
| 77 | + for row in result: | ||
| 78 | + val = row[0] if row[0] is not None else 'NULL' | ||
| 79 | + count = row[1] | ||
| 80 | + print(f"{val:<20} {count:>15,}") | ||
| 81 | + | ||
| 82 | + # 查询不同 buyer_from 的示例 | ||
| 83 | + print("\n示例买家名称:") | ||
| 84 | + for bfrom in [1, 2, 3, 4, 5, 6, 10, 11, 12]: | ||
| 85 | + sql = """ | ||
| 86 | + SELECT id, name, buyer_from, countries | ||
| 87 | + FROM buy_buyer | ||
| 88 | + WHERE buyer_from = %s AND is_delete = 0 | ||
| 89 | + LIMIT 3 | ||
| 90 | + """ | ||
| 91 | + cursor.execute(sql, (bfrom,)) | ||
| 92 | + samples = cursor.fetchall() | ||
| 93 | + if samples: | ||
| 94 | + print(f"\n buyer_from={bfrom} 的示例:") | ||
| 95 | + for s in samples: | ||
| 96 | + print(f" - [{s[0]}] {s[1]} (国家:{s[3]})") | ||
| 97 | + | ||
| 98 | + # 3. nature_of_company 字段 | ||
| 99 | + print("\n" + "=" * 100) | ||
| 100 | + print("【3. nature_of_company 字段(公司性质)】") | ||
| 101 | + sql = """ | ||
| 102 | + SELECT nature_of_company, COUNT(*) as count | ||
| 103 | + FROM buy_buyer | ||
| 104 | + WHERE is_delete = 0 | ||
| 105 | + GROUP BY nature_of_company | ||
| 106 | + ORDER BY nature_of_company | ||
| 107 | + """ | ||
| 108 | + cursor.execute(sql) | ||
| 109 | + result = cursor.fetchall() | ||
| 110 | + | ||
| 111 | + print(f"{'nature_of_company':<20} {'买家数量':>15}") | ||
| 112 | + print("-" * 50) | ||
| 113 | + for row in result: | ||
| 114 | + val = row[0] if row[0] is not None else 'NULL' | ||
| 115 | + count = row[1] | ||
| 116 | + print(f"{val:<20} {count:>15,}") | ||
| 117 | + | ||
| 118 | + # 查询不同 nature_of_company 的示例 | ||
| 119 | + print("\n示例买家名称:") | ||
| 120 | + for nature in [51, 52]: | ||
| 121 | + sql = """ | ||
| 122 | + SELECT id, name, nature_of_company, countries | ||
| 123 | + FROM buy_buyer | ||
| 124 | + WHERE nature_of_company = %s AND is_delete = 0 | ||
| 125 | + LIMIT 5 | ||
| 126 | + """ | ||
| 127 | + cursor.execute(sql, (nature,)) | ||
| 128 | + samples = cursor.fetchall() | ||
| 129 | + if samples: | ||
| 130 | + print(f"\n nature_of_company={nature} 的示例:") | ||
| 131 | + for s in samples: | ||
| 132 | + print(f" - [{s[0]}] {s[1]} (国家:{s[3]})") | ||
| 133 | + | ||
| 134 | + # 4. 查找是否有数据字典表 | ||
| 135 | + print("\n" + "=" * 100) | ||
| 136 | + print("【查找数据字典表】") | ||
| 137 | + sql = """ | ||
| 138 | + SHOW TABLES LIKE '%dict%' | ||
| 139 | + """ | ||
| 140 | + cursor.execute(sql) | ||
| 141 | + dict_tables = cursor.fetchall() | ||
| 142 | + | ||
| 143 | + if dict_tables: | ||
| 144 | + print("\n找到以下字典表:") | ||
| 145 | + for table in dict_tables: | ||
| 146 | + print(f" - {table[0]}") | ||
| 147 | + else: | ||
| 148 | + print("\n未找到包含 'dict' 关键字的表") | ||
| 149 | + | ||
| 150 | + # 5. 组合查询 - 看看这些字段的关系 | ||
| 151 | + print("\n" + "=" * 100) | ||
| 152 | + print("【客户类型字段组合分布】") | ||
| 153 | + sql = """ | ||
| 154 | + SELECT | ||
| 155 | + customer_type, | ||
| 156 | + buyer_from, | ||
| 157 | + nature_of_company, | ||
| 158 | + COUNT(*) as count | ||
| 159 | + FROM buy_buyer | ||
| 160 | + WHERE is_delete = 0 | ||
| 161 | + GROUP BY customer_type, buyer_from, nature_of_company | ||
| 162 | + ORDER BY count DESC | ||
| 163 | + LIMIT 20 | ||
| 164 | + """ | ||
| 165 | + cursor.execute(sql) | ||
| 166 | + result = cursor.fetchall() | ||
| 167 | + | ||
| 168 | + print(f"{'customer_type':<15} {'buyer_from':<15} {'nature_of_company':<20} {'数量':>10}") | ||
| 169 | + print("-" * 80) | ||
| 170 | + for row in result: | ||
| 171 | + ct = row[0] if row[0] is not None else 'NULL' | ||
| 172 | + bf = row[1] if row[1] is not None else 'NULL' | ||
| 173 | + nc = row[2] if row[2] is not None else 'NULL' | ||
| 174 | + count = row[3] | ||
| 175 | + print(f"{ct:<15} {bf:<15} {nc:<20} {count:>10,}") | ||
| 176 | + | ||
| 177 | + except Exception as e: | ||
| 178 | + print(f"查询失败: {e}") | ||
| 179 | + import traceback | ||
| 180 | + traceback.print_exc() | ||
| 181 | + finally: | ||
| 182 | + if 'connection' in locals(): | ||
| 183 | + connection.close() | ||
| 184 | + | ||
| 185 | +if __name__ == "__main__": | ||
| 186 | + query_buyer_type_details() |
| @@ -0,0 +1,186 @@ | @@ -0,0 +1,186 @@ | ||
| 1 | +#!/usr/bin/env python3 | ||
| 2 | +"""查询 buy_buyer_type 采购商分类字典""" | ||
| 3 | + | ||
| 4 | +import os | ||
| 5 | +import pymysql | ||
| 6 | +import sys | ||
| 7 | +sys.path.append(os.path.dirname(os.path.abspath(__file__))) | ||
| 8 | +from config.offline_config import DB_CONFIG | ||
| 9 | + | ||
| 10 | +def query_buyer_type(): | ||
| 11 | + """查询采购商分类字典""" | ||
| 12 | + try: | ||
| 13 | + connection = pymysql.connect( | ||
| 14 | + host=DB_CONFIG['host'], | ||
| 15 | + port=int(DB_CONFIG['port']), | ||
| 16 | + user=DB_CONFIG['username'], | ||
| 17 | + password=DB_CONFIG['password'], | ||
| 18 | + database=DB_CONFIG['database'], | ||
| 19 | + charset='utf8mb4' | ||
| 20 | + ) | ||
| 21 | + | ||
| 22 | + with connection.cursor() as cursor: | ||
| 23 | + print("=" * 100) | ||
| 24 | + print("【buy_buyer_type - 采购商分类】") | ||
| 25 | + print("=" * 100) | ||
| 26 | + | ||
| 27 | + sql = """ | ||
| 28 | + SELECT i.id, i.code, i.name, i.sort_order, il.lang, il.name as lang_name | ||
| 29 | + FROM pub_dict_item i | ||
| 30 | + LEFT JOIN pub_dict_item_lang il ON i.id = il.dict_item_id | ||
| 31 | + WHERE i.category_code = 'buy_buyer_type' | ||
| 32 | + ORDER BY i.sort_order, i.id, il.lang | ||
| 33 | + """ | ||
| 34 | + cursor.execute(sql) | ||
| 35 | + items = cursor.fetchall() | ||
| 36 | + | ||
| 37 | + if items: | ||
| 38 | + print(f"{'code':<10} {'name(zh)':<40} {'name(en/ru)':<40} {'sort':<10}") | ||
| 39 | + print("-" * 120) | ||
| 40 | + | ||
| 41 | + current_code = None | ||
| 42 | + zh_name = None | ||
| 43 | + | ||
| 44 | + for item in items: | ||
| 45 | + code = item[1] if item[1] else 'NULL' | ||
| 46 | + name = item[2] if item[2] else 'NULL' | ||
| 47 | + sort = item[3] if item[3] else 'NULL' | ||
| 48 | + lang = item[4] if item[4] else 'NULL' | ||
| 49 | + lang_name = item[5] if item[5] else 'NULL' | ||
| 50 | + | ||
| 51 | + if code != current_code: | ||
| 52 | + if current_code is not None: | ||
| 53 | + print(f"{current_code:<10} {zh_name:<40} {other_langs:<40} {sort:<10}") | ||
| 54 | + current_code = code | ||
| 55 | + zh_name = name if lang == 'zh' else name | ||
| 56 | + other_langs = '' | ||
| 57 | + | ||
| 58 | + if lang != 'zh': | ||
| 59 | + other_langs += f"{lang}:{lang_name} " if lang_name else '' | ||
| 60 | + | ||
| 61 | + # 打印最后一项 | ||
| 62 | + if current_code is not None: | ||
| 63 | + print(f"{current_code:<10} {zh_name:<40} {other_langs:<40} {sort:<10}") | ||
| 64 | + | ||
| 65 | + # 2. 查询 buyer_from 的所有值(包括6, 10, 11, 12) | ||
| 66 | + print("\n" + "=" * 100) | ||
| 67 | + print("【buyer_from - 采购商来源】") | ||
| 68 | + print("=" * 100) | ||
| 69 | + | ||
| 70 | + sql = """ | ||
| 71 | + SELECT i.id, i.code, i.name, i.sort_order, il.lang, il.name as lang_name | ||
| 72 | + FROM pub_dict_item i | ||
| 73 | + LEFT JOIN pub_dict_item_lang il ON i.id = il.dict_item_id | ||
| 74 | + WHERE i.category_code = 'buyer_from' | ||
| 75 | + ORDER BY i.sort_order, i.id, il.lang | ||
| 76 | + """ | ||
| 77 | + cursor.execute(sql) | ||
| 78 | + items = cursor.fetchall() | ||
| 79 | + | ||
| 80 | + if items: | ||
| 81 | + print(f"{'code':<10} {'name':<30} {'sort':<10}") | ||
| 82 | + print("-" * 60) | ||
| 83 | + | ||
| 84 | + for item in items: | ||
| 85 | + code = item[1] if item[1] else 'NULL' | ||
| 86 | + name = item[2] if item[2] else 'NULL' | ||
| 87 | + sort = item[3] if item[3] else 'NULL' | ||
| 88 | + lang = item[4] if item[4] else 'NULL' | ||
| 89 | + | ||
| 90 | + if lang in ['zh', None]: | ||
| 91 | + print(f"{code:<10} {name:<30} {sort:<10}") | ||
| 92 | + | ||
| 93 | + # 3. 查询 buyer_nature (公司性质) | ||
| 94 | + print("\n" + "=" * 100) | ||
| 95 | + print("【buyer_nature - 公司性质】") | ||
| 96 | + print("=" * 100) | ||
| 97 | + | ||
| 98 | + sql = """ | ||
| 99 | + SELECT i.id, i.code, i.name, i.sort_order, il.lang, il.name as lang_name | ||
| 100 | + FROM pub_dict_item i | ||
| 101 | + LEFT JOIN pub_dict_item_lang il ON i.id = il.dict_item_id | ||
| 102 | + WHERE i.category_code = 'buyer_nature' | ||
| 103 | + ORDER BY i.sort_order, i.id, il.lang | ||
| 104 | + """ | ||
| 105 | + cursor.execute(sql) | ||
| 106 | + items = cursor.fetchall() | ||
| 107 | + | ||
| 108 | + if items: | ||
| 109 | + print(f"{'code':<10} {'name':<50} {'sort':<10}") | ||
| 110 | + print("-" * 80) | ||
| 111 | + | ||
| 112 | + for item in items: | ||
| 113 | + code = item[1] if item[1] else 'NULL' | ||
| 114 | + name = item[2] if item[2] else 'NULL' | ||
| 115 | + sort = item[3] if item[3] else 'NULL' | ||
| 116 | + lang = item[4] if item[4] else 'NULL' | ||
| 117 | + | ||
| 118 | + if lang in ['zh', None]: | ||
| 119 | + print(f"{code:<10} {name:<50} {sort:<10}") | ||
| 120 | + | ||
| 121 | + # 4. 查询 nature_of_company | ||
| 122 | + print("\n" + "=" * 100) | ||
| 123 | + print("【nature_of_company - 公司类型】") | ||
| 124 | + print("=" * 100) | ||
| 125 | + | ||
| 126 | + sql = """ | ||
| 127 | + SELECT i.id, i.code, i.name, i.sort_order, il.lang, il.name as lang_name | ||
| 128 | + FROM pub_dict_item i | ||
| 129 | + LEFT JOIN pub_dict_item_lang il ON i.id = il.dict_item_id | ||
| 130 | + WHERE i.category_code = 'nature_of_company' | ||
| 131 | + ORDER BY i.sort_order, i.id, il.lang | ||
| 132 | + """ | ||
| 133 | + cursor.execute(sql) | ||
| 134 | + items = cursor.fetchall() | ||
| 135 | + | ||
| 136 | + if items: | ||
| 137 | + print(f"{'code':<10} {'name':<50} {'sort':<10}") | ||
| 138 | + print("-" * 80) | ||
| 139 | + | ||
| 140 | + for item in items: | ||
| 141 | + code = item[1] if item[1] else 'NULL' | ||
| 142 | + name = item[2] if item[2] else 'NULL' | ||
| 143 | + sort = item[3] if item[3] else 'NULL' | ||
| 144 | + lang = item[4] if item[4] else 'NULL' | ||
| 145 | + | ||
| 146 | + if lang in ['zh', None]: | ||
| 147 | + print(f"{code:<10} {name:<50} {sort:<10}") | ||
| 148 | + | ||
| 149 | + # 5. 实际数据验证 | ||
| 150 | + print("\n" + "=" * 120) | ||
| 151 | + print("【实际数据验证 - 查询 buy_buyer 表中的客户类型分布】") | ||
| 152 | + print("=" * 120) | ||
| 153 | + | ||
| 154 | + # 尝试不同的客户类型字段 | ||
| 155 | + type_fields = ['customer_type', 'buyer_from', 'nature_of_company', 'cooperate_type'] | ||
| 156 | + | ||
| 157 | + for field in type_fields: | ||
| 158 | + print(f"\n--- {field} 字段分布 ---") | ||
| 159 | + sql = f""" | ||
| 160 | + SELECT {field}, COUNT(*) as count | ||
| 161 | + FROM buy_buyer | ||
| 162 | + WHERE is_delete = 0 | ||
| 163 | + GROUP BY {field} | ||
| 164 | + ORDER BY count DESC | ||
| 165 | + """ | ||
| 166 | + cursor.execute(sql) | ||
| 167 | + result = cursor.fetchall() | ||
| 168 | + | ||
| 169 | + if result: | ||
| 170 | + print(f"{'值':<10} {'数量':>10}") | ||
| 171 | + print("-" * 30) | ||
| 172 | + for row in result[:10]: | ||
| 173 | + val = str(row[0]) if row[0] is not None else 'NULL' | ||
| 174 | + count = row[1] | ||
| 175 | + print(f"{val:<10} {count:>10,}") | ||
| 176 | + | ||
| 177 | + except Exception as e: | ||
| 178 | + print(f"查询失败: {e}") | ||
| 179 | + import traceback | ||
| 180 | + traceback.print_exc() | ||
| 181 | + finally: | ||
| 182 | + if 'connection' in locals(): | ||
| 183 | + connection.close() | ||
| 184 | + | ||
| 185 | +if __name__ == "__main__": | ||
| 186 | + query_buyer_type() |
| @@ -0,0 +1,139 @@ | @@ -0,0 +1,139 @@ | ||
| 1 | +#!/usr/bin/env python3 | ||
| 2 | +"""查询 buy_buyer 表的客户类型字段""" | ||
| 3 | + | ||
| 4 | +import os | ||
| 5 | +import pymysql | ||
| 6 | +import sys | ||
| 7 | +sys.path.append(os.path.dirname(os.path.abspath(__file__))) | ||
| 8 | +from config.offline_config import DB_CONFIG | ||
| 9 | + | ||
| 10 | +def query_customer_type_fields(): | ||
| 11 | + """查询客户类型相关字段""" | ||
| 12 | + try: | ||
| 13 | + connection = pymysql.connect( | ||
| 14 | + host=DB_CONFIG['host'], | ||
| 15 | + port=int(DB_CONFIG['port']), | ||
| 16 | + user=DB_CONFIG['username'], | ||
| 17 | + password=DB_CONFIG['password'], | ||
| 18 | + database=DB_CONFIG['database'], | ||
| 19 | + charset='utf8mb4' | ||
| 20 | + ) | ||
| 21 | + | ||
| 22 | + with connection.cursor() as cursor: | ||
| 23 | + # 先获取所有字段名 | ||
| 24 | + cursor.execute("DESCRIBE buy_buyer") | ||
| 25 | + all_fields = [row[0] for row in cursor.fetchall()] | ||
| 26 | + | ||
| 27 | + print("=" * 80) | ||
| 28 | + print("可能包含客户类型信息的字段") | ||
| 29 | + print("=" * 80) | ||
| 30 | + | ||
| 31 | + # 查找可能包含类型信息的字段 | ||
| 32 | + type_keywords = ['type', 'category', 'class', 'nature', 'customer'] | ||
| 33 | + relevant_fields = [] | ||
| 34 | + | ||
| 35 | + for field in all_fields: | ||
| 36 | + field_lower = field.lower() | ||
| 37 | + for keyword in type_keywords: | ||
| 38 | + if keyword in field_lower: | ||
| 39 | + relevant_fields.append(field) | ||
| 40 | + break | ||
| 41 | + | ||
| 42 | + for field in relevant_fields: | ||
| 43 | + print(f" - {field}") | ||
| 44 | + | ||
| 45 | + # 重点查询几个可能的字段 | ||
| 46 | + priority_fields = [ | ||
| 47 | + 'customer_type', | ||
| 48 | + 'buyer_from', | ||
| 49 | + 'nature_of_company', | ||
| 50 | + 'cooperate_type', | ||
| 51 | + 'price_level', | ||
| 52 | + 'maturity', | ||
| 53 | + 'status' | ||
| 54 | + ] | ||
| 55 | + | ||
| 56 | + print("\n" + "=" * 100) | ||
| 57 | + print("关键字段的值分布分析") | ||
| 58 | + print("=" * 100) | ||
| 59 | + | ||
| 60 | + for field_name in priority_fields: | ||
| 61 | + if field_name in all_fields: | ||
| 62 | + print(f"\n【{field_name}】") | ||
| 63 | + sql = f""" | ||
| 64 | + SELECT {field_name}, COUNT(*) as count | ||
| 65 | + FROM buy_buyer | ||
| 66 | + WHERE is_delete = 0 AND {field_name} IS NOT NULL | ||
| 67 | + GROUP BY {field_name} | ||
| 68 | + ORDER BY count DESC | ||
| 69 | + LIMIT 15 | ||
| 70 | + """ | ||
| 71 | + | ||
| 72 | + try: | ||
| 73 | + cursor.execute(sql) | ||
| 74 | + result = cursor.fetchall() | ||
| 75 | + | ||
| 76 | + if result: | ||
| 77 | + for row in result: | ||
| 78 | + value = row[0] if row[0] else '(NULL)' | ||
| 79 | + count = row[1] | ||
| 80 | + # 截断过长的值 | ||
| 81 | + if isinstance(value, str) and len(value) > 30: | ||
| 82 | + value = value[:27] + '...' | ||
| 83 | + print(f" {value:<35} {count:>10,} 条") | ||
| 84 | + else: | ||
| 85 | + print(f" (无数据或全为NULL)") | ||
| 86 | + | ||
| 87 | + except Exception as e: | ||
| 88 | + print(f" 查询失败: {e}") | ||
| 89 | + | ||
| 90 | + # 查询包含客户类型信息的完整记录示例 | ||
| 91 | + print("\n" + "=" * 140) | ||
| 92 | + print("包含客户类型字段的完整记录示例") | ||
| 93 | + print("=" * 140) | ||
| 94 | + | ||
| 95 | + valid_fields = [f for f in priority_fields if f in all_fields] | ||
| 96 | + select_fields = ['id', 'name', 'customer_type', 'buyer_from', | ||
| 97 | + 'nature_of_company', 'cooperate_type', 'price_level'] | ||
| 98 | + | ||
| 99 | + # 只选择存在的字段 | ||
| 100 | + final_fields = [f for f in select_fields if f in all_fields] | ||
| 101 | + | ||
| 102 | + sql = f""" | ||
| 103 | + SELECT {', '.join(final_fields)} | ||
| 104 | + FROM buy_buyer | ||
| 105 | + WHERE is_delete = 0 | ||
| 106 | + LIMIT 20 | ||
| 107 | + """ | ||
| 108 | + | ||
| 109 | + cursor.execute(sql) | ||
| 110 | + samples = cursor.fetchall() | ||
| 111 | + | ||
| 112 | + # 打印表头 | ||
| 113 | + header = " | ".join([f"{field:<20}" for field in final_fields]) | ||
| 114 | + print(header) | ||
| 115 | + print("-" * len(header)) | ||
| 116 | + | ||
| 117 | + # 打印数据 | ||
| 118 | + for sample in samples: | ||
| 119 | + row_data = [] | ||
| 120 | + for val in sample: | ||
| 121 | + if val is None: | ||
| 122 | + row_data.append(f"{'NULL':<20}") | ||
| 123 | + elif isinstance(val, str): | ||
| 124 | + val_str = (val[:17] + '..') if len(val) > 20 else val | ||
| 125 | + row_data.append(f"{val_str:<20}") | ||
| 126 | + else: | ||
| 127 | + row_data.append(f"{str(val):<20}") | ||
| 128 | + print(" | ".join(row_data)) | ||
| 129 | + | ||
| 130 | + except Exception as e: | ||
| 131 | + print(f"查询失败: {e}") | ||
| 132 | + import traceback | ||
| 133 | + traceback.print_exc() | ||
| 134 | + finally: | ||
| 135 | + if 'connection' in locals(): | ||
| 136 | + connection.close() | ||
| 137 | + | ||
| 138 | +if __name__ == "__main__": | ||
| 139 | + query_customer_type_fields() |
| @@ -0,0 +1,230 @@ | @@ -0,0 +1,230 @@ | ||
| 1 | +#!/usr/bin/env python3 | ||
| 2 | +"""查询 customer_type 字段的具体值""" | ||
| 3 | + | ||
| 4 | +import os | ||
| 5 | +import pymysql | ||
| 6 | +import sys | ||
| 7 | +sys.path.append(os.path.dirname(os.path.abspath(__file__))) | ||
| 8 | +from config.offline_config import DB_CONFIG | ||
| 9 | + | ||
| 10 | +def query_customer_type_values(): | ||
| 11 | + """查询 customer_type 的具体值""" | ||
| 12 | + try: | ||
| 13 | + connection = pymysql.connect( | ||
| 14 | + host=DB_CONFIG['host'], | ||
| 15 | + port=int(DB_CONFIG['port']), | ||
| 16 | + user=DB_CONFIG['username'], | ||
| 17 | + password=DB_CONFIG['password'], | ||
| 18 | + database=DB_CONFIG['database'], | ||
| 19 | + charset='utf8mb4' | ||
| 20 | + ) | ||
| 21 | + | ||
| 22 | + with connection.cursor() as cursor: | ||
| 23 | + print("=" * 140) | ||
| 24 | + print(" customer_type 字段详细分析") | ||
| 25 | + print("=" * 140) | ||
| 26 | + | ||
| 27 | + # 1. 查询 customer_type 的统计分布 | ||
| 28 | + print("\n【1. customer_type 字段分布统计】") | ||
| 29 | + sql = """ | ||
| 30 | + SELECT customer_type, COUNT(*) as count, | ||
| 31 | + ROUND(COUNT(*) * 100.0 / SUM(COUNT(*)) OVER(), 2) as percentage | ||
| 32 | + FROM buy_buyer | ||
| 33 | + WHERE is_delete = 0 | ||
| 34 | + GROUP BY customer_type | ||
| 35 | + ORDER BY customer_type | ||
| 36 | + """ | ||
| 37 | + cursor.execute(sql) | ||
| 38 | + result = cursor.fetchall() | ||
| 39 | + | ||
| 40 | + print(f"{'customer_type':<20} {'买家数量':>12} {'占比':>10}") | ||
| 41 | + print("-" * 60) | ||
| 42 | + total = 0 | ||
| 43 | + for row in result: | ||
| 44 | + val = str(row[0]) if row[0] is not None else 'NULL' | ||
| 45 | + count = row[1] | ||
| 46 | + percentage = row[2] | ||
| 47 | + total += count | ||
| 48 | + print(f"{val:<20} {count:>12,} {percentage:>10}%") | ||
| 49 | + | ||
| 50 | + print("-" * 60) | ||
| 51 | + print(f"{'总计':<20} {total:>12,}") | ||
| 52 | + | ||
| 53 | + # 2. 查询 customer_type=1 的详细示例 | ||
| 54 | + print("\n" + "=" * 140) | ||
| 55 | + print("【2. customer_type = 1 的买家示例(前30个)】") | ||
| 56 | + sql = """ | ||
| 57 | + SELECT id, name, customer_type, countries, lang, | ||
| 58 | + main_category, nature_of_company, buyer_from | ||
| 59 | + FROM buy_buyer | ||
| 60 | + WHERE customer_type = 1 AND is_delete = 0 | ||
| 61 | + ORDER BY id | ||
| 62 | + LIMIT 30 | ||
| 63 | + """ | ||
| 64 | + cursor.execute(sql) | ||
| 65 | + result = cursor.fetchall() | ||
| 66 | + | ||
| 67 | + if result: | ||
| 68 | + print(f"{'ID':<10} {'买家名称':<35} {'国家':<8} {'语言':<6} {'主营类目':<30} {'公司性质':<15} {'来源':<10}") | ||
| 69 | + print("-" * 140) | ||
| 70 | + | ||
| 71 | + for row in result: | ||
| 72 | + id_val = str(row[0]) | ||
| 73 | + name = (row[1][:32] + '..') if row[1] and len(row[1]) > 35 else (row[1] if row[1] else '') | ||
| 74 | + countries = str(row[3]) if row[3] else '' | ||
| 75 | + lang = str(row[4]) if row[4] else '' | ||
| 76 | + main_category = (row[5][:27] + '..') if row[5] and len(row[5]) > 30 else (row[5] if row[5] else '') | ||
| 77 | + nature = str(row[6]) if row[6] else '' | ||
| 78 | + buyer_from = str(row[7]) if row[7] else '' | ||
| 79 | + | ||
| 80 | + print(f"{id_val:<10} {name:<35} {countries:<8} {lang:<6} {main_category:<30} {nature:<15} {buyer_from:<10}") | ||
| 81 | + else: | ||
| 82 | + print(" (无数据)") | ||
| 83 | + | ||
| 84 | + # 3. 查询 customer_type=2 的详细示例 | ||
| 85 | + print("\n" + "=" * 140) | ||
| 86 | + print("【3. customer_type = 2 的买家示例(全部)】") | ||
| 87 | + sql = """ | ||
| 88 | + SELECT id, name, customer_type, countries, lang, | ||
| 89 | + main_category, nature_of_company, buyer_from | ||
| 90 | + FROM buy_buyer | ||
| 91 | + WHERE customer_type = 2 AND is_delete = 0 | ||
| 92 | + ORDER BY id | ||
| 93 | + """ | ||
| 94 | + cursor.execute(sql) | ||
| 95 | + result = cursor.fetchall() | ||
| 96 | + | ||
| 97 | + if result: | ||
| 98 | + print(f"{'ID':<10} {'买家名称':<35} {'国家':<8} {'语言':<6} {'主营类目':<30} {'公司性质':<15} {'来源':<10}") | ||
| 99 | + print("-" * 140) | ||
| 100 | + | ||
| 101 | + for row in result: | ||
| 102 | + id_val = str(row[0]) | ||
| 103 | + name = (row[1][:32] + '..') if row[1] and len(row[1]) > 35 else (row[1] if row[1] else '') | ||
| 104 | + countries = str(row[3]) if row[3] else '' | ||
| 105 | + lang = str(row[4]) if row[4] else '' | ||
| 106 | + main_category = (row[5][:27] + '..') if row[5] and len(row[5]) > 30 else (row[5] if row[5] else '') | ||
| 107 | + nature = str(row[6]) if row[6] else '' | ||
| 108 | + buyer_from = str(row[7]) if row[7] else '' | ||
| 109 | + | ||
| 110 | + print(f"{id_val:<10} {name:<35} {countries:<8} {lang:<6} {main_category:<30} {nature:<15} {buyer_from:<10}") | ||
| 111 | + else: | ||
| 112 | + print(" (无数据)") | ||
| 113 | + | ||
| 114 | + # 4. 分析 customer_type 与其他字段的关联 | ||
| 115 | + print("\n" + "=" * 140) | ||
| 116 | + print("【4. customer_type 与其他字段的关联分析】") | ||
| 117 | + | ||
| 118 | + # 与国家分布 | ||
| 119 | + print("\n 4.1 按国家分布:") | ||
| 120 | + sql = """ | ||
| 121 | + SELECT customer_type, countries, COUNT(*) as count | ||
| 122 | + FROM buy_buyer | ||
| 123 | + WHERE customer_type IS NOT NULL AND is_delete = 0 | ||
| 124 | + GROUP BY customer_type, countries | ||
| 125 | + ORDER BY customer_type, count DESC | ||
| 126 | + """ | ||
| 127 | + cursor.execute(sql) | ||
| 128 | + result = cursor.fetchall() | ||
| 129 | + | ||
| 130 | + current_type = None | ||
| 131 | + for row in result: | ||
| 132 | + ctype = row[0] | ||
| 133 | + country = row[1] if row[1] else 'NULL' | ||
| 134 | + count = row[2] | ||
| 135 | + | ||
| 136 | + if ctype != current_type: | ||
| 137 | + print(f"\n customer_type = {ctype}:") | ||
| 138 | + current_type = ctype | ||
| 139 | + | ||
| 140 | + print(f" - 国家{country}: {count} 个买家") | ||
| 141 | + | ||
| 142 | + # 与公司性质关联 | ||
| 143 | + print("\n 4.2 与 nature_of_company 关联:") | ||
| 144 | + sql = """ | ||
| 145 | + SELECT customer_type, nature_of_company, COUNT(*) as count | ||
| 146 | + FROM buy_buyer | ||
| 147 | + WHERE customer_type IS NOT NULL AND is_delete = 0 | ||
| 148 | + GROUP BY customer_type, nature_of_company | ||
| 149 | + ORDER BY customer_type, nature_of_company | ||
| 150 | + """ | ||
| 151 | + cursor.execute(sql) | ||
| 152 | + result = cursor.fetchall() | ||
| 153 | + | ||
| 154 | + for row in result: | ||
| 155 | + ctype = row[0] | ||
| 156 | + nature = row[1] if row[1] is not None else 'NULL' | ||
| 157 | + count = row[2] | ||
| 158 | + print(f" - customer_type={ctype}, nature_of_company={nature}: {count} 个买家") | ||
| 159 | + | ||
| 160 | + # 与来源关联 | ||
| 161 | + print("\n 4.3 与 buyer_from 关联:") | ||
| 162 | + sql = """ | ||
| 163 | + SELECT customer_type, buyer_from, COUNT(*) as count | ||
| 164 | + FROM buy_buyer | ||
| 165 | + WHERE customer_type IS NOT NULL AND is_delete = 0 | ||
| 166 | + GROUP BY customer_type, buyer_from | ||
| 167 | + ORDER BY customer_type, buyer_from | ||
| 168 | + """ | ||
| 169 | + cursor.execute(sql) | ||
| 170 | + result = cursor.fetchall() | ||
| 171 | + | ||
| 172 | + for row in result: | ||
| 173 | + ctype = row[0] | ||
| 174 | + bfrom = row[1] if row[1] is not None else 'NULL' | ||
| 175 | + count = row[2] | ||
| 176 | + print(f" - customer_type={ctype}, buyer_from={bfrom}: {count} 个买家") | ||
| 177 | + | ||
| 178 | + # 5. 查询是否有 customer_type 的字典定义 | ||
| 179 | + print("\n" + "=" * 140) | ||
| 180 | + print("【5. 查找 customer_type 的字典定义】") | ||
| 181 | + | ||
| 182 | + sql = """ | ||
| 183 | + SELECT i.code, i.name, il.lang, il.name as lang_name | ||
| 184 | + FROM pub_dict_item i | ||
| 185 | + LEFT JOIN pub_dict_item_lang il ON i.id = il.dict_item_id | ||
| 186 | + WHERE i.category_code = 'customer_type' | ||
| 187 | + ORDER BY i.sort_order, i.code, il.lang | ||
| 188 | + """ | ||
| 189 | + cursor.execute(sql) | ||
| 190 | + dict_result = cursor.fetchall() | ||
| 191 | + | ||
| 192 | + if dict_result: | ||
| 193 | + print(f"\n找到 customer_type 字典定义:") | ||
| 194 | + print(f"{'code':<10} {'name':<30} {'lang':<10} {'翻译名称'}") | ||
| 195 | + print("-" * 80) | ||
| 196 | + for row in dict_result: | ||
| 197 | + code = str(row[0]) if row[0] else 'NULL' | ||
| 198 | + name = row[1] if row[1] else 'NULL' | ||
| 199 | + lang = row[2] if row[2] else 'NULL' | ||
| 200 | + lang_name = row[3] if row[3] else '' | ||
| 201 | + | ||
| 202 | + if lang in ['zh', 'en', 'ru', None]: | ||
| 203 | + print(f"{code:<10} {name:<30} {lang if lang else 'NULL':<10} {lang_name}") | ||
| 204 | + else: | ||
| 205 | + print("\n未找到 customer_type 字典定义") | ||
| 206 | + print("\n尝试查找相关字典...") | ||
| 207 | + sql = """ | ||
| 208 | + SELECT code, name | ||
| 209 | + FROM pub_dict_category | ||
| 210 | + WHERE code LIKE '%customer%' OR code LIKE '%type%' | ||
| 211 | + ORDER BY code | ||
| 212 | + """ | ||
| 213 | + cursor.execute(sql) | ||
| 214 | + categories = cursor.fetchall() | ||
| 215 | + | ||
| 216 | + if categories: | ||
| 217 | + print("\n找到相关字典类别:") | ||
| 218 | + for cat in categories: | ||
| 219 | + print(f" - {cat[0]}: {cat[1]}") | ||
| 220 | + | ||
| 221 | + except Exception as e: | ||
| 222 | + print(f"查询失败: {e}") | ||
| 223 | + import traceback | ||
| 224 | + traceback.print_exc() | ||
| 225 | + finally: | ||
| 226 | + if 'connection' in locals(): | ||
| 227 | + connection.close() | ||
| 228 | + | ||
| 229 | +if __name__ == "__main__": | ||
| 230 | + query_customer_type_values() |
| @@ -0,0 +1,140 @@ | @@ -0,0 +1,140 @@ | ||
| 1 | +#!/usr/bin/env python3 | ||
| 2 | +"""查询字典表获取客户类型代码的含义""" | ||
| 3 | + | ||
| 4 | +import os | ||
| 5 | +import pymysql | ||
| 6 | +import sys | ||
| 7 | +sys.path.append(os.path.dirname(os.path.abspath(__file__))) | ||
| 8 | +from config.offline_config import DB_CONFIG | ||
| 9 | + | ||
| 10 | +def query_dict_meanings(): | ||
| 11 | + """查询字典表中的代码含义""" | ||
| 12 | + try: | ||
| 13 | + connection = pymysql.connect( | ||
| 14 | + host=DB_CONFIG['host'], | ||
| 15 | + port=int(DB_CONFIG['port']), | ||
| 16 | + user=DB_CONFIG['username'], | ||
| 17 | + password=DB_CONFIG['password'], | ||
| 18 | + database=DB_CONFIG['database'], | ||
| 19 | + charset='utf8mb4' | ||
| 20 | + ) | ||
| 21 | + | ||
| 22 | + with connection.cursor() as cursor: | ||
| 23 | + print("=" * 100) | ||
| 24 | + print("字典表结构分析") | ||
| 25 | + print("=" * 100) | ||
| 26 | + | ||
| 27 | + # 查看字典表结构 | ||
| 28 | + for table in ['pub_dict_item', 'pub_dict_item_lang']: | ||
| 29 | + print(f"\n【{table} 表结构】") | ||
| 30 | + sql = f"DESCRIBE {table}" | ||
| 31 | + cursor.execute(sql) | ||
| 32 | + result = cursor.fetchall() | ||
| 33 | + | ||
| 34 | + for row in result: | ||
| 35 | + print(f" {row[0]:<30} {row[1]:<20}") | ||
| 36 | + | ||
| 37 | + # 查询 pub_dict_category | ||
| 38 | + print("\n" + "=" * 100) | ||
| 39 | + print("【pub_dict_category 表内容】") | ||
| 40 | + sql = "SELECT * FROM pub_dict_category LIMIT 20" | ||
| 41 | + cursor.execute(sql) | ||
| 42 | + result = cursor.fetchall() | ||
| 43 | + | ||
| 44 | + cursor.execute("DESCRIBE pub_dict_category") | ||
| 45 | + columns = [row[0] for row in cursor.fetchall()] | ||
| 46 | + | ||
| 47 | + print(f"{' | '.join(columns)}") | ||
| 48 | + print("-" * 100) | ||
| 49 | + for row in result: | ||
| 50 | + print(" | ".join([str(v) if v is not None else 'NULL' for v in row])) | ||
| 51 | + | ||
| 52 | + # 查询 pub_dict_item 中与客户类型相关的条目 | ||
| 53 | + print("\n" + "=" * 100) | ||
| 54 | + print("【pub_dict_item 表内容 - 查找客户类型相关的字典】") | ||
| 55 | + sql = """ | ||
| 56 | + SELECT * FROM pub_dict_item | ||
| 57 | + WHERE dict_code LIKE '%customer%' | ||
| 58 | + OR dict_code LIKE '%buyer%' | ||
| 59 | + OR dict_code LIKE '%type%' | ||
| 60 | + OR dict_code LIKE '%nature%' | ||
| 61 | + OR dict_code LIKE '%from%' | ||
| 62 | + LIMIT 20 | ||
| 63 | + """ | ||
| 64 | + cursor.execute(sql) | ||
| 65 | + result = cursor.fetchall() | ||
| 66 | + | ||
| 67 | + if result: | ||
| 68 | + cursor.execute("DESCRIBE pub_dict_item") | ||
| 69 | + columns = [row[0] for row in cursor.fetchall()] | ||
| 70 | + | ||
| 71 | + print(f"\n找到 {len(result)} 条相关字典") | ||
| 72 | + print(f"{' | '.join(columns)}") | ||
| 73 | + print("-" * 140) | ||
| 74 | + for row in result: | ||
| 75 | + print(" | ".join([str(v) if v is not None else 'NULL' for v in row])) | ||
| 76 | + else: | ||
| 77 | + print("未找到相关字典,显示所有字典类别:") | ||
| 78 | + sql = "SELECT DISTINCT dict_code FROM pub_dict_item LIMIT 30" | ||
| 79 | + cursor.execute(sql) | ||
| 80 | + all_dicts = cursor.fetchall() | ||
| 81 | + | ||
| 82 | + for d in all_dicts: | ||
| 83 | + print(f" - {d[0]}") | ||
| 84 | + | ||
| 85 | + # 查询 pub_dict_item_lang 获取具体值 | ||
| 86 | + print("\n" + "=" * 100) | ||
| 87 | + print("【pub_dict_item_lang 表内容 - 客户类型值查询】") | ||
| 88 | + | ||
| 89 | + # 尝试查询几个常见的字典代码 | ||
| 90 | + possible_codes = ['customer_type', 'buyer_from', 'nature_of_company', 'cooperate_type'] | ||
| 91 | + | ||
| 92 | + for dict_code in possible_codes: | ||
| 93 | + print(f"\n--- 查询字典: {dict_code} ---") | ||
| 94 | + sql = """ | ||
| 95 | + SELECT i.*, il.item_name, il.lang | ||
| 96 | + FROM pub_dict_item i | ||
| 97 | + LEFT JOIN pub_dict_item_lang il ON i.id = il.dict_item_id | ||
| 98 | + WHERE i.dict_code = %s | ||
| 99 | + ORDER BY i.item_value | ||
| 100 | + """ | ||
| 101 | + cursor.execute(sql, (dict_code,)) | ||
| 102 | + result = cursor.fetchall() | ||
| 103 | + | ||
| 104 | + if result: | ||
| 105 | + print(f"{'item_value':<15} {'item_code':<20} {'item_name':<30} {'lang':<10}") | ||
| 106 | + print("-" * 80) | ||
| 107 | + for row in result: | ||
| 108 | + item_value = str(row[3]) if row[3] is not None else 'NULL' | ||
| 109 | + item_code = str(row[2]) if row[2] is not None else 'NULL' | ||
| 110 | + item_name = row[7] if row[7] is not None else 'NULL' | ||
| 111 | + lang = row[8] if row[8] is not None else 'NULL' | ||
| 112 | + print(f"{item_value:<15} {item_code:<20} {item_name:<30} {lang:<10}") | ||
| 113 | + else: | ||
| 114 | + print(" (未找到)") | ||
| 115 | + | ||
| 116 | + # 如果上述都没找到,显示所有字典代码 | ||
| 117 | + print("\n" + "=" * 100) | ||
| 118 | + print("【所有可用的字典代码】") | ||
| 119 | + sql = """ | ||
| 120 | + SELECT DISTINCT dict_code | ||
| 121 | + FROM pub_dict_item | ||
| 122 | + ORDER BY dict_code | ||
| 123 | + """ | ||
| 124 | + cursor.execute(sql) | ||
| 125 | + all_dict_codes = cursor.fetchall() | ||
| 126 | + | ||
| 127 | + print(f"总共 {len(all_dict_codes)} 个字典:") | ||
| 128 | + for i, d in enumerate(all_dict_codes, 1): | ||
| 129 | + print(f" {i:3}. {d[0]}") | ||
| 130 | + | ||
| 131 | + except Exception as e: | ||
| 132 | + print(f"查询失败: {e}") | ||
| 133 | + import traceback | ||
| 134 | + traceback.print_exc() | ||
| 135 | + finally: | ||
| 136 | + if 'connection' in locals(): | ||
| 137 | + connection.close() | ||
| 138 | + | ||
| 139 | +if __name__ == "__main__": | ||
| 140 | + query_dict_meanings() |
| @@ -0,0 +1,132 @@ | @@ -0,0 +1,132 @@ | ||
| 1 | +#!/usr/bin/env python3 | ||
| 2 | +"""查询地理代码的含义""" | ||
| 3 | + | ||
| 4 | +import os | ||
| 5 | +import pymysql | ||
| 6 | +import sys | ||
| 7 | +sys.path.append(os.path.dirname(os.path.abspath(__file__))) | ||
| 8 | +from config.offline_config import DB_CONFIG | ||
| 9 | + | ||
| 10 | +def query_geo_info(): | ||
| 11 | + """查询地理代码相关信息""" | ||
| 12 | + try: | ||
| 13 | + connection = pymysql.connect( | ||
| 14 | + host=DB_CONFIG['host'], | ||
| 15 | + port=int(DB_CONFIG['port']), | ||
| 16 | + user=DB_CONFIG['username'], | ||
| 17 | + password=DB_CONFIG['password'], | ||
| 18 | + database=DB_CONFIG['database'], | ||
| 19 | + charset='utf8mb4' | ||
| 20 | + ) | ||
| 21 | + | ||
| 22 | + with connection.cursor() as cursor: | ||
| 23 | + print("=" * 100) | ||
| 24 | + print("国家代码示例 (countries 字段)") | ||
| 25 | + print("=" * 100) | ||
| 26 | + | ||
| 27 | + # 查询不同国家代码的示例 | ||
| 28 | + sql = """ | ||
| 29 | + SELECT | ||
| 30 | + countries, | ||
| 31 | + COUNT(*) as count | ||
| 32 | + FROM buy_buyer | ||
| 33 | + WHERE is_delete = 0 AND countries IS NOT NULL | ||
| 34 | + GROUP BY countries | ||
| 35 | + ORDER BY count DESC | ||
| 36 | + LIMIT 15 | ||
| 37 | + """ | ||
| 38 | + cursor.execute(sql) | ||
| 39 | + result = cursor.fetchall() | ||
| 40 | + | ||
| 41 | + for row in result: | ||
| 42 | + country_code = row[0] | ||
| 43 | + count = row[1] | ||
| 44 | + print(f"国家代码 {country_code:<6} | 买家数量: {count:>6}") | ||
| 45 | + | ||
| 46 | + print("\n" + "=" * 100) | ||
| 47 | + print("有地理信息的完整记录示例") | ||
| 48 | + print("=" * 100) | ||
| 49 | + | ||
| 50 | + # 查询有完整地理信息的记录 | ||
| 51 | + sql = """ | ||
| 52 | + SELECT id, lang, customer_brand, | ||
| 53 | + countries, province, city, area, city_code, | ||
| 54 | + address_city_code, address_city_name | ||
| 55 | + FROM buy_buyer | ||
| 56 | + WHERE is_delete = 0 | ||
| 57 | + AND countries IS NOT NULL | ||
| 58 | + AND province IS NOT NULL | ||
| 59 | + LIMIT 15 | ||
| 60 | + """ | ||
| 61 | + cursor.execute(sql) | ||
| 62 | + result = cursor.fetchall() | ||
| 63 | + | ||
| 64 | + print(f"{'ID':<10} {'语言':<8} {'国家':<8} {'省份':<10} {'城市':<15} {'区域':<10} {'城市代码':<12} {'地址城市代码':<15} {'地址城市名称'}") | ||
| 65 | + print("-" * 140) | ||
| 66 | + | ||
| 67 | + for row in result: | ||
| 68 | + id_val = str(row[0]) | ||
| 69 | + lang = row[1] if row[1] else '' | ||
| 70 | + brand = row[2][:12] + '..' if row[2] and len(row[2]) > 15 else (row[2] if row[2] else '') | ||
| 71 | + countries = str(row[3]) if row[3] is not None else '' | ||
| 72 | + province = str(row[4]) if row[4] is not None else '' | ||
| 73 | + city = str(row[5])[:12] if row[5] else '' | ||
| 74 | + area = str(row[6]) if row[6] is not None else '' | ||
| 75 | + city_code = str(row[7]) if row[7] is not None else '' | ||
| 76 | + addr_city_code = str(row[8]) if row[8] is not None else '' | ||
| 77 | + addr_city_name = str(row[9])[:15] if row[9] else '' | ||
| 78 | + | ||
| 79 | + print(f"{id_val:<10} {lang:<8} {countries:<8} {province:<10} {city:<15} {area:<10} {city_code:<12} {addr_city_code:<15} {addr_city_name}") | ||
| 80 | + | ||
| 81 | + print("\n" + "=" * 100) | ||
| 82 | + print("城市代码 (city_code) 字段分析") | ||
| 83 | + print("=" * 100) | ||
| 84 | + | ||
| 85 | + sql = """ | ||
| 86 | + SELECT city_code, COUNT(*) as count, countries | ||
| 87 | + FROM buy_buyer | ||
| 88 | + WHERE is_delete = 0 AND city_code IS NOT NULL AND city_code != '' | ||
| 89 | + GROUP BY city_code, countries | ||
| 90 | + ORDER BY count DESC | ||
| 91 | + LIMIT 15 | ||
| 92 | + """ | ||
| 93 | + cursor.execute(sql) | ||
| 94 | + result = cursor.fetchall() | ||
| 95 | + | ||
| 96 | + for row in result: | ||
| 97 | + city_code = row[0] | ||
| 98 | + count = row[1] | ||
| 99 | + countries = row[2] | ||
| 100 | + print(f"城市代码: {city_code:<12} | 数量: {count:>6} | 国家代码: {countries}") | ||
| 101 | + | ||
| 102 | + print("\n" + "=" * 100) | ||
| 103 | + print("地址城市代码 (address_city_code) 字段分析") | ||
| 104 | + print("=" * 100) | ||
| 105 | + | ||
| 106 | + sql = """ | ||
| 107 | + SELECT address_city_code, address_city_name, COUNT(*) as count | ||
| 108 | + FROM buy_buyer | ||
| 109 | + WHERE is_delete = 0 AND address_city_code IS NOT NULL AND address_city_code != '' | ||
| 110 | + GROUP BY address_city_code, address_city_name | ||
| 111 | + ORDER BY count DESC | ||
| 112 | + LIMIT 15 | ||
| 113 | + """ | ||
| 114 | + cursor.execute(sql) | ||
| 115 | + result = cursor.fetchall() | ||
| 116 | + | ||
| 117 | + for row in result: | ||
| 118 | + city_code = row[0] | ||
| 119 | + city_name = row[1] if row[1] else '' | ||
| 120 | + count = row[2] | ||
| 121 | + print(f"代码: {city_code:<12} | 名称: {city_name:<30} | 数量: {count:>6}") | ||
| 122 | + | ||
| 123 | + except Exception as e: | ||
| 124 | + print(f"查询失败: {e}") | ||
| 125 | + import traceback | ||
| 126 | + traceback.print_exc() | ||
| 127 | + finally: | ||
| 128 | + if 'connection' in locals(): | ||
| 129 | + connection.close() | ||
| 130 | + | ||
| 131 | +if __name__ == "__main__": | ||
| 132 | + query_geo_info() |
offline_tasks/test/query_sensors_client_platform.py
0 → 100644
| @@ -0,0 +1,140 @@ | @@ -0,0 +1,140 @@ | ||
| 1 | +#!/usr/bin/env python3 | ||
| 2 | +"""查询 sensors_events 表的 client_platform 字段值""" | ||
| 3 | + | ||
| 4 | +import os | ||
| 5 | +import pymysql | ||
| 6 | +import sys | ||
| 7 | +sys.path.append(os.path.dirname(os.path.abspath(__file__))) | ||
| 8 | +from config.offline_config import DB_CONFIG | ||
| 9 | + | ||
| 10 | +def query_client_platform(): | ||
| 11 | + """查询 client_platform 字段值分布""" | ||
| 12 | + try: | ||
| 13 | + connection = pymysql.connect( | ||
| 14 | + host=DB_CONFIG['host'], | ||
| 15 | + port=int(DB_CONFIG['port']), | ||
| 16 | + user=DB_CONFIG['username'], | ||
| 17 | + password=DB_CONFIG['password'], | ||
| 18 | + database=DB_CONFIG['database'], | ||
| 19 | + charset='utf8mb4' | ||
| 20 | + ) | ||
| 21 | + | ||
| 22 | + with connection.cursor() as cursor: | ||
| 23 | + print("=" * 80) | ||
| 24 | + print("sensors_events 表的 client_platform 字段值分布") | ||
| 25 | + print("=" * 80) | ||
| 26 | + | ||
| 27 | + # 查询 client_platform 的分布情况 | ||
| 28 | + sql = """ | ||
| 29 | + SELECT | ||
| 30 | + client_platform, | ||
| 31 | + COUNT(*) as count, | ||
| 32 | + ROUND(COUNT(*) * 100.0 / SUM(COUNT(*)) OVER(), 2) as percentage | ||
| 33 | + FROM sensors_events | ||
| 34 | + WHERE client_platform IS NOT NULL | ||
| 35 | + GROUP BY client_platform | ||
| 36 | + ORDER BY count DESC | ||
| 37 | + """ | ||
| 38 | + | ||
| 39 | + cursor.execute(sql) | ||
| 40 | + result = cursor.fetchall() | ||
| 41 | + | ||
| 42 | + print(f"{'client_platform':<30} {'数量':>15} {'占比':>10}") | ||
| 43 | + print("-" * 80) | ||
| 44 | + | ||
| 45 | + total_count = 0 | ||
| 46 | + for row in result: | ||
| 47 | + platform = row[0] if row[0] else '(NULL)' | ||
| 48 | + count = row[1] | ||
| 49 | + percentage = row[2] | ||
| 50 | + total_count += count | ||
| 51 | + | ||
| 52 | + print(f"{platform:<30} {count:>15,} {percentage:>10}%") | ||
| 53 | + | ||
| 54 | + print("-" * 80) | ||
| 55 | + print(f"{'总计':<30} {total_count:>15,}") | ||
| 56 | + | ||
| 57 | + # 查询一些示例记录 | ||
| 58 | + print("\n" + "=" * 100) | ||
| 59 | + print("示例记录(包含 client_platform 相关字段)") | ||
| 60 | + print("=" * 100) | ||
| 61 | + | ||
| 62 | + sql_sample = """ | ||
| 63 | + SELECT | ||
| 64 | + id, | ||
| 65 | + client_platform, | ||
| 66 | + platform, | ||
| 67 | + browser, | ||
| 68 | + browser_language, | ||
| 69 | + device_id, | ||
| 70 | + event, | ||
| 71 | + time | ||
| 72 | + FROM sensors_events | ||
| 73 | + WHERE client_platform IS NOT NULL | ||
| 74 | + LIMIT 20 | ||
| 75 | + """ | ||
| 76 | + | ||
| 77 | + cursor.execute(sql_sample) | ||
| 78 | + samples = cursor.fetchall() | ||
| 79 | + | ||
| 80 | + print(f"{'ID':<15} {'client_platform':<20} {'platform':<15} {'browser':<20} {'browser_language':<15} {'event':<30}") | ||
| 81 | + print("-" * 140) | ||
| 82 | + | ||
| 83 | + for sample in samples: | ||
| 84 | + id_val = str(sample[0]) | ||
| 85 | + client_platform = sample[1] if sample[1] else '' | ||
| 86 | + platform = sample[2] if sample[2] else '' | ||
| 87 | + browser = sample[3] if sample[3] else '' | ||
| 88 | + browser_language = sample[4] if sample[4] else '' | ||
| 89 | + event = sample[6] if sample[6] else '' | ||
| 90 | + | ||
| 91 | + # 截断过长的字段 | ||
| 92 | + event = (event[:27] + '..') if len(event) > 30 else event | ||
| 93 | + | ||
| 94 | + print(f"{id_val:<15} {client_platform:<20} {platform:<15} {browser:<20} {browser_language:<15} {event:<30}") | ||
| 95 | + | ||
| 96 | + print("-" * 140) | ||
| 97 | + print(f"总计: {len(samples)} 条示例记录") | ||
| 98 | + | ||
| 99 | + # 查询 client_platform 和 platform 的组合分布 | ||
| 100 | + print("\n" + "=" * 100) | ||
| 101 | + print("client_platform 与 platform 的组合分布") | ||
| 102 | + print("=" * 100) | ||
| 103 | + | ||
| 104 | + sql_combo = """ | ||
| 105 | + SELECT | ||
| 106 | + client_platform, | ||
| 107 | + platform, | ||
| 108 | + COUNT(*) as count | ||
| 109 | + FROM sensors_events | ||
| 110 | + WHERE client_platform IS NOT NULL | ||
| 111 | + AND platform IS NOT NULL | ||
| 112 | + GROUP BY client_platform, platform | ||
| 113 | + ORDER BY client_platform, count DESC | ||
| 114 | + """ | ||
| 115 | + | ||
| 116 | + cursor.execute(sql_combo) | ||
| 117 | + combos = cursor.fetchall() | ||
| 118 | + | ||
| 119 | + current_platform = None | ||
| 120 | + for combo in combos: | ||
| 121 | + client_platform = combo[0] | ||
| 122 | + platform = combo[1] | ||
| 123 | + count = combo[2] | ||
| 124 | + | ||
| 125 | + if client_platform != current_platform: | ||
| 126 | + print(f"\n【{client_platform}】") | ||
| 127 | + current_platform = client_platform | ||
| 128 | + | ||
| 129 | + print(f" {platform:<30} {count:>12,} 条") | ||
| 130 | + | ||
| 131 | + except Exception as e: | ||
| 132 | + print(f"查询失败: {e}") | ||
| 133 | + import traceback | ||
| 134 | + traceback.print_exc() | ||
| 135 | + finally: | ||
| 136 | + if 'connection' in locals(): | ||
| 137 | + connection.close() | ||
| 138 | + | ||
| 139 | +if __name__ == "__main__": | ||
| 140 | + query_client_platform() |
| @@ -0,0 +1,127 @@ | @@ -0,0 +1,127 @@ | ||
| 1 | +#!/usr/bin/env python3 | ||
| 2 | +"""查询 sensors_events 表的字段信息""" | ||
| 3 | + | ||
| 4 | +import os | ||
| 5 | +import pymysql | ||
| 6 | +import sys | ||
| 7 | +sys.path.append(os.path.dirname(os.path.abspath(__file__))) | ||
| 8 | +from config.offline_config import DB_CONFIG | ||
| 9 | + | ||
| 10 | +def query_table_structure(): | ||
| 11 | + """查询表结构""" | ||
| 12 | + try: | ||
| 13 | + connection = pymysql.connect( | ||
| 14 | + host=DB_CONFIG['host'], | ||
| 15 | + port=int(DB_CONFIG['port']), | ||
| 16 | + user=DB_CONFIG['username'], | ||
| 17 | + password=DB_CONFIG['password'], | ||
| 18 | + database=DB_CONFIG['database'], | ||
| 19 | + charset='utf8mb4' | ||
| 20 | + ) | ||
| 21 | + | ||
| 22 | + with connection.cursor() as cursor: | ||
| 23 | + # 查询表结构 | ||
| 24 | + sql = "DESCRIBE sensors_events" | ||
| 25 | + cursor.execute(sql) | ||
| 26 | + result = cursor.fetchall() | ||
| 27 | + | ||
| 28 | + print("=" * 100) | ||
| 29 | + print(f"{'字段名':<30} {'类型':<25} {'允许NULL':<10} {'键':<10} {'额外信息'}") | ||
| 30 | + print("=" * 100) | ||
| 31 | + | ||
| 32 | + for row in result: | ||
| 33 | + field = row[0] | ||
| 34 | + type_info = row[1] | ||
| 35 | + null = row[2] | ||
| 36 | + key = row[3] | ||
| 37 | + extra = row[5] if len(row) > 5 else '' | ||
| 38 | + | ||
| 39 | + # 只显示主要字段 | ||
| 40 | + if any(keyword in field.lower() for keyword in ['platform', 'browser', 'device', 'event', 'time', 'user', 'id']): | ||
| 41 | + print(f"{field:<30} {type_info:<25} {null:<10} {key:<10} {extra}") | ||
| 42 | + | ||
| 43 | + print("=" * 100) | ||
| 44 | + print(f"\n表总共有 {len(result)} 个字段") | ||
| 45 | + | ||
| 46 | + # 查询 client_platform 的详细分布 | ||
| 47 | + print("\n" + "=" * 80) | ||
| 48 | + print("client_platform 字段值分布详情") | ||
| 49 | + print("=" * 80) | ||
| 50 | + | ||
| 51 | + sql_platform = """ | ||
| 52 | + SELECT | ||
| 53 | + client_platform, | ||
| 54 | + COUNT(*) as count, | ||
| 55 | + ROUND(COUNT(*) * 100.0 / SUM(COUNT(*)) OVER(), 2) as percentage | ||
| 56 | + FROM sensors_events | ||
| 57 | + GROUP BY client_platform | ||
| 58 | + ORDER BY count DESC | ||
| 59 | + """ | ||
| 60 | + | ||
| 61 | + cursor.execute(sql_platform) | ||
| 62 | + platforms = cursor.fetchall() | ||
| 63 | + | ||
| 64 | + print(f"{'client_platform':<20} {'记录数':>15} {'占比':>10}") | ||
| 65 | + print("-" * 80) | ||
| 66 | + | ||
| 67 | + for plat in platforms: | ||
| 68 | + platform = plat[0] if plat[0] else '(NULL)' | ||
| 69 | + count = plat[1] | ||
| 70 | + percentage = plat[2] if plat[2] else 0 | ||
| 71 | + print(f"{platform:<20} {count:>15,} {percentage:>10}%") | ||
| 72 | + | ||
| 73 | + # 查询示例数据 | ||
| 74 | + print("\n" + "=" * 120) | ||
| 75 | + print("示例记录(包含 client_platform 相关字段)") | ||
| 76 | + print("=" * 120) | ||
| 77 | + | ||
| 78 | + # 获取所有字段名 | ||
| 79 | + cursor.execute("DESCRIBE sensors_events") | ||
| 80 | + all_fields = [row[0] for row in cursor.fetchall()] | ||
| 81 | + | ||
| 82 | + # 选择相关字段进行展示 | ||
| 83 | + display_fields = [] | ||
| 84 | + for field in all_fields: | ||
| 85 | + if any(keyword in field.lower() for keyword in ['id', 'platform', 'browser', 'device', 'event', 'time', 'user', 'type']): | ||
| 86 | + display_fields.append(field) | ||
| 87 | + | ||
| 88 | + # 限制显示字段数量 | ||
| 89 | + display_fields = display_fields[:8] | ||
| 90 | + | ||
| 91 | + sql_sample = f""" | ||
| 92 | + SELECT {', '.join(display_fields)} | ||
| 93 | + FROM sensors_events | ||
| 94 | + LIMIT 10 | ||
| 95 | + """ | ||
| 96 | + | ||
| 97 | + cursor.execute(sql_sample) | ||
| 98 | + samples = cursor.fetchall() | ||
| 99 | + | ||
| 100 | + # 打印表头 | ||
| 101 | + header = " | ".join([f"{field:<15}" for field in display_fields]) | ||
| 102 | + print(header) | ||
| 103 | + print("-" * len(header)) | ||
| 104 | + | ||
| 105 | + # 打印数据 | ||
| 106 | + for sample in samples: | ||
| 107 | + row_data = [] | ||
| 108 | + for i, val in enumerate(sample): | ||
| 109 | + if val is None: | ||
| 110 | + row_data.append(f"{'NULL':<15}") | ||
| 111 | + elif isinstance(val, str): | ||
| 112 | + val_str = (val[:12] + '..') if len(val) > 15 else val | ||
| 113 | + row_data.append(f"{val_str:<15}") | ||
| 114 | + else: | ||
| 115 | + row_data.append(f"{str(val):<15}") | ||
| 116 | + print(" | ".join(row_data)) | ||
| 117 | + | ||
| 118 | + except Exception as e: | ||
| 119 | + print(f"查询失败: {e}") | ||
| 120 | + import traceback | ||
| 121 | + traceback.print_exc() | ||
| 122 | + finally: | ||
| 123 | + if 'connection' in locals(): | ||
| 124 | + connection.close() | ||
| 125 | + | ||
| 126 | +if __name__ == "__main__": | ||
| 127 | + query_table_structure() |
| @@ -0,0 +1,146 @@ | @@ -0,0 +1,146 @@ | ||
| 1 | +#!/usr/bin/env python3 | ||
| 2 | +"""查询 sensors_events 表的 platform 组合分布""" | ||
| 3 | + | ||
| 4 | +import os | ||
| 5 | +import pymysql | ||
| 6 | +import sys | ||
| 7 | +sys.path.append(os.path.dirname(os.path.abspath(__file__))) | ||
| 8 | +from config.offline_config import DB_CONFIG | ||
| 9 | + | ||
| 10 | +def query_platform_combinations(): | ||
| 11 | + """查询 platform 相关字段的组合分布""" | ||
| 12 | + try: | ||
| 13 | + connection = pymysql.connect( | ||
| 14 | + host=DB_CONFIG['host'], | ||
| 15 | + port=int(DB_CONFIG['port']), | ||
| 16 | + user=DB_CONFIG['username'], | ||
| 17 | + password=DB_CONFIG['password'], | ||
| 18 | + database=DB_CONFIG['database'], | ||
| 19 | + charset='utf8mb4' | ||
| 20 | + ) | ||
| 21 | + | ||
| 22 | + with connection.cursor() as cursor: | ||
| 23 | + print("=" * 100) | ||
| 24 | + print("client_platform 与 business_platform 的组合分布") | ||
| 25 | + print("=" * 100) | ||
| 26 | + | ||
| 27 | + sql = """ | ||
| 28 | + SELECT | ||
| 29 | + client_platform, | ||
| 30 | + business_platform, | ||
| 31 | + COUNT(*) as count, | ||
| 32 | + ROUND(COUNT(*) * 100.0 / SUM(COUNT(*)) OVER(PARTITION BY client_platform), 2) as percentage_in_client | ||
| 33 | + FROM sensors_events | ||
| 34 | + WHERE client_platform IS NOT NULL | ||
| 35 | + AND business_platform IS NOT NULL | ||
| 36 | + GROUP BY client_platform, business_platform | ||
| 37 | + ORDER BY client_platform, count DESC | ||
| 38 | + """ | ||
| 39 | + | ||
| 40 | + cursor.execute(sql) | ||
| 41 | + result = cursor.fetchall() | ||
| 42 | + | ||
| 43 | + current_platform = None | ||
| 44 | + for row in result: | ||
| 45 | + client_platform = row[0] | ||
| 46 | + business_platform = row[1] | ||
| 47 | + count = row[2] | ||
| 48 | + percentage = row[3] | ||
| 49 | + | ||
| 50 | + if client_platform != current_platform: | ||
| 51 | + print(f"\n【client_platform = {client_platform}】") | ||
| 52 | + current_platform = client_platform | ||
| 53 | + | ||
| 54 | + print(f" business_platform: {business_platform:<20} | 数量: {count:>12,} | 占该平台: {percentage:>6}%") | ||
| 55 | + | ||
| 56 | + # 查询不同 client_platform 的用户行为统计 | ||
| 57 | + print("\n" + "=" * 100) | ||
| 58 | + print("不同 client_platform 的用户行为统计") | ||
| 59 | + print("=" * 100) | ||
| 60 | + | ||
| 61 | + sql_events = """ | ||
| 62 | + SELECT | ||
| 63 | + client_platform, | ||
| 64 | + event, | ||
| 65 | + COUNT(*) as count | ||
| 66 | + FROM sensors_events | ||
| 67 | + WHERE client_platform IS NOT NULL | ||
| 68 | + AND event IS NOT NULL | ||
| 69 | + GROUP BY client_platform, event | ||
| 70 | + ORDER BY client_platform, count DESC | ||
| 71 | + """ | ||
| 72 | + | ||
| 73 | + cursor.execute(sql_events) | ||
| 74 | + events = cursor.fetchall() | ||
| 75 | + | ||
| 76 | + current_platform = None | ||
| 77 | + event_count = 0 | ||
| 78 | + max_events = 5 # 每个平台最多显示5个事件 | ||
| 79 | + | ||
| 80 | + for row in events: | ||
| 81 | + client_platform = row[0] | ||
| 82 | + event = row[1] | ||
| 83 | + count = row[2] | ||
| 84 | + | ||
| 85 | + if client_platform != current_platform: | ||
| 86 | + if current_platform is not None: | ||
| 87 | + print("") | ||
| 88 | + print(f"\n【client_platform = {client_platform}】Top 事件:") | ||
| 89 | + current_platform = client_platform | ||
| 90 | + event_count = 0 | ||
| 91 | + | ||
| 92 | + if event_count < max_events: | ||
| 93 | + print(f" {event:<40} | {count:>12,}") | ||
| 94 | + event_count += 1 | ||
| 95 | + | ||
| 96 | + # 查询不同 client_platform 的时间分布 | ||
| 97 | + print("\n" + "=" * 100) | ||
| 98 | + print("不同 client_platform 的时间分布(最近30天)") | ||
| 99 | + print("=" * 100) | ||
| 100 | + | ||
| 101 | + sql_time = """ | ||
| 102 | + SELECT | ||
| 103 | + client_platform, | ||
| 104 | + DATE(create_time) as event_date, | ||
| 105 | + COUNT(*) as count | ||
| 106 | + FROM sensors_events | ||
| 107 | + WHERE client_platform IS NOT NULL | ||
| 108 | + AND create_time >= DATE_SUB(CURDATE(), INTERVAL 30 DAY) | ||
| 109 | + GROUP BY client_platform, DATE(create_time) | ||
| 110 | + ORDER BY client_platform, event_date DESC | ||
| 111 | + LIMIT 30 | ||
| 112 | + """ | ||
| 113 | + | ||
| 114 | + cursor.execute(sql_time) | ||
| 115 | + time_data = cursor.fetchall() | ||
| 116 | + | ||
| 117 | + current_platform = None | ||
| 118 | + day_count = 0 | ||
| 119 | + max_days = 5 # 每个平台最多显示5天 | ||
| 120 | + | ||
| 121 | + for row in time_data: | ||
| 122 | + client_platform = row[0] | ||
| 123 | + event_date = str(row[1]) | ||
| 124 | + count = row[2] | ||
| 125 | + | ||
| 126 | + if client_platform != current_platform: | ||
| 127 | + if current_platform is not None: | ||
| 128 | + print("") | ||
| 129 | + print(f"\n【client_platform = {client_platform}】最近活动:") | ||
| 130 | + current_platform = client_platform | ||
| 131 | + day_count = 0 | ||
| 132 | + | ||
| 133 | + if day_count < max_days: | ||
| 134 | + print(f" {event_date} | {count:>12,}") | ||
| 135 | + day_count += 1 | ||
| 136 | + | ||
| 137 | + except Exception as e: | ||
| 138 | + print(f"查询失败: {e}") | ||
| 139 | + import traceback | ||
| 140 | + traceback.print_exc() | ||
| 141 | + finally: | ||
| 142 | + if 'connection' in locals(): | ||
| 143 | + connection.close() | ||
| 144 | + | ||
| 145 | +if __name__ == "__main__": | ||
| 146 | + query_platform_combinations() |
| @@ -0,0 +1,191 @@ | @@ -0,0 +1,191 @@ | ||
| 1 | +#!/usr/bin/env python3 | ||
| 2 | +"""验证 buyer_nature 字典信息的来源""" | ||
| 3 | + | ||
| 4 | +import os | ||
| 5 | +import pymysql | ||
| 6 | +import sys | ||
| 7 | +sys.path.append(os.path.dirname(os.path.abspath(__file__))) | ||
| 8 | +from config.offline_config import DB_CONFIG | ||
| 9 | + | ||
| 10 | +def verify_buyer_nature_source(): | ||
| 11 | + """验证 buyer_nature 字典来源""" | ||
| 12 | + try: | ||
| 13 | + connection = pymysql.connect( | ||
| 14 | + host=DB_CONFIG['host'], | ||
| 15 | + port=int(DB_CONFIG['port']), | ||
| 16 | + user=DB_CONFIG['username'], | ||
| 17 | + password=DB_CONFIG['password'], | ||
| 18 | + database=DB_CONFIG['database'], | ||
| 19 | + charset='utf8mb4' | ||
| 20 | + ) | ||
| 21 | + | ||
| 22 | + with connection.cursor() as cursor: | ||
| 23 | + print("=" * 120) | ||
| 24 | + print(" 验证 buyer_nature 字典信息的来源") | ||
| 25 | + print("=" * 120) | ||
| 26 | + | ||
| 27 | + # 1. 查询 pub_dict_category 表,确认 buyer_nature 类别存在 | ||
| 28 | + print("\n【步骤1】查询 pub_dict_category 表,确认 buyer_nature 字典类别") | ||
| 29 | + print("=" * 120) | ||
| 30 | + | ||
| 31 | + sql = """ | ||
| 32 | + SELECT code, name, id | ||
| 33 | + FROM pub_dict_category | ||
| 34 | + WHERE code = 'buyer_nature' | ||
| 35 | + """ | ||
| 36 | + cursor.execute(sql) | ||
| 37 | + category_result = cursor.fetchall() | ||
| 38 | + | ||
| 39 | + if category_result: | ||
| 40 | + for row in category_result: | ||
| 41 | + print(f"\n✓ 找到字典类别:") | ||
| 42 | + print(f" - ID: {row[2]}") | ||
| 43 | + print(f" - code: {row[0]}") | ||
| 44 | + print(f" - name: {row[1]}") | ||
| 45 | + else: | ||
| 46 | + print("\n✗ 未找到 buyer_nature 字典类别") | ||
| 47 | + return | ||
| 48 | + | ||
| 49 | + # 2. 查询 pub_dict_item 表,获取 buyer_nature 的所有字典项 | ||
| 50 | + print("\n" + "=" * 120) | ||
| 51 | + print("【步骤2】查询 pub_dict_item 表,获取 buyer_nature 的所有字典项") | ||
| 52 | + print("=" * 120) | ||
| 53 | + | ||
| 54 | + sql = """ | ||
| 55 | + SELECT id, code, name, sort_order, is_enable, is_delete | ||
| 56 | + FROM pub_dict_item | ||
| 57 | + WHERE category_code = 'buyer_nature' | ||
| 58 | + ORDER BY sort_order, code | ||
| 59 | + """ | ||
| 60 | + cursor.execute(sql) | ||
| 61 | + items = cursor.fetchall() | ||
| 62 | + | ||
| 63 | + if items: | ||
| 64 | + print(f"\n✓ 找到 {len(items)} 个字典项:\n") | ||
| 65 | + print(f"{'ID':<10} {'code':<10} {'name':<40} {'sort_order':<15} {'is_enable':<10} {'is_delete':<10}") | ||
| 66 | + print("-" * 120) | ||
| 67 | + | ||
| 68 | + for item in items: | ||
| 69 | + item_id = item[0] | ||
| 70 | + code = str(item[1]) if item[1] else 'NULL' | ||
| 71 | + name = item[2] if item[2] else 'NULL' | ||
| 72 | + sort_order = item[3] if item[3] else 'NULL' | ||
| 73 | + is_enable = item[4] if item[4] is not None else 'NULL' | ||
| 74 | + is_delete = item[5] if item[5] is not None else 'NULL' | ||
| 75 | + | ||
| 76 | + print(f"{item_id:<10} {code:<10} {name:<40} {sort_order:<15} {is_enable:<10} {is_delete:<10}") | ||
| 77 | + else: | ||
| 78 | + print("\n✗ 未找到字典项") | ||
| 79 | + | ||
| 80 | + # 3. 查询 pub_dict_item_lang 表,获取多语言翻译 | ||
| 81 | + print("\n" + "=" * 120) | ||
| 82 | + print("【步骤3】查询 pub_dict_item_lang 表,获取多语言翻译") | ||
| 83 | + print("=" * 120) | ||
| 84 | + | ||
| 85 | + sql = """ | ||
| 86 | + SELECT i.id, i.code, i.name as zh_name, | ||
| 87 | + il.lang, il.name as lang_name | ||
| 88 | + FROM pub_dict_item i | ||
| 89 | + LEFT JOIN pub_dict_item_lang il ON i.id = il.dict_item_id | ||
| 90 | + WHERE i.category_code = 'buyer_nature' | ||
| 91 | + ORDER BY i.sort_order, i.code, il.lang | ||
| 92 | + """ | ||
| 93 | + cursor.execute(sql) | ||
| 94 | + lang_items = cursor.fetchall() | ||
| 95 | + | ||
| 96 | + if lang_items: | ||
| 97 | + print(f"\n✓ 找到 {len(lang_items)} 条翻译记录:\n") | ||
| 98 | + print(f"{'ID':<8} {'code':<8} {'中文名称':<25} {'lang':<8} {'翻译名称'}") | ||
| 99 | + print("-" * 120) | ||
| 100 | + | ||
| 101 | + current_code = None | ||
| 102 | + for item in lang_items: | ||
| 103 | + item_id = item[0] | ||
| 104 | + code = str(item[1]) if item[1] else 'NULL' | ||
| 105 | + zh_name = item[2] if item[2] else 'NULL' | ||
| 106 | + lang = item[3] if item[3] else 'NULL' | ||
| 107 | + lang_name = item[4] if item[4] else '' | ||
| 108 | + | ||
| 109 | + if code != current_code: | ||
| 110 | + if current_code is not None: | ||
| 111 | + print() # 空行分隔 | ||
| 112 | + current_code = code | ||
| 113 | + | ||
| 114 | + lang_display = f"{lang}:" if lang else '' | ||
| 115 | + print(f"{item_id:<8} {code:<8} {zh_name:<25} {lang_display:<8} {lang_name}") | ||
| 116 | + else: | ||
| 117 | + print("\n✗ 未找到翻译记录") | ||
| 118 | + | ||
| 119 | + # 4. 总结信息来源 | ||
| 120 | + print("\n" + "=" * 120) | ||
| 121 | + print("【总结】buyer_nature 字典信息的来源") | ||
| 122 | + print("=" * 120) | ||
| 123 | + | ||
| 124 | + print(""" | ||
| 125 | +这些信息来源于数据库中的字典表系统: | ||
| 126 | + | ||
| 127 | +1️⃣ pub_dict_category(字典类别表) | ||
| 128 | + - 存储字典类别定义 | ||
| 129 | + - buyer_nature 是其中一个类别,表示"公司性质" | ||
| 130 | + | ||
| 131 | +2️⃣ pub_dict_item(字典项表) | ||
| 132 | + - 存储具体的字典项 | ||
| 133 | + - 包含字段:id, category_code, code, name, sort_order 等 | ||
| 134 | + - buyer_nature 的字典项包括: | ||
| 135 | + • code=1, name="贸易公司" | ||
| 136 | + • code=2, name="零售商" | ||
| 137 | + • code=3, name="批发商" | ||
| 138 | + • code=4, name="分销商" | ||
| 139 | + • code=5, name="代理商" | ||
| 140 | + • code=6, name="自有品牌" | ||
| 141 | + • code=7, name="其他" | ||
| 142 | + | ||
| 143 | +3️⃣ pub_dict_item_lang(字典项多语言表) | ||
| 144 | + - 存储字典项的多语言翻译 | ||
| 145 | + - 包含字段:dict_item_id, lang, name 等 | ||
| 146 | + - 支持 zh(中文), en(英文), ru(俄语) 等多种语言 | ||
| 147 | + | ||
| 148 | +💡 数据库结构: | ||
| 149 | + pub_dict_category (1) ←→ (N) pub_dict_item (1) ←→ (N) pub_dict_item_lang | ||
| 150 | + ↓ ↓ ↓ | ||
| 151 | + 字典类别定义 具体字典项 多语言翻译 | ||
| 152 | + | ||
| 153 | +⚠️ 重要发现: | ||
| 154 | + - buyer_nature 字典在数据库中已定义 | ||
| 155 | + - 但 buy_buyer 表中并没有使用这个字段 | ||
| 156 | + - 这是一个"已定义但未使用"的字典 | ||
| 157 | + """) | ||
| 158 | + | ||
| 159 | + # 5. 验证表结构 | ||
| 160 | + print("\n" + "=" * 120) | ||
| 161 | + print("【验证】显示相关表结构") | ||
| 162 | + print("=" * 120) | ||
| 163 | + | ||
| 164 | + print("\n1️⃣ pub_dict_category 表结构(关键字段):") | ||
| 165 | + cursor.execute("DESCRIBE pub_dict_category") | ||
| 166 | + cols = cursor.fetchall() | ||
| 167 | + for col in cols[:10]: | ||
| 168 | + print(f" - {col[0]:<20} {col[1]:<20}") | ||
| 169 | + | ||
| 170 | + print("\n2️⃣ pub_dict_item 表结构(关键字段):") | ||
| 171 | + cursor.execute("DESCRIBE pub_dict_item") | ||
| 172 | + cols = cursor.fetchall() | ||
| 173 | + for col in cols[:15]: | ||
| 174 | + print(f" - {col[0]:<30} {col[1]:<20}") | ||
| 175 | + | ||
| 176 | + print("\n3️⃣ pub_dict_item_lang 表结构(关键字段):") | ||
| 177 | + cursor.execute("DESCRIBE pub_dict_item_lang") | ||
| 178 | + cols = cursor.fetchall() | ||
| 179 | + for col in cols[:10]: | ||
| 180 | + print(f" - {col[0]:<30} {col[1]:<20}") | ||
| 181 | + | ||
| 182 | + except Exception as e: | ||
| 183 | + print(f"查询失败: {e}") | ||
| 184 | + import traceback | ||
| 185 | + traceback.print_exc() | ||
| 186 | + finally: | ||
| 187 | + if 'connection' in locals(): | ||
| 188 | + connection.close() | ||
| 189 | + | ||
| 190 | +if __name__ == "__main__": | ||
| 191 | + verify_buyer_nature_source() |