diff --git a/video-gen-api/app/api/v1/admin.py b/video-gen-api/app/api/v1/admin.py index 113b9c79..9ee06f4e 100644 --- a/video-gen-api/app/api/v1/admin.py +++ b/video-gen-api/app/api/v1/admin.py @@ -1859,10 +1859,10 @@ async def get_stats( # ── 每日各模块积分消耗(按日期+模块分组) from sqlalchemy import Date, cast as sa_cast _day_expr = sa_cast(CreditRecord.created_at, Date) - daily_credits_rows = (await db.execute( + _inner = ( select( _day_expr.label('date'), - func.coalesce(CreditRecord.source_module, 'other').label('module'), + CreditRecord.source_module.label('module'), func.coalesce(func.sum(func.abs(CreditRecord.amount)), 0).label('credits'), ) .where( @@ -1870,8 +1870,15 @@ async def get_stats( CreditRecord.created_at >= date_start, CreditRecord.created_at <= date_end, ) - .group_by(_day_expr, func.coalesce(CreditRecord.source_module, 'other')) - .order_by(_day_expr) + .group_by(_day_expr, CreditRecord.source_module) + .subquery() + ) + daily_credits_rows = (await db.execute( + select( + _inner.c.date, + func.coalesce(_inner.c.module, 'other').label('module'), + _inner.c.credits, + ).order_by(_inner.c.date) )).all() daily_credits_by_module = [ DailyCreditOut(date=str(row.date), module=row.module, credits=float(row.credits or 0))