"""查询 DWD 层各表的最新记录时间和行数。"""
import os
import sys
from dotenv import load_dotenv

load_dotenv()

PG_DSN = os.environ.get("TEST_DB_DSN") or os.environ.get("PG_DSN")
if not PG_DSN:
    print("ERROR: TEST_DB_DSN / PG_DSN 未设置", file=sys.stderr)
    sys.exit(1)

# 如果拿到的是正式库 DSN，替换为测试库
if "etl_feiqiu" in PG_DSN and "test_" not in PG_DSN:
    PG_DSN = PG_DSN.replace("etl_feiqiu", "test_etl_feiqiu")

import psycopg2

QUERIES = [
    ("dwd_settlement_head", "SELECT MAX(create_time), MAX(pay_time), COUNT(*) FROM dwd.dwd_settlement_head"),
    ("dwd_settlement_head_ex", "SELECT MAX(revoke_time), NULL::timestamptz, COUNT(*) FROM dwd.dwd_settlement_head_ex"),
    ("dwd_table_fee_log", "SELECT MAX(create_time), MAX(update_time), COUNT(*) FROM dwd.dwd_table_fee_log"),
    ("dwd_table_fee_log_ex", "SELECT NULL::timestamptz, NULL::timestamptz, COUNT(*) FROM dwd.dwd_table_fee_log_ex"),
    ("dwd_table_fee_adjust", "SELECT MAX(adjust_time), NULL::timestamptz, COUNT(*) FROM dwd.dwd_table_fee_adjust"),
    ("dwd_assistant_service_log", "SELECT MAX(create_time), MAX(update_time), COUNT(*) FROM dwd.dwd_assistant_service_log"),
    ("dwd_assistant_service_log_ex", "SELECT NULL::timestamptz, NULL::timestamptz, COUNT(*) FROM dwd.dwd_assistant_service_log_ex"),
    ("dwd_store_goods_sale", "SELECT MAX(create_time), NULL::timestamptz, COUNT(*) FROM dwd.dwd_store_goods_sale"),
    ("dwd_store_goods_sale_ex", "SELECT NULL::timestamptz, NULL::timestamptz, COUNT(*) FROM dwd.dwd_store_goods_sale_ex"),
    ("dwd_recharge_order", "SELECT MAX(create_time), MAX(pay_time), COUNT(*) FROM dwd.dwd_recharge_order"),
    ("dwd_recharge_order_ex", "SELECT MAX(revoke_time), NULL::timestamptz, COUNT(*) FROM dwd.dwd_recharge_order_ex"),
    ("dwd_member_balance_change", "SELECT MAX(change_time), NULL::timestamptz, COUNT(*) FROM dwd.dwd_member_balance_change"),
    ("dwd_member_balance_change_ex", "SELECT NULL::timestamptz, NULL::timestamptz, COUNT(*) FROM dwd.dwd_member_balance_change_ex"),
    ("dwd_groupbuy_redemption", "SELECT MAX(create_time), MAX(update_time), COUNT(*) FROM dwd.dwd_groupbuy_redemption"),
    ("dwd_groupbuy_redemption_ex", "SELECT NULL::timestamptz, NULL::timestamptz, COUNT(*) FROM dwd.dwd_groupbuy_redemption_ex"),
    ("dwd_payment", "SELECT MAX(create_time), MAX(pay_date::text::timestamptz), COUNT(*) FROM dwd.dwd_payment"),
    ("dwd_refund", "SELECT MAX(create_time), MAX(update_time), COUNT(*) FROM dwd.dwd_refund"),
    ("dwd_refund_ex", "SELECT NULL::timestamptz, NULL::timestamptz, COUNT(*) FROM dwd.dwd_refund_ex"),
    ("dwd_goods_stock_summary", "SELECT MAX(fetched_at), NULL::timestamptz, COUNT(*) FROM dwd.dwd_goods_stock_summary"),
    ("dwd_goods_stock_movement", "SELECT MAX(create_time), NULL::timestamptz, COUNT(*) FROM dwd.dwd_goods_stock_movement"),
    ("dwd_platform_coupon_redemption", "SELECT MAX(create_time), MAX(update_time), COUNT(*) FROM dwd.dwd_platform_coupon_redemption"),
]

conn = psycopg2.connect(PG_DSN)
try:
    cur = conn.cursor()
    print(f"{'表名':<42} {'时间字段1(max)':<28} {'时间字段2(max)':<28} {'行数':>8}")
    print("-" * 110)
    for tbl, sql in QUERIES:
        try:
            cur.execute(sql)
            row = cur.fetchone()
            t1 = str(row[0])[:25] if row[0] else "-"
            t2 = str(row[1])[:25] if row[1] else "-"
            cnt = row[2] if row[2] else 0
            print(f"{tbl:<42} {t1:<28} {t2:<28} {cnt:>8}")
        except Exception as e:
            print(f"{tbl:<42} ERROR: {e}")
            conn.rollback()
finally:
    conn.close()
