#!/usr/bin/env python3
"""
生成导入数据的SQL脚本
因为scp网络有问题，直接生成SQL文本传到容器执行
"""

import pandas as pd
from vnpy.trader.constant import Exchange, Interval

parquet_path = "/Users/chufeng/nas/stock-data/sanguo_quant_live/zhaoyun-data/data/raw/daily/sh510300_daily.parquet"
symbol = "510300"
exchange = Exchange.SSE
exchange_code = exchange.value
interval = Interval.DAILY
interval_code = interval.value

df = pd.read_parquet(parquet_path)
print(f"读取数据: {len(df)} 行")

output_file = "/Users/chufeng/.openclaw/workspace-jiangwei/import_data.sql"

with open(output_file, 'w') as f:
    f.write("BEGIN TRANSACTION;\n")
    f.write("DELETE FROM dbbardata WHERE symbol = ? AND exchange = ?;\n")
    f.write(f"-- 准备插入 {len(df)} 条数据\n")
    
    for idx, row in df.iterrows():
        dt = row['trade_date']
        # 转换为Unix时间戳？不，vnpy存datetime
        dt_str = dt.strftime('%Y-%m-%d %H:%M:%S')
        open_price = row['open']
        high_price = row['high']
        low_price = row['low']
        close_price = row['close']
        volume = row['volume']
        turnover = row['amount']
        
        # vnpy_sqlite表结构dbbardata:
        # id (INTEGER PRIMARY KEY AUTOINCREMENT)
        # symbol (TEXT)
        # exchange (TEXT)
        # interval (TEXT)
        # datetime (datetime)
        # open_price (float)
        # high_price (float)
        # low_price (float)
        # close_price (float)
        # volume (int)
        # turnover (float)
        
        sql = f"""INSERT INTO dbbardata (symbol, exchange, interval, datetime, open_price, high_price, low_price, close_price, volume, turnover) VALUES ('{symbol}', '{exchange_code}', '{interval_code}', '{dt_str}', {open_price}, {high_price}, {low_price}, {close_price}, {volume}, {turnover});\n"""
        f.write(sql)
    
    f.write("COMMIT;\n")
    f.write("-- 导入完成\n")

print(f"SQL脚本生成完成: {output_file}")
print(f"文件大小: {open(output_file).read().__len__()} bytes")
