#!/usr/bin/env python3
"""
检查数据库表结构
"""
import pymysql

def check_tables(host, port, user, password, database):
    """检查表是否存在及字段"""
    try:
        connection = pymysql.connect(
            host=host,
            port=port,
            user=user,
            password=password,
            database=database,
            charset='utf8mb4',
            cursorclass=pymysql.cursors.DictCursor
        )
        
        print(f"✓ 连接数据库成功: {database}")
        
        tables = [
            'asset_parking',
            'asset_customer', 
            'asset_vehicle_base',
            'asset_vehicle_business',
            'asset_contract'
        ]
        
        with connection.cursor() as cursor:
            for table in tables:
                # 检查表是否存在
                cursor.execute(f"SHOW TABLES LIKE '{table}'")
                result = cursor.fetchone()
                
                if result:
                    print(f"\n✓ 表 {table} 存在")
                    
                    # 显示字段
                    cursor.execute(f"DESCRIBE {table}")
                    columns = cursor.fetchall()
                    print(f"  字段: {', '.join([col['Field'] for col in columns[:10]])}")
                    if len(columns) > 10:
                        print(f"  ... 共 {len(columns)} 个字段")
                else:
                    print(f"\n✗ 表 {table} 不存在")
        
        connection.close()
        
    except Exception as e:
        print(f"✗ 错误: {e}")

if __name__ == '__main__':
    DB_HOST = '47.103.115.36'
    DB_PORT = 3306
    DB_USER = 'root'
    DB_PASSWORD = 'Passw0rd2026'
    
    print("检查 oneos_asset 数据库:")
    check_tables(DB_HOST, DB_PORT, DB_USER, DB_PASSWORD, 'oneos_asset')
    
    print("\n" + "="*60)
    print("检查 oneos_energy 数据库:")
    check_tables(DB_HOST, DB_PORT, DB_USER, DB_PASSWORD, 'oneos_energy')
