diff --git a/video-gen-api/app/api/v1/admin.py b/video-gen-api/app/api/v1/admin.py index 9ffe1331..113b9c79 100644 --- a/video-gen-api/app/api/v1/admin.py +++ b/video-gen-api/app/api/v1/admin.py @@ -1856,10 +1856,12 @@ async def get_stats( ) )).scalar() or 0 - # ── 每日各模块积分消耗(按日期+模块分组,子查询排序避免 PG GROUP BY 限制) - _daily_subq = ( + # ── 每日各模块积分消耗(按日期+模块分组) + from sqlalchemy import Date, cast as sa_cast + _day_expr = sa_cast(CreditRecord.created_at, Date) + daily_credits_rows = (await db.execute( select( - func.to_char(CreditRecord.created_at, 'YYYY-MM-DD').label('date'), + _day_expr.label('date'), func.coalesce(CreditRecord.source_module, 'other').label('module'), func.coalesce(func.sum(func.abs(CreditRecord.amount)), 0).label('credits'), ) @@ -1868,15 +1870,11 @@ async def get_stats( CreditRecord.created_at >= date_start, CreditRecord.created_at <= date_end, ) - .group_by(func.to_char(CreditRecord.created_at, 'YYYY-MM-DD'), func.coalesce(CreditRecord.source_module, 'other')) - .subquery() - ) - daily_credits_rows = (await db.execute( - select(_daily_subq.c.date, _daily_subq.c.module, _daily_subq.c.credits) - .order_by(_daily_subq.c.date) + .group_by(_day_expr, func.coalesce(CreditRecord.source_module, 'other')) + .order_by(_day_expr) )).all() daily_credits_by_module = [ - DailyCreditOut(date=row.date, module=row.module, credits=float(row.credits or 0)) + DailyCreditOut(date=str(row.date), module=row.module, credits=float(row.credits or 0)) for row in daily_credits_rows ]