fix(names): 会话/统计弹窗/zip 文件名的共享好友昵称读 link 快照

用户报 Lily 聊天窗口「聊天好友」列表里 A**郑 显示成 wxid_ir6uxy2li8no22
——A**郑是 14 个员工共享的客户,档案行(customer 全局单行 last-wins)挂在
别的员工名下,按 owner 查档案查空,三个显示位回退 wxid:

- sessions._meta(会话列表名字/头像/delete_flag)
- staff_chat_stats._customer_names(五数字弹窗明细)
- chat-html-zip 文件名

统一改为优先 customer_account_link per-owner 快照(link 未覆盖回退档案,
空表过渡安全)。staff_stats(统计分析新客户等)仍是档案口径,属分析语义
另行评估。613 测试绿(新增会话共享好友昵称 2 例)。

Co-Authored-By: Claude Code <noreply@anthropic.com>
This commit is contained in:
浅念
2026-09-09 11:44:58 +08:00
parent b8c2bef6df
commit 2b516362cd
4 changed files with 131 additions and 29 deletions
+28 -10
View File
@@ -19,7 +19,7 @@ from fastapi import APIRouter, Depends, HTTPException, Query, Response
from sqlalchemy.ext.asyncio import AsyncSession
from app.api.deps import get_session
from app.db.models import Customer, Message
from app.db.models import Customer, CustomerAccountLink, Message
router = APIRouter(prefix="/api/export", tags=["export"])
@@ -209,22 +209,40 @@ async def export_chat_html_zip(
status_code=422, detail=f"一次最多导出 {ZIP_MAX_CONVS} 个会话"
)
# 文件名显示名:批量取一次 Customer(备注 > 昵称 > friend_id)
cust_rows = (await session.execute(
# 文件名显示名:批量取一次(备注 > 昵称 > friend_id)。优先
# customer_account_link per-owner 快照——共享客户的档案行可能挂在别的
# 员工名下,按 owner 查档案会查空、zip 文件名回退成 wxid(2026-09-09,
# 同 sessions._meta 修复);link 没覆盖的回退档案。
link_rows = (await session.execute(
sa.select(
Customer.friend_id,
Customer.friend_nickname,
Customer.friend_remark,
CustomerAccountLink.friend_id,
CustomerAccountLink.friend_nickname,
CustomerAccountLink.friend_remark,
).where(
Customer.channel == channel,
Customer.owner_account_id == owner_account_id,
Customer.friend_id.in_(ids),
CustomerAccountLink.channel == channel,
CustomerAccountLink.owner_account_id == owner_account_id,
CustomerAccountLink.friend_id.in_(ids),
)
)).all()
names = {
r.friend_id: (r.friend_remark or r.friend_nickname or r.friend_id)
for r in cust_rows
for r in link_rows
}
missing = [t for t in ids if t not in names]
if missing:
cust_rows = (await session.execute(
sa.select(
Customer.friend_id,
Customer.friend_nickname,
Customer.friend_remark,
).where(
Customer.channel == channel,
Customer.owner_account_id == owner_account_id,
Customer.friend_id.in_(missing),
)
)).all()
for r in cust_rows:
names[r.friend_id] = (r.friend_remark or r.friend_nickname or r.friend_id)
buf = io.BytesIO()
with zipfile.ZipFile(buf, "w", zipfile.ZIP_DEFLATED) as zf: