feat(backend): update dashboard router, seed demo data, and investor agents page

This commit is contained in:
selfrelease
2026-07-21 18:05:49 +08:00
parent af91d843d8
commit 3bae5fbfc1
3 changed files with 728 additions and 60 deletions
+35 -13
View File
@@ -80,17 +80,28 @@ async def get_dashboard_summary(
pending_result = await db.execute(pending_query)
pending_reports = pending_result.scalar_one()
# 最近评分(最多 10 条
# 最近评分 — 每家企业只取最新一条(避免历史数据导致企业重复
subq = (
select(
HealthScore.company_id,
func.max(HealthScore.calculated_at).label("max_at"),
)
.join(Company, HealthScore.company_id == Company.id)
.where(Company.tenant_id == tenant_id)
.group_by(HealthScore.company_id)
)
if company_id:
subq = subq.where(HealthScore.company_id == company_id)
subq = subq.subquery()
recent_query = (
select(HealthScore, Company.name.label("company_name"))
.join(Company, HealthScore.company_id == Company.id)
.where(Company.tenant_id == tenant_id)
)
if company_id:
recent_query = recent_query.where(HealthScore.company_id == company_id)
recent_result = await db.execute(
recent_query.order_by(HealthScore.calculated_at.desc()).limit(10)
.join(subq, (HealthScore.company_id == subq.c.company_id) & (HealthScore.calculated_at == subq.c.max_at))
.order_by(HealthScore.calculated_at.desc())
.limit(10)
)
recent_result = await db.execute(recent_query)
recent_rows = recent_result.all()
recent_scores = []
for row in recent_rows:
@@ -118,16 +129,27 @@ async def list_health_scores(
db: AsyncSession = Depends(get_db),
user: User = Depends(get_current_user),
):
"""获取健康度评分列表。"""
"""获取健康度评分列表 — 每家企业只返回最新一条"""
subq = (
select(
HealthScore.company_id,
func.max(HealthScore.calculated_at).label("max_at"),
)
.join(Company, HealthScore.company_id == Company.id)
.where(Company.tenant_id == user.tenant_id)
.group_by(HealthScore.company_id)
)
if company_id:
subq = subq.where(HealthScore.company_id == company_id)
subq = subq.subquery()
query = (
select(HealthScore, Company.name.label("company_name"))
.join(Company, HealthScore.company_id == Company.id)
.where(Company.tenant_id == user.tenant_id)
.join(subq, (HealthScore.company_id == subq.c.company_id) & (HealthScore.calculated_at == subq.c.max_at))
.order_by(HealthScore.calculated_at.desc())
.limit(limit)
)
if company_id:
query = query.where(HealthScore.company_id == company_id)
query = query.order_by(HealthScore.calculated_at.desc()).limit(limit)
result = await db.execute(query)
scores = []
for row in result.all():