""" 测试 tenant-ids API """ import pymysql import os from dotenv import load_dotenv load_dotenv() # 数据库连接配置 DB_CONFIG = { 'host': os.getenv('DB_HOST', '152.136.177.240'), 'port': int(os.getenv('DB_PORT', 5012)), 'user': os.getenv('DB_USER', 'finyx'), 'password': os.getenv('DB_PASSWORD', '6QsGK6MpePZDE57Z'), 'database': os.getenv('DB_NAME', 'finyx'), 'charset': 'utf8mb4' } def test_query(): """测试查询逻辑""" conn = pymysql.connect(**DB_CONFIG) cursor = conn.cursor(pymysql.cursors.DictCursor) try: print("测试查询所有 tenant_id...") cursor.execute(""" SELECT DISTINCT tenant_id FROM ( SELECT tenant_id FROM f_polic_field WHERE tenant_id IS NOT NULL UNION SELECT tenant_id FROM f_polic_file_config WHERE tenant_id IS NOT NULL UNION SELECT tenant_id FROM f_polic_file_field WHERE tenant_id IS NOT NULL ) AS all_tenants ORDER BY tenant_id """) tenant_ids = [row['tenant_id'] for row in cursor.fetchall()] print(f"查询结果: {tenant_ids}") print(f"数量: {len(tenant_ids)}") print(f"类型: {[type(tid).__name__ for tid in tenant_ids]}") # 测试 JSON 序列化 import json result = { 'isSuccess': True, 'data': { 'tenant_ids': tenant_ids } } json_str = json.dumps(result, ensure_ascii=False, indent=2) print(f"\nJSON 序列化结果:\n{json_str}") finally: cursor.close() conn.close() if __name__ == '__main__': test_query()