""" 检查数据库中的现有数据,确认匹配情况 """ import os import json import pymysql from pathlib import Path # 数据库连接配置 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 check_existing_data(): """检查数据库中的现有数据""" print("="*80) print("检查数据库中的现有数据") print("="*80) try: conn = pymysql.connect(**DB_CONFIG) cursor = conn.cursor(pymysql.cursors.DictCursor) # 查询所有记录 sql = """ SELECT id, name, parent_id, template_code, input_data, file_path, state FROM f_polic_file_config WHERE tenant_id = %s ORDER BY name """ cursor.execute(sql, (TENANT_ID,)) configs = cursor.fetchall() print(f"\n共找到 {len(configs)} 条记录\n") # 按 parent_id 分组统计 with_parent = [] without_parent = [] for config in configs: # 尝试从 input_data 中提取 template_code 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 config['extracted_template_code'] = template_code if config.get('parent_id'): with_parent.append(config) else: without_parent.append(config) print(f"有 parent_id 的记录: {len(with_parent)} 条") print(f"无 parent_id 的记录: {len(without_parent)} 条\n") # 显示无 parent_id 的记录 print("="*80) print("无 parent_id 的记录列表:") print("="*80) for i, config in enumerate(without_parent, 1): print(f"\n{i}. {config['name']}") print(f" ID: {config['id']}") print(f" template_code: {config.get('extracted_template_code') or config.get('template_code') or '无'}") print(f" file_path: {config.get('file_path', '无')}") print(f" state: {config.get('state')}") # 显示有 parent_id 的记录(树状结构) print("\n" + "="*80) print("有 parent_id 的记录(树状结构):") print("="*80) # 构建ID到名称的映射 id_to_name = {config['id']: config['name'] for config in configs} for config in with_parent: parent_name = id_to_name.get(config['parent_id'], f"ID:{config['parent_id']}") print(f"\n{config['name']}") print(f" ID: {config['id']}") print(f" 父节点: {parent_name} (ID: {config['parent_id']})") print(f" template_code: {config.get('extracted_template_code') or config.get('template_code') or '无'}") cursor.close() conn.close() except Exception as e: print(f"错误: {e}") import traceback traceback.print_exc() if __name__ == '__main__': check_existing_data()