2026-03-11 16:28:04 +08:00
|
|
|
from uuid import UUID
|
|
|
|
|
from decimal import Decimal
|
|
|
|
|
|
|
|
|
|
from sqlalchemy import select, func
|
|
|
|
|
from sqlalchemy.ext.asyncio import AsyncSession
|
|
|
|
|
|
|
|
|
|
from app.models.case import Case
|
|
|
|
|
from app.models.assessment import FraudAssessment, ReviewStatus
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
async def recalculate_case_total(case_id: UUID, db: AsyncSession) -> float:
|
2026-03-17 22:39:05 +08:00
|
|
|
"""Recalculate and persist the recognized fraud amount for a case.
|
|
|
|
|
|
|
|
|
|
The case-level amount shown in case list/workspace represents the current
|
|
|
|
|
recognized amount after analysis, so it should include pending/confirmed/
|
|
|
|
|
needs_info items and only exclude explicitly rejected records.
|
|
|
|
|
"""
|
2026-03-11 16:28:04 +08:00
|
|
|
result = await db.execute(
|
|
|
|
|
select(func.coalesce(func.sum(FraudAssessment.assessed_amount), 0))
|
|
|
|
|
.where(FraudAssessment.case_id == case_id)
|
2026-03-17 22:39:05 +08:00
|
|
|
.where(FraudAssessment.assessed_amount > 0)
|
|
|
|
|
.where(FraudAssessment.review_status != ReviewStatus.rejected)
|
2026-03-11 16:28:04 +08:00
|
|
|
)
|
|
|
|
|
total = float(result.scalar() or 0)
|
|
|
|
|
case = await db.get(Case, case_id)
|
|
|
|
|
if case:
|
|
|
|
|
case.total_amount = total
|
|
|
|
|
await db.flush()
|
|
|
|
|
return total
|