168 lines
7.6 KiB
Python
168 lines
7.6 KiB
Python
"""
|
||
详细测试关联关系数据
|
||
"""
|
||
import pymysql
|
||
import os
|
||
import json
|
||
import requests
|
||
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'
|
||
}
|
||
|
||
TENANT_ID = 615873064429507639
|
||
API_BASE_URL = 'http://localhost:7500'
|
||
|
||
def test_specific_template():
|
||
"""测试特定模板的关联关系"""
|
||
conn = pymysql.connect(**DB_CONFIG)
|
||
cursor = conn.cursor(pymysql.cursors.DictCursor)
|
||
|
||
try:
|
||
# 测试模板:1.请示报告卡(初核谈话)
|
||
template_name = "1.请示报告卡(初核谈话)"
|
||
|
||
print(f"测试模板: {template_name}")
|
||
print("=" * 80)
|
||
|
||
# 1. 从数据库查询模板ID
|
||
cursor.execute("""
|
||
SELECT id, name
|
||
FROM f_polic_file_config
|
||
WHERE tenant_id = %s AND name = %s
|
||
""", (TENANT_ID, template_name))
|
||
template = cursor.fetchone()
|
||
|
||
if not template:
|
||
print(f"模板 '{template_name}' 不存在")
|
||
return
|
||
|
||
template_id = template['id']
|
||
print(f"模板ID: {template_id} (类型: {type(template_id).__name__})")
|
||
|
||
# 2. 查询该模板的所有关联关系
|
||
cursor.execute("""
|
||
SELECT fff.file_id, fff.filed_id, fff.state, f.name as field_name, f.field_type
|
||
FROM f_polic_file_field fff
|
||
INNER JOIN f_polic_field f ON fff.filed_id = f.id AND fff.tenant_id = f.tenant_id
|
||
WHERE fff.tenant_id = %s AND fff.file_id = %s
|
||
ORDER BY f.field_type, f.name
|
||
""", (TENANT_ID, template_id))
|
||
relations = cursor.fetchall()
|
||
|
||
print(f"\n数据库中的关联关系数: {len(relations)}")
|
||
print(f"启用的关联关系数: {len([r for r in relations if r['state'] == 1])}")
|
||
|
||
input_fields = [r for r in relations if r['field_type'] == 1]
|
||
output_fields = [r for r in relations if r['field_type'] == 2]
|
||
|
||
print(f"\n输入字段关联: {len(input_fields)}")
|
||
for r in input_fields:
|
||
print(f" - {r['field_name']} (ID: {r['filed_id']}, state: {r['state']})")
|
||
|
||
print(f"\n输出字段关联: {len(output_fields)}")
|
||
for r in output_fields[:10]: # 只显示前10个
|
||
print(f" - {r['field_name']} (ID: {r['filed_id']}, state: {r['state']})")
|
||
if len(output_fields) > 10:
|
||
print(f" ... 还有 {len(output_fields) - 10} 个输出字段")
|
||
|
||
# 3. 调用 API 获取数据
|
||
print("\n" + "=" * 80)
|
||
print("调用 API 获取数据...")
|
||
try:
|
||
response = requests.get(
|
||
f'{API_BASE_URL}/api/template-field-relations',
|
||
params={'tenant_id': TENANT_ID},
|
||
timeout=10
|
||
)
|
||
if response.status_code == 200:
|
||
result = response.json()
|
||
if result.get('isSuccess'):
|
||
api_data = result.get('data', {})
|
||
api_relations = api_data.get('relations', {})
|
||
|
||
# 检查 relations 的 key 类型
|
||
print(f"\nAPI 返回的 relations 对象:")
|
||
print(f" relations 类型: {type(api_relations).__name__}")
|
||
print(f" relations key 数量: {len(api_relations)}")
|
||
|
||
# 尝试用不同的 key 类型查找
|
||
template_id_str = str(template_id)
|
||
template_id_int = int(template_id)
|
||
|
||
print(f"\n尝试查找模板关联关系:")
|
||
print(f" 使用字符串 key '{template_id_str}': {template_id_str in api_relations}")
|
||
print(f" 使用数字 key {template_id_int}: {template_id_int in api_relations}")
|
||
|
||
# 检查 relations 的所有 key
|
||
sample_keys = list(api_relations.keys())[:5]
|
||
print(f"\n relations 的 key 示例 (前5个):")
|
||
for key in sample_keys:
|
||
print(f" key: {repr(key)}, 类型: {type(key).__name__}, 值数量: {len(api_relations[key])}")
|
||
|
||
# 查找该模板的关联关系
|
||
related_field_ids = None
|
||
if template_id_str in api_relations:
|
||
related_field_ids = api_relations[template_id_str]
|
||
print(f"\n 找到关联关系 (使用字符串key): {len(related_field_ids)} 个字段")
|
||
elif template_id_int in api_relations:
|
||
related_field_ids = api_relations[template_id_int]
|
||
print(f"\n 找到关联关系 (使用数字key): {len(related_field_ids)} 个字段")
|
||
else:
|
||
print(f"\n 未找到该模板的关联关系")
|
||
print(f" 可用的模板ID: {list(api_relations.keys())[:10]}")
|
||
|
||
if related_field_ids:
|
||
print(f"\n API 返回的关联字段ID: {related_field_ids[:10]}{'...' if len(related_field_ids) > 10 else ''}")
|
||
|
||
# 检查字段类型
|
||
api_input_fields = api_data.get('input_fields', [])
|
||
api_output_fields = api_data.get('output_fields', [])
|
||
|
||
input_field_ids = {f['id'] for f in api_input_fields}
|
||
output_field_ids = {f['id'] for f in api_output_fields}
|
||
|
||
related_input = [fid for fid in related_field_ids if fid in input_field_ids]
|
||
related_output = [fid for fid in related_field_ids if fid in output_field_ids]
|
||
|
||
print(f"\n 关联的输入字段ID: {related_input}")
|
||
print(f" 关联的输出字段ID: {related_output[:10]}{'...' if len(related_output) > 10 else ''}")
|
||
|
||
# 对比数据库和 API
|
||
db_field_ids = {r['filed_id'] for r in relations if r['state'] == 1}
|
||
api_field_ids = set(related_field_ids)
|
||
|
||
print(f"\n 对比结果:")
|
||
print(f" 数据库关联字段数: {len(db_field_ids)}")
|
||
print(f" API 关联字段数: {len(api_field_ids)}")
|
||
print(f" 一致: {db_field_ids == api_field_ids}")
|
||
|
||
if db_field_ids != api_field_ids:
|
||
missing_in_api = db_field_ids - api_field_ids
|
||
extra_in_api = api_field_ids - db_field_ids
|
||
if missing_in_api:
|
||
print(f" 数据库有但API没有: {sorted(list(missing_in_api))[:10]}")
|
||
if extra_in_api:
|
||
print(f" API有但数据库没有: {sorted(list(extra_in_api))[:10]}")
|
||
else:
|
||
print(f"API 返回错误: {result.get('errorMsg')}")
|
||
else:
|
||
print(f"API 请求失败: {response.status_code}")
|
||
except Exception as e:
|
||
print(f"API 请求异常: {e}")
|
||
|
||
finally:
|
||
cursor.close()
|
||
conn.close()
|
||
|
||
if __name__ == '__main__':
|
||
test_specific_template()
|