#!/usr/bin/env python3
"""
调查 DWD 处理逻辑，查明为什么系统性地丢失 50% 的数据
"""

import os
import psycopg2
from datetime import datetime
from dotenv import load_dotenv

def main():
    # 加载环境变量
    load_dotenv()
    
    test_db_dsn = os.environ.get('TEST_DB_DSN')
    if not test_db_dsn:
        raise RuntimeError("TEST_DB_DSN 环境变量未设置")
    
    print("🔍 调查 DWD 数据处理逻辑问题")
    print("=" * 50)
    
    with psycopg2.connect(test_db_dsn) as conn:
        with conn.cursor() as cur:
            
            # 1. 检查 ODS 中被 DWD 遗漏的具体记录
            print("\n📊 1. 分析被遗漏的 ODS 记录特征")
            cur.execute("""
                SELECT 
                    o.paytime::date as pay_date,
                    COUNT(*) as missing_count,
                    AVG(o.payamount) as avg_amount,
                    MIN(o.payamount) as min_amount,
                    MAX(o.payamount) as max_amount,
                    COUNT(CASE WHEN o.payamount = 0 THEN 1 END) as zero_amount_count,
                    COUNT(CASE WHEN o.payamount < 0 THEN 1 END) as negative_amount_count,
                    COUNT(CASE WHEN o.memberid = 0 THEN 1 END) as non_member_count
                FROM ods.settlement_records o
                LEFT JOIN dwd.dwd_settlement_head d ON o.id = d.order_settle_id
                WHERE d.order_settle_id IS NULL
                AND o.paytime::date BETWEEN '2026-02-10' AND '2026-02-14'
                GROUP BY o.paytime::date
                ORDER BY pay_date
            """)
            
            missing_analysis = cur.fetchall()
            print("被遗漏记录的特征分析:")
            for pay_date, missing_count, avg_amount, min_amount, max_amount, zero_count, neg_count, non_member_count in missing_analysis:
                print(f"  {pay_date}: {missing_count:,} 条遗漏")
                print(f"    金额: 平均 {avg_amount:.2f}, 范围 [{min_amount:.2f}, {max_amount:.2f}]")
                print(f"    零金额: {zero_count:,} 条, 负金额: {neg_count:,} 条, 非会员: {non_member_count:,} 条")
            
            # 2. 检查是否有特定的过滤条件
            print("\n🔍 2. 检查可能的过滤条件")
            
            # 检查 settlement_status
            cur.execute("""
                SELECT 
                    o.settlestatus,
                    COUNT(*) as total_count,
                    COUNT(d.order_settle_id) as processed_count,
                    COUNT(*) - COUNT(d.order_settle_id) as missing_count
                FROM ods.settlement_records o
                LEFT JOIN dwd.dwd_settlement_head d ON o.id = d.order_settle_id
                WHERE o.paytime::date BETWEEN '2026-02-10' AND '2026-02-14'
                GROUP BY o.settlestatus
                ORDER BY missing_count DESC
            """)
            
            status_analysis = cur.fetchall()
            print("按 settlestatus 分析:")
            for status, total, processed, missing in status_analysis:
                missing_rate = (missing / total) * 100 if total > 0 else 0
                print(f"  状态 {status}: 总数 {total:,}, 已处理 {processed:,}, 遗漏 {missing:,} ({missing_rate:.1f}%)")
            
            # 检查 settlement_type
            cur.execute("""
                SELECT 
                    o.settletype,
                    COUNT(*) as total_count,
                    COUNT(d.order_settle_id) as processed_count,
                    COUNT(*) - COUNT(d.order_settle_id) as missing_count
                FROM ods.settlement_records o
                LEFT JOIN dwd.dwd_settlement_head d ON o.id = d.order_settle_id
                WHERE o.paytime::date BETWEEN '2026-02-10' AND '2026-02-14'
                GROUP BY o.settletype
                ORDER BY missing_count DESC
            """)
            
            type_analysis = cur.fetchall()
            print("按 settletype 分析:")
            for stype, total, processed, missing in type_analysis:
                missing_rate = (missing / total) * 100 if total > 0 else 0
                print(f"  类型 {stype}: 总数 {total:,}, 已处理 {processed:,}, 遗漏 {missing:,} ({missing_rate:.1f}%)")
            
            # 3. 检查时间范围过滤
            print("\n⏰ 3. 检查时间范围过滤")
            cur.execute("""
                SELECT 
                    EXTRACT(hour FROM o.paytime) as hour_of_day,
                    COUNT(*) as total_count,
                    COUNT(d.order_settle_id) as processed_count,
                    COUNT(*) - COUNT(d.order_settle_id) as missing_count
                FROM ods.settlement_records o
                LEFT JOIN dwd.dwd_settlement_head d ON o.id = d.order_settle_id
                WHERE o.paytime::date BETWEEN '2026-02-10' AND '2026-02-14'
                GROUP BY EXTRACT(hour FROM o.paytime)
                HAVING COUNT(*) - COUNT(d.order_settle_id) > 0
                ORDER BY missing_count DESC
                LIMIT 10
            """)
            
            hour_analysis = cur.fetchall()
            print("按小时分析 (只显示有遗漏的):")
            for hour, total, processed, missing in hour_analysis:
                missing_rate = (missing / total) * 100 if total > 0 else 0
                print(f"  {int(hour):02d}:xx 时段: 总数 {total:,}, 已处理 {processed:,}, 遗漏 {missing:,} ({missing_rate:.1f}%)")
            
            # 4. 检查具体的遗漏记录样本
            print("\n📋 4. 遗漏记录样本分析")
            cur.execute("""
                SELECT 
                    o.id,
                    o.paytime,
                    o.payamount,
                    o.settlestatus,
                    o.settletype,
                    o.memberid,
                    o.siteid
                FROM ods.settlement_records o
                LEFT JOIN dwd.dwd_settlement_head d ON o.id = d.order_settle_id
                WHERE d.order_settle_id IS NULL
                AND o.paytime::date = '2026-02-13'  -- 选择遗漏最多的一天
                ORDER BY o.paytime
                LIMIT 10
            """)
            
            sample_records = cur.fetchall()
            print("2026-02-13 遗漏记录样本 (前10条):")
            for record in sample_records:
                oid, paytime, payamount, status, stype, memberid, siteid = record
                print(f"  ID: {oid}, 时间: {paytime}, 金额: {payamount}, 状态: {status}, 类型: {stype}, 会员: {memberid}")
            
            # 5. 检查是否有重复处理的情况
            print("\n🔄 5. 检查重复处理情况")
            cur.execute("""
                SELECT 
                    order_settle_id,
                    COUNT(*) as duplicate_count
                FROM dwd.dwd_settlement_head
                GROUP BY order_settle_id
                HAVING COUNT(*) > 1
                ORDER BY duplicate_count DESC
                LIMIT 10
            """)
            
            duplicates = cur.fetchall()
            if duplicates:
                print("发现重复处理的记录:")
                for oid, count in duplicates:
                    print(f"  订单 {oid}: 重复 {count} 次")
            else:
                print("未发现重复处理的记录")
            
            # 6. 检查 DWD 表的约束和索引
            print("\n🔧 6. 检查 DWD 表结构")
            cur.execute("""
                SELECT 
                    conname as constraint_name,
                    contype as constraint_type,
                    pg_get_constraintdef(oid) as constraint_definition
                FROM pg_constraint 
                WHERE conrelid = 'dwd.dwd_settlement_head'::regclass
                AND contype IN ('p', 'u', 'c')  -- primary, unique, check
            """)
            
            constraints = cur.fetchall()
            print("DWD 表约束:")
            for name, ctype, definition in constraints:
                constraint_types = {'p': 'PRIMARY KEY', 'u': 'UNIQUE', 'c': 'CHECK'}
                print(f"  {constraint_types.get(ctype, ctype)}: {name}")
                print(f"    {definition}")

if __name__ == "__main__":
    main()