"""统计 2026-03-05 营业日经营数据"""
import os, sys
from pathlib import Path
from dotenv import load_dotenv

load_dotenv(Path(__file__).resolve().parents[2] / ".env")

dsn = os.environ.get("TEST_DB_DSN")
if not dsn:
    dsn = os.environ.get("PG_DSN", "")
    if "etl_feiqiu" in dsn:
        dsn = dsn.replace("etl_feiqiu", "test_etl_feiqiu")
if not dsn:
    sys.exit("ERROR: TEST_DB_DSN / PG_DSN not set")

import psycopg2
conn = psycopg2.connect(dsn)
cur = conn.cursor()

sql = """
WITH day_label AS (
  SELECT '3月5日' AS day_name,
         '2026-03-05 08:00:00+08'::timestamptz AS day_start,
         '2026-03-06 08:00:00+08'::timestamptz AS day_end
),
cash_online AS (
  SELECT dl.day_name, dl.day_start,
         COALESCE(SUM(h.pay_amount), 0) AS v
  FROM day_label dl
  LEFT JOIN dwd.dwd_settlement_head h
    ON h.create_time >= dl.day_start AND h.create_time < dl.day_end
    AND h.settle_type IN (1, 3)
  GROUP BY dl.day_name, dl.day_start
),
recharge AS (
  SELECT dl.day_name, dl.day_start,
         COALESCE(SUM(ro.pay_amount), 0) AS v
  FROM day_label dl
  LEFT JOIN dwd.dwd_recharge_order ro
    ON ro.create_time >= dl.day_start AND ro.create_time < dl.day_end
    AND ro.settle_type = 5
  GROUP BY dl.day_name, dl.day_start
),
groupbuy AS (
  SELECT dl.day_name, dl.day_start,
         COALESCE(SUM(pcr.sale_price * 0.75), 0) AS v
  FROM day_label dl
  LEFT JOIN dwd.dwd_platform_coupon_redemption pcr
    ON pcr.create_time >= dl.day_start AND pcr.create_time < dl.day_end
    AND pcr.is_delete = 0
  GROUP BY dl.day_name, dl.day_start
),
member_guest AS (
  SELECT dl.day_name, dl.day_start,
         COUNT(*) FILTER (WHERE h.member_id > 0) AS member_v,
         COUNT(*) FILTER (WHERE h.member_id = 0) AS guest_v
  FROM day_label dl
  LEFT JOIN dwd.dwd_settlement_head h
    ON h.create_time >= dl.day_start AND h.create_time < dl.day_end
    AND h.settle_type IN (1, 3)
  GROUP BY dl.day_name, dl.day_start
),
new_member AS (
  SELECT dl.day_name, dl.day_start,
         COUNT(DISTINCT m.member_id) AS v
  FROM day_label dl
  LEFT JOIN dwd.dim_member m
    ON m.create_time >= dl.day_start AND m.create_time < dl.day_end
    AND m.scd2_is_current = 1
  GROUP BY dl.day_name, dl.day_start
),
old_recharge AS (
  SELECT dl.day_name, dl.day_start,
         COUNT(DISTINCT ro.member_id) FILTER (WHERE ro.member_id > 0) AS persons,
         COALESCE(SUM(ro.pay_amount) FILTER (WHERE ro.member_id > 0), 0) AS amount
  FROM day_label dl
  LEFT JOIN dwd.dwd_recharge_order ro
    ON ro.create_time >= dl.day_start AND ro.create_time < dl.day_end
    AND ro.settle_type = 5
  GROUP BY dl.day_name, dl.day_start
)
SELECT co.day_name,
       ROUND(co.v + r.v + gb.v, 2) AS total,
       co.v AS cash_online,
       r.v AS recharge,
       ROUND(gb.v, 2) AS groupbuy,
       mg.member_v, mg.guest_v,
       nm.v AS new_members,
       omr.persons AS old_recharge_persons,
       omr.amount AS old_recharge_amount
FROM cash_online co
JOIN recharge r USING (day_name, day_start)
JOIN groupbuy gb USING (day_name, day_start)
JOIN member_guest mg USING (day_name, day_start)
JOIN new_member nm USING (day_name, day_start)
JOIN old_recharge omr USING (day_name, day_start);
"""

cur.execute(sql)
row = cur.fetchone()
cols = [d[0] for d in cur.description]
print(dict(zip(cols, row)))
conn.close()
