#!/usr/bin/env python3
"""
检查数据库中可能的标的名称格式
"""

import sqlite3
import os

def check_all_symbols():
    """检查数据库中所有标的"""
    db_path = '/Users/chufeng/.openclaw/workspace-zhaoyun/zhaoyun-data/data/database_test.db'
    
    if not os.path.exists(db_path):
        print(f"❌ 数据库不存在: {db_path}")
        return []
    
    print(f"🔍 检查数据库中的标的: {db_path}")
    print("="*60)
    
    try:
        conn = sqlite3.connect(db_path)
        cursor = conn.cursor()
        
        # 检查表是否存在
        cursor.execute("SELECT name FROM sqlite_master WHERE type='table' AND name='dbbardata';")
        if not cursor.fetchone():
            print("❌ dbbardata 表不存在")
            return []
        
        # 获取所有标的
        cursor.execute("SELECT DISTINCT symbol FROM dbbardata;")
        symbols = [row[0] for row in cursor.fetchall()]
        
        print(f"📊 数据库中有 {len(symbols)} 个标的:")
        for symbol in symbols:
            cursor.execute("SELECT COUNT(*) FROM dbbardata WHERE symbol = ?", (symbol,))
            count = cursor.fetchone()[0]
            cursor.execute("SELECT MIN(datetime), MAX(datetime) FROM dbbardata WHERE symbol = ?", (symbol,))
            min_dt, max_dt = cursor.fetchone()
            print(f"  - {symbol}: {count} 行, {min_dt} ~ {max_dt}")
        
        conn.close()
        return symbols
        
    except Exception as e:
        print(f"❌ 查询出错: {e}")
        return []

def check_510300_variants():
    """检查 510300 的各种可能格式"""
    print("\n🔍 检查 510300 的各种可能格式...")
    print("="*60)
    
    variants = [
        '510300.SSE',
        '510300.SH', 
        '510300.XSHG',
        '510300',
        '510300.XSH',
        'SH510300',
        '510300.SHFE',
    ]
    
    db_path = '/Users/chufeng/.openclaw/workspace-zhaoyun/zhaoyun-data/data/database_test.db'
    
    try:
        conn = sqlite3.connect(db_path)
        cursor = conn.cursor()
        
        for variant in variants:
            cursor.execute("SELECT COUNT(*) FROM dbbardata WHERE symbol = ?", (variant,))
            count = cursor.fetchone()[0]
            if count > 0:
                print(f"✅ {variant}: {count} 行数据")
            else:
                print(f"❌ {variant}: 0 行数据")
        
        conn.close()
        
    except Exception as e:
        print(f"❌ 查询出错: {e}")

def main():
    """主函数"""
    print("🚀 检查数据库标的格式")
    print("="*60)
    
    # 列出所有标的
    symbols = check_all_symbols()
    
    # 检查510300变体
    check_510300_variants()
    
    print("\n" + "="*60)
    print("总结:")
    
    if not symbols:
        print("❌ dbbardata 表为空，没有任何数据")
        print("\n📋 需要处理:")
        print("1. 联系赵云将军下载 510300 数据")
        print("2. 将数据转换为vn.py格式")
        print("3. 配置正确的数据路径")
    else:
        print(f"✅ 数据库中有 {len(symbols)} 个标的")
        print("请检查关羽将军使用的标的名称是否与数据库一致")
    
    print("="*60)

if __name__ == "__main__":
    main()