ai-business-write/verify_tree_structure.py

170 lines
4.9 KiB
Python
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

"""
验证树状结构更新结果
"""
import os
import json
import pymysql
from typing import Dict, List
# 数据库连接配置
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
def print_tree_structure(conn):
"""打印树状结构"""
cursor = conn.cursor(pymysql.cursors.DictCursor)
sql = """
SELECT id, name, parent_id, template_code, input_data, state
FROM f_polic_file_config
WHERE tenant_id = %s
ORDER BY parent_id, name
"""
cursor.execute(sql, (TENANT_ID,))
configs = cursor.fetchall()
# 构建ID到配置的映射
id_to_config = {config['id']: config for config in configs}
# 找出根节点parent_id为NULL
root_nodes = [config for config in configs if config.get('parent_id') is None]
def print_node(config, indent=0, visited=None):
"""递归打印节点"""
if visited is None:
visited = set()
if config['id'] in visited:
return
visited.add(config['id'])
prefix = " " * indent
parent_info = ""
if config.get('parent_id'):
parent_name = id_to_config.get(config['parent_id'], {}).get('name', f"ID:{config['parent_id']}")
parent_info = f" [父: {parent_name}]"
template_code = config.get('template_code')
if not template_code and config.get('input_data'):
try:
input_data = json.loads(config['input_data']) if isinstance(config['input_data'], str) else config['input_data']
if isinstance(input_data, dict):
template_code = input_data.get('template_code')
except:
pass
template_info = f" [code: {template_code}]" if template_code else ""
state_info = " [启用]" if config.get('state') == 1 else " [未启用]"
print(f"{prefix}├─ {config['name']}{parent_info}{template_info}{state_info}")
# 打印子节点
children = [c for c in configs if c.get('parent_id') == config['id']]
for i, child in enumerate(sorted(children, key=lambda x: x['name'])):
is_last = i == len(children) - 1
if is_last:
print_node(child, indent + 1, visited)
else:
print_node(child, indent + 1, visited)
print("="*80)
print("树状结构")
print("="*80)
for root in sorted(root_nodes, key=lambda x: x['name']):
print_node(root)
print()
# 统计信息
print("="*80)
print("统计信息")
print("="*80)
print(f"总记录数: {len(configs)}")
print(f"根节点数: {len(root_nodes)}")
print(f"有父节点的记录: {len([c for c in configs if c.get('parent_id')])}")
print(f"无父节点的记录: {len([c for c in configs if not c.get('parent_id')])}")
cursor.close()
def verify_parent_relationships(conn):
"""验证父子关系"""
cursor = conn.cursor(pymysql.cursors.DictCursor)
sql = """
SELECT id, name, parent_id
FROM f_polic_file_config
WHERE tenant_id = %s AND parent_id IS NOT NULL
"""
cursor.execute(sql, (TENANT_ID,))
configs = cursor.fetchall()
print("\n" + "="*80)
print("验证父子关系")
print("="*80)
errors = []
for config in configs:
parent_id = config['parent_id']
check_sql = """
SELECT id, name FROM f_polic_file_config
WHERE id = %s AND tenant_id = %s
"""
cursor.execute(check_sql, (parent_id, TENANT_ID))
parent = cursor.fetchone()
if not parent:
errors.append({
'child': config['name'],
'child_id': config['id'],
'parent_id': parent_id,
'error': '父节点不存在'
})
if errors:
print(f"\n✗ 发现 {len(errors)} 个错误:")
for error in errors:
print(f" - {error['child']} (ID: {error['child_id']})")
print(f" 父节点ID {error['parent_id']} 不存在")
else:
print("\n✓ 所有父子关系验证通过")
cursor.close()
return len(errors) == 0
def main():
"""主函数"""
print("="*80)
print("验证树状结构")
print("="*80)
try:
conn = pymysql.connect(**DB_CONFIG)
print("✓ 数据库连接成功\n")
print_tree_structure(conn)
verify_parent_relationships(conn)
conn.close()
except Exception as e:
print(f"✗ 错误: {e}")
import traceback
traceback.print_exc()
if __name__ == '__main__':
main()