feat(customers): 发送/接收列改今日口径(对齐云客;本地消息表Asia/Shanghai今日聚合)

- CustomerOut 加 send_today/receive_today(路由赋值,非ORM列)
- /api/customers 每页一次 GROUP BY(owner IN 走复合索引前缀)
- 前端列优先今日值,无消息数据回退原raw字段;列名对齐云客「发送条数/接收条数」
- TDD:今日3发2收+昨日不计入+无消息客户0

Co-Authored-By: Claude <noreply@anthropic.com>
This commit is contained in:
浅念
2026-08-22 14:39:17 +08:00
parent 00ae13b5c2
commit a290e39523
5 changed files with 88 additions and 5 deletions
+38
View File
@@ -35,3 +35,41 @@ async def test_declared_total_sums_contacts_num(make_client):
assert body["wechat"]["account_count"] == 2 # 只计有值的员工
assert body["whatsapp"]["declared_total"] == 15
assert body["whatsapp"]["account_count"] == 2
# ---------------------------------------------------------------------------
# send_today / receive_today — 今日发送/接收条数(对齐云客口径,2026-08-22)
# ---------------------------------------------------------------------------
async def _seed_today_counts(session):
import datetime as dt
from app.db.models import Customer as C, Message as M
now_ms = int(dt.datetime.now(dt.UTC).timestamp() * 1000)
yesterday_ms = now_ms - 86_400_000
session.add_all([
C(channel="wechat", owner_account_id="o1", friend_id="f1"),
C(channel="wechat", owner_account_id="o1", friend_id="f2"),
])
# f1: 今日 3发2收 + 昨日 5发(不应计入)
for i in range(3):
session.add(M(msg_id=f"t1s{i}", channel="wechat", owner_account_id="o1", talker_id="f1", is_send=True, timestamp=now_ms - i * 1000))
for i in range(2):
session.add(M(msg_id=f"t1r{i}", channel="wechat", owner_account_id="o1", talker_id="f1", is_send=False, timestamp=now_ms - i * 1000))
for i in range(5):
session.add(M(msg_id=f"t0s{i}", channel="wechat", owner_account_id="o1", talker_id="f1", is_send=True, timestamp=yesterday_ms))
# f2: 无消息
await session.commit()
async def test_customers_today_send_receive(make_client):
"""发送/接收今日列 = 本地消息表按 Asia/Shanghai 今日聚合。"""
client = await make_client(_seed_today_counts)
r = await client.get("/api/customers", params={"channel": "wechat"})
assert r.status_code == 200
by_fid = {it["friend_id"]: it for it in r.json()["items"]}
assert by_fid["f1"]["send_today"] == 3
assert by_fid["f1"]["receive_today"] == 2
assert by_fid["f2"]["send_today"] == 0
assert by_fid["f2"]["receive_today"] == 0