"""
融资融券数据采集模块-采集太麻烦了，已被废弃，用东方财富的非常好用
python -m src.valuation_analysis.cli rzrq --action init --output-sql
提供从同花顺网站采集融资融券数据并存储到数据库的功能
功能包括：
1. 采集融资融券数据
2. 存储数据到数据库
3. 定时自动更新数据
"""

import requests
import pandas as pd
from bs4 import BeautifulSoup
import datetime
import logging
import time
import re
from sqlalchemy import create_engine, text

from .config import DB_URL, LOG_FILE

# 配置日志
logging.basicConfig(
    level=logging.INFO,
    format='%(asctime)s - %(name)s - %(levelname)s - %(message)s',
    handlers=[
        logging.FileHandler(LOG_FILE),
        logging.StreamHandler()
    ]
)
logger = logging.getLogger("rzrq_collector")


class RzrqCollector:
    """融资融券数据采集器类"""
    
    def __init__(self, db_url: str = DB_URL):
        """
        初始化融资融券数据采集器
        
        Args:
            db_url: 数据库连接URL
        """
        self.engine = create_engine(
            db_url,
            pool_size=5,
            max_overflow=10,
            pool_recycle=3600
        )
        self.base_url = "https://data.10jqka.com.cn/market/rzrq/board/getRzrqPage/page/{}/ajax/1/"
        self.headers = {
                            "Accept": "text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.7",
                            "Accept-Encoding": "gzip, deflate, br, zstd",
                            "Accept-Language": "zh-CN,zh;q=0.9,en;q=0.8",
                            "Cache-Control": "max-age=0",
                            "Connection": "keep-alive",
                            "Cookie": "Hm_lvt_722143063e4892925903024537075d0d=1746513000; HMACCOUNT=8B64A2E3C307C8C0; Hm_lvt_929f8b362150b1f77b477230541dbbc2=1746513000; Hm_lvt_78c58f01938e4d85eaf619eae71b4ed1=1744946910,1746513000; Hm_lvt_60bad21af9c824a4a0530d5dbf4357ca=1746513010; Hm_lvt_f79b64788a4e377c608617fba4c736e2=1746513010; Hm_lpvt_722143063e4892925903024537075d0d=1746513010; Hm_lpvt_929f8b362150b1f77b477230541dbbc2=1746513010; Hm_lpvt_60bad21af9c824a4a0530d5dbf4357ca=1747277468; Hm_lpvt_78c58f01938e4d85eaf619eae71b4ed1=1747277468; Hm_lpvt_f79b64788a4e377c608617fba4c736e2=1747277468; v=AyrTmSqbNs9Y6LqjfoQNiqCwe5vJm6-AoB8imrTj1n0I58QFnCv-BXCvlqGH",
                            "Host": "data.10jqka.com.cn",
                            "Sec-Fetch-Dest": "document",
                            "Sec-Fetch-Mode": "navigate",
                            "Sec-Fetch-Site": "none",
                            "Sec-Fetch-User": "?1",
                            "Upgrade-Insecure-Requests": "1",
                            "User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/134.0.0.0 Safari/537.36",
                            "sec-ch-ua": "\"Chromium\";v=\"134\", \"Not:A-Brand\";v=\"24\", \"Google Chrome\";v=\"134\"",
                            "sec-ch-ua-mobile": "?0",
                            "sec-ch-ua-platform": "\"Windows\""
                        }
        logger.info("融资融券数据采集器初始化完成")
    
    def _parse_date(self, date_str: str) -> datetime.date:
        """将日期字符串解析为日期对象"""
        if not date_str:
            return None
        try:
            return datetime.datetime.strptime(date_str, "%Y-%m-%d").date()
        except ValueError:
            logger.error(f"日期解析失败: {date_str}")
            return None
    
    def _parse_amount(self, amount_str: str) -> float:
        """将金额字符串解析为浮点数"""
        if not amount_str:
            return 0.0
        try:
            # 去除逗号和其他非数字字符（除了小数点）
            clean_str = re.sub(r'[^\d.]', '', amount_str)
            return float(clean_str)
        except ValueError:
            logger.error(f"金额解析失败: {amount_str}")
            return 0.0
    
    def _extract_table_data(self, html_content: str) -> pd.DataFrame:
        """
        从HTML内容中提取表格数据
        
        Args:
            html_content: HTML内容
            
        Returns:
            包含融资融券数据的DataFrame
        """
        try:
            soup = BeautifulSoup(html_content, 'html.parser')
            table = soup.find('table', class_='m-table')
            
            if not table:
                logger.error("未找到数据表格")
                return pd.DataFrame()
            
            rows = table.find_all('tr')
            data = []
            
            # 跳过表头行（前两行）
            for row in rows[2:]:  # 从第三行开始是数据行
                cells = row.find_all('td')
                if len(cells) >= 17:  # 确保行中有足够的单元格
                    row_data = {
                        'trade_date': self._parse_date(cells[0].text.strip()),
                        'sh_financing_balance': self._parse_amount(cells[1].text.strip()),
                        'sz_financing_balance': self._parse_amount(cells[2].text.strip()),
                        'bj_financing_balance': self._parse_amount(cells[3].text.strip()),
                        'total_financing_balance': self._parse_amount(cells[4].text.strip()),
                        'sh_financing_buy': self._parse_amount(cells[5].text.strip()),
                        'sz_financing_buy': self._parse_amount(cells[6].text.strip()),
                        'bj_financing_buy': self._parse_amount(cells[7].text.strip()),
                        'total_financing_buy': self._parse_amount(cells[8].text.strip()),
                        'sh_securities_balance': self._parse_amount(cells[9].text.strip()),
                        'sz_securities_balance': self._parse_amount(cells[10].text.strip()),
                        'bj_securities_balance': self._parse_amount(cells[11].text.strip()),
                        'total_securities_balance': self._parse_amount(cells[12].text.strip()),
                        'sh_rzrq_balance': self._parse_amount(cells[13].text.strip()),
                        'sz_rzrq_balance': self._parse_amount(cells[14].text.strip()),
                        'bj_rzrq_balance': self._parse_amount(cells[15].text.strip()),
                        'total_rzrq_balance': self._parse_amount(cells[16].text.strip()),
                    }
                    data.append(row_data)
            
            df = pd.DataFrame(data)
            return df
            
        except Exception as e:
            logger.error(f"提取表格数据失败: {e}")
            return pd.DataFrame()
    
    def fetch_page_data(self, page: int = 1) -> pd.DataFrame:
        """
        获取指定页码的融资融券数据
        
        Args:
            page: 页码
            
        Returns:
            包含该页融资融券数据的DataFrame
        """
        try:
            url = self.base_url.format(page)
            logger.info(f"开始获取第 {page} 页数据: {url}")
            
            response = requests.get(url, headers=self.headers)
            print(response.text)
            if response.status_code != 200:
                logger.error(f"获取第 {page} 页数据失败: HTTP {response.status_code}")
                return pd.DataFrame()
            
            df = self._extract_table_data(response.text)
            
            if df.empty:
                logger.warning(f"第 {page} 页未找到有效数据")
            else:
                logger.info(f"第 {page} 页数据获取成功，包含 {len(df)} 条记录")
                
            return df
            
        except Exception as e:
            logger.error(f"获取第 {page} 页数据失败: {e}")
            return pd.DataFrame()
    
    def fetch_all_data(self, pages: int = 4) -> pd.DataFrame:
        """
        获取所有页的融资融券数据
        
        Args:
            pages: 要获取的总页数
            
        Returns:
            包含所有融资融券数据的DataFrame
        """
        all_data = []
        
        for page in range(1, pages + 1):
            page_data = self.fetch_page_data(page)
            if not page_data.empty:
                all_data.append(page_data)
            # 添加延迟，避免请求过于频繁
            time.sleep(60)
        
        if all_data:
            combined_df = pd.concat(all_data, ignore_index=True)
            logger.info(f"所有页数据获取完成，共 {len(combined_df)} 条记录")
            return combined_df
        else:
            logger.warning("未获取到任何有效数据")
            return pd.DataFrame()
    
    def _ensure_table_exists(self) -> bool:
        """
        确保数据表存在，如果不存在则创建
        
        Returns:
            是否成功确保表存在
        """
        try:
            create_table_query = text("""
            CREATE TABLE IF NOT EXISTS rzrq_data (
                trade_date DATE PRIMARY KEY,
                sh_financing_balance DECIMAL(12,2) COMMENT '上海融资余额（亿元）',
                sz_financing_balance DECIMAL(12,2) COMMENT '深圳融资余额（亿元）',
                bj_financing_balance DECIMAL(12,2) COMMENT '北京融资余额（亿元）',
                total_financing_balance DECIMAL(12,2) COMMENT '融资余额合计（亿元）',
                sh_financing_buy DECIMAL(12,2) COMMENT '上海融资买入额（亿元）',
                sz_financing_buy DECIMAL(12,2) COMMENT '深圳融资买入额（亿元）',
                bj_financing_buy DECIMAL(12,2) COMMENT '北京融资买入额（亿元）',
                total_financing_buy DECIMAL(12,2) COMMENT '融资买入额合计（亿元）',
                sh_securities_balance DECIMAL(12,2) COMMENT '上海融券余量余额（亿元）',
                sz_securities_balance DECIMAL(12,2) COMMENT '深圳融券余量余额（亿元）',
                bj_securities_balance DECIMAL(12,2) COMMENT '北京融券余量余额（亿元）',
                total_securities_balance DECIMAL(12,2) COMMENT '融券余量余额合计（亿元）',
                sh_rzrq_balance DECIMAL(12,2) COMMENT '上海融资融券余额（亿元）',
                sz_rzrq_balance DECIMAL(12,2) COMMENT '深圳融资融券余额（亿元）',
                bj_rzrq_balance DECIMAL(12,2) COMMENT '北京融资融券余额（亿元）',
                total_rzrq_balance DECIMAL(12,2) COMMENT '融资融券余额合计（亿元）',
                created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
                updated_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP
            ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COMMENT='融资融券数据表';
            """)
            
            with self.engine.connect() as conn:
                conn.execute(create_table_query)
                conn.commit()
                
            logger.info("融资融券数据表创建成功")
            return True
            
        except Exception as e:
            logger.error(f"确保数据表存在失败: {e}")
            return False
    
    def save_to_database(self, data: pd.DataFrame) -> bool:
        """
        将数据保存到数据库
        
        首次存储时，存储所有数据；
        后续更新时，如果数据已存在则更新，不存在则插入
        
        Args:
            data: 要保存的数据DataFrame
            
        Returns:
            是否成功保存数据
        """
        if data.empty:
            logger.warning("没有数据需要保存")
            return False
        
        try:
            # 确保数据表存在
            if not self._ensure_table_exists():
                return False
            
            # 添加数据或更新已有数据
            inserted_count = 0
            updated_count = 0
            
            with self.engine.connect() as conn:
                for _, row in data.iterrows():
                    # 检查该日期的数据是否已存在
                    check_query = text("""
                    SELECT COUNT(*) FROM rzrq_data WHERE trade_date = :trade_date
                    """)
                    result = conn.execute(check_query, {"trade_date": row['trade_date']}).scalar()
                    
                    if result > 0:  # 数据已存在，执行更新
                        update_query = text("""
                        UPDATE rzrq_data SET
                            sh_financing_balance = :sh_financing_balance,
                            sz_financing_balance = :sz_financing_balance,
                            bj_financing_balance = :bj_financing_balance,
                            total_financing_balance = :total_financing_balance,
                            sh_financing_buy = :sh_financing_buy,
                            sz_financing_buy = :sz_financing_buy,
                            bj_financing_buy = :bj_financing_buy,
                            total_financing_buy = :total_financing_buy,
                            sh_securities_balance = :sh_securities_balance,
                            sz_securities_balance = :sz_securities_balance,
                            bj_securities_balance = :bj_securities_balance,
                            total_securities_balance = :total_securities_balance,
                            sh_rzrq_balance = :sh_rzrq_balance,
                            sz_rzrq_balance = :sz_rzrq_balance,
                            bj_rzrq_balance = :bj_rzrq_balance,
                            total_rzrq_balance = :total_rzrq_balance
                        WHERE trade_date = :trade_date
                        """)
                        conn.execute(update_query, row.to_dict())
                        updated_count += 1
                    else:  # 数据不存在，执行插入
                        insert_query = text("""
                        INSERT INTO rzrq_data (
                            trade_date, 
                            sh_financing_balance, sz_financing_balance, bj_financing_balance, total_financing_balance,
                            sh_financing_buy, sz_financing_buy, bj_financing_buy, total_financing_buy,
                            sh_securities_balance, sz_securities_balance, bj_securities_balance, total_securities_balance,
                            sh_rzrq_balance, sz_rzrq_balance, bj_rzrq_balance, total_rzrq_balance
                        ) VALUES (
                            :trade_date,
                            :sh_financing_balance, :sz_financing_balance, :bj_financing_balance, :total_financing_balance,
                            :sh_financing_buy, :sz_financing_buy, :bj_financing_buy, :total_financing_buy,
                            :sh_securities_balance, :sz_securities_balance, :bj_securities_balance, :total_securities_balance,
                            :sh_rzrq_balance, :sz_rzrq_balance, :bj_rzrq_balance, :total_rzrq_balance
                        )
                        """)
                        conn.execute(insert_query, row.to_dict())
                        inserted_count += 1
                
                conn.commit()
            
            logger.info(f"数据保存成功：新增 {inserted_count} 条记录，更新 {updated_count} 条记录")
            return True
            
        except Exception as e:
            logger.error(f"保存数据到数据库失败: {e}")
            return False

    def update_latest_data(self) -> bool:
        """
        更新最新一天的融资融券数据
        
        仅获取第一页的第一行数据，如果数据库中已存在，则更新；否则插入
        
        Returns:
            是否成功更新最新数据
        """
        try:
            logger.info("开始更新最新一天的融资融券数据")
            
            # 获取第一页数据
            df = self.fetch_page_data(1)
            if df.empty:
                logger.warning("未获取到最新数据")
                return False
            
            # 只保留第一行（最新一天的数据）
            latest_data = df.iloc[:1]
            
            # 保存数据到数据库
            result = self.save_to_database(latest_data)
            
            if result:
                logger.info(f"最新数据（{latest_data.iloc[0]['trade_date']}）更新成功")
            else:
                logger.warning("最新数据更新失败")
                
            return result
            
        except Exception as e:
            logger.error(f"更新最新数据失败: {e}")
            return False

    def initial_data_collection(self) -> bool:
        """
        首次全量采集融资融券数据
        
        采集所有页的数据并保存到数据库
        
        Returns:
            是否成功采集所有数据
        """
        try:
            logger.info("开始首次全量采集融资融券数据")
            
            # 获取所有页的数据
            df = self.fetch_all_data()
            if df.empty:
                logger.warning("未获取到任何数据")
                return False
            
            # 保存数据到数据库
            result = self.save_to_database(df)
            
            if result:
                logger.info(f"全量数据采集完成，共采集 {len(df)} 条记录")
            else:
                logger.warning("全量数据采集失败")
                
            return result
            
        except Exception as e:
            logger.error(f"首次全量采集失败: {e}")
            return False

    def get_chart_data(self, limit_days: int = 30) -> dict:
        """
        获取融资融券数据用于图表展示
        
        Args:
            limit_days: 获取最近多少天的数据，默认30天
            
        Returns:
            dict: 包含图表所需数据的字典
        """
        try:
            logger.info(f"获取最近 {limit_days} 天融资融券数据用于图表展示")
            
            # 构建SQL查询
            query = text("""
            SELECT 
                trade_date,
                total_financing_balance,
                total_financing_buy,
                total_securities_balance,
                total_rzrq_balance
            FROM rzrq_data
            ORDER BY trade_date DESC
            LIMIT :limit_days
            """)
            
            # 执行查询
            with self.engine.connect() as conn:
                result = conn.execute(query, {"limit_days": limit_days}).fetchall()
                
            if not result:
                logger.warning("未找到融资融券数据")
                return {"success": False, "message": "未找到融资融券数据"}
            
            # 将结果转换为列表并倒序，使日期按升序排列
            rows = []
            for row in result:
                # 将每行结果转换为字典
                row_dict = {
                    'trade_date': row.trade_date,
                    'total_financing_balance': row.total_financing_balance,
                    'total_financing_buy': row.total_financing_buy, 
                    'total_securities_balance': row.total_securities_balance,
                    'total_rzrq_balance': row.total_rzrq_balance
                }
                rows.append(row_dict)
            
            # 反转列表使日期按升序排列
            rows.reverse()
            
            # 准备数据
            dates = []
            total_rzrq_balance = []  # 融资融券余额合计
            total_financing_buy = []  # 融资买入额合计
            total_financing_balance = []  # 融资余额合计
            financing_repayment = []  # 融资偿还
            securities_balance = []  # 融券余额
            
            prev_financing_balance = None  # 上一日融资余额
            
            for i, row in enumerate(rows):
                dates.append(row['trade_date'].strftime('%Y-%m-%d'))
                total_rzrq_balance.append(float(row['total_rzrq_balance']))
                total_financing_buy.append(float(row['total_financing_buy']))
                total_financing_balance.append(float(row['total_financing_balance']))
                
                # 计算融券余额 = 融资融券余额合计 - 融资余额合计
                securities_bal = float(row['total_rzrq_balance']) - float(row['total_financing_balance'])
                securities_balance.append(securities_bal)
                
                # 计算融资偿还 = 融资买入额 + 前一日融资余额 - 当日融资余额
                if i > 0 and prev_financing_balance is not None:
                    repayment = float(row['total_financing_buy']) + prev_financing_balance - float(row['total_financing_balance'])
                    financing_repayment.append(repayment)
                else:
                    financing_repayment.append(None)  # 第一天无法计算
                
                prev_financing_balance = float(row['total_financing_balance'])
            
            # 计算市场风险分析指标
            risk_indicators = self.analyze_market_risk(
                dates=dates,
                total_rzrq_balance=total_rzrq_balance,
                total_financing_balance=total_financing_balance,
                total_financing_buy=total_financing_buy,
                securities_balance=securities_balance
            )
            
            # 构建返回数据
            chart_data = {
                "success": True,
                "dates": dates,
                "series": [
                    {
                        "name": "融资融券余额合计",
                        "data": total_rzrq_balance,
                        "unit": "亿元"
                    },
                    {
                        "name": "融资买入额合计",
                        "data": total_financing_buy,
                        "unit": "亿元"
                    },
                    {
                        "name": "融资余额合计",
                        "data": total_financing_balance,
                        "unit": "亿元"
                    },
                    {
                        "name": "融资偿还",
                        "data": financing_repayment,
                        "unit": "亿元"
                    },
                    {
                        "name": "融券余额",
                        "data": securities_balance,
                        "unit": "亿元"
                    }
                ],
                "risk_indicators": risk_indicators
            }
            
            # 添加更新时间
            chart_data["last_update"] = datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S")
            
            logger.info(f"成功获取 {len(dates)} 天的融资融券图表数据")
            return chart_data
            
        except Exception as e:
            logger.error(f"获取融资融券图表数据失败: {e}")
            return {"success": False, "message": f"获取数据失败: {str(e)}"}
            
    def analyze_market_risk(self, dates, total_rzrq_balance, total_financing_balance, total_financing_buy, securities_balance):
        """
        分析融资融券数据，计算市场风险指标
        
        Args:
            dates: 日期列表
            total_rzrq_balance: 融资融券余额合计列表
            total_financing_balance: 融资余额合计列表
            total_financing_buy: 融资买入额合计列表
            securities_balance: 融券余额列表
            
        Returns:
            dict: 包含风险指标的字典
        """
        try:
            # 确保数据长度足够
            if len(dates) < 5:
                return {"warning": "数据量不足，无法进行完整分析"}
                
            risk_indicators = {}
            
            # 1. 计算融资融券余额趋势 - 近期变化率
            recent_days = 5  # 分析最近5天的变化
            if len(total_rzrq_balance) >= recent_days:
                latest_balance = total_rzrq_balance[-1]
                prev_balance = total_rzrq_balance[-recent_days]
                
                if prev_balance > 0:
                    balance_change_rate = (latest_balance - prev_balance) / prev_balance * 100
                    balance_change_direction = "上升" if balance_change_rate > 0 else "下降"
                    
                    risk_indicators["recent_balance_change"] = {
                        "rate": round(balance_change_rate, 2),
                        "direction": balance_change_direction,
                        "days": recent_days,
                        "start_date": dates[-recent_days],
                        "end_date": dates[-1],
                        "start_value": round(prev_balance, 2),
                        "end_value": round(latest_balance, 2)
                    }
                    
                    # 风险评级 - 融资融券余额变化
                    if balance_change_rate > 10:
                        balance_risk_level = "高"
                        balance_risk_desc = f"近{recent_days}天融资融券余额快速上升{abs(round(balance_change_rate, 2))}%，市场杠杆水平快速提升，风险较高"
                    elif balance_change_rate > 5:
                        balance_risk_level = "中"
                        balance_risk_desc = f"近{recent_days}天融资融券余额上升{abs(round(balance_change_rate, 2))}%，市场杠杆水平有所提升"
                    elif balance_change_rate < -10:
                        balance_risk_level = "高"
                        balance_risk_desc = f"近{recent_days}天融资融券余额快速下降{abs(round(balance_change_rate, 2))}%，市场杠杆水平快速下降，可能伴随抛压"
                    elif balance_change_rate < -5:
                        balance_risk_level = "中"
                        balance_risk_desc = f"近{recent_days}天融资融券余额下降{abs(round(balance_change_rate, 2))}%，市场杠杆水平有所下降"
                    else:
                        balance_risk_level = "低"
                        balance_risk_desc = f"近{recent_days}天融资融券余额变化{round(balance_change_rate, 2)}%，市场杠杆水平相对稳定"
                        
                    risk_indicators["balance_risk"] = {
                        "level": balance_risk_level,
                        "description": balance_risk_desc
                    }
            
            # 2. 计算融资偿还比例 - 衡量资金偿还压力
            # 融资偿还比例 = 当日融资偿还 / 当日融资买入
            recent_days_repay = min(5, len(total_financing_buy) - 1)  # 考虑数据长度
            if recent_days_repay > 0:
                # 计算近几天的平均偿还买入比
                repay_buy_ratios = []
                for i in range(1, recent_days_repay + 1):
                    if total_financing_buy[-i] > 0:
                        # 融资偿还 = 当日融资买入额 + 前一日融资余额 - 当日融资余额
                        if i < len(total_financing_balance) and i+1 < len(total_financing_balance):
                            repayment = total_financing_buy[-i] + total_financing_balance[-(i+1)] - total_financing_balance[-i]
                            ratio = repayment / total_financing_buy[-i]
                            repay_buy_ratios.append(ratio)
                
                if repay_buy_ratios:
                    avg_repay_buy_ratio = sum(repay_buy_ratios) / len(repay_buy_ratios)
                    
                    risk_indicators["repay_buy_ratio"] = {
                        "value": round(avg_repay_buy_ratio, 2),
                        "days": recent_days_repay
                    }
                    
                    # 风险评级 - 偿还率
                    if avg_repay_buy_ratio > 1.2:
                        repay_risk_level = "高"
                        repay_risk_desc = f"近期融资偿还与买入比率为{round(avg_repay_buy_ratio, 2)}，偿还明显大于买入，市场存在较强的抛压"
                    elif avg_repay_buy_ratio > 1.05:
                        repay_risk_level = "中"
                        repay_risk_desc = f"近期融资偿还与买入比率为{round(avg_repay_buy_ratio, 2)}，偿还略大于买入，市场抛压增加"
                    elif avg_repay_buy_ratio < 0.8:
                        repay_risk_level = "中"
                        repay_risk_desc = f"近期融资偿还与买入比率为{round(avg_repay_buy_ratio, 2)}，买入明显大于偿还，杠杆快速提升"
                    else:
                        repay_risk_level = "低"
                        repay_risk_desc = f"近期融资偿还与买入比率为{round(avg_repay_buy_ratio, 2)}，买入与偿还较为平衡"
                        
                    risk_indicators["repay_risk"] = {
                        "level": repay_risk_level,
                        "description": repay_risk_desc
                    }
            
            # 3. 融券余额变化 - 分析空头力量
            if len(securities_balance) >= recent_days:
                latest_securities = securities_balance[-1]
                prev_securities = securities_balance[-recent_days]
                
                if prev_securities > 0:
                    securities_change_rate = (latest_securities - prev_securities) / prev_securities * 100
                    securities_change_direction = "上升" if securities_change_rate > 0 else "下降"
                    
                    risk_indicators["securities_balance_change"] = {
                        "rate": round(securities_change_rate, 2),
                        "direction": securities_change_direction,
                        "days": recent_days,
                        "start_value": round(prev_securities, 2),
                        "end_value": round(latest_securities, 2)
                    }
                    
                    # 风险评级 - 融券余额变化
                    if securities_change_rate > 15:
                        securities_risk_level = "高"
                        securities_risk_desc = f"近{recent_days}天融券余额快速上升{abs(round(securities_change_rate, 2))}%，空头力量显著增强"
                    elif securities_change_rate > 8:
                        securities_risk_level = "中"
                        securities_risk_desc = f"近{recent_days}天融券余额上升{abs(round(securities_change_rate, 2))}%，空头力量有所增强"
                    elif securities_change_rate < -15:
                        securities_risk_level = "低"
                        securities_risk_desc = f"近{recent_days}天融券余额快速下降{abs(round(securities_change_rate, 2))}%，空头力量显著减弱"
                    elif securities_change_rate < -8:
                        securities_risk_level = "低"
                        securities_risk_desc = f"近{recent_days}天融券余额下降{abs(round(securities_change_rate, 2))}%，空头力量有所减弱"
                    else:
                        securities_risk_level = "正常"
                        securities_risk_desc = f"近{recent_days}天融券余额变化{round(securities_change_rate, 2)}%，空头力量相对稳定"
                        
                    risk_indicators["securities_risk"] = {
                        "level": securities_risk_level,
                        "description": securities_risk_desc
                    }
            
            # 4. 融资占比 - 融资余额占融资融券余额的比例
            if total_rzrq_balance[-1] > 0:
                financing_ratio = total_financing_balance[-1] / total_rzrq_balance[-1]
                risk_indicators["financing_ratio"] = round(financing_ratio * 100, 2)
                
                # 融资占比的历史百分位数计算
                if len(total_rzrq_balance) > 20:  # 至少需要20天数据计算有意义的百分位数
                    historical_ratios = []
                    for i in range(len(total_rzrq_balance)):
                        if total_rzrq_balance[i] > 0:
                            ratio = total_financing_balance[i] / total_rzrq_balance[i]
                            historical_ratios.append(ratio)
                    
                    if historical_ratios:
                        # 计算融资占比的百分位数
                        sorted_ratios = sorted(historical_ratios)
                        percentile_rank = sorted_ratios.index(financing_ratio) / len(sorted_ratios) * 100 if financing_ratio in sorted_ratios else 0
                        
                        # 如果不在列表中，找到最接近的位置
                        if percentile_rank == 0:
                            for i, ratio in enumerate(sorted_ratios):
                                if ratio > financing_ratio:
                                    percentile_rank = i / len(sorted_ratios) * 100
                                    break
                            if percentile_rank == 0 and financing_ratio > sorted_ratios[-1]:
                                percentile_rank = 100
                                
                        risk_indicators["financing_ratio_percentile"] = round(percentile_rank, 2)
            
            # 5. 综合风险评估
            risk_levels = []
            if "balance_risk" in risk_indicators:
                risk_levels.append(risk_indicators["balance_risk"]["level"])
            if "repay_risk" in risk_indicators:
                risk_levels.append(risk_indicators["repay_risk"]["level"])
            if "securities_risk" in risk_indicators:
                risk_levels.append(risk_indicators["securities_risk"]["level"])
                
            # 计算综合风险等级
            high_count = risk_levels.count("高")
            medium_count = risk_levels.count("中")
            
            if high_count >= 2:
                overall_risk = "高"
                overall_desc = "多个指标显示市场风险较高，融资融券数据反映市场杠杆或波动风险增加"
            elif high_count == 1 or medium_count >= 2:
                overall_risk = "中"
                overall_desc = "部分指标显示市场风险增加，建议关注融资融券数据变化"
            else:
                overall_risk = "低"
                overall_desc = "融资融券数据显示市场风险较低，杠杆水平相对稳定"
                
            risk_indicators["overall_risk"] = {
                "level": overall_risk,
                "description": overall_desc
            }
            
            return risk_indicators
            
        except Exception as e:
            logger.error(f"分析市场风险指标失败: {e}")
            return {"error": f"分析失败: {str(e)}"}


def get_create_table_sql() -> str:
    """
    获取创建融资融券数据表的SQL语句
    
    Returns:
        创建表的SQL语句
    """
    return """
    CREATE TABLE IF NOT EXISTS rzrq_data (
        trade_date DATE PRIMARY KEY,
        sh_financing_balance DECIMAL(12,2) COMMENT '上海融资余额（亿元）',
        sz_financing_balance DECIMAL(12,2) COMMENT '深圳融资余额（亿元）',
        bj_financing_balance DECIMAL(12,2) COMMENT '北京融资余额（亿元）',
        total_financing_balance DECIMAL(12,2) COMMENT '融资余额合计（亿元）',
        sh_financing_buy DECIMAL(12,2) COMMENT '上海融资买入额（亿元）',
        sz_financing_buy DECIMAL(12,2) COMMENT '深圳融资买入额（亿元）',
        bj_financing_buy DECIMAL(12,2) COMMENT '北京融资买入额（亿元）',
        total_financing_buy DECIMAL(12,2) COMMENT '融资买入额合计（亿元）',
        sh_securities_balance DECIMAL(12,2) COMMENT '上海融券余量余额（亿元）',
        sz_securities_balance DECIMAL(12,2) COMMENT '深圳融券余量余额（亿元）',
        bj_securities_balance DECIMAL(12,2) COMMENT '北京融券余量余额（亿元）',
        total_securities_balance DECIMAL(12,2) COMMENT '融券余量余额合计（亿元）',
        sh_rzrq_balance DECIMAL(12,2) COMMENT '上海融资融券余额（亿元）',
        sz_rzrq_balance DECIMAL(12,2) COMMENT '深圳融资融券余额（亿元）',
        bj_rzrq_balance DECIMAL(12,2) COMMENT '北京融资融券余额（亿元）',
        total_rzrq_balance DECIMAL(12,2) COMMENT '融资融券余额合计（亿元）',
        created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
        updated_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP
    ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COMMENT='融资融券数据表';
    """


# 示例使用方式
if __name__ == "__main__":
    # 创建融资融券数据采集器
    collector = RzrqCollector()
    
    # 输出创建表的SQL语句
    print("创建表的SQL语句：")
    print(get_create_table_sql())
    
    # 首次全量采集数据
    print("\n开始首次全量采集数据...")
    collector.initial_data_collection()
