"""查询 auth.user_applications 和对应用户状态"""
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 a.id, a.user_id, a.site_code, a.applied_role_text, a.phone,
           a.status AS app_status, a.created_at,
           u.wx_openid, u.status AS user_status, u.nickname
      FROM auth.user_applications a
      JOIN auth.users u ON u.id = a.user_id
     ORDER BY a.created_at DESC
     LIMIT 20
""")
rows = cur.fetchall()
cols = [d[0] for d in cur.description]

print(f"共 {len(rows)} 条申请记录:\n")
for row in rows:
    for c, v in zip(cols, row):
        print(f"  {c}: {v}")
    print()

conn.close()
