import psycopg2
import pandas as pd

# 读取密码
DB_PASSWORD = ""
with open('/root/.openclaw/workspace/secrets.env', 'r') as f:
    for line in f:
        if line.startswith('PG_ONLINE_PASSWORD='):
            DB_PASSWORD = line.strip().split('=', 1)[1].strip('"\'')
            break

# 数据库连接
conn = psycopg2.connect(
    host='bj-postgres-16pob4sg.sql.tencentcdb.com',
    port=28591,
    user='ai_member',
    password=DB_PASSWORD,
    database='vala_bi'
)

# 查询指定订单的所有退费记录（不限制状态）
order_no = "dd202602111013471770776027390252"
sql = """
SELECT 
    r.id AS refund_id,
    r.trade_no AS order_no,
    r.created_at AS refund_apply_time,
    r.updated_at AS refund_update_time,
    CAST(r.refund_amount AS NUMERIC) / 100 AS refund_amount,
    r.status AS refund_status_code,
    CASE r.status 
        WHEN 0 THEN '待审核'
        WHEN 1 THEN '审核通过'
        WHEN 2 THEN '审核拒绝'
        WHEN 3 THEN '退款成功'
        WHEN 4 THEN '退款失败'
        ELSE '其他状态'
    END AS refund_status_name,
    CASE r.refund_type WHEN 1 THEN '仅退款' WHEN 2 THEN '全额退货退款' WHEN 3 THEN '部分退货退款' END AS refund_type
FROM bi_refund_order r
WHERE r.trade_no = %s
"""

df = pd.read_sql(sql, conn, params=(order_no,))
conn.close()

if df.empty:
    print(f"订单号 {order_no} 无任何退费申请记录")
else:
    print(f"🔍 订单 {order_no} 的所有退费记录：")
    print(df.to_string(index=False))
