feat(link): customer_account_link 账号↔客户多对多——共享好友员工维度对齐云客
用户报「Lily 好友数 36,点进员工客户页只有 12」。真机探明根因:那 24 个 是共享客户(同一人加多个员工),云客 per-employee 各算一个;而 customer 表 PK=friend_id 全局单行、归属 last-wins,其他员工名下必然「消失」。 抓取器本身每员工都拿全了(日志 declared=36 rows=36),丢在入库模型层。 - 新表 customer_account_link(渠道×账号×客户一行,per-relation 快照: 昵称/备注/头像/手机号/收发计数/最近沟通/add_time/fromType/isDel) - 写:repos.upsert_customer 随档案同事务维护(所有好友抓取路径一处挂全); 新增 prune_customer_links 对账 - 读:/api/customers?owner_account_id= 改读 link(与云客 contacts_num 口径 一致);该账号 link 为空时回退档案路径(过渡安全);与 dept/user 等 过滤组合时维持旧行为;CustomerOut 透传 from_type - 抓取器:friends/wa-friends 逐员工整批 upsert 后按名单对账(取满声明 total 才删,防半份名单误删) - 前端:StaffFriends 数据来源列优先 link 的 from_type 611 测试绿(link 新 6 例);本地验证空表回退/造数接管/from_type 透传。 上线后需重跑 friends+wa-friends 回填 link(存量关系无法从档案反推)。 Co-Authored-By: Claude Code <noreply@anthropic.com>
This commit is contained in:
@@ -267,10 +267,10 @@ async def scrape_friends(cookie_str: str, max_employees: int = 100):
|
||||
小漂移可忽略,且与云客后台「按员工看客户列表」口径一致。员工清单来自
|
||||
本地 wechat_account(user_id 列)。
|
||||
"""
|
||||
from app.db.base import async_engine
|
||||
from app.db.base import async_engine, async_session_maker
|
||||
from sqlalchemy import text as sa_text
|
||||
from app.scraper.wechat import _normalize
|
||||
from app.storage.repos import upsert_customer
|
||||
from app.storage.repos import prune_customer_links, upsert_customer
|
||||
|
||||
dept = await _root_dept_id()
|
||||
async with async_engine.connect() as conn:
|
||||
@@ -334,9 +334,21 @@ async def scrape_friends(cookie_str: str, max_employees: int = 100):
|
||||
if records:
|
||||
await _upsert_all(upsert_customer, records)
|
||||
total_upserted += len(records)
|
||||
# 对账(2026-09-08):云客 isDel=0 返回当前好友;名单取满(>=声明
|
||||
# total)才可信,删掉该账号名下已不在名单里的 link(转移/删好友)。
|
||||
# 半份名单宁可不对账,防误删。
|
||||
pruned = 0
|
||||
if declared is not None and len(rows) >= declared:
|
||||
keep_ids = [r.get("friendWxId") for r in rows if r.get("friendWxId")]
|
||||
async with async_session_maker() as session:
|
||||
pruned = await prune_customer_links(
|
||||
session, channel="wechat", owner_account_id=wx_id,
|
||||
keep_friend_ids=keep_ids,
|
||||
)
|
||||
await session.commit()
|
||||
logger.info(
|
||||
f" [{i+1}/{len(employees)}] {wx_id[:16]}..: declared={declared} "
|
||||
f"rows={len(rows)} (total fetched {total_fetched})"
|
||||
f"rows={len(rows)} pruned={pruned} (total fetched {total_fetched})"
|
||||
)
|
||||
await asyncio.sleep(2 + (i % 3))
|
||||
|
||||
@@ -1463,10 +1475,10 @@ async def scrape_wa_friends(cookie_str: str, max_pages: int = 2000):
|
||||
全局查询有 ~1 万行硬上限(实测云客声明 11931 vs 全局翻页只到 10000,
|
||||
2026-08-20);逐员工 ``uid`` 过滤(员工 WA 号)范围小无上限,页码字段是
|
||||
``page``,total-aware 翻页(短页继续,满声明数或连续空页停)。"""
|
||||
from app.db.base import async_engine
|
||||
from app.db.base import async_engine, async_session_maker
|
||||
from sqlalchemy import text as sa_text
|
||||
from app.scraper.whatsapp import _normalize_whatsapp_friend
|
||||
from app.storage.repos import upsert_customer
|
||||
from app.storage.repos import prune_customer_links, upsert_customer
|
||||
|
||||
dept = await _root_dept_id()
|
||||
async with async_engine.connect() as conn:
|
||||
@@ -1520,8 +1532,20 @@ async def scrape_wa_friends(cookie_str: str, max_pages: int = 2000):
|
||||
if records:
|
||||
await _upsert_all(upsert_customer, records)
|
||||
total_upserted += len(records)
|
||||
# 对账(2026-09-08,同微信 friends):名单取满才删该账号名下
|
||||
# 不在当前列表里的 link。
|
||||
pruned = 0
|
||||
if declared is not None and len(rows) >= declared:
|
||||
keep_ids = [r.get("friendUid") for r in rows if r.get("friendUid")]
|
||||
async with async_session_maker() as session:
|
||||
pruned = await prune_customer_links(
|
||||
session, channel="whatsapp", owner_account_id=wa_uid,
|
||||
keep_friend_ids=keep_ids,
|
||||
)
|
||||
await session.commit()
|
||||
logger.info(
|
||||
f" [{i+1}/{len(accounts)}] {wa_uid[:18]}..: declared={declared} rows={len(rows)}"
|
||||
f" [{i+1}/{len(accounts)}] {wa_uid[:18]}..: declared={declared} "
|
||||
f"rows={len(rows)} pruned={pruned}"
|
||||
)
|
||||
await asyncio.sleep(2 + (i % 3))
|
||||
|
||||
|
||||
Reference in New Issue
Block a user