fad458b2a7
- UIUX 文档:填充 19 个缺口(多主体画像/健康度/AI+看板/增长域/洞察域/创始人端/OODA/助推/商密) - UIUX 文档:插入 6 个新章节(十四~十九),旧章节重编号为二十~三十一,更新目录和交叉引用 - 作业指导书 x5:导航改为 6 域分组,新增 Context Bar/工作模式/Insight Rail/决策线程/多工作区等 UI 概念 - 新建 docs/2-task-uiux.md:50 个代码落地开发任务,按 P0-P6 分优先级 + 8 Sprint 规划 - 后端/前端:大量新增模型、路由、组件(来自之前 Phase 开发)
70 lines
2.3 KiB
Python
70 lines
2.3 KiB
Python
"""财务数据校验 Agent。
|
|
|
|
交叉验证不同来源数据一致性、检测报表内部逻辑矛盾、追踪历史数据修订。
|
|
"""
|
|
|
|
from sqlalchemy import select
|
|
from sqlalchemy.ext.asyncio import AsyncSession
|
|
|
|
from app.models.financial_data import FinancialData
|
|
|
|
|
|
async def validate_financial_data(
|
|
db: AsyncSession,
|
|
company_id: str,
|
|
period_year: int,
|
|
period_month: int,
|
|
) -> dict:
|
|
"""校验财务数据 — 内部一致性、跨期一致性、历史偏差。
|
|
|
|
返回校验结果和可信度评分。
|
|
"""
|
|
result = await db.execute(
|
|
select(FinancialData)
|
|
.where(
|
|
FinancialData.company_id == company_id,
|
|
FinancialData.period_year == period_year,
|
|
FinancialData.period_month == period_month,
|
|
)
|
|
)
|
|
statements = result.scalars().all()
|
|
|
|
if not statements:
|
|
return {"credibility_score": 0.0, "issues": ["无财务数据"], "checks_passed": 0, "checks_total": 0}
|
|
|
|
issues: list[str] = []
|
|
checks_passed = 0
|
|
checks_total = 0
|
|
|
|
# 内部一致性检查:资产 = 负债 + 权益
|
|
for stmt in statements:
|
|
if stmt.statement_type == "balance_sheet" and stmt.data_json:
|
|
checks_total += 1
|
|
assets = stmt.data_json.get("total_assets")
|
|
liabilities = stmt.data_json.get("total_liabilities")
|
|
equity = stmt.data_json.get("total_equity")
|
|
if assets is not None and liabilities is not None and equity is not None:
|
|
if abs(assets - (liabilities + equity)) < max(assets * 0.01, 100):
|
|
checks_passed += 1
|
|
else:
|
|
issues.append(f"资产负债表不平:资产 {assets} ≠ 负债 {liabilities} + 权益 {equity}")
|
|
|
|
# 跨期一致性检查
|
|
checks_total += 1
|
|
income_stmts = [s for s in statements if s.statement_type == "income"]
|
|
if len(income_stmts) >= 1 and income_stmts[0].data_json:
|
|
revenue = income_stmts[0].data_json.get("revenue")
|
|
if revenue is not None and revenue < 0:
|
|
issues.append("收入为负数,数据异常")
|
|
else:
|
|
checks_passed += 1
|
|
|
|
credibility = round(checks_passed / max(checks_total, 1) * 100, 1)
|
|
|
|
return {
|
|
"credibility_score": credibility,
|
|
"issues": issues,
|
|
"checks_passed": checks_passed,
|
|
"checks_total": checks_total,
|
|
}
|