From 1893c01f545bf5b0914dd9cf5dda875f421492f0 Mon Sep 17 00:00:00 2001 From: OuYingJun1024 <1034284404@qq.com> Date: Fri, 5 Jun 2026 10:45:21 +0800 Subject: [PATCH] =?UTF-8?q?feat(welfare):=20savings/battle=20=E8=BF=94?= =?UTF-8?q?=E5=9B=9E=20compare=5Fcount(=E7=B4=AF=E8=AE=A1=E5=AE=8C?= =?UTF-8?q?=E6=88=90=E6=AF=94=E4=BB=B7=E6=AC=A1=E6=95=B0)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - SavingsBattle 仓储/schema/接口新增 compare_count(= 有效记录数,与 order_count 同源) - 更新 docs/api/savings-battle.md 与 tests/test_welfare.py 断言 Co-Authored-By: Claude Opus 4.8 --- app/api/v1/savings.py | 1 + app/repositories/savings.py | 6 +++++- app/schemas/welfare.py | 1 + docs/api/savings-battle.md | 3 ++- tests/test_welfare.py | 2 ++ 5 files changed, 11 insertions(+), 2 deletions(-) diff --git a/app/api/v1/savings.py b/app/api/v1/savings.py index 50e756d..bbbfa18 100644 --- a/app/api/v1/savings.py +++ b/app/api/v1/savings.py @@ -42,6 +42,7 @@ def battle(user: CurrentUser, db: DbSession) -> SavingsBattleOut: week_saved_cents=b.week_saved_cents, beat_percent=b.beat_percent, streak_days=b.streak_days, + compare_count=b.compare_count, ) diff --git a/app/repositories/savings.py b/app/repositories/savings.py index b1901a8..dbbbfe5 100644 --- a/app/repositories/savings.py +++ b/app/repositories/savings.py @@ -52,6 +52,7 @@ class SavingsBattle: week_saved_cents: int # 本周(周一起)已省 beat_percent: int # 超过百分之多少用户 streak_days: int # 连续省钱天数 + compare_count: int # 累计完成比价次数(= 有效记录数:真实用 compare 记录、否则 demo 兜底) def _local_date(dt: datetime): @@ -163,7 +164,10 @@ def get_battle(db: Session, user_id: int) -> SavingsBattle: beat_percent = _compute_beat_percent(db, user_id) return SavingsBattle( - week_saved_cents=week_saved, beat_percent=beat_percent, streak_days=streak + week_saved_cents=week_saved, + beat_percent=beat_percent, + streak_days=streak, + compare_count=len(records), ) diff --git a/app/schemas/welfare.py b/app/schemas/welfare.py index c7630fc..0b3fc9f 100644 --- a/app/schemas/welfare.py +++ b/app/schemas/welfare.py @@ -194,6 +194,7 @@ class SavingsBattleOut(BaseModel): week_saved_cents: int = Field(..., description="本周已省(分)") beat_percent: int = Field(..., description="超过百分之多少用户") streak_days: int = Field(..., description="连续省钱天数") + compare_count: int = Field(..., description="累计完成比价次数(= 比价上报记录数)") class SavingsRecordOut(BaseModel): diff --git a/docs/api/savings-battle.md b/docs/api/savings-battle.md index 7bd982e..91940ec 100644 --- a/docs/api/savings-battle.md +++ b/docs/api/savings-battle.md @@ -13,6 +13,7 @@ | `week_saved_cents` | int | 本周已省(分) | | `beat_percent` | int | 超过百分之多少用户 | | `streak_days` | int | 连续省钱天数 | +| `compare_count` | int | 累计完成比价次数(= 比价上报记录数;有真实 `compare` 记录用真实数,否则 demo 兜底) | ## 说明 -「我的」页「省钱战绩」卡数据源。接口已通但无真实业务写入,实际多为 0。 +「我的」页「省钱战绩」卡数据源(三列:本周已省 / 完成比价 / 连续比价)。`compare_count` 与 `/summary` 的 `order_count` 同源(均 = 有效记录数),对应原型「完成比价(次)」列。接口已通但无真实业务写入时走 demo 兜底(seed 约 23 条)。 diff --git a/tests/test_welfare.py b/tests/test_welfare.py index 4cf38ca..1803815 100644 --- a/tests/test_welfare.py +++ b/tests/test_welfare.py @@ -226,6 +226,8 @@ def test_savings_summary_and_battle(client) -> None: assert b["streak_days"] >= 1 assert b["week_saved_cents"] >= 0 assert 0 <= b["beat_percent"] <= 100 + # 完成比价次数与 summary 的 order_count 同源(均 = 有效记录数) + assert b["compare_count"] == s["order_count"] def test_savings_seeder_idempotent(client) -> None: