#!/usr/bin/env python
# -*- coding: utf-8 -*-

import os
import sys

# 添加backend目录到路径
sys.path.insert(0, os.path.join(os.path.dirname(__file__), 'backend'))

os.environ.setdefault("DB_HOST", "localhost")
os.environ.setdefault("DB_PORT", "5432")
os.environ.setdefault("DB_NAME", "accounting_db")
os.environ.setdefault("DB_USER", "postgres")
os.environ.setdefault("DB_PASSWORD", "root")

from app.core.database import get_connection

def check_accounts_data():
    """检查701账户的数据"""
    with get_connection() as conn:
        with conn.cursor() as cur:
            # 查询原始交易数据
            print("=== 521年6月份701账户的原始交易数据 ===")
            sql = """
            SELECT 
                e.journal_entry_id,
                e.entry_date,
                e.description,
                l.account_id,
                a.account_name,
                COALESCE(l.debit, 0) as debit,
                COALESCE(l.credit, 0) as credit,
                e.is_deleted,
                e.parent_entry_id
            FROM journal_lines l
            JOIN journal_entries e ON e.journal_entry_id = l.journal_entry_id
            JOIN accounts a ON a.account_id = l.account_id
            WHERE a.account_id = 701
              AND e.entry_date >= '2025-06-01'
              AND e.entry_date <= '2025-06-30'
            ORDER BY e.entry_date, e.journal_entry_id
            """
            
            cur.execute(sql)
            rows = cur.fetchall()
            
            total_debit_all = 0
            total_credit_all = 0
            total_debit_filtered = 0
            total_credit_filtered = 0
            
            for row in rows:
                journal_id, date, desc, account_id, account_name, debit, credit, is_deleted, parent_id = row
                print(f"ID:{journal_id:4d} | 日期:{date} | {desc:30s} | 借:{debit:12} | 贷:{credit:12} | 删除:{is_deleted} | Parent:{parent_id}")
                
                total_debit_all += debit
                total_credit_all += credit
                
                # 只统计未删除、且不是被修正元的交易
                if not is_deleted and parent_id is None:
                    total_debit_filtered += debit
                    total_credit_filtered += credit
            
            print()
            print(f"全部交易合计 - 借方: {total_debit_all:>12,} | 贷方: {total_credit_all:>12,}")
            print(f"过滤后合计   - 借方: {total_debit_filtered:>12,} | 贷方: {total_credit_filtered:>12,}")
            
            # 查看是否有逆仕訳标记
            print()
            print("=== 检查是否有逆仕訳 ===")
            sql2 = """
            SELECT COUNT(*) as cnt
            FROM journal_entries
            WHERE entry_date >= '2025-06-01'
              AND entry_date <= '2025-06-30'
              AND description LIKE '%[逆仕訳]%'
            """
            
            cur.execute(sql2)
            result = cur.fetchone()
            print(f"标记为逆仕訳的交易数: {result['cnt']}")
            
            # 检查是否有parent_entry_id的交易
            print()
            print("=== 检查修正記録 ===")
            sql3 = """
            SELECT COUNT(*) as cnt
            FROM journal_entries
            WHERE entry_date >= '2025-06-01'
              AND entry_date <= '2025-06-30'
              AND parent_entry_id IS NOT NULL
            """
            
            cur.execute(sql3)
            result = cur.fetchone()
            print(f"有parent_entry_id的交易数: {result['cnt']}")
            
            # 查看哪些交易是作为修正元的
            print()
            print("=== 作为修正元的交易 ===")
            sql4 = """
            SELECT DISTINCT parent_entry_id
            FROM journal_entries
            WHERE parent_entry_id IS NOT NULL
              AND parent_entry_id IN (
                  SELECT journal_entry_id
                  FROM journal_entries
                  WHERE entry_date >= '2025-06-01'
                    AND entry_date <= '2025-06-30'
              )
            """
            
            cur.execute(sql4)
            results = cur.fetchall()
            for result in results:
                print(f"  被修正的交易ID: {result['parent_entry_id']}")

if __name__ == "__main__":
    check_accounts_data()
