"""查询 dev_test_openid 用户及其申请"""
import os
import psycopg2
from dotenv import load_dotenv

load_dotenv()
dsn = os.environ["APP_DB_DSN"]
conn = psycopg2.connect(dsn)
cur = conn.cursor()

# 查用户
cur.execute("SELECT id, wx_openid, status, nickname, created_at, updated_at FROM auth.users WHERE wx_openid = 'dev_test_openid'")
row = cur.fetchone()
if row:
    print(f"用户: id={row[0]}, openid={row[1]}, status={row[2]}, nickname={row[3]}")
    print(f"  created_at={row[4]}, updated_at={row[5]}")
    
    # 查申请
    cur.execute("""
        SELECT id, site_code, applied_role_text, phone, status, review_note, created_at
          FROM auth.user_applications WHERE user_id = %s ORDER BY created_at DESC
    """, (row[0],))
    apps = cur.fetchall()
    print(f"\n申请记录 ({len(apps)} 条):")
    for a in apps:
        print(f"  id={a[0]}, site_code={a[1]}, role={a[2]}, phone={a[3]}, status={a[4]}, note={a[5]}, created={a[6]}")
else:
    print("未找到 dev_test_openid 用户")

conn.close()
