为什么改:应用建的协同群只能代答,销售在群里发文件、@人、问报价都对不上人; 本次把群对话交给智价通BOT,应用只负责拉群和解散群,并把几处销售已反馈的口径补齐。 群与身份: - 新增 group_bot 短协调(Redis 带环境前缀):记录 BOT 已接管群、当前关联工单、 @BOT 指令窗口与窗口外先到的附件,只做协调不当六态,六态仍在主账。 - 群里没有 @BOT 的文字不回;非 @ 文字断开当前单关联,之后必须重新 @BOT 带工单号。 - 先发文件再 @BOT 也认:窗口外附件先缓冲,等 @BOT 认领后补办。 - 会话存档只补附件、不重复办理 BOT 已办的同一句文字,避免同一条消息回两遍。 - 入站 from.userid 是加密 open_userid 时按官方接口换明文(带 Redis 缓存), 否则与主账 sales_wecom_id 永远比不上,航线本人会被判成「不是本人」。 - 桥侧只认本群 req_id,不再借用别的群 req_id,避免测服把话发进别的群。 报价单: - 建群后只发字段摘要和成交卡,Excel/PDF 等群里 @BOT 说「生成报价单」再出; 多段逐段出,没价出空白表;出表后补推成交跟进不涨 deal_version、不重置等待计时。 - 陆运转人工先选业务类型和报价场景再建群,按选定模板名直取,不再靠关键词猜。 - 空运舱位只回一条时直接出该条报价,不再发选择卡。 字段与金额: - 客户名称改为海/空/陆询价非必填,有值写回工单列,后台列表、报价单与 factsJson 一致。 - 空运新增计费重投影:体积重=CBM×166.7,计费重=max(毛重,体积重); TMS 无价、航线手工按 KG 报价时按计费重×单价算整票金额,群摘要与报价单同公式。 - 陆运报价附加条件统一到 land_quote_condition:未含税/含0税/未含燃油/含燃油费, 燃油只乘主运费,不乘提货送货报关等杂费;跨境整车无备注列时追加到底部备注事项。 - 海运拼柜不适用箱型箱量:不进必填、不进补问、不出站,改回整柜可再用。 - 海运提货地址/派送地址改为非必填,写了才出站(placeOfReceipt/deliveryAddress), 禁止从港口或贸易条款反推地址;TMS 查不到价时把 TMS 原话透给销售。 - 中港集拼与跨境集拼同口径:按体积+毛重,不采车型数量;修正被国内拼车别名抢单。 - 报关费区只收报关类费目;口岸海关监管费+接驳费按车型列回填固定行。 主账与后台: - 看板处理量按协同群处理人统计;下线「平均报价销售额」;处理人搜索先把姓名换回企微 id。 - 「转人工」筛选同时兼容旧六态与新 collab_mode=handoff,新版单不再漏。 - 报价单客户名称、提货/派送地址、空运计费重、拼柜空箱型按新口径填表。 - 新增协同成员查询支持与 AIR_NEED_WO v2 迁移脚本。 文档:04 补海运/空运/陆运必填与地址口径,10 补地址出站规则,12 补测服独立应用 1000015 的部署与切换记录。 Co-authored-by: Cursor <cursoragent@cursor.com>
327 lines
10 KiB
Python
327 lines
10 KiB
Python
"""
|
|
报价模板匹配:询价需求对模板关键词,未命中则同业务默认模板。
|
|
|
|
本文件职责:纯函数,与 Java QuoteTemplateMatcher 同一套规则。
|
|
调用:MemoryLedger / 单测;生产出单以主账 render 为准。
|
|
禁止:在此发明价格;禁止读仓库 xlsx 当生产真相。
|
|
"""
|
|
|
|
from __future__ import annotations
|
|
|
|
from dataclasses import dataclass
|
|
from typing import Any, Iterable, Optional
|
|
|
|
# 与 Java QuoteTemplateMatcher.FAIL_MESSAGE 保持一字不差
|
|
FAIL_MESSAGE = "生成报价单失败,请联系IT运维维护后台报价模板。"
|
|
|
|
|
|
@dataclass(frozen=True)
|
|
class TemplatePick:
|
|
"""一次匹配结果。"""
|
|
|
|
template_id: str
|
|
name: str
|
|
match_type: str
|
|
raw: dict[str, Any]
|
|
|
|
|
|
def normalize_biz(raw: str) -> str:
|
|
"""业务线归一:AIR/空 → 空运,SEA/海 → 海运,LAND/陆 → 陆运。"""
|
|
t = (raw or "").strip()
|
|
if not t:
|
|
return ""
|
|
lower = t.lower()
|
|
if lower == "air" or t == "空" or "空运" in t or t == "AIR":
|
|
return "空运"
|
|
if lower == "sea" or t == "海" or "海运" in t or t == "SEA":
|
|
return "海运"
|
|
if lower == "land" or t == "陆" or "陆运" in t or t == "LAND":
|
|
return "陆运"
|
|
return t
|
|
|
|
|
|
def _as_keywords(raw: Any) -> list[str]:
|
|
"""摊开关键词。列表里若整段是 [海运,整柜] 也要拆开,与 Java asKeywords 一致。"""
|
|
if raw is None:
|
|
return []
|
|
if isinstance(raw, (list, tuple)):
|
|
out: list[str] = []
|
|
for x in raw:
|
|
s = str(x).strip()
|
|
if s.startswith("["):
|
|
out.extend(_as_keywords(s))
|
|
elif s:
|
|
out.append(s)
|
|
return out
|
|
text = str(raw).strip()
|
|
if not text or text.lower() == "null":
|
|
return []
|
|
if text.startswith("["):
|
|
inner = text[1:-1] if text.endswith("]") else text[1:]
|
|
out: list[str] = []
|
|
for part in inner.split(","):
|
|
s = part.strip().strip("\"'")
|
|
if s:
|
|
out.append(s)
|
|
return out
|
|
return [text]
|
|
|
|
|
|
def _is_default(raw: Any) -> bool:
|
|
if raw is True:
|
|
return True
|
|
if isinstance(raw, (int, float)) and int(raw) == 1:
|
|
return True
|
|
s = str(raw or "").strip().lower()
|
|
return s in {"1", "true"}
|
|
|
|
|
|
def _append(parts: list[str], val: Any) -> None:
|
|
if val is None:
|
|
return
|
|
s = str(val).strip()
|
|
if not s or s.lower() == "null":
|
|
return
|
|
parts.append(s)
|
|
|
|
|
|
def build_inquiry_haystack(
|
|
*,
|
|
business_line: str,
|
|
facts: Optional[dict[str, Any]] = None,
|
|
quote: Optional[dict[str, Any]] = None,
|
|
) -> str:
|
|
"""拼匹配文本:已冻结字段 + 业务线 + 报价费用名。"""
|
|
parts: list[str] = []
|
|
_append(parts, business_line)
|
|
_append(parts, normalize_biz(business_line))
|
|
for key, val in (facts or {}).items():
|
|
_append(parts, key)
|
|
_append(parts, val)
|
|
q = quote or {}
|
|
for key in ("source", "source_label", "freight", "total"):
|
|
_append(parts, q.get(key))
|
|
lines = q.get("fee_lines") or []
|
|
if isinstance(lines, Iterable) and not isinstance(lines, (str, bytes)):
|
|
for row in lines:
|
|
if isinstance(row, dict):
|
|
_append(parts, row.get("name"))
|
|
_append(parts, row.get("amount"))
|
|
else:
|
|
_append(parts, row)
|
|
return " ".join(parts)
|
|
|
|
|
|
def _row_of(item: Any) -> dict[str, Any]:
|
|
if isinstance(item, dict):
|
|
return item
|
|
return {
|
|
"templateId": getattr(item, "template_id", "") or getattr(item, "code", ""),
|
|
"name": getattr(item, "filename", "") or getattr(item, "code", ""),
|
|
"bizType": getattr(item, "biz_type", ""),
|
|
"keywords": list(getattr(item, "keywords", None) or []),
|
|
"isDefault": bool(getattr(item, "is_default", False)),
|
|
}
|
|
|
|
|
|
# 模板名归一:去掉这些词再比,后台叫「陆运国内城配、打包」与「国内城配/打包」要能对上。
|
|
_NAME_NOISE = (
|
|
"陆运",
|
|
"空运",
|
|
"海运",
|
|
"报价单",
|
|
"报价模板",
|
|
"模板",
|
|
"报价",
|
|
"新版",
|
|
"v1",
|
|
"v2",
|
|
)
|
|
_NAME_PUNCT = " \t\r\n-_/\\|、,,。.()()[]【】"
|
|
|
|
|
|
def normalize_template_name(raw: str) -> str:
|
|
"""
|
|
模板名归一:小写、去标点、去业务线前缀。
|
|
|
|
只用于出单时按名取模板,不写回库,也不影响后台展示。
|
|
"""
|
|
text = (raw or "").strip().lower()
|
|
for token in _NAME_PUNCT:
|
|
text = text.replace(token, "")
|
|
for token in _NAME_NOISE:
|
|
text = text.replace(token, "")
|
|
return text
|
|
|
|
|
|
def pick_by_name(
|
|
*,
|
|
templates: Optional[list[Any]],
|
|
template_name: str,
|
|
business_line: str = "",
|
|
) -> Optional[dict[str, Any]]:
|
|
"""
|
|
按模板名找表:先全等,再互相包含,取名字更长(更具体)的那张。
|
|
|
|
返回 None 表示没找到,调用方退回关键词匹配,绝不能因为改了模板名就出不了单。
|
|
"""
|
|
want_name = normalize_template_name(template_name)
|
|
if not want_name:
|
|
return None
|
|
want_biz = normalize_biz(business_line)
|
|
exact: Optional[dict[str, Any]] = None
|
|
fuzzy: Optional[dict[str, Any]] = None
|
|
fuzzy_len = 0
|
|
for item in templates or []:
|
|
row = _row_of(item)
|
|
biz = normalize_biz(str(row.get("bizType") or ""))
|
|
if want_biz and biz and want_biz != biz:
|
|
continue
|
|
have = normalize_template_name(str(row.get("name") or ""))
|
|
if not have:
|
|
continue
|
|
if have == want_name:
|
|
exact = row
|
|
break
|
|
if want_name in have or have in want_name:
|
|
if len(have) > fuzzy_len:
|
|
fuzzy = row
|
|
fuzzy_len = len(have)
|
|
return exact or fuzzy
|
|
|
|
|
|
def pick_quote_template(
|
|
*,
|
|
templates: Optional[list[Any]],
|
|
business_line: str,
|
|
facts: Optional[dict[str, Any]] = None,
|
|
quote: Optional[dict[str, Any]] = None,
|
|
template_name: str = "",
|
|
) -> Optional[TemplatePick]:
|
|
"""
|
|
同业务线:先按指定模板名直取,再按关键词打分(运输类型上的词优先),最后退回默认模板。
|
|
「陆运/国内」不当关键词,避免中港模板抢走拼车单。
|
|
参数 templates:主账 listEnabled 行或 TemplateEntry。
|
|
template_name:转人工卡片选定的业务类型模板,非空时优先,避免 7 种业务靠猜。
|
|
返回 None 时销售侧用 FAIL_MESSAGE。
|
|
"""
|
|
want = normalize_biz(business_line)
|
|
haystack = build_inquiry_haystack(
|
|
business_line=business_line, facts=facts, quote=quote
|
|
).lower()
|
|
type_val = str((facts or {}).get("运输类型") or "").strip().lower()
|
|
named = pick_by_name(
|
|
templates=templates,
|
|
template_name=template_name,
|
|
business_line=business_line,
|
|
)
|
|
if named is not None:
|
|
return TemplatePick(
|
|
template_id=str(named.get("templateId") or named.get("id") or ""),
|
|
name=str(named.get("name") or ""),
|
|
match_type="name",
|
|
raw=named,
|
|
)
|
|
keyword_hit: Optional[dict[str, Any]] = None
|
|
best: tuple[int, int] = (-1, -1)
|
|
default_hit: Optional[dict[str, Any]] = None
|
|
for item in templates or []:
|
|
row = _row_of(item)
|
|
biz = normalize_biz(str(row.get("bizType") or ""))
|
|
if want and biz and want != biz:
|
|
continue
|
|
if default_hit is None and _is_default(row.get("isDefault")):
|
|
default_hit = row
|
|
score = _keyword_score(row.get("keywords"), haystack, type_val)
|
|
if score == (0, 0):
|
|
continue
|
|
if score > best:
|
|
best = score
|
|
keyword_hit = row
|
|
hit = keyword_hit or default_hit
|
|
if hit is None:
|
|
return None
|
|
match_type = "keyword" if keyword_hit is not None else "default"
|
|
return TemplatePick(
|
|
template_id=str(hit.get("templateId") or hit.get("id") or ""),
|
|
name=str(hit.get("name") or ""),
|
|
match_type=match_type,
|
|
raw=hit,
|
|
)
|
|
|
|
|
|
_GENERIC_KEYWORDS = frozenset(
|
|
{"海运", "空运", "陆运", "sea", "air", "land", "国内"}
|
|
)
|
|
|
|
# 后台陆运模板常用「国内拼车」,询价字段是「国内运输拼车」,不能当两回事。
|
|
_LAND_KEYWORD_ALIASES = {
|
|
"国内拼车": ("国内运输拼车", "拼车"),
|
|
"国内零担": ("国内运输拼车", "国内长途/零担", "零担"),
|
|
# 「整车」不能当国内整车别名:跨境整车 / 中港整车都含这两个字。
|
|
"国内整车": ("国内运输整车",),
|
|
"跨境整车": ("跨境整车",),
|
|
"跨境集拼": ("跨境集拼",),
|
|
"跨境零担": ("跨境集拼",),
|
|
}
|
|
|
|
|
|
def _keyword_needles(kw: str) -> list[str]:
|
|
"""关键词本身 + 陆运别名,都小写。"""
|
|
raw = (kw or "").strip()
|
|
if not raw:
|
|
return []
|
|
out = [raw.lower()]
|
|
for alias in _LAND_KEYWORD_ALIASES.get(raw, ()):
|
|
token = alias.lower()
|
|
if token not in out:
|
|
out.append(token)
|
|
return out
|
|
|
|
|
|
# 单独出现时过宽:国内模板别名常拆出这些短词,会误配「中港零担/集拼」「跨境整车」。
|
|
# 中港/跨境模板须用带前缀的词(中港零担、跨境集拼),不能靠裸「零担/集拼」抢单。
|
|
_BARE_LAND_NEEDLES = frozenset({"整车", "零担", "拼车", "集拼"})
|
|
|
|
|
|
def _needle_in(text: str, needle: str) -> bool:
|
|
"""
|
|
陆运针:普通包含。
|
|
|
|
裸「整车/零担/拼车/集拼」不命中含跨境/中港的文本:
|
|
否则国内拼车模板靠「零担」「集拼」别名会抢走中港集拼单(WO202611180001)。
|
|
"""
|
|
hay = (text or "").lower()
|
|
n = (needle or "").strip().lower()
|
|
if not hay or not n or n not in hay:
|
|
return False
|
|
if n in _BARE_LAND_NEEDLES and ("跨境" in hay or "中港" in hay):
|
|
return False
|
|
return True
|
|
|
|
|
|
def _keyword_score(raw: Any, haystack: str, type_val: str) -> tuple[int, int]:
|
|
"""
|
|
运输类型上的命中优先,其次全文词长。
|
|
「陆运/国内」跳过,避免中港、国内整车抢走拼车单。
|
|
「国内拼车」要对上「国内运输拼车」。
|
|
"""
|
|
type_hits = 0
|
|
length = 0
|
|
any_hit = False
|
|
for kw in _as_keywords(raw):
|
|
k = kw.lower()
|
|
if k in _GENERIC_KEYWORDS:
|
|
continue
|
|
needles = _keyword_needles(kw)
|
|
if type_val and any(_needle_in(type_val, n) for n in needles):
|
|
type_hits += 1
|
|
length += len(k)
|
|
any_hit = True
|
|
elif any(_needle_in(haystack, n) for n in needles):
|
|
length += len(k)
|
|
any_hit = True
|
|
if not any_hit:
|
|
return (0, 0)
|
|
return (type_hits, length)
|