# -*- coding: utf-8 -*-
"""导出第四次执行结果报告。"""
from pathlib import Path
from dotenv import load_dotenv

load_dotenv(Path(__file__).resolve().parents[2] / ".env")
from _env_paths import get_output_path

out = get_output_path("SYSTEM_LOG_ROOT")

report = """# 第四次 ETL 执行结果报告

- execution_id: `efd4f421-ee10-4244-833f-7b2d68c3c05b`
- 时间: 2026-02-21 19:57:02 ~ 20:08:57
- 耗时: 11 分 55 秒 (715s)
- 整体状态: success (exit_code=0)
- 任务总数: 31

## 成功任务 (10 个)

| # | 任务 | 耗时 | 统计 |
|---|------|------|------|
| 1 | DWS_ASSISTANT_DAILY | ~2m28s | fetched=367, inserted=367, deleted=367 |
| 2 | DWS_ASSISTANT_MONTHLY | ~12s | fetched=25, inserted=25, deleted=25 |
| 3 | DWS_ASSISTANT_CUSTOMER | ~1m22s | fetched=486, inserted=486 |
| 4 | DWS_ASSISTANT_SALARY | <1s | 非工资结算期，跳过 |
| 5 | DWS_ASSISTANT_FINANCE | ~1m10s | fetched=367, inserted=367, deleted=367 |
| 6 | ODS_SETTLEMENT_RECORDS | ~1m46s | fetched=10366, updated=10366 |
| 7 | ODS_PAYMENT | ~4m0s | fetched=42500, updated=42500 |
| 8 | ODS_REFUND | ~3s | fetched=116, updated=116 |
| 9 | DWS_BUILD_ORDER_SUMMARY | ~1s | inserted=13296 |
| 10 | DWS_MEMBER_CONSUMPTION | ~43s | fetched=198, inserted=198 |

## BUG 4 修复验证

- DWS_MEMBER_CONSUMPTION ✅ 不再报 UndefinedColumn site_id
- DWS_MEMBER_VISIT ❌ 新错误（BUG 5）
- DWS_FINANCE_RECHARGE ❌ 级联失败（未能独立验证）

## 新发现 BUG 5

- 任务: `DWS_MEMBER_VISIT`
- 错误: `UndefinedColumn: 字段 "birthday" 不存在`
- 位置: `member_visit_task.py` → `_extract_member_info()` line ~312
- 根因: SQL 查询 `dwd.dim_member` 时引用了 `birthday` 字段，但该表没有此字段
- DWS 表 `dws_member_visit_detail` 设计了 `member_birthday DATE` 列，但上游 dim_member 未提供此数据
- 级联影响: 后续 20 个任务全部 InFailedSqlTransaction

## 失败任务 (21 个)

| 类型 | 任务 | 错误 |
|------|------|------|
| 🔴 根因 | DWS_MEMBER_VISIT | UndefinedColumn: birthday |
| 级联 | ODS_GOODS_CATEGORY | InFailedSqlTransaction |
| 级联 | ODS_STORE_GOODS | InFailedSqlTransaction |
| 级联 | ODS_STORE_GOODS_SALES | InFailedSqlTransaction |
| 级联 | ODS_TENANT_GOODS | InFailedSqlTransaction |
| 级联 | ODS_PLATFORM_COUPON | InFailedSqlTransaction |
| 级联 | ODS_GROUP_PACKAGE | InFailedSqlTransaction |
| 级联 | ODS_GROUP_BUY_REDEMPTION | InFailedSqlTransaction |
| 级联 | ODS_INVENTORY_STOCK | InFailedSqlTransaction |
| 级联 | ODS_INVENTORY_CHANGE | InFailedSqlTransaction |
| 级联 | DWS_GOODS_STOCK_DAILY | InFailedSqlTransaction |
| 级联 | DWS_GOODS_STOCK_WEEKLY | InFailedSqlTransaction |
| 级联 | DWS_GOODS_STOCK_MONTHLY | InFailedSqlTransaction |
| 级联 | DWS_FINANCE_DAILY | InFailedSqlTransaction |
| 级联 | DWS_FINANCE_RECHARGE | InFailedSqlTransaction |
| 级联 | DWS_FINANCE_INCOME_STRUCTURE | InFailedSqlTransaction |
| 级联 | DWS_FINANCE_DISCOUNT_DETAIL | InFailedSqlTransaction |
| 级联 | DWS_WINBACK_INDEX | InFailedSqlTransaction |
| 级联 | DWS_NEWCONV_INDEX | InFailedSqlTransaction |
| 级联 | DWS_RELATION_INDEX | InFailedSqlTransaction |
| 级联 | DWD_LOAD_FROM_ODS | InFailedSqlTransaction |

## BUG 5 修复

- 文件: `member_visit_task.py`
- 改动 1: `_extract_member_info` SQL 移除 `birthday` 字段
- 改动 2: transform 中 `member_birthday` 改为 `None`
- 已添加 CHANGE 注释
"""

(out / "2026-02-21__etl_run_result_v4.md").write_text(report, encoding="utf-8")
print("报告已保存")
