"""创始人专属 API — 经营概览 + 自身健康度 + AI 副驾驶完整版。""" from fastapi import APIRouter, Depends, Query from sqlalchemy import select from sqlalchemy.ext.asyncio import AsyncSession from app.core.database import get_db from app.core.dependencies import get_current_user from app.models.company import Company from app.models.health_score import HealthScore from app.models.report import MonthlyReport from app.models.user import User from app.schemas.common import ApiResponse, success from app.services.founder_copilot import financing_planner, org_diagnostic, investor_comm_prep router = APIRouter(prefix="/founder", tags=["founder"]) @router.get("/overview", response_model=ApiResponse[dict]) async def founder_overview( db: AsyncSession = Depends(get_db), user: User = Depends(get_current_user), ): """创始人经营概览。""" # 获取创始人关联的企业 company_result = await db.execute( select(Company).where(Company.tenant_id == user.tenant_id).limit(1) ) company = company_result.scalar_one_or_none() if not company: return success(data={"message": "暂无关联企业"}) # 获取最新健康度 health_result = await db.execute( select(HealthScore) .where(HealthScore.company_id == company.id) .order_by(HealthScore.calculated_at.desc()) .limit(1) ) health = health_result.scalar_one_or_none() # 获取最新月报 report_result = await db.execute( select(MonthlyReport) .where(MonthlyReport.company_id == company.id) .order_by(MonthlyReport.period_year.desc(), MonthlyReport.period_month.desc()) .limit(1) ) report = report_result.scalar_one_or_none() return success(data={ "company": {"id": str(company.id), "name": company.name, "industry": company.industry, "stage": company.stage}, "health_score": { "total_score": health.total_score if health else None, "trend": health.trend if health else None, } if health else None, "latest_report": { "id": str(report.id), "period": f"{report.period_year}-{report.period_month:02d}", "status": report.status, } if report else None, }) @router.get("/health", response_model=ApiResponse[dict]) async def founder_health( db: AsyncSession = Depends(get_db), user: User = Depends(get_current_user), ): """创始人查看自身健康度。""" company_result = await db.execute( select(Company).where(Company.tenant_id == user.tenant_id).limit(1) ) company = company_result.scalar_one_or_none() if not company: return success(data=None) result = await db.execute( select(HealthScore) .where(HealthScore.company_id == company.id) .order_by(HealthScore.calculated_at.desc()) .limit(1) ) health = result.scalar_one_or_none() if not health: return success(data=None) return success(data={ "total_score": health.total_score, "financial_score": health.financial_score, "operational_score": health.operational_score, "ai_commercial_score": health.ai_commercial_score, "ai_cost_score": health.ai_cost_score, "trend": health.trend, "recommendations": health.recommendations_json, }) @router.post("/financing-plan", response_model=ApiResponse[dict]) async def founder_financing_plan( req: dict, user: User = Depends(get_current_user), ): """AI 融资规划 — 节奏/估值/投资人画像。 Args: req: 包含 company_data 字段,描述企业当前融资情况 Returns: AI 生成的融资规划建议 """ result = await financing_planner(req.get("company_data", "")) return success(data=result) @router.post("/org-diagnostic", response_model=ApiResponse[dict]) async def founder_org_diagnostic( req: dict, user: User = Depends(get_current_user), ): """AI 组织诊断 — 团队结构/关键岗位风险/人才缺口。 Args: req: 包含 team_data 字段,描述团队当前情况 Returns: AI 生成的组织诊断报告 """ result = await org_diagnostic(req.get("team_data", "")) return success(data=result) @router.post("/investor-comm-prep", response_model=ApiResponse[dict]) async def founder_investor_comm_prep( req: dict, user: User = Depends(get_current_user), ): """AI 投资人沟通准备 — 董事会材料/投资人问答。 Args: req: 包含 board_context 字段,描述董事会/投资人会议背景 Returns: AI 生成的投资人沟通准备材料 """ result = await investor_comm_prep(req.get("board_context", "")) return success(data=result)