# coding:utf-8

import requests
import pandas as pd
from sqlalchemy import create_engine, text
from datetime import datetime
from config import XUEQIU_HEADERS

def ensure_hk_stocks_table_exists(engine):
    """确保港股热门股票表存在"""
    try:
        create_table_query = text("""
            CREATE TABLE IF NOT EXISTS hk_hot_stocks (
                id INT AUTO_INCREMENT PRIMARY KEY,
                symbol VARCHAR(10),
                name VARCHAR(100),
                value INT,
                increment INT,
                rank_change INT,
                percent FLOAT,
                current FLOAT,
                chg FLOAT,
                exchange VARCHAR(10),
                stock_type INT,
                add_time DATETIME
            )
        """)
        with engine.connect() as conn:
            conn.execute(create_table_query)
            conn.commit()
        print("港股热门股票表检查/创建完成")
    except Exception as e:
        print("创建港股热门股票表时发生错误: {}".format(str(e)))

def fetch_hk_hot_stocks(db_url):
    """获取港股热门股票数据并保存到数据库"""
    url = 'https://stock.xueqiu.com/v5/stock/hot_stock/list.json'
    params = {
        'page': 1,
        'size': 20,
        '_type': 13,
        'type': 13
    }
    headers = XUEQIU_HEADERS

    try:
        # 发送请求获取数据
        response = requests.get(url, headers=headers, params=params)
        if response.status_code != 200:
            print(f"请求失败，状态码：{response.status_code}")
            return

        data = response.json()
        if data.get('error_code') != 0:
            print(f"获取数据失败：{data.get('error_description')}")
            return

        # 提取股票列表数据
        stocks = data['data']['items']
        
        # 转换为DataFrame
        df = pd.DataFrame(stocks)
        
        # 选择需要的列
        selected_columns = [
            'symbol', 'name', 'value', 'increment', 'rank_change',
            'percent', 'current', 'chg', 'exchange', 'stock_type'
        ]
        df = df[selected_columns]
        
        # 添加时间戳
        df['add_time'] = datetime.now().strftime('%Y-%m-%d %H:%M:%S')

        # 创建数据库连接
        engine = create_engine(db_url)
        
        # 确保表存在
        ensure_hk_stocks_table_exists(engine)
        
        # 将数据写入数据库
        df.to_sql('hk_hot_stocks', con=engine, if_exists='append', index=False)
        
        print(f"成功保存{len(df)}条港股热门股票数据")
        
        # 清理历史数据（只保留当天的数据）
        clean_historical_data(engine)
        
    except Exception as e:
        print("获取港股热门股票数据时发生错误: {}".format(str(e)))

def clean_historical_data(engine):
    """清理当天0点之前的历史数据"""
    try:
        today = datetime.now().strftime('%Y-%m-%d 00:00:00')
        
        clean_query = text("""
            DELETE FROM hk_hot_stocks 
            WHERE add_time < :today
        """)
        
        with engine.connect() as conn:
            result = conn.execute(clean_query, {'today': today})
            conn.commit()
            
        print("清理历史数据完成: 删除了{}条记录".format(result.rowcount))
        
    except Exception as e:
        print("清理历史数据时发生错误: {}".format(str(e)))

if __name__ == "__main__":
    # 示例调用
    db_url = 'mysql+pymysql://root:Chlry#$.8@192.168.18.199:3306/db_gp_cj'
    fetch_hk_hot_stocks(db_url) 