#!/usr/bin/env python3
import os
import pymysql
from pymysql.cursors import DictCursor

# 配置线上MySQL环境变量
os.environ['MYSQL_HOST_online'] = 'bj-cdb-dh2fkqa0.sql.tencentcdb.com'
os.environ['MYSQL_USERNAME_online'] = 'read_only'
os.environ['MYSQL_PASSWORD_online'] = 'fsdo45ijfmfmuu77$%^&'
os.environ['MYSQL_PORT_online'] = '27751'

def get_role_ids_by_account_id(account_id):
    host = os.getenv("MYSQL_HOST_online")
    user = os.getenv("MYSQL_USERNAME_online")
    password = os.getenv("MYSQL_PASSWORD_online")
    port = int(os.getenv("MYSQL_PORT_online"))

    print(f"正在连接线上MySQL... host={host}, port={port}")
    conn = pymysql.connect(
        host=host,
        user=user,
        password=password,
        port=port,
        database="vala_user",
        charset="utf8mb4",
        cursorclass=DictCursor
    )
    print("连接成功！")

    try:
        with conn.cursor() as cursor:
            sql = "SELECT id FROM vala_app_character WHERE account_id = %s"
            print(f"执行SQL: {sql} 参数: {account_id}")
            cursor.execute(sql, (account_id,))
            result = cursor.fetchall()
            role_ids = [str(row["id"]) for row in result]
            print(f"账户ID {account_id} 对应的角色ID: {role_ids}")
            return role_ids
    finally:
        conn.close()

if __name__ == "__main__":
    get_role_ids_by_account_id(5980)
