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

import os
import sys
sys.path.insert(0, 'backend')

os.environ['DB_HOST'] = '192.168.0.61'
os.environ['DB_NAME'] = 'njts_acct'
os.environ['DB_USER'] = 'njts_app'
os.environ['DB_PASSWORD'] = 'njts_app2025'
os.environ['DB_PORT'] = '55432'

from app.core.database import get_connection

with get_connection() as conn:
    with conn.cursor() as cur:
        print('\n【 journal_lines 表结构 】')
        cur.execute("""
            SELECT column_name, data_type, is_nullable
            FROM information_schema.columns
            WHERE table_name = 'journal_lines'
            ORDER BY ordinal_position
        """)
        for row in cur.fetchall():
            print(f'{row["column_name"]:<20} {row["data_type"]:<15} {row["is_nullable"]}')

        print('\n【 journal_lines 前5条数据 】')
        cur.execute("SELECT * FROM journal_lines LIMIT 5")
        cols = [desc[0] for desc in cur.description]
        print('\t'.join(cols))
        for row in cur.fetchall():
            print('\t'.join(str(v) for v in row))

        print('\n【 accounts 表结构 】')
        cur.execute("""
            SELECT column_name, data_type, is_nullable
            FROM information_schema.columns
            WHERE table_name = 'accounts'
            ORDER BY ordinal_position
        """)
        for row in cur.fetchall():
            print(f'{row["column_name"]:<20} {row["data_type"]:<15} {row["is_nullable"]}')

        print('\n【 accounts 数据示例 】')
        cur.execute("SELECT account_id, account_code, account_name FROM accounts WHERE account_code IN ('701', '601') LIMIT 5")
        cols = [desc[0] for desc in cur.description]
        print('\t'.join(cols))
        for row in cur.fetchall():
            print('\t'.join(str(v) for v in row))
