#!/usr/bin/env python3
"""
检查赵云将军本地数据中的 510300.SSE 标的
"""

import sqlite3
import pandas as pd
import sys
import os

def find_vnpy_database():
    """查找vn.py数据库文件"""
    db_paths = [
        '/Users/chufeng/.openclaw/workspace-zhaoyun/sanguo_quant_live/zhaoyun-data/data/running_data/database_test.db',
        '/Users/chufeng/.openclaw/workspace-zhaoyun/zhaoyun-data/data/database_test.db',
        '/Users/chufeng/.openclaw/memory/zhaoyun-data.sqlite',
    ]
    
    existing_dbs = []
    for path in db_paths:
        if os.path.exists(path):
            existing_dbs.append(path)
            print(f"✅ 找到数据库: {path}")
        else:
            print(f"❌ 不存在: {path}")
    
    return existing_dbs

def check_symbol_in_db(db_path, symbol):
    """检查数据库中是否存在指定标的"""
    print(f"\n🔍 检查数据库 {db_path} 中的 {symbol}...")
    
    try:
        conn = sqlite3.connect(db_path)
        
        # 列出所有表
        cursor = conn.cursor()
        cursor.execute("SELECT name FROM sqlite_master WHERE type='table';")
        tables = cursor.fetchall()
        print(f"📊 数据库中的表: {[t[0] for t in tables]}")
        
        # 检查可能的表名
        for table_name in ['dbbardata', 'bar_data', 'daily_data', '1d', 'daily']:
            try:
                # 检查表是否存在
                cursor.execute(f"SELECT EXISTS (SELECT 1 FROM sqlite_master WHERE type='table' AND name='{table_name}');")
                exists = cursor.fetchone()[0]
                
                if exists:
                    print(f"\n🔍 检查表 {table_name}...")
                    
                    # 获取总行数
                    cursor.execute(f"SELECT COUNT(*) FROM {table_name};")
                    total_rows = cursor.fetchone()[0]
                    print(f"  总行数: {total_rows}")
                    
                    # 检查symbol是否存在
                    cursor.execute(f"SELECT DISTINCT symbol FROM {table_name} WHERE symbol = ?", (symbol,))
                    result = cursor.fetchone()
                    
                    if result:
                        print(f"  ✅ 找到标的: {symbol}")
                        
                        # 获取该标的数据量
                        cursor.execute(f"SELECT COUNT(*) FROM {table_name} WHERE symbol = ?", (symbol,))
                        count = cursor.fetchone()[0]
                        print(f"  数据行数: {count}")
                        
                        # 获取时间范围
                        cursor.execute(f"SELECT MIN(datetime), MAX(datetime) FROM {table_name} WHERE symbol = ?", (symbol,))
                        min_dt, max_dt = cursor.fetchone()
                        print(f"  时间范围: {min_dt} -> {max_dt}")
                        
                        # 获取前5行数据
                        cursor.execute(f"SELECT * FROM {table_name} WHERE symbol = ? ORDER BY datetime LIMIT 5", (symbol,))
                        rows = cursor.fetchall()
                        
                        # 获取列名
                        cursor.execute(f"PRAGMA table_info({table_name})")
                        columns = [col[1] for col in cursor.fetchall()]
                        print(f"  列名: {columns}")
                        
                        print(f"  前5行数据:")
                        for row in rows:
                            print(f"    {row}")
                        
                        conn.close()
                        return True, {
                            'table': table_name,
                            'count': count,
                            'min_dt': min_dt,
                            'max_dt': max_dt,
                            'columns': columns
                        }
                    else:
                        print(f"  ❌ 未找到标的 {symbol}")
                        
                        # 列出可用标的供参考
                        cursor.execute(f"SELECT DISTINCT symbol FROM {table_name} LIMIT 10")
                        symbols = cursor.fetchall()
                        if symbols:
                            print(f"  可用标的 (前10个): {[s[0] for s in symbols]}")
            
            except Exception as e:
                print(f"  ⚠️ 检查表出错: {e}")
                continue
        
        conn.close()
        return False, None
        
    except Exception as e:
        print(f"❌ 连接数据库出错: {e}")
        return False, None

def main():
    """主函数"""
    print("🚀 检查赵云将军本地数据中的 510300.SSE 标的")
    print("="*60)
    
    symbol_to_check = "510300.SSE"
    print(f"目标标的: {symbol_to_check}")
    
    # 查找数据库
    dbs = find_vnpy_database()
    
    if not dbs:
        print("❌ 未找到任何数据库文件")
        print("\n📋 可能的原因:")
        print("1. 赵云将军的数据还未转换到vn.py格式")
        print("2. 数据路径配置错误")
        print("3. 510300.SSE 数据还未下载")
        return False
    
    # 检查每个数据库
    found = False
    result = None
    
    for db in dbs:
        found_db, result_db = check_symbol_in_db(db, symbol_to_check)
        if found_db:
            found = True
            result = result_db
            break
    
    # 总结
    print("\n" + "="*60)
    print("检查结果:")
    
    if found:
        print(f"✅ 找到 {symbol_to_check} 数据")
        print(f"  数据行数: {result['count']}")
        print(f"  时间范围: {result['min_dt']} -> {result['max_dt']}")
        print(f"  数据列: {result['columns']}")
        print(f"  表名: {result['table']}")
    else:
        print(f"❌ 未找到 {symbol_to_check} 数据")
        print("\n📋 需要检查:")
        print("1. 赵云将军是否已下载 510300.SSE 数据")
        print("2. 数据是否已转换为vn.py格式")
        print("3. 数据路径配置是否正确")
    
    print("="*60)
    return found

if __name__ == "__main__":
    main()