guzhi/app/utils/industry_calculator.py
邹方成 2b4b9a2e9c feat: 添加前端项目基础架构和功能模块
refactor: 优化市场价值计算逻辑和行业均值计算

fix: 修复环境变量和配置文件问题

chore: 更新Docker镜像版本至v1.4

docs: 更新需求文档和部署说明

style: 调整代码格式和样式

build: 配置Vite构建工具和依赖管理

test: 添加前端组件测试基础

ci: 设置CI/CD脚本和工作流

perf: 优化前端性能和数据加载
2025-11-06 16:15:26 +08:00

95 lines
2.7 KiB
Python
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

"""
行业数据查询和计算工具
"""
import logging
from typing import Optional, Dict, Any
from app.models.industry import Industry
from app.models.index import Index
logger = logging.getLogger(__name__)
async def get_industry_data_by_name(industry_name: str) -> Optional[Dict[str, Any]]:
"""
根据行业名称查询行业数据
Args:
industry_name: 行业名称
Returns:
包含行业数据的字典如果未找到则返回None
{
'code': '行业代码',
'name': '行业名称',
'roe': 平均ROE,
'fix_num': 行业修正系数,
'remark': '备注'
}
"""
try:
industry = await Industry.filter(name=industry_name).first()
if industry:
return {
'code': industry.code,
'name': industry.name,
'roe': industry.roe,
'fix_num': industry.fix_num,
'remark': industry.remark
}
else:
logger.warning(f"未找到行业数据: {industry_name}")
return None
except Exception as e:
logger.error(f"查询行业数据失败: {industry_name}, 错误: {str(e)}")
return None
async def calculate_industry_average_s2(industry_name: str) -> float:
# todo : 使用index 搜索的数据
"""
计算行业均值S2
Args:
industry_name: 行业名称
Returns:
行业均值S2如果查询失败则返回0.0
"""
try:
index_data = await Index.filter(name=industry_name).first()
if index_data:
# S2 = ROE * 修正系数
logger.info(f"行业 {industry_name} S2计算: S2={index_data.search_num}")
return index_data.search_num
else:
logger.warning(f"未找到行业 {industry_name} 的数据返回默认值0.01")
return 0.01 # 返回小的正数而不是0.0
except Exception as e:
logger.error(f"计算行业均值S2失败: {industry_name}, 错误: {str(e)}")
return 0.01 # 返回小的正数而不是0.0
async def get_all_industries() -> list:
"""
获取所有行业列表
Returns:
行业列表
"""
try:
industries = await Industry.all()
return [
{
'code': industry.code,
'name': industry.name,
'roe': industry.roe,
'fix_num': industry.fix_num,
'remark': industry.remark
}
for industry in industries
]
except Exception as e:
logger.error(f"获取行业列表失败: {str(e)}")
return []