"""
财务分析模块

提供从MySQL和MongoDB获取财务数据并进行分析的功能
"""

import pandas as pd
import numpy as np
from sqlalchemy import create_engine
from pymongo import MongoClient
import logging
from typing import Dict, List, Optional, Union, Tuple
import json
import requests
import redis

from .config import DB_URL, MONGO_CONFIG,MONGO_CONFIG2, LOG_FILE
from .stock_price_collector import StockPriceCollector
from .industry_analysis import IndustryAnalyzer

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

# 初始化Redis连接
redis_client = redis.Redis(
    host='192.168.18.208',  # Redis服务器地址，根据实际情况调整
    port=6379,
    password='wlkj2018',
    db=13,
    socket_timeout=5,
    decode_responses=True
)

class FinancialAnalyzer:
    """财务分析器类"""
    
    def __init__(self):
        """初始化财务分析器"""
        # 初始化MySQL连接
        self.mysql_engine = create_engine(
            DB_URL,
            pool_size=5,
            max_overflow=10,
            pool_recycle=3600
        )
        
        # 初始化MongoDB连接
        self.mongo_client = MongoClient(
            host=MONGO_CONFIG['host'],
            port=MONGO_CONFIG['port'],
            username=MONGO_CONFIG['username'],
            password=MONGO_CONFIG['password']
        )
        # 初始化MongoDB连接
        self.mongo_client2 = MongoClient(
            host=MONGO_CONFIG2['host'],
            port=MONGO_CONFIG2['port'],
            username=MONGO_CONFIG2['username'],
            password=MONGO_CONFIG2['password']
        )
        self.mongo_db = self.mongo_client[MONGO_CONFIG['db']]
        self.mongo_collection = self.mongo_db[MONGO_CONFIG['collection']]
        self.wacc_collection = self.mongo_db['wind_stock_wacc_roic']
        
        logger.info("财务分析器初始化完成")
    
    def _get_growth_indicators(self, wind_data: Dict) -> Dict[str, float]:
        """
        从wind_data中提取增长指标
        
        Args:
            wind_data: wind_data字典
            
        Returns:
            包含增长指标的字典
        """
        indicators = {
            'rd_expense_growth': self._find_indicator(wind_data, 'grows', '研发费用同比增长'),
            'roe_growth': self._find_indicator(wind_data, 'grows', '净资产收益率(摊薄)(同比增长)'),
            'diluted_eps_growth': self._find_indicator(wind_data, 'grows', '稀释每股收益(同比增长率)'),
            'operating_cash_flow_per_share_growth': self._find_indicator(wind_data, 'grows', '每股经营活动产生的现金流量净额(同比增长率)'),
            'revenue_growth': self._find_indicator(wind_data, 'grows', '营业收入(同比增长率)'),
            'operating_profit_growth': self._find_indicator(wind_data, 'grows', '营业利润(同比增长率)'),
            'net_profit_growth_excl_nonrecurring': self._find_indicator(wind_data, 'grows', '归属母公司股东的净利润-扣除非经常性损益(同比增长率)'),
            'operating_cash_flow_growth': self._find_indicator(wind_data, 'grows', '经营活动产生的现金流量净额(同比增长率)')
        }
        return indicators
    
    def _calculate_growth_change(self, current_value: float, previous_value: float) -> Optional[float]:
        """
        计算增长率的增长变化
        
        Args:
            current_value: 当前值
            previous_value: 上一期值
            
        Returns:
            增长变化率，如果无法计算则返回None
        """
        try:
            if current_value is None or previous_value is None:
                return None
            if previous_value == 0:
                return None
            return (current_value - previous_value) / abs(previous_value)
        except Exception as e:
            logger.error(f"计算增长变化率失败: {str(e)}")
            return None
    
    def get_growth_change_indicators(self, stock_code: str, current_year: str = '2024-12-31', previous_year: str = '2023-12-31') -> Dict:
        """
        获取增长指标的变化率
        
        Args:
            stock_code: 股票代码
            current_year: 当前年份，格式为'YYYY-12-31'，默认为'2024-12-31'
            previous_year: 上一年份，格式为'YYYY-12-31'，默认为'2023-12-31'
            
        Returns:
            包含增长指标变化率的字典
        """
        try:
            # 查询MongoDB
            record = self.mongo_collection.find_one({'code': stock_code})
            if not record or 'wind_data' not in record:
                return {
                    'success': False,
                    'message': f'未找到股票 {stock_code} 的财务数据'
                }
            
            # 获取指定年份的数据
            wind_data_current = None
            wind_data_previous = None
            
            for data in record['wind_data']:
                if data['time'] == current_year:
                    wind_data_current = data
                elif data['time'] == previous_year:
                    wind_data_previous = data
            
            if not wind_data_current or not wind_data_previous:
                return {
                    'success': False,
                    'message': f'未找到股票 {stock_code} 的{current_year}或{previous_year}年财务数据'
                }
            
            # 获取两个时间点的指标
            indicators_current = self._get_growth_indicators(wind_data_current)
            indicators_previous = self._get_growth_indicators(wind_data_previous)
            
            # 计算变化率
            growth_changes = {}
            for key in indicators_previous.keys():
                current_value = indicators_current.get(key)
                previous_value = indicators_previous.get(key)
                growth_changes[key] = self._calculate_growth_change(current_value, previous_value)
            
            return {
                'success': True,
                'stock_code': stock_code,
                'data': {
                    f'indicators_{previous_year}': indicators_previous,
                    f'indicators_{current_year}': indicators_current,
                    'growth_changes': growth_changes
                }
            }
            
        except Exception as e:
            logger.error(f"获取增长指标变化率失败: {str(e)}")
            return {
                'success': False,
                'message': f'获取增长指标变化率失败: {str(e)}'
            }
    
    def get_wacc_data(self, stock_code: str) -> Optional[float]:
        """
        获取股票的WACC数据
        
        Args:
            stock_code: 股票代码
            
        Returns:
            WACC值，如果未找到则返回None
        """
        try:
            # 查询MongoDB
            record = self.wacc_collection.find_one({
                'code': stock_code,
                'endTime': '20241231'
            })
            
            if not record:
                # logger.warning(f"未找到股票 {stock_code} 的WACC数据")
                return None
                
            return record['wacc']
            
        except Exception as e:
            logger.error(f"获取WACC数据失败: {str(e)}")
            return None
    
    def _calculate_profit_years(self, wind_data_list: List[Dict]) -> int:
        """
        计算近五年的盈利年数
        
        Args:
            wind_data_list: 按时间排序的wind_data列表
            
        Returns:
            盈利年数
        """
        try:
            profit_years = 0
            # 获取最近5年的数据
            recent_years = sorted(wind_data_list, key=lambda x: x['time'], reverse=True)[:5]
            
            for year_data in recent_years:
                # 获取销售净利率
                net_profit_ratio = self._find_indicator(year_data, 'profitability', '净利润/营业总收入')
                if net_profit_ratio is not None and net_profit_ratio > 0:
                    profit_years += 1
                    
            return profit_years
            
        except Exception as e:
            logger.error(f"计算盈利年数失败: {str(e)}")
            return 0

    def extract_financial_indicators(self, stock_code: str, year: str = None) -> Dict:
        """
        从MongoDB中提取指定的财务指标
        
        Args:
            stock_code: 股票代码
            year: 财报年份（可选，格式'YYYY-12-31'），如不传则取最新
        Returns:
            包含财务指标的字典
        """
        try:
            # 查询MongoDB
            record = self.mongo_collection.find_one({'code': stock_code})
            if not record or 'wind_data' not in record:
                return {
                    'success': False,
                    'message': f'未找到股票 {stock_code} 的财务数据'
                }
            # 获取指定年份的财务数据（按时间排序）
            if year:
                wind_data = next((x for x in record['wind_data'] if x['time'] == year), None)
                if not wind_data:
                    return {
                        'success': False,
                        'message': f'未找到股票 {stock_code} 的{year}年财务数据'
                    }
            else:
                wind_data = sorted(record['wind_data'], key=lambda x: x['time'], reverse=True)[0]
            # 获取WACC数据
            wacc = self.get_wacc_data(stock_code)
            # 计算近五年盈利年数
            profit_years = self._calculate_profit_years(record['wind_data'])

            
            # 定义指标映射
            indicators = {
                # 偿债能力指标
                'debt_equity_ratio': self._find_indicator(wind_data, 'solvency', '产权比率'),
                'debt_ebitda_ratio': self._find_indicator(wind_data, 'solvency', '全部债务/EBITDA'),
                'interest_coverage_ratio': self._find_indicator(wind_data, 'solvency', '已获利息倍数(EBIT/利息费用)'),
                'current_ratio': self._find_indicator(wind_data, 'solvency', '流动比率'),
                'quick_ratio': self._find_indicator(wind_data, 'solvency', '速动比率'),
                'cash_ratio': self._find_indicator(wind_data, 'solvency', '现金比率'),
                'cash_to_debt_ratio': self._find_indicator(wind_data, 'solvency', '经营活动产生的现金流量净额/负债合计'),
                
                # 资本结构指标
                'equity_ratio': self._find_indicator(wind_data, 'capitalStructure', '股东权益比'),
                
                # 盈利能力指标
                'profit_years': profit_years,  # 添加近五年盈利年数
                
                # 成长能力指标
                'diluted_eps_growth': self._find_indicator(wind_data, 'grows', '稀释每股收益(同比增长率)'),
                'operating_cash_flow_per_share_growth': self._find_indicator(wind_data, 'grows', '每股经营活动产生的现金流量净额(同比增长率)'),
                'revenue_growth': self._find_indicator(wind_data, 'grows', '营业收入(同比增长率)'),
                'operating_profit_growth': self._find_indicator(wind_data, 'grows', '营业利润(同比增长率)'),
                'net_profit_growth_excl_nonrecurring': self._find_indicator(wind_data, 'grows', '归属母公司股东的净利润-扣除非经常性损益(同比增长率)'),
                'operating_cash_flow_growth': self._find_indicator(wind_data, 'grows', '经营活动产生的现金流量净额(同比增长率)'),
                'rd_expense_growth': self._find_indicator(wind_data, 'grows', '研发费用同比增长'),
                'roe_growth': self._find_indicator(wind_data, 'grows', '净资产收益率(摊薄)(同比增长)'),
                
                # Z值相关指标
                'working_capital_to_assets': self._find_indicator(wind_data, 'ZValue', '营运资本/总资产'),
                'retained_earnings_to_assets': self._find_indicator(wind_data, 'ZValue', '留存收益/总资产'),
                'ebit_to_assets': self._find_indicator(wind_data, 'ZValue', '息税前利润(TTM)/总资产'),
                'market_value_to_liabilities': self._find_indicator(wind_data, 'ZValue', '当日总市值/负债总计'),
                'equity_to_liabilities': self._find_indicator(wind_data, 'ZValue', '股东权益合计(含少数)/负债总计'),
                'revenue_to_assets': self._find_indicator(wind_data, 'ZValue', '营业收入/总资产'),
                'z_score': self._find_indicator(wind_data, 'ZValue', 'Z值'),
                
                # 营运能力指标
                'inventory_turnover_days': self._find_indicator(wind_data, 'operatingCapacity', '存货周转天数'),
                'receivables_turnover_days': self._find_indicator(wind_data, 'operatingCapacity', '应收账款周转天数'),
                'payables_turnover_days': self._find_indicator(wind_data, 'operatingCapacity', '应付账款周转天数'),
                
                # 盈利能力指标
                'gross_profit_margin': self._find_indicator(wind_data, 'profitability', '销售毛利率'),
                'operating_profit_margin': self._find_indicator(wind_data, 'profitability', '营业利润/营业总收入'),
                'net_profit_margin': self._find_indicator(wind_data, 'profitability', '销售净利率'),
                'roe': self._find_indicator(wind_data, 'profitability', '净资产收益率ROE(平均)'),
                'roa': self._find_indicator(wind_data, 'profitability', '总资产净利率ROA'),
                'roic': self._find_indicator(wind_data, 'profitability', '投入资本回报率ROIC'),
                
                # WACC数据
                'wacc': wacc
            }
            
            # 对数值进行四舍五入处理
            for key, value in indicators.items():
                if isinstance(value, (int, float)) and value is not None:
                    indicators[key] = round(value, 3)
            
            # 添加数据时间
            indicators['data_time'] = wind_data['time']

            
            return {
                'success': True,
                'stock_code': stock_code,
                'indicators': indicators
            }
            
        except Exception as e:
            logger.error(f"提取财务指标失败: {str(e)}")
            return {
                'success': False,
                'message': f'提取财务指标失败: {str(e)}'
            }
    
    def _find_indicator(self, data: Dict, category: str, meaning: str) -> Optional[float]:
        """
        在指定类别中查找指标值
        
        Args:
            data: 财务数据字典
            category: 指标类别
            meaning: 指标含义
            
        Returns:
            指标值，如果未找到则返回None
        """
        try:
            if category not in data:
                return None
                
            for item in data[category]['list']:
                if item['meaning'] == meaning:
                    return item['data']
            return None
            
        except Exception as e:
            logger.error(f"查找指标 {category}.{meaning} 失败: {str(e)}")
            return None
    
    def test_mongo_structure(self, stock_code: str = None) -> Dict:
        """
        测试方法：查看MongoDB集合的字段结构
        
        Args:
            stock_code: 股票代码，如果为None则返回第一条记录
            
        Returns:
            包含字段结构的字典
        """
        try:
            # 构建查询条件
            query = {}
            if stock_code:
                query['code'] = stock_code
                
            # 获取一条记录
            record = self.mongo_collection.find_one(query)
            
            if not record:
                return {
                    'success': False,
                    'message': f'未找到股票 {stock_code} 的记录' if stock_code else '集合为空'
                }
            
            # 移除MongoDB的_id字段
            if '_id' in record:
                record.pop('_id')
            
            # 获取所有字段名
            fields = list(record.keys())
            
            # 格式化输出
            result = {
                'success': True,
                'fields': fields,
                'sample_data': record
            }
            
            # 打印字段信息
            logger.info(f"集合字段列表: {json.dumps(fields, ensure_ascii=False, indent=2)}")
            logger.info(f"示例数据: {json.dumps(record, ensure_ascii=False, indent=2)}")
            
            return result
            
        except Exception as e:
            logger.error(f"查询MongoDB结构失败: {str(e)}")
            return {
                'success': False,
                'message': f'查询失败: {str(e)}'
            }
    
    def get_momentum_indicators(self, stock_code: str, industry_codes: List[str]) -> Dict:
        """
        获取动量指标数据
        
        Args:
            stock_code: 目标股票代码
            industry_codes: 行业股票代码列表
            
        Returns:
            动量指标数据字典
        """
        try:
            url = "http://192.168.18.42:5000/api/dify/getStockMomentumIndex"
            payload = {
                "code_list": industry_codes,
                "target_code": stock_code
            }
            
            response = requests.post(url, json=payload)
            if response.status_code != 200:
                return {
                    'success': False,
                    'message': f'获取动量指标失败: HTTP {response.status_code}'
                }
                
            data = response.json()
            
            # 计算OBV和NATR的rank_score
            obv_rank_score = round((1 - data['obv_rank'] / len(industry_codes)) * 10, 3)
            natr_rank_score = round((1 - data['natr_rank'] / len(industry_codes)) * 10, 3)
            
            return {
                'success': True,
                'indicators': [
                    {
                        'key': 'obv',
                        'desc': '能量',
                        'value': round(data['OBV'], 3),
                        'rank_score': obv_rank_score
                    },
                    {
                        'key': 'form',
                        'desc': f'技术形态-{data["form"]}',
                        'value': round(data['form_probability'], 3),
                        'rank_score': round(data['form_probability'] * 10, 3)  # 将概率转换为0-10的分数
                    },
                    {
                        'key': 'natr',
                        'desc': '阶段位置',
                        'value': round(data['NATR'], 3),
                        'rank_score': natr_rank_score
                    }
                ],
                'avg_score': round((obv_rank_score + natr_rank_score + round(data['form_probability'] * 10, 3)) / 3, 3)
            }
            
        except Exception as e:
            logger.error(f"获取动量指标失败: {str(e)}")
            return {
                'success': False,
                'message': f'获取动量指标失败: {str(e)}'
            }

    def analyze_financial_data(self, stock_code: str, force_update: bool = False, current_year: str = '2024-12-31', previous_year: str = '2023-12-31') -> Dict:
        """
        分析财务数据
        
        Args:
            stock_code: 股票代码
            force_update: 是否强制更新缓存，默认为False
            current_year: 当前年份，格式为'YYYY-12-31'，默认为'2024-12-31'
            previous_year: 上一年份，格式为'YYYY-12-31'，默认为'2023-12-31'
            
        Returns:
            分析结果字典，包含所有财务指标及其排名得分
        """
        try:
            cache_key = f"financial_analysis:{stock_code}:{current_year}"
            # 检查缓存
            if not force_update:
                
                cached_data = redis_client.get(cache_key)
                if cached_data:
                    try:
                        # 尝试解析缓存的JSON数据
                        result = json.loads(cached_data)
                        logger.info(f"从缓存获取股票 {stock_code} 的财务分析数据")
                        return result
                    except Exception as cache_error:
                        logger.warning(f"解析缓存的财务分析数据失败，将重新查询: {cache_error}")

            # 获取股票价格数据
            price_collector = StockPriceCollector()
            price_data = price_collector.get_stock_price_data(stock_code)
            
            # 获取概念板块数据
            industry_analyzer = IndustryAnalyzer()
            concepts = industry_analyzer.get_stock_concepts(stock_code)
            industry_list = industry_analyzer.get_stock_industry(stock_code)
            
            # 获取同行业股票列表
            industry_stocks = self.get_industry_stocks(stock_code)
            
            # 获取行业平均估值
            industry_valuation = None
            if industry_list:
                valuation_result = industry_analyzer.get_latest_industry_valuation(industry_list[0])
                if valuation_result.get('success'):
                    industry_valuation = {
                        'industry_name': industry_list[0],
                        'avg_pe': valuation_result.get('avg_pe'),
                        'avg_pb': valuation_result.get('avg_pb')
                    }
            
            # 获取动量指标数据
            momentum_result = self.get_momentum_indicators(stock_code, industry_stocks)
            
            # 获取基础财务指标
            base_result = self.extract_financial_indicators(stock_code, current_year)
            if not base_result.get('success'):
                return base_result
                
            # 获取增长指标变化
            growth_result = self.get_growth_change_indicators(stock_code, current_year, previous_year)
            if not growth_result.get('success'):
                return growth_result
                
            # 获取行业排名
            rank_result = self.calculate_industry_rankings(stock_code, current_year, previous_year)
            if not rank_result.get('success'):
                return rank_result
            
            # 获取指定年份的数据
            record = self.mongo_collection.find_one({'code': stock_code})
            if not record or 'wind_data' not in record:
                return {
                    'success': False,
                    'message': f'未找到股票 {stock_code} 的财务数据'
                }
            
            # 获取指定年份的数据
            wind_data_current = None
            wind_data_previous = None
            
            for data in record['wind_data']:
                if data['time'] == current_year:
                    wind_data_current = data
                elif data['time'] == previous_year:
                    wind_data_previous = data
            
            if not wind_data_current or not wind_data_previous:
                return {
                    'success': False,
                    'message': f'未找到股票 {stock_code} 的{current_year}或{previous_year}年财务数据'
                }
            
            # 定义指标说明映射（前端显示用）
            indicator_descriptions = {
                # 财务实力指标
                'debt_equity_ratio': '债务股本比率',
                'debt_ebitda_ratio': '债务／税息折旧及摊销前利润',
                'interest_coverage_ratio': '利息保障倍数',
                'cash_to_debt_ratio': '现金负债率',
                'equity_ratio': '股东权益比',
                'wacc': '加权平均资本成本',
                'roic': '资本回报率',
                
                # 盈利能力指标
                'profit_years': '过去五年盈利年数',
                'gross_profit_margin': '毛利率',
                'operating_profit_margin': '营业利润率',
                'net_profit_margin': '净利率',
                'roe': '股本回报率ROE',
                'roa': '资产收益率ROA',
                
                # 成长能力指标
                'diluted_eps_growth': '稀释每股收益增长率',
                'operating_cash_flow_per_share_growth': '每股经营活动产生的现金流量净额增长率',
                'revenue_growth': '营业收入增长率',
                'operating_profit_growth': '营业利润增长率',
                'net_profit_growth_excl_nonrecurring': '扣非净利润增长率',
                'operating_cash_flow_growth': '经营活动产生的现金流量净额增长率',
                'rd_expense_growth': '研发费用增长率',
                'roe_growth': '净资产收益率增长率',
                
                # 价值评级指标
                'z_score': 'Z值',
                'working_capital_to_assets': '营运资本/总资产',
                'retained_earnings_to_assets': '留存收益/总资产',
                'ebit_to_assets': '息税前利润/总资产',
                'market_value_to_liabilities': '总市值/负债总计',
                'equity_to_liabilities': '股东权益/负债总计',
                'revenue_to_assets': '营业收入/总资产',
                
                # 流动性指标
                'inventory_turnover_days': '存货周转天数',
                'receivables_turnover_days': '应收账款周转天数',
                'payables_turnover_days': '应付账款周转天数',
                'current_ratio': '流动比率',
                'quick_ratio': '速动比率',
                'cash_ratio': '现金比率'
            }
            
            # 定义MongoDB字段映射（用于从MongoDB获取数据）
            mongo_field_mapping = {
                # 偿债能力指标
                'debt_equity_ratio': ('solvency', '产权比率'),
                'debt_ebitda_ratio': ('solvency', '全部债务/EBITDA'),
                'interest_coverage_ratio': ('solvency', '已获利息倍数(EBIT/利息费用)'),
                'current_ratio': ('solvency', '流动比率'),
                'quick_ratio': ('solvency', '速动比率'),
                'cash_ratio': ('solvency', '现金比率'),
                'cash_to_debt_ratio': ('solvency', '经营活动产生的现金流量净额/负债合计'),
                
                # 资本结构指标
                'equity_ratio': ('capitalStructure', '股东权益比'),
                
                # 成长能力指标
                'diluted_eps_growth': ('grows', '稀释每股收益(同比增长率)'),
                'operating_cash_flow_per_share_growth': ('grows', '每股经营活动产生的现金流量净额(同比增长率)'),
                'revenue_growth': ('grows', '营业收入(同比增长率)'),
                'operating_profit_growth': ('grows', '营业利润(同比增长率)'),
                'net_profit_growth_excl_nonrecurring': ('grows', '归属母公司股东的净利润-扣除非经常性损益(同比增长率)'),
                'operating_cash_flow_growth': ('grows', '经营活动产生的现金流量净额(同比增长率)'),
                'rd_expense_growth': ('grows', '研发费用同比增长'),
                'roe_growth': ('grows', '净资产收益率(摊薄)(同比增长)'),
                
                # Z值相关指标
                'working_capital_to_assets': ('ZValue', '营运资本/总资产'),
                'retained_earnings_to_assets': ('ZValue', '留存收益/总资产'),
                'ebit_to_assets': ('ZValue', '息税前利润(TTM)/总资产'),
                'market_value_to_liabilities': ('ZValue', '当日总市值/负债总计'),
                'equity_to_liabilities': ('ZValue', '股东权益合计(含少数)/负债总计'),
                'revenue_to_assets': ('ZValue', '营业收入/总资产'),
                'z_score': ('ZValue', 'Z值'),
                
                # 营运能力指标
                'inventory_turnover_days': ('operatingCapacity', '存货周转天数'),
                'receivables_turnover_days': ('operatingCapacity', '应收账款周转天数'),
                'payables_turnover_days': ('operatingCapacity', '应付账款周转天数'),
                
                # 盈利能力指标
                'gross_profit_margin': ('profitability', '销售毛利率'),
                'operating_profit_margin': ('profitability', '营业利润/营业总收入'),
                'net_profit_margin': ('profitability', '销售净利率'),
                'roe': ('profitability', '净资产收益率ROE(平均)'),
                'roa': ('profitability', '总资产净利率ROA'),
                'roic': ('profitability', '投入资本回报率ROIC')
            }
            
            # 定义指标是否越高越好
            higher_better_indicators = {
                # 偿债能力指标
                'current_ratio': True,
                'quick_ratio': True,
                'cash_ratio': True,
                'interest_coverage_ratio': True,
                
                # 资本结构指标
                'equity_ratio': True,
                
                # 盈利能力指标
                'profit_years': True,
                
                # 成长能力指标
                'diluted_eps_growth': True,
                'operating_cash_flow_per_share_growth': True,
                'revenue_growth': True,
                'operating_profit_growth': True,
                'net_profit_growth_excl_nonrecurring': True,
                'operating_cash_flow_growth': True,
                'rd_expense_growth': True,
                'roe_growth': True,
                
                # Z值相关指标
                'working_capital_to_assets': True,
                'retained_earnings_to_assets': True,
                'ebit_to_assets': True,
                'market_value_to_liabilities': True,
                'equity_to_liabilities': True,
                'revenue_to_assets': True,
                'z_score': True,
                
                # 营运能力指标
                'inventory_turnover_days': False,
                'receivables_turnover_days': False,
                'payables_turnover_days': False,
                
                # 盈利能力指标
                'gross_profit_margin': True,
                'operating_profit_margin': True,
                'net_profit_margin': True,
                'roe': True,
                'roa': True,
                'roic': True,
                
                # WACC数据
                'wacc': False
            }
            
            # 构建基础指标数据
            base_indicators = base_result.get('indicators', {})
            rankings = rank_result.get('rankings', {})
            
            def get_indicator_value(data: Dict, key: str) -> Optional[float]:
                """获取指定指标的值"""
                if key not in mongo_field_mapping:
                    return None
                    
                category, meaning = mongo_field_mapping[key]
                if category not in data:
                    return None
                    
                for item in data[category]['list']:
                    if item['meaning'] == meaning:
                        return item['data']
                return None
            
            def calculate_change(current: Optional[float], previous: Optional[float], is_higher_better: bool) -> Dict:
                """计算指标变化情况"""
                if current is None or previous is None:
                    return {
                        'change': None,
                        'change_rate': None,
                        'is_better': None
                    }
                
                change = current - previous
                change_rate = (change / abs(previous)) * 100 if previous != 0 else None
                
                # 判断是否变好
                if is_higher_better:
                    is_better = change > 0
                else:
                    is_better = change < 0
                
                return {
                    'change': round(change, 3),
                    'change_rate': round(change_rate, 2) if change_rate is not None else None,
                    'is_better': is_better
                }
            
            # 定义各板块的指标列表
            financial_strength_indicators = [
                'debt_equity_ratio', 'debt_ebitda_ratio', 'interest_coverage_ratio',
                'cash_to_debt_ratio', 'equity_ratio', 'wacc', 'roic'
            ]
            
            profitability_indicators = [
                'gross_profit_margin', 'operating_profit_margin',
                'net_profit_margin', 'roe', 'roa', 'roic', 'profit_years'
            ]
            
            growth_indicators = [
                'diluted_eps_growth', 'operating_cash_flow_per_share_growth',
                'revenue_growth', 'operating_profit_growth',
                'net_profit_growth_excl_nonrecurring', 'operating_cash_flow_growth',
                'rd_expense_growth', 'roe_growth'
            ]
            
            value_rating_indicators = [
                'z_score', 'working_capital_to_assets', 'retained_earnings_to_assets',
                'ebit_to_assets', 'market_value_to_liabilities',
                'equity_to_liabilities', 'revenue_to_assets'
            ]
            
            liquidity_indicators = [
                'inventory_turnover_days', 'receivables_turnover_days',
                'payables_turnover_days', 'current_ratio', 'quick_ratio', 'cash_ratio'
            ]
            
            def process_indicators(indicator_list):
                result = []
                total_score = 0
                valid_scores = 0
                
                for key in indicator_list:
                    if key in base_indicators:
                        current_value = base_indicators[key]
                        previous_value = get_indicator_value(wind_data_previous, key)
                        
                        # 计算变化情况
                        is_higher_better = higher_better_indicators.get(key, True)
                        change_info = calculate_change(current_value, previous_value, is_higher_better)
                        
                        rank_score = rankings.get(key, 0)
                        if rank_score is not None:
                            total_score += rank_score
                            valid_scores += 1
                            
                        result.append({
                            'key': key,
                            'desc': indicator_descriptions.get(key, key),
                            'value': current_value,
                            'previous_value': previous_value,
                            'change': change_info['change'],
                            'change_rate': change_info['change_rate'],
                            'is_better': change_info['is_better'],
                            'rank_score': rank_score
                        })
                
                # 计算平均得分
                avg_score = round(total_score / valid_scores, 2) if valid_scores > 0 else 0
                
                return {
                    'indicators': result,
                    'avg_score': avg_score
                }
            
            # 处理各板块指标
            financial_strength = process_indicators(financial_strength_indicators)
            profitability = process_indicators(profitability_indicators)
            value_rating = process_indicators(value_rating_indicators)
            liquidity = process_indicators(liquidity_indicators)
            
            # 处理增长指标变化
            growth_changes = growth_result.get('data', {}).get('growth_changes', {})
            growth_changes_list = []
            total_growth_score = 0
            valid_growth_scores = 0
            
            for key in growth_indicators:
                if key in growth_changes:
                    value = growth_changes[key]
                    if isinstance(value, (int, float)) and value is not None:
                        value = round(value, 3)
                    
                    rank_score = rankings.get(f'{key}_change', 0)
                    if rank_score is not None:
                        total_growth_score += rank_score
                        valid_growth_scores += 1
                        
                    growth_changes_list.append({
                        'key': f'{key}_change',
                        'desc': f'{indicator_descriptions.get(key, key)}增长变化率',
                        'value': value,
                        'rank_score': rank_score
                    })
            
            # 计算增长指标的平均得分
            growth_avg_score = round(total_growth_score / valid_growth_scores, 2) if valid_growth_scores > 0 else 0
            
            # 构建增长指标数据
            growth_data = {
                'indicators': growth_changes_list,
                'avg_score': growth_avg_score
            }
            

            concepts += industry_list

            # 在返回结果之前，缓存数据
            result = {
                'success': True,
                'stock_code': stock_code,
                'data_time': base_indicators.get('data_time'),
                'financial_strength': financial_strength,
                'profitability': profitability,
                'growth': growth_data,
                'value_rating': value_rating,
                'liquidity': liquidity,
                'momentum': momentum_result.get('indicators', []),
                'concepts': concepts,
                'price_data': price_data,
                'industry_valuation': industry_valuation
            }
            
            # 缓存结果，有效期1天（86400秒）
            try:
                kkk = redis_client.set(
                    cache_key,
                    json.dumps(result, default=str),  # 使用default=str处理日期等特殊类型
                    ex=86400  # 1天的秒数
                )
                print(kkk)
                logger.info(f"已缓存股票 {stock_code} 的财务分析数据，有效期为1天")
            except Exception as cache_error:
                logger.warning(f"缓存财务分析数据失败: {cache_error}")
            
            return result
            
        except Exception as e:
            logger.error(f"分析财务数据失败: {str(e)}")
            return {
                'success': False,
                'message': f'分析财务数据失败: {str(e)}'
            }
    
    def __del__(self):
        """析构函数，关闭数据库连接"""
        if hasattr(self, 'mongo_client'):
            self.mongo_client.close()
        logger.info("数据库连接已关闭")

    def _convert_stock_code_format(self, stock_code: str) -> str:
        """
        转换股票代码格式
        
        Args:
            stock_code: 原始股票代码，格式如 "603507.SH"
            
        Returns:
            转换后的股票代码，格式如 "SH603507"
        """
        try:
            code, market = stock_code.split('.')
            return f"{market}{code}"
        except Exception as e:
            logger.error(f"转换股票代码格式失败: {str(e)}")
            return stock_code

    def get_industry_stocks(self, stock_code: str) -> List:
        """
        获取同行业股票列表
        
        Args:
            stock_code: 股票代码，格式如 "603507.SH"
            
        Returns:
            包含同行业股票列表的字典
        """
        try:
            # 转换股票代码格式
            formatted_code = self._convert_stock_code_format(stock_code)
            
            # 查询行业板块
            query = """
                SELECT bk_name 
                FROM gp_hybk 
                WHERE gp_code = %s
            """
            result = pd.read_sql(query, self.mysql_engine, params=(formatted_code,))
            
            if result.empty:
                return {
                    'success': False,
                    'message': f'未找到股票 {stock_code} 的行业信息'
                }
            
            bk_name = result.iloc[0]['bk_name']
            
            # 查询同行业股票
            query = """
                SELECT gp_code 
                FROM gp_hybk 
                WHERE bk_name = %s
            """
            stocks = pd.read_sql(query, self.mysql_engine, params=(bk_name,))
            
            # 转换回原始格式
            stock_list = []
            for code in stocks['gp_code']:
                if code.startswith('SH'):
                    stock_list.append(f"{code[2:]}.SH")
                elif code.startswith('SZ'):
                    stock_list.append(f"{code[2:]}.SZ")
            
            return stock_list
            
        except Exception as e:
            logger.error(f"获取同行业股票列表失败: {str(e)}")
            return []

    def _calculate_industry_rank_score(self, value: float, values: List[float], is_higher_better: bool = True) -> float:
        """
        计算行业排名得分
        
        Args:
            value: 当前值
            values: 行业所有值列表
            is_higher_better: 是否越高越好，默认为True
            
        Returns:
            0-10的得分，10表示第一名，0表示最后一名
        """
        try:
            if value is None or not values:
                return 0
                
            # 过滤掉None值
            valid_values = [v for v in values if v is not None]
            if not valid_values:
                return 0
                
            # 计算排名
            if is_higher_better:
                rank = sum(1 for x in valid_values if x > value) + 1
            else:
                rank = sum(1 for x in valid_values if x < value) + 1
                
            # 计算得分 (10 * (1 - (rank - 1) / (n - 1)))
            n = len(valid_values)
            if n == 1:
                return 10
            score = 10 * (1 - (rank - 1) / (n - 1))
            return round(score, 2)
            
        except Exception as e:
            logger.error(f"计算行业排名得分失败: {str(e)}")
            return 0

    def _get_industry_indicators(self, stock_list: List[str]) -> Dict[str, List[float]]:
        """
        获取行业所有公司的指标值
        
        Args:
            stock_list: 股票代码列表
            
        Returns:
            包含所有指标值的字典，key为指标名，value为该指标的所有公司值列表
        """
        try:
            industry_indicators = {}
            
            # 遍历所有股票获取指标
            for stock_code in stock_list:
                result = self.extract_financial_indicators(stock_code)
                if not result.get('success'):
                    continue
                    
                indicators = result.get('indicators', {})
                for key, value in indicators.items():
                    if key != 'data_time':
                        if key not in industry_indicators:
                            industry_indicators[key] = []
                        industry_indicators[key].append(value)
                        
            return industry_indicators
            
        except Exception as e:
            logger.error(f"获取行业指标失败: {str(e)}")
            return {}

    def calculate_industry_rankings(self, stock_code: str, current_year: str = '2024-12-31', previous_year: str = '2023-12-31') -> Dict:
        """
        计算公司在行业中的排名得分
        
        Args:
            stock_code: 股票代码
            current_year: 当前年份，格式为'YYYY-12-31'，默认为'2024-12-31'
            previous_year: 上一年份，格式为'YYYY-12-31'，默认为'2023-12-31'
        Returns:
            包含所有指标排名得分的字典
        """
        try:
            # 获取同行业股票列表
            stock_list = self.get_industry_stocks(stock_code)
            if not stock_list:
                return {
                    'success': False,
                    'message': f'未找到股票 {stock_code} 的同行业公司'
                }
                
            # 获取当前公司的指标
            current_result = self.extract_financial_indicators(stock_code, year=current_year)
            if not current_result.get('success'):
                return current_result
                
            # 获取当前公司的增长指标变化
            current_growth_result = self.get_growth_change_indicators(stock_code, current_year, previous_year)
            if not current_growth_result.get('success'):
                return current_growth_result
                
            # 获取行业所有公司的指标
            industry_indicators = {}
            for stock in stock_list:
                result = self.extract_financial_indicators(stock, year=current_year)
                if not result.get('success'):
                    continue
                indicators = result.get('indicators', {})
                for key, value in indicators.items():
                    if key != 'data_time':
                        if key not in industry_indicators:
                            industry_indicators[key] = []
                        industry_indicators[key].append(value)
            
            # 获取行业所有公司的增长指标变化
            industry_growth_indicators = {}
            for stock in stock_list:
                growth_result = self.get_growth_change_indicators(stock, current_year, previous_year)
                if growth_result.get('success'):
                    growth_changes = growth_result.get('data', {}).get('growth_changes', {})
                    for key, value in growth_changes.items():
                        if key not in industry_growth_indicators:
                            industry_growth_indicators[key] = []
                        industry_growth_indicators[key].append(value)
            
            # 定义指标是否越高越好
            higher_better_indicators = {
                # 偿债能力指标
                'current_ratio': True,
                'quick_ratio': True,
                'cash_ratio': True,
                'interest_coverage_ratio': True,
                
                # 资本结构指标
                'equity_ratio': True,
                
                # 盈利能力指标
                'profit_years': True,  # 盈利年数越高越好
                
                # 成长能力指标
                'diluted_eps_growth': True,
                'operating_cash_flow_per_share_growth': True,
                'revenue_growth': True,
                'operating_profit_growth': True,
                'net_profit_growth_excl_nonrecurring': True,
                'operating_cash_flow_growth': True,
                'rd_expense_growth': True,
                'roe_growth': True,
                
                # Z值相关指标
                'working_capital_to_assets': True,
                'retained_earnings_to_assets': True,
                'ebit_to_assets': True,
                'market_value_to_liabilities': True,
                'equity_to_liabilities': True,
                'revenue_to_assets': True,
                'z_score': True,
                
                # 营运能力指标
                'inventory_turnover_days': False,
                'receivables_turnover_days': False,
                'payables_turnover_days': False,
                
                # 盈利能力指标
                'gross_profit_margin': True,
                'operating_profit_margin': True,
                'net_profit_margin': True,
                'roe': True,
                'roa': True,
                'roic': True,
                
                # WACC数据
                'wacc': False
            }
            
            # 计算每个指标的排名得分
            current_indicators = current_result.get('indicators', {})
            rankings = {}
            
            # 计算基础指标的排名
            for key, value in current_indicators.items():
                if key != 'data_time' and key in industry_indicators:
                    is_higher_better = higher_better_indicators.get(key, True)
                    score = self._calculate_industry_rank_score(
                        value,
                        industry_indicators[key],
                        is_higher_better
                    )
                    rankings[key] = score
            
            # 计算增长指标的排名
            current_growth_changes = current_growth_result.get('data', {}).get('growth_changes', {})
            for key, value in current_growth_changes.items():
                if key in industry_growth_indicators:
                    is_higher_better = higher_better_indicators.get(key, True)
                    score = self._calculate_industry_rank_score(
                        value,
                        industry_growth_indicators[key],
                        is_higher_better
                    )
                    rankings[f'{key}_change'] = score
            
            return {
                'success': True,
                'stock_code': stock_code,
                'data_time': current_indicators.get('data_time'),
                'rankings': rankings
            }
            
        except Exception as e:
            logger.error(f"计算行业排名失败: {str(e)}")
            return {
                'success': False,
                'message': f'计算行业排名失败: {str(e)}'
            }

    def get_pep_stock_info_by_shortname(self, short_name: str) -> dict:
        """
        根据股票简称从pep_stock_info集合获取对应的全部字段数据
        Args:
            short_name: 股票简称
        Returns:
            查询到的文档（dict），未找到则返回{'success': False, 'message': ...}
        """
        try:
            pep_collection = self.mongo_client[MONGO_CONFIG2['db']]['pep_stock_info']
            record = pep_collection.find_one({'shortName': short_name})
            if not record:
                return {'success': False, 'message': f'未找到简称为{short_name}的股票数据'}
            if '_id' in record:
                record['_id'] = str(record['_id'])  # 转为字符串，便于JSON序列化
            record['success'] = True
            return record
        except Exception as e:
            logger.error(f"查询pep_stock_info失败: {str(e)}")
            return {'success': False, 'message': f'查询pep_stock_info失败: {str(e)}'}

    def get_pep_stock_info_by_code(self, short_code: str) -> dict:
        """
        根据股票简称从pep_stock_info集合获取对 应的全部字段数据
        Args:
            short_code: 股票简称
        Returns:
            查询到的文档（dict），未找到则返回{'success': False, 'message': ...}
        """
        try:
            pep_collection = self.mongo_client2[MONGO_CONFIG2['db']]['pep_stock_info']
            record = pep_collection.find_one({'code': short_code})
            if not record:
                return {'success': False, 'message': f'未找到简称为{short_code}的股票数据'}
            if '_id' in record:
                record['_id'] = str(record['_id'])  # 转为字符串，便于JSON序列化
            record['success'] = True
            return record
        except Exception as e:
            logger.error(f"查询pep_stock_info失败: {str(e)}")
            return {'success': False, 'message': f'查询pep_stock_info失败: {str(e)}'}