import pymysql
from app.core.config import settings

def check_user_login_table():
    try:
        # 解析连接字符串
        import re
        pattern = r'mysql\+pymysql://([^:]+):([^@]+)@([^:]+):(\d+)/(.+)'
        match = re.match(pattern, settings.DATABASE_URL)

        if match:
            username, password, host, port, database = match.groups()

            connection = pymysql.connect(
                host=host,
                port=int(port),
                user=username,
                password=password,
                database=database,
                charset='utf8mb4'
            )

            with connection.cursor() as cursor:
                # 检查user_login表结构
                print("=== user_login表结构 ===")
                cursor.execute("DESCRIBE user_login")
                columns = cursor.fetchall()
                for column in columns:
                    print(f"{column[0]}: {column[1]} {column[2]} {column[3]} {column[4]}")

                print("\n=== user_login表数据示例 ===")
                cursor.execute("SELECT * FROM user_login LIMIT 3")
                rows = cursor.fetchall()
                for row in rows:
                    print(row)

                print("\n=== 检查是否有用户相关的表 ===")
                cursor.execute("SHOW TABLES LIKE '%user%'")
                user_tables = cursor.fetchall()
                for table in user_tables:
                    print(f"- {table[0]}")

            connection.close()
            return True

    except Exception as e:
        print(f"检查表结构失败: {str(e)}")
        return False

if __name__ == "__main__":
    check_user_login_table()