fix: F1-5b MP-3 + MP-5 沙箱业务日小程序适配 (W1)

MP-3 customer-detail coachTasks.lastService 业务日上界裁剪:
- apps/backend/app/services/customer_service.py
  - import as_runtime_today_param 从 late import 提至模块顶部
  - _build_coach_tasks 开头取 ref_date,供两段 SQL 共用
  - 第一条直查 biz.coach_tasks 加 `AND updated_at < (%s::date + INTERVAL '1 day')::timestamptz`
  - 删除原方法内重复 ref_date 调用
- 业务影响:sandbox=2026-04-20 时,customer-detail 的"上次服务"
  时间不再展示 sandbox 业务日之后的助教任务更新(沙箱不读未来)
- 测试:apps/backend/tests/test_customer_detail_mp3_lastservice.py
  本地通过,因 .gitignore:71 不入仓(同 T1 / af02446 处理方式)

MP-5 coach-service-records 接入 getBusinessClock:
- apps/miniprogram/miniprogram/pages/coach-service-records/coach-service-records.ts
  - import getBusinessClock + data 加 clockYear/clockMonth/clockDay 字段
  - onLoad 改 async,await getBusinessClock() 取 business_year/month/date
  - loadData / switchMonth 4 处 new Date() → clockYear/Month/Day
- 业务影响:sandbox=2026-04-20 时,coach-service-records 默认显示
  "2026 年 4 月"业绩(而非 today 月),canGoNext=false 阻止翻到 5 月,
  "前 5 日预估金额"规则按 sandbox business_date 判断

双口径验证(weixin-devtools-mcp + DB 直查):
- MP-3 4a live: lastService 最大 04-19(无未来时间)
- MP-3 4b sandbox=4-20: 5-01 任务 task_id=8348/8347 完全消失
- MP-5 4a live: clockYear/Month/Day=2026/5/5,monthLabel="2026年5月"
- MP-5 4b sandbox=4-20: monthLabel="2026年4月" + 35 笔/¥4,657
   first group=2026-04-20(后端 SQL 上界裁剪生效)

审计:
- docs/audit/changes/2026-05-05__wave1_f1_5b_mp3_lastservice_upper_bound.md
- docs/audit/changes/2026-05-05__wave1_f1_5b_mp5_coach_service_records_clock.md

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
This commit is contained in:
Neo
2026-05-05 18:43:08 +08:00
parent 3c8d72edd4
commit 96dae0c778
4 changed files with 225 additions and 19 deletions

View File

@@ -24,6 +24,7 @@ from fastapi import HTTPException
from decimal import Decimal
from app.services import fdw_queries
from app.services.runtime_context import as_runtime_today_param
from app.services.task_generator import compute_heart_icon
from app.trace.decorators import trace_service
@@ -458,7 +459,12 @@ def _build_coach_tasks(
构建 coachTasks 模块。
CHANGE 2026-03-29 | 性能优化:所有助教信息改为批量查询,消除 N+1
CHANGE 2026-05-05 | F1-5b MP-3: lastService (updated_at) 加 business_date 上界,沙箱不读未来
"""
# 业务日sandbox 取 sandbox_business_datelive 取 CURRENT_DATE。
# 该值同时用于第一条 SQLcoach_tasks 上界)和后续 60 天统计ref_date
ref_date = as_runtime_today_param(site_id, conn=conn)
with conn.cursor() as cur:
cur.execute(
"""
@@ -466,9 +472,10 @@ def _build_coach_tasks(
FROM biz.coach_tasks
WHERE member_id = %s
AND status IN ('active', 'inactive')
AND updated_at < (%s::date + INTERVAL '1 day')::timestamptz
ORDER BY created_at DESC
""",
(customer_id,),
(customer_id, ref_date),
)
rows = cur.fetchall()
@@ -502,8 +509,7 @@ def _build_coach_tasks(
# 批量查询 60 天统计(一次 FDW 查询)
# CHANGE 2026-05-02 | 用 business_date 替代 CURRENT_DATE沙箱不读「未来」
from app.services.runtime_context import as_runtime_today_param
ref_date = as_runtime_today_param(site_id, conn=conn)
# ref_date 已在方法开头取得,此处直接复用
stats_map: dict = {}
try:
with fdw_queries._fdw_context(conn, site_id) as cur: