# 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_convertible_bonds_table_exists(engine):
    """确保可转债表存在"""
    try:
        create_table_query = text("""
            CREATE TABLE IF NOT EXISTS convertible_bonds (
                id INT AUTO_INCREMENT PRIMARY KEY,
                symbol VARCHAR(20),
                name VARCHAR(100),
                current FLOAT,
                percent FLOAT,
                chg FLOAT,
                underlying_symbol VARCHAR(20),
                underlying_name VARCHAR(100),
                underlying_current FLOAT,
                underlying_percent FLOAT,
                underlying_pb FLOAT,
                underlying_navps FLOAT,
                conversion_price FLOAT,
                conversion_value FLOAT,
                premium_rate FLOAT,
                convert_bond_ratio FLOAT,
                remain_year FLOAT,
                total_issue_scale FLOAT,
                benefit_before_tax FLOAT,
                benefit_after_tax FLOAT,
                putback_price FLOAT,
                maturity_time BIGINT,
                interest_memo TEXT,
                add_time DATETIME
            )
        """)
        with engine.connect() as conn:
            conn.execute(create_table_query)
            conn.commit()
    except Exception as e:
        print("创建可转债表时发生错误: {}".format(str(e)))

def fetch_convertible_bonds(db_url):
    """获取可转债数据并保存到数据库"""
    base_url = 'https://stock.xueqiu.com/v5/stock/screener/quote/list.json'
    headers = XUEQIU_HEADERS
    all_data = []
    page = 1
    
    try:
        while True:
            params = {
                'page': page,
                'size': 90,
                'order': 'desc',
                'order_by': 'underlying_percent',
                'market': 'CN',
                'type': 'convert'
            }

            response = requests.get(base_url, headers=headers, params=params)

            if response.status_code != 200:
                print(f"响应内容: {response.text}")
                break

            data = response.json()
            if data.get('error_code') != 0:
                break

            bonds = data['data']['list']
            if not bonds:  # 如果没有更多数据了
                print("没有更多数据了")
                break
                
            all_data.extend(bonds)
            total_count = data['data'].get('count', 0)

            # 如果已经获取了所有数据，就退出循环
            if len(all_data) >= total_count:
                break
            page += 1

        if all_data:
            # 转换为DataFrame
            df = pd.DataFrame(all_data)
            
            # 选择需要的列
            selected_columns = [
                'symbol', 'name', 'current', 'percent', 'chg',
                'underlying_symbol', 'underlying_name', 'underlying_current',
                'underlying_percent', 'underlying_pb', 'underlying_navps',
                'conversion_price', 'conversion_value', 'premium_rate',
                'convert_bond_ratio', 'remain_year', 'total_issue_scale',
                'benefit_before_tax', 'benefit_after_tax', 'putback_price',
                'maturity_time', 'interest_memo'
            ]
            
            df = df[selected_columns]
            
            # 添加时间戳
            df['add_time'] = datetime.now().strftime('%Y-%m-%d %H:%M:%S')
            
            # 创建数据库连接
            engine = create_engine(db_url)
            
            # 确保表存在
            ensure_convertible_bonds_table_exists(engine)
            
            # 将数据写入数据库（使用replace模式）
            df.to_sql('convertible_bonds', con=engine, if_exists='replace', index=False)

        else:
            print("未获取到可转债数据")
            
    except Exception as e:
        print("获取可转债数据时发生错误:")
        print(f"错误类型: {type(e).__name__}")

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