This commit is contained in:
2026-07-21 10:10:16 +08:00
parent 2a9c2d868c
commit 3c6deed786
+8 -10
View File
@@ -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
]