为什么改:应用建的协同群只能代答,销售在群里发文件、@人、问报价都对不上人; 本次把群对话交给智价通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>
5123 lines
200 KiB
Python
5123 lines
200 KiB
Python
"""
|
||
空运文字询价出站话术(销售可见)。
|
||
|
||
本文件职责:集中文案;按钮字面量必须与 handler 精确匹配。
|
||
销售可见固定句经 bot_reply 按调用编码读后台;停用/失败回退本文件默认句。
|
||
禁止:在本文件直接 request 主账;禁止把密钥写进话术。
|
||
"""
|
||
|
||
from __future__ import annotations
|
||
|
||
import json
|
||
import re
|
||
import time
|
||
import uuid
|
||
from typing import Any, Iterable
|
||
|
||
from agent.schema.field_validate import (
|
||
AIR_COLLAB_FIELDS,
|
||
AIR_OPTIONAL_FIELDS,
|
||
AIR_REQUIRED_FIELDS,
|
||
LAND_DISPLAY,
|
||
SEA_COLLAB_FIELDS,
|
||
SEA_OPTIONAL_FIELDS,
|
||
SEA_REQUIRED_FIELDS,
|
||
collab_display_name,
|
||
collab_fields_for_ticket,
|
||
display_name,
|
||
is_sea_lcl,
|
||
)
|
||
from agent.schema.land_options import option_prompt as _land_option_prompt
|
||
from agent.schema.quote_adjust import total_price_label
|
||
|
||
def _r(code: str, default: str, **kwargs: Any) -> str:
|
||
"""
|
||
按后台调用编码取回复语。
|
||
|
||
停用、库空、主账失败返回 default。本函数不打主账,只调 bot_reply 短缓存。
|
||
"""
|
||
from agent.policy.bot_reply import render
|
||
|
||
return render(code, default, **kwargs)
|
||
|
||
|
||
def _cabin_copy(code: str, default: str, **kwargs: Any) -> str:
|
||
"""
|
||
舱位话术。库里旧句仍写「释放」时,新发出的改用当前默认句。
|
||
|
||
先把填进去的原因、工单号抠掉再看骨架,避免 TMS 原文里的「释放」把新句子打回去。
|
||
已经发出的历史消息不在这里改。
|
||
"""
|
||
text = _r(code, default, **kwargs)
|
||
skeleton = text
|
||
for value in kwargs.values():
|
||
token = str(value or "")
|
||
if token:
|
||
skeleton = skeleton.replace(token, "")
|
||
if "释放" in skeleton:
|
||
return default
|
||
return text
|
||
|
||
|
||
# 空运补问:必填项 + 非必填。报价日期非必填,没说不默认当天,不进待补充。
|
||
_AIR_EXAMPLES = {
|
||
"起运港": "南京",
|
||
"目的港": "吉隆坡",
|
||
"品名": "茶叶",
|
||
"件数": "10",
|
||
"毛重": "100",
|
||
"体积": "2",
|
||
"包装方式": "托盘、散货",
|
||
"报价日期": "2026-09-15",
|
||
"货源地": "东莞",
|
||
"货好时间": "随时可提",
|
||
"提货地址": "东莞仓库",
|
||
"收货地址": "马尼拉",
|
||
"贸易条款": "DDP、DAP、CIF、CIP",
|
||
"客户名称": "某某贸易有限公司",
|
||
"added_services": "托盘、前置仓、带电、卡转、卡转低消、MUST GO、备装、分泡、温控、快件、DG、TC",
|
||
"尺寸": "1.1*2.1*2.2",
|
||
"比例": "1:212",
|
||
}
|
||
_AIR_CLARIFY = tuple(
|
||
(key, label, _AIR_EXAMPLES[key]) for key, label in AIR_REQUIRED_FIELDS
|
||
)
|
||
_AIR_OPTIONAL_CLARIFY = tuple(
|
||
(key, label, _AIR_EXAMPLES.get(key, "")) for key, label in AIR_OPTIONAL_FIELDS
|
||
)
|
||
AIR_CONFIRM_TAIL = "请回复“确定”进行TMS查价,内容有误时,请重新发内容。"
|
||
# 核对卡已取消时效,不再对销售发送此句;常量仅保留避免旧引用报错。
|
||
AIR_CONFIRM_EXPIRED = "刚才的空运核对已过期,请把空运需求再发一次,出核对卡后再回复确定。"
|
||
|
||
# 空运人工报价「舱位信息」里的固定说明(产品 2026-11-22 截图确认必须保留):
|
||
# 让客户知道这一行覆盖航司、线路、班期、舱位日期四件事,不能被精简掉。
|
||
_AIR_CABIN_SCOPE_NOTE = "(包含航司、线路、班期、舱位日期哪天起)"
|
||
# 海运私聊与空运同一句。核对卡没有工单号,回复确定后才建单查价。
|
||
SEA_CONFIRM_TAIL = "请回复“确定”进行TMS查价,内容有误时,请重新发内容。"
|
||
# 核对卡已取消时效,不再对销售发送此句。
|
||
SEA_CONFIRM_EXPIRED = "刚才的海运核对已过期,请把海运需求再发一次,出核对卡后再回复确定。"
|
||
|
||
# 海运补问:与合同 10 项一致。报价日期默认当天,不进待补充。
|
||
# 提货地址、派送地址是非必填(产品口径 2026-11-22),不出现在待补充清单里;
|
||
# 销售主动写了就回显/写入工单,缺省不挡查价。
|
||
_SEA_EXAMPLES = {
|
||
"起运港": "上海",
|
||
"目的港": "洛杉矶",
|
||
"品名": "普货",
|
||
"货量": "20吨",
|
||
"整柜或拼柜": "整柜",
|
||
"箱型箱量": "1x40HQ",
|
||
# 销售看到的海运贸易条款举例是四选一,不是 FOB。举例行不参与已填判断。
|
||
"贸易条款": "门到门、港到港、门到港、港到门",
|
||
"运输分类": "Port to Port",
|
||
"报价日期": "2026-09-15",
|
||
"提货地址": "深圳坪山仓库",
|
||
"收货地址": "洛杉矶仓库",
|
||
# 客户名称:询价时填写(非必填),与空运/陆运同一内部键。
|
||
"客户名称": "某某贸易有限公司",
|
||
}
|
||
_SEA_CLARIFY = tuple(
|
||
(key, label, _SEA_EXAMPLES[key]) for key, label in SEA_REQUIRED_FIELDS
|
||
)
|
||
|
||
# 文字按钮(销售点卡或原样回复均可)
|
||
BTN_SKIP_COLLAB = "不需要协同,直接生成报价单"
|
||
BTN_SKIP_COLLAB_OLD = "不需要协同,直接生成基础报价单"
|
||
# 海运/陆运已有 TMS 报价时价格明细卡用短文案;空运仍用上面长文案。点卡 EventKey 不变。
|
||
BTN_SKIP_COLLAB_TMS = "直接生成报价单"
|
||
BTN_SKIP_COLLAB_KEY = "skip_collab"
|
||
BTN_SKIP_COLLAB_DONE = "已提交"
|
||
BTN_PULL_COLLAB = "拉产品进群协同"
|
||
BTN_PULL_COLLAB_KEY = "pull_collab"
|
||
BTN_ADOPT_EXCEL = "采用,生成Excel报价单"
|
||
BTN_ADOPT_PDF = "采用,生成PDF报价单"
|
||
BTN_REJECT = "不采用"
|
||
# 企微一张卡三个按钮只能横排,长文案会被裁。卡片用短文案才能看全。
|
||
BTN_ADOPT_EXCEL_CARD = "生成Excel"
|
||
BTN_ADOPT_PDF_CARD = "生成PDF"
|
||
BTN_ADOPT_EXCEL_KEY = "adopt_excel"
|
||
BTN_ADOPT_PDF_KEY = "adopt_pdf"
|
||
BTN_REJECT_KEY = "reject"
|
||
BTN_DEAL = "成交"
|
||
BTN_LOST = "未成交"
|
||
BTN_NEGOTIATE = "协商中"
|
||
BTN_DEAL_KEY = "deal"
|
||
BTN_LOST_KEY = "lost"
|
||
BTN_NEGOTIATE_KEY = "negotiate"
|
||
# 旧卡 EventKey 兼容;新暂无报价卡按现网图:无按钮。
|
||
BTN_COPY_TICKET = "复制工单号"
|
||
BTN_COPY_TICKET_KEY = "copy_ticket"
|
||
# 陆运转人工多项选择卡的提交按钮。正文=EventKey(handoff_land_submit:令牌),
|
||
# 选项内容在回调的 SelectedItems 里,由 handoff_options 解析。
|
||
BTN_HANDOFF_LAND = "提交并转人工"
|
||
BTN_HANDOFF_LAND_KEY = "handoff_land_submit"
|
||
# 陆运补问卡(业务类型/报价场景 两项)的提交按钮。转人工前后都出这张卡。
|
||
BTN_LAND_OPTIONS = "提交选项"
|
||
BTN_LAND_OPTIONS_KEY = "land_options_submit"
|
||
|
||
_WO_NO = re.compile(r"(?:WO|VT)\d{12}", re.IGNORECASE)
|
||
# 仅给无网 stub / 工单号回填用,正式续办意图由 DeepSeek A 判断,不要求销售说这些词。
|
||
_CONTINUE_HINT = (
|
||
"继续",
|
||
"接着",
|
||
"回到",
|
||
"再看",
|
||
"再出",
|
||
"找回",
|
||
"重开",
|
||
"那单",
|
||
"这单",
|
||
"历史工单",
|
||
"咨询工单",
|
||
"刚才那",
|
||
"上次那",
|
||
)
|
||
|
||
|
||
ASK_TRANSPORT = (
|
||
"请回复数字或文字,告知我询价的运输方式:\n"
|
||
"1.陆运\n"
|
||
"2.海运\n"
|
||
"3.空运"
|
||
)
|
||
|
||
|
||
def ask_transport() -> str:
|
||
"""未识别运输方式。后台 inquiring.ask_transport。"""
|
||
return _r("inquiring.ask_transport", ASK_TRANSPORT)
|
||
|
||
|
||
def first_chat() -> str:
|
||
"""销售首次单聊。后台 inquiring.first_chat。"""
|
||
return _r("inquiring.first_chat", FIRST_CHAT)
|
||
|
||
|
||
def image_recognizing() -> str:
|
||
"""正在识别图片。后台 inquiring.image_recognizing。"""
|
||
return _r("inquiring.image_recognizing", IMAGE_RECOGNIZING)
|
||
|
||
|
||
def image_tech_fail() -> str:
|
||
"""图片技术失败。后台 inquiring.image_tech_fail。"""
|
||
return _r("inquiring.image_tech_fail", IMAGE_TECH_FAIL)
|
||
|
||
|
||
def attach_recognizing() -> str:
|
||
"""正在识别附件。后台 inquiring.attach_recognizing。"""
|
||
return _r("inquiring.attach_recognizing", ATTACH_RECOGNIZING)
|
||
|
||
|
||
def attach_tech_fail() -> str:
|
||
"""附件技术失败。后台 inquiring.attach_tech_fail。"""
|
||
return _r("inquiring.attach_tech_fail", ATTACH_TECH_FAIL)
|
||
|
||
|
||
def attach_h5_expired() -> str:
|
||
"""H5 上传过期。后台 inquiring.attach_h5_expired。"""
|
||
return _r("inquiring.attach_h5_expired", ATTACH_H5_EXPIRED)
|
||
|
||
|
||
def attach_h5_ok() -> str:
|
||
"""H5 上传成功。后台 inquiring.attach_h5_ok。"""
|
||
return _r("inquiring.attach_h5_ok", ATTACH_H5_OK)
|
||
|
||
|
||
def continue_need_wo() -> str:
|
||
"""续办但未提供工单号。后台 inquiring.continue_need_wo。"""
|
||
return _r("inquiring.continue_need_wo", "请告诉我要继续的工单号,例如 WO202609140034。")
|
||
|
||
|
||
# 结束询价:销售短指令,断开当前私聊上下文后可发新询价。
|
||
_END_INQUIRY_MARKERS = ("结束询价", "终止询价")
|
||
_END_INQUIRY_QUESTION_TAILS = frozenset({"吗", "呢", "吧", "啊", "呀", "嘛", "?", "?"})
|
||
END_INQUIRY_ACK = "已结束当前询价。您可以直接发起新的询价。"
|
||
END_INQUIRY_IDLE = "当前没有进行中的询价,可以直接描述新的需求。"
|
||
END_INQUIRY_CLOSED = "已结束当前询价(工单 {ticket_no} 已关闭)。您可以直接发起新的询价。"
|
||
|
||
|
||
def looks_like_end_inquiry(text: str) -> bool:
|
||
"""
|
||
是否在说结束/终止当前询价。
|
||
|
||
产品口令「结束询价」「终止询价」为主;短句带请/帮我也可。
|
||
问句(结束询价吗)不算。长段运输需求里偶然夹这几个字不算。
|
||
正式路由仍可由模型标 end_inquiry;本函数给口令快路径与夹具用。
|
||
"""
|
||
blob = (text or "").replace(" ", "").replace("\u3000", "").strip()
|
||
if not blob or blob[-1] in _END_INQUIRY_QUESTION_TAILS:
|
||
return False
|
||
compact = blob.rstrip("。.!!,~,、")
|
||
for marker in _END_INQUIRY_MARKERS:
|
||
if compact == marker:
|
||
return True
|
||
if marker in compact and len(compact) <= len(marker) + 6:
|
||
return True
|
||
return False
|
||
|
||
|
||
def end_inquiry_ack(*, work_order_no: str = "", closed: bool = False, had_session: bool = True) -> str:
|
||
"""
|
||
结束询价回执。后台 inquiring.end_inquiry_*。
|
||
|
||
had_session=False:本来就没有当前书签。
|
||
closed=True 且有正式工单号:顺带关主账后带工单号说明。
|
||
"""
|
||
if not had_session:
|
||
return _r("inquiring.end_inquiry_idle", END_INQUIRY_IDLE)
|
||
no = (work_order_no or "").strip()
|
||
if closed and no:
|
||
return _r(
|
||
"inquiring.end_inquiry_closed",
|
||
END_INQUIRY_CLOSED.format(ticket_no=no),
|
||
ticket_no=no,
|
||
)
|
||
return _r("inquiring.end_inquiry_ack", END_INQUIRY_ACK)
|
||
|
||
|
||
def continue_ticket_not_found(work_order_no: str) -> str:
|
||
"""续办找不到工单。后台 inquiring.continue_not_found(工单号后有空格)。"""
|
||
no = (work_order_no or "").strip()
|
||
return _r(
|
||
"inquiring.continue_not_found",
|
||
f"找不到工单 {no},请核对工单号后再试。",
|
||
ticket_no=no,
|
||
)
|
||
|
||
|
||
def quote_generating() -> str:
|
||
"""报价单生成中。后台 inquiring.quote_generating。"""
|
||
return _r("inquiring.quote_generating", "报价单正在生成,请稍候。")
|
||
|
||
|
||
# 待补充清单:请销售整段复制,只在冒号后填值;示例行不算已填。
|
||
CLARIFY_PASTE_HINT = "请复制粘贴以下内容,通过回复数字或文字方式,告知我以下字段值:"
|
||
_PENDING_UNITS = {}
|
||
HANDOFF_ALREADY_COLLAB = "该工单已拉产品进群协同。"
|
||
|
||
|
||
def already_collab() -> str:
|
||
"""该工单已拉过产品进群。后台 transfer.already_collab。"""
|
||
return _r("transfer.already_collab", HANDOFF_ALREADY_COLLAB)
|
||
|
||
|
||
HANDOFF_CLARIFY_REASON = "参数提取不全(连续 3 次反问仍不完整)"
|
||
HANDOFF_INTENT_REASON = "已识别您希望转人工处理"
|
||
CLARIFY_LIMIT_HINT = "连续 3 次反问仍不完整将触发转人工"
|
||
# 私聊图片询价:先回识别中;看不清/失败与补问拼成一条,销售侧不加 [test]。
|
||
IMAGE_RECOGNIZING = "正在识别图片"
|
||
IMAGE_UNREADABLE = "图片没看清询价信息,请换一张更清楚的,或直接打字。"
|
||
IMAGE_TECH_FAIL = "图片暂时没认出来,请再发一张或直接打字。"
|
||
# 私聊附件询价:H5 或发图开始认;看不清/失败与补问拼成一条。
|
||
ATTACH_RECOGNIZING = "正在识别附件"
|
||
ATTACH_UNREADABLE = "附件没看清询价信息,请换一份更清楚的 Excel / PDF(或把图片发到会话),或直接打字。"
|
||
ATTACH_TECH_FAIL = "附件暂时没认出来,请再传一次或直接打字。"
|
||
ATTACH_H5_REJECT_TYPE = "只接受 Excel、PDF;图片请发到当前企微会话。"
|
||
ATTACH_H5_NEED_FILE = "请选择要上传的 Excel 或 PDF。"
|
||
ATTACH_H5_EXPIRED = "已过期,请回企微重新说附件询价。"
|
||
ATTACH_H5_OK = "已收到,请回企微查看识别结果。"
|
||
|
||
|
||
def attachment_upload_card(url: str) -> dict[str, Any]:
|
||
"""
|
||
附件上传入口模板卡(text_notice 跳转 H5)。
|
||
|
||
必须跟现网同一卡型:企微 news_notice 强制要 card_image.url,缺了会 42044,
|
||
出站再回退成一句「上传询价附件」,销售侧就看不到卡片。
|
||
文案对齐规格:Excel/PDF 走 H5,图片发当前会话。url 必须已含 token、不含 userid。
|
||
"""
|
||
jump = (url or "").strip()
|
||
fallback = "https://ai.ytd-scm.com/inquiry-form"
|
||
action_url = jump or fallback
|
||
card: dict[str, Any] = {
|
||
"card_type": "text_notice",
|
||
"source": {"desc": "附件询价", "desc_color": 0},
|
||
"main_title": {
|
||
"title": "上传询价附件",
|
||
"desc": "附件入口支持 Excel、PDF;图片请直接在企微聊天窗口发送",
|
||
},
|
||
"sub_title_text": (
|
||
"Excel、PDF 请点击下方入口上传;图片请直接在当前企微会话发送。"
|
||
"字段完整时自动进入建单与查价;缺失字段会在企微中补问"
|
||
),
|
||
"jump_list": [{"type": 1, "title": "上传询价附件", "url": action_url}],
|
||
"card_action": {"type": 1, "url": action_url},
|
||
}
|
||
return {"msgtype": "template_card", "template_card": card}
|
||
|
||
|
||
def image_fail_then_clarify(reason: str, clarify: str) -> str:
|
||
"""
|
||
看不清或技术失败时,说明 + 文字询价补问写在同一条回复。
|
||
|
||
禁止只回说明就把人丢在那里。两端空白去掉,中间空一行。
|
||
"""
|
||
head = (reason or "").strip()
|
||
body = (clarify or "").strip()
|
||
if head and body:
|
||
return f"{head}\n\n{body}"
|
||
return head or body
|
||
|
||
# 旧一句只问运输类型;合并后销售一次看到业务类型和报价场景。残留调用也走完整清单。
|
||
ASK_LAND_SUBTYPE = _land_option_prompt(current={})
|
||
|
||
|
||
def land_sample_image_payload() -> dict[str, Any]:
|
||
"""
|
||
填写样例表图片载荷。编号清单上线后不再主动出站,保留给旧调用。
|
||
|
||
走企微 image,不走文件气泡。禁止在回调线程上传。
|
||
"""
|
||
from agent.schema.land_options import sample_image_path
|
||
|
||
path = sample_image_path()
|
||
return {
|
||
"msgtype": "image",
|
||
"filename": "填写样例.png",
|
||
"filepath": str(path),
|
||
}
|
||
|
||
|
||
def land_sample_image_fallback() -> str:
|
||
"""旧图失败兜底。新补问不再发图,一般不会走到这里。"""
|
||
from agent.schema.land_options import sample_fallback_text
|
||
|
||
return sample_fallback_text()
|
||
FIRST_CHAT = "你好,我是询价机器人。请直接描述客户需求,例如「上海到洛杉矶空运,3 件 180kg,下周三前到」,我会解析需求并查询 TMS 标准报价。"
|
||
ROUTE_MODEL_FAILED = "刚才没听清,请再说一次:是新询价,还是要接着办哪张工单?"
|
||
LAND_CONFIRM_TAIL = "请回复“确定”进行TMS查价,内容有误时,请重新发内容。"
|
||
# 核对卡已取消时效。仅工单号丢失时内部兜底,不对销售当「过期」主路径。
|
||
LAND_CONFIRM_EXPIRED = "刚才的陆运核对已过期,请把陆运需求再发一次,出核对卡后再回复确定。"
|
||
LAND_TMS_RETRY = "工单 {wo} 上次查价没成功。回复「确定」按原需求再查一次,不用重新填。"
|
||
LAND_TMS_RETRYING = "正在重新查询TMS报价。工单:{wo}"
|
||
|
||
_LAND_LCL_KEYS = ("起运港", "目的港", "品名", "体积", "毛重", "报价日期")
|
||
_LAND_FTL_KEYS = ("起运港", "目的港", "品名", "车型/数量", "报价日期")
|
||
# 中港零担/集拼:核对卡/方案卡不展示车型/数量(中港整车要采车型/数量)。
|
||
# 2026-11-22 产品口径:中港集拼按体积+毛重查价,与跨境集拼同一套货物字段。
|
||
_LAND_HK_LCL_KEYS = ("起运港", "目的港", "品名", "体积", "毛重", "报价日期")
|
||
_LAND_FTL_TYPES = frozenset({"国内运输整车", "跨境整车", "中港整车"})
|
||
_LAND_HK_NO_VEHICLE_TYPES = frozenset({"中港零担/集拼"})
|
||
_LAND_CUSTOMS_TYPES = frozenset({"跨境集拼", "跨境整车", "中港整车", "中港零担/集拼"})
|
||
_LAND_EXAMPLES = {
|
||
"起运港": "广州",
|
||
"目的港": "深圳",
|
||
"品名": "衣服",
|
||
"体积": "100CBM",
|
||
"毛重": "50kg",
|
||
"车型/数量": "9米6/1",
|
||
"通关口岸": "友谊关",
|
||
# 客户名称:询价时填写(非必填),给销售一个照抄样例。
|
||
"客户名称": "某某贸易有限公司",
|
||
"运输类型": "国内运输拼车",
|
||
"线路类别": "国内长途/零担",
|
||
"运输分类": "急件",
|
||
}
|
||
|
||
|
||
def _land_cargo_keys(land_type: str, facts: dict[str, str] | None = None) -> tuple[str, ...]:
|
||
"""当前运输类型要展示的货物内部键。中港集拼看体积毛重、不要车型/数量。"""
|
||
_ = facts
|
||
if land_type in _LAND_HK_NO_VEHICLE_TYPES:
|
||
return _LAND_HK_LCL_KEYS
|
||
if land_type in _LAND_FTL_TYPES:
|
||
return _LAND_FTL_KEYS
|
||
return _LAND_LCL_KEYS
|
||
|
||
|
||
def land_confirm_card(facts: dict[str, str] | None) -> str:
|
||
"""
|
||
确定查价前的核对卡。没有工单号。
|
||
|
||
行列出运输类型/线路/分类和该类型货物字段。
|
||
国内不出现通关口岸;跨境/中港通关口岸非必填,没填留空行。
|
||
不再单独列出中港装载方式。
|
||
"""
|
||
src = dict(facts or {})
|
||
land_type = (src.get("运输类型") or "").strip()
|
||
lines = [
|
||
f"运输类型(必填):{land_type}",
|
||
f"线路类别(必填):{(src.get('线路类别') or '').strip()}",
|
||
f"运输分类(必填):{(src.get('运输分类') or '').strip()}",
|
||
]
|
||
for key in _land_cargo_keys(land_type, src):
|
||
label = LAND_DISPLAY.get(key, key)
|
||
if key == "品名":
|
||
from agent.schema.land_options import land_cargo_name
|
||
|
||
val = land_cargo_name(src)
|
||
else:
|
||
val = (src.get(key) or "").strip()
|
||
lines.append(f"{label}(必填):{val}")
|
||
# 非必填行:通关口岸(仅跨境/中港)、客户名称(全线)。空着也占行,
|
||
# 让销售知道有这栏可填;缺省不挡查价。
|
||
for key in _land_optional_keys(land_type):
|
||
label = LAND_DISPLAY.get(key, key)
|
||
lines.append(f"{label}(非必填):{(src.get(key) or '').strip()}")
|
||
lines.append("")
|
||
lines.append(LAND_CONFIRM_TAIL)
|
||
card = "\n".join(lines)
|
||
return card
|
||
|
||
|
||
def land_group_no_staff() -> str:
|
||
"""没有对上线路的陆运产品,不建空群。后台 inquiring.land_no_staff。"""
|
||
return _r(
|
||
"inquiring.land_no_staff",
|
||
"没有可拉的产品人员。请先在后台「员工管理」维护本线路的陆运产品岗后再试。",
|
||
)
|
||
|
||
|
||
def land_group_pair_rejected() -> str:
|
||
"""类型+线路不在填写样例里,禁止拉群。"""
|
||
return "拉群失败,请排查填写内容,并重新发起工单。"
|
||
|
||
|
||
def _land_optional_keys(land_type: str) -> tuple[str, ...]:
|
||
"""
|
||
当前类型询价非必填内部键。
|
||
|
||
客户名称 2026-11-22 从协同改到询价:所有陆运类型都列(非必填)。
|
||
国内没有通关口岸;跨境/中港才有。
|
||
"""
|
||
if land_type in _LAND_CUSTOMS_TYPES:
|
||
return ("通关口岸", "客户名称")
|
||
return ("客户名称",)
|
||
|
||
|
||
def _pending_field_block(
|
||
index: int,
|
||
label: str,
|
||
example: str = "",
|
||
*,
|
||
optional: bool = False,
|
||
unit: str = "",
|
||
) -> str:
|
||
"""
|
||
待补充一项:编号 + 字段名 + 必填/单位,示例另起一行。
|
||
|
||
销售侧如:
|
||
1.件数(必填):
|
||
(如:10)
|
||
2.重量(KG)(必填):
|
||
(如:100)
|
||
复制回去时只认冒号后面的值;下一行「(如:…)」不算已填。
|
||
"""
|
||
mark = "(非必填)" if optional else "(必填)"
|
||
extra = f"({unit})" if (unit or "").strip() else ""
|
||
head = f"{index}.{label}{extra}{mark}:"
|
||
shown = (example or "").strip()
|
||
if shown:
|
||
return f"{head}\n(如:{shown})"
|
||
return head
|
||
|
||
|
||
def _land_field_line(key: str, value: str, *, required: bool, example: str = "") -> str:
|
||
"""已识别一行:名称(必填/非必填):值。待补充走 _pending_field_block。"""
|
||
label = LAND_DISPLAY.get(key, key)
|
||
mark = "必填" if required else "非必填"
|
||
if value:
|
||
return f"{label}({mark}):{value}"
|
||
_ = example
|
||
return f"{label}({mark}):"
|
||
|
||
|
||
def clarify_progress_line(retry: int) -> str:
|
||
"""补问次数提醒:参数提取不全(1/3)。"""
|
||
n = int(retry or 0)
|
||
if n < 1:
|
||
n = 1
|
||
if n > 3:
|
||
n = 3
|
||
return f"参数提取不全({n}/3)。{CLARIFY_LIMIT_HINT}"
|
||
|
||
|
||
def with_clarify_progress(body: str, retry: int) -> str:
|
||
"""把次数提醒顶在补问正文前面。第一次补问(retry=0)不打(1/3)。"""
|
||
text = (body or "").strip()
|
||
n = int(retry or 0)
|
||
if n < 1:
|
||
return text
|
||
head = clarify_progress_line(n)
|
||
if not text:
|
||
return head
|
||
return head + "\n" + text
|
||
|
||
|
||
def ask_land_fields(
|
||
*,
|
||
facts: dict[str, str] | None = None,
|
||
missing_keys: Iterable[str] = (),
|
||
land_subtype: str = "",
|
||
) -> str:
|
||
"""
|
||
陆运货物缺项补问。不出核对卡、不出现确定句。
|
||
|
||
已识别和待补充都标明必填/非必填。跨境/中港空着的通关口岸也列进待补充。
|
||
报价日期不进待补充;货物品名与其他字段同口径——缺了就排进待补充清单,
|
||
不再单独问一句「请回复货物品名」,销售看到的就是一张完整待补充表。
|
||
品名固定排在第 1 位(它原来是选项清单里的项,做卡后并回常规必填清单最前面)。
|
||
"""
|
||
src = dict(facts or {})
|
||
skip = {"报价日期"}
|
||
missing = [k for k in missing_keys if k and k not in skip]
|
||
# 已经填上的项不再进待补充:品名常以别名键(货物名称等)落库,
|
||
# 上游若仍把它当缺项传进来,这里按同一口径重新判一次,避免同一项又回显又追问。
|
||
from agent.schema.land_options import land_cargo_name
|
||
|
||
if "品名" in missing and land_cargo_name(src):
|
||
missing = [k for k in missing if k != "品名"]
|
||
# 货物品名排第 1:它原来是「运输类型/线路类别/运输分类」同一张清单里的项,
|
||
# 那三项改成卡片后,这里把它并进常规必填清单的第一位(产品口径,顺序为 1)。
|
||
if "品名" in missing:
|
||
missing = ["品名"] + [k for k in missing if k != "品名"]
|
||
land_type = (land_subtype or src.get("运输类型") or "").strip()
|
||
optional = _land_optional_keys(land_type)
|
||
lines = ["请确认并补充以下信息:", "运输方式:陆运"]
|
||
if land_type:
|
||
lines.append(f"运输类型:{land_type}")
|
||
for key in ("线路类别", "运输分类"):
|
||
val = (src.get(key) or "").strip()
|
||
if val:
|
||
lines.append(_land_field_line(key, val, required=True))
|
||
for key in _land_cargo_keys(land_type, src):
|
||
if key == "品名":
|
||
val = land_cargo_name(src)
|
||
else:
|
||
val = (src.get(key) or "").strip()
|
||
if val:
|
||
lines.append(_land_field_line(key, val, required=True))
|
||
for key in optional:
|
||
val = (src.get(key) or "").strip()
|
||
if val:
|
||
lines.append(_land_field_line(key, val, required=False))
|
||
pending = list(missing)
|
||
for key in optional:
|
||
if not (src.get(key) or "").strip() and key not in pending:
|
||
pending.append(key)
|
||
if not pending:
|
||
return ""
|
||
lines.append("")
|
||
lines.append(CLARIFY_PASTE_HINT)
|
||
seq = 0
|
||
for key in pending:
|
||
seq += 1
|
||
required = key not in optional
|
||
example = _LAND_EXAMPLES.get(key, "")
|
||
label = LAND_DISPLAY.get(key, key)
|
||
lines.append(
|
||
_pending_field_block(
|
||
seq,
|
||
label,
|
||
example,
|
||
optional=not required,
|
||
unit=_PENDING_UNITS.get(key, ""),
|
||
)
|
||
)
|
||
return "\n".join(lines)
|
||
|
||
|
||
def _air_known_value(facts: dict[str, str], key: str) -> str:
|
||
"""已识别字段的销售侧展示值;没有则空串。"""
|
||
src = facts or {}
|
||
if key == "起运港":
|
||
return (src.get("起运港") or "").strip()
|
||
if key == "目的港":
|
||
return (src.get("目的港") or "").strip()
|
||
if key == "品名":
|
||
return (src.get("品名") or "").strip()
|
||
if key == "件数":
|
||
raw = (src.get("件数") or "").strip()
|
||
return _keep_or_add_unit(raw, ("件", "PCS"), "件") if raw else ""
|
||
if key == "毛重":
|
||
raw = (src.get("毛重") or "").strip()
|
||
return _keep_or_add_unit(raw, ("KGS", "KG", "公斤", "千克"), "") if raw else ""
|
||
if key == "体积":
|
||
raw = (src.get("体积") or "").strip()
|
||
return _keep_or_add_unit(raw, ("CBM", "M3", "M³", "立方米", "立方", "方"), "") if raw else ""
|
||
if key == "包装方式":
|
||
return (src.get("包装方式") or src.get("包装类型") or "").strip()
|
||
if key == "报价日期":
|
||
from agent.schema.field_validate import is_blank_quote_date
|
||
|
||
val = (src.get("报价日期") or "").strip()
|
||
return "" if is_blank_quote_date(val) else val
|
||
from agent.llm.extract_text import clip_inquiry_value
|
||
|
||
return clip_inquiry_value(src.get(key) or "")
|
||
|
||
|
||
def _sea_known_value(facts: dict[str, str], key: str) -> str:
|
||
"""海运已识别字段的销售侧展示值。"""
|
||
src = facts or {}
|
||
if key == "货量":
|
||
return (src.get("货量") or src.get("货物数量") or "").strip()
|
||
return (src.get(key) or "").strip()
|
||
|
||
|
||
def _air_field_line(key: str, value: str, *, required: bool, example: str = "") -> str:
|
||
"""空运核对卡一行:名称(必填/非必填):值。待补充走 _pending_field_block。"""
|
||
label = AIR_DISPLAY_LABEL(key)
|
||
mark = "必填" if required else "非必填"
|
||
text = (value or "").strip()
|
||
if text:
|
||
return f"{label}({mark}):{text}"
|
||
_ = example
|
||
return f"{label}({mark}):"
|
||
|
||
|
||
def AIR_DISPLAY_LABEL(key: str) -> str:
|
||
"""核对/补问对外名。空运起运港对外写起运地,内部键不变。"""
|
||
return display_name(key, "AIR")
|
||
|
||
|
||
def air_confirm_card(facts: dict[str, str] | None) -> str:
|
||
"""
|
||
确定查价前的核对卡。没有工单号。
|
||
|
||
必填项一律列出(方便核对)。非必填与报价日期:有识别结果才展示,空着不占行。
|
||
起运地有值时紧挨在目的地上面。报价日期没说不填当天、也不出空行。
|
||
出卡前再分列一次:体积只接受 CBM/方数,尺寸式不得出现在体积行。
|
||
"""
|
||
from agent.schema.field_validate import separate_air_size_from_volume
|
||
|
||
src = separate_air_size_from_volume(dict(facts or {}))
|
||
lines = ["运输方式(必填):空运"]
|
||
origin_val = _air_known_value(src, "起运港")
|
||
if origin_val:
|
||
lines.append(_air_field_line("起运港", origin_val, required=False))
|
||
for key, _label in AIR_REQUIRED_FIELDS:
|
||
if key == "报价日期":
|
||
val = _air_known_value(src, key)
|
||
# 说了才展示;没说不占行、不填当天(产品:空非必填不显示)。
|
||
if val:
|
||
lines.append(f"报价日期:{val}")
|
||
continue
|
||
lines.append(_air_field_line(key, _air_known_value(src, key), required=True))
|
||
for key, _label in AIR_OPTIONAL_FIELDS:
|
||
if key == "起运港":
|
||
continue
|
||
opt_val = _air_known_value(src, key)
|
||
if not opt_val:
|
||
continue
|
||
opt_line = _air_field_line(key, opt_val, required=False)
|
||
lines.append(opt_line)
|
||
lines.append("")
|
||
lines.append(AIR_CONFIRM_TAIL)
|
||
return "\n".join(lines)
|
||
|
||
|
||
def sea_confirm_card(facts: dict[str, str] | None) -> str:
|
||
"""
|
||
海运确定查价前的核对卡。没有工单号。
|
||
|
||
销售侧必填都列出(货物数量对应内部键货量)。报价日期没说则用已经填上的当天。
|
||
提货地址/派送地址是非必填:有值才占行,空着不出现,避免销售以为漏填了东西。
|
||
拼柜(散货/LCL)不占「箱型箱量」行:拼柜按体积计费,没有指定箱型。
|
||
协同字段不在这张卡上,不挡首次查价。
|
||
"""
|
||
src = dict(facts or {})
|
||
lcl = is_sea_lcl(src.get("整柜或拼柜"))
|
||
lines = ["运输方式(必填):海运"]
|
||
for key, label in SEA_REQUIRED_FIELDS:
|
||
if key == "箱型箱量" and lcl:
|
||
# 拼柜不适用箱型箱量(产品口径 2026-11-22),整行不出现。
|
||
continue
|
||
if key == "货量":
|
||
val = (src.get("货量") or src.get("货物数量") or "").strip()
|
||
else:
|
||
val = (src.get(key) or "").strip()
|
||
lines.append(f"{label}(必填):{val}")
|
||
# 非必填:只有识别到值才列出,销售才知道这单是门到门还是港到港的口径。
|
||
for key, label in SEA_OPTIONAL_FIELDS:
|
||
val = (src.get(key) or "").strip()
|
||
if not val or val in {"-", "—", "-"}:
|
||
continue
|
||
lines.append(f"{label}(非必填):{val}")
|
||
lines.append("")
|
||
lines.append(SEA_CONFIRM_TAIL)
|
||
return "\n".join(lines)
|
||
|
||
|
||
def ask_clarify(
|
||
*,
|
||
facts: dict[str, str] | None = None,
|
||
missing_keys: Iterable[str] = (),
|
||
transport_mode: str = "空运",
|
||
) -> str:
|
||
"""
|
||
缺项补问:先回显已识别,再给可复制清单;示例另起一行,如「1.件数(必填):」下一行「(如:10)」。
|
||
|
||
空运报价日期:说了就写成「报价日期:日期」,没说写成「报价日期(非必填):」,不进待补充。
|
||
海运报价日期写进已识别(销售说了用原话,没说用当天),不出现在待补充。
|
||
空运:仅当仍有必填缺口时,才把空着的非必填一并放进待补充;
|
||
必填已齐则返回空串,由核对卡引导回复「确定」,禁止只甩一堆非必填逼回复。
|
||
海运(2026-11-22 产品确认,与空运对齐):同样只在有必填缺口时列出
|
||
空着的「提货地址(非必填)」「派送地址(非必填)」;销售不回也能出核对卡。
|
||
海运拼柜(散货/LCL):箱型箱量不适用,既不回显也不列进待补(产品口径 2026-11-22)。
|
||
"""
|
||
src = dict(facts or {})
|
||
skip = {"报价日期"}
|
||
missing = [k for k in missing_keys if k and k not in skip]
|
||
missing_set = set(missing)
|
||
sea = (transport_mode or "").strip() == "海运"
|
||
if not sea:
|
||
# 口径 A 兜底:补问回显前再分列一次,防止上游漏跑把尺寸式写进体积行
|
||
from agent.schema.field_validate import separate_air_size_from_volume
|
||
|
||
src = separate_air_size_from_volume(src)
|
||
rows = _SEA_CLARIFY if sea else _AIR_CLARIFY
|
||
# 海运拼柜:箱型箱量不适用,回显与待补都不出现(产品口径 2026-11-22)。
|
||
sea_lcl = sea and is_sea_lcl(src.get("整柜或拼柜"))
|
||
if sea_lcl:
|
||
rows = tuple(row for row in rows if row[0] != "箱型箱量")
|
||
known = _sea_known_value if sea else _air_known_value
|
||
lines = ["请确认并补充以下信息:"]
|
||
if transport_mode:
|
||
lines.append(f"运输方式:{transport_mode}")
|
||
if not sea:
|
||
origin = known(src, "起运港")
|
||
if origin:
|
||
lines.append(f"起运地:{origin}")
|
||
for key, label, _example in rows:
|
||
val = known(src, key)
|
||
if not sea and key == "报价日期":
|
||
# 空运报价日期挪到已识别区最下(尺寸等非必填之后),这里先跳过
|
||
continue
|
||
# 体积空着不进上方回显:只进下方待补充(体积必填)
|
||
if val:
|
||
lines.append(f"{label}:{val}")
|
||
if sea:
|
||
# 海运非必填:说了就先回显(销售能核对门到门口径);
|
||
# 没说的稍后在待补充里以「(非必填)」列出,不逼回复(见下方 optional_pending)。
|
||
for key, label in SEA_OPTIONAL_FIELDS:
|
||
val = (src.get(key) or "").strip()
|
||
if val and val not in {"-", "—", "-"}:
|
||
lines.append(f"{label}(非必填):{val}")
|
||
if not sea:
|
||
for key, label, _example in _AIR_OPTIONAL_CLARIFY:
|
||
if key == "起运港":
|
||
continue
|
||
val = known(src, key)
|
||
if val:
|
||
lines.append(f"{label}:{val}")
|
||
# 报价日期:有值写日期,没说占「非必填」空行;一律贴在已识别区最下面
|
||
qdate = known(src, "报价日期")
|
||
lines.append(f"报价日期:{qdate}" if qdate else "报价日期(非必填):")
|
||
# 先判断有没有必填缺口。仅非必填空着时不发「请复制粘贴」,交给核对卡让销售直接回复确定。
|
||
required_items: list[tuple[str, str, str, str]] = []
|
||
if "运输方式" in missing_set:
|
||
required_items.append(("运输方式", "运输方式", "空运", ""))
|
||
for key, label, example in rows:
|
||
if key in missing_set:
|
||
if sea and key == "运输分类":
|
||
required_items.append((key, "运输分类", "Port to Port、其他", ""))
|
||
elif sea and key == "箱型箱量" and sea_lcl:
|
||
# 兜底:上游若仍把箱型箱量算进缺项(拼柜),这里也不逼销售填。
|
||
continue
|
||
else:
|
||
required_items.append(
|
||
(key, label, example, _PENDING_UNITS.get(key, ""))
|
||
)
|
||
optional_pending: list[tuple[str, str, str]] = []
|
||
if required_items and sea:
|
||
# 海运非必填(产品口径 2026-11-22 align_air_four):
|
||
# 与空运一致——只要还有必填缺口,就把空着的提货地址/派送地址一并列成「(非必填)」,
|
||
# 让销售知道这两个字段存在;销售不回也能走(只补必填即可出核对卡),
|
||
# 已填过的不重复列,避免同一字段在上面回显、下面又让补。
|
||
for key, label in SEA_OPTIONAL_FIELDS:
|
||
val = (src.get(key) or "").strip()
|
||
if val and val not in {"-", "—", "-"}:
|
||
continue
|
||
optional_pending.append((key, label, _SEA_EXAMPLES.get(key, "")))
|
||
elif required_items:
|
||
# 已有必填要补时,空运非必填一并列出;起运地排在目的地前面。
|
||
# 体积始终是必填:空着走 required_items,不进非必填清单。
|
||
if not known(src, "起运港"):
|
||
optional_pending.append(
|
||
("起运港", "起运地", _AIR_EXAMPLES["起运港"])
|
||
)
|
||
optional_pending.extend(
|
||
(key, label, example)
|
||
for key, label, example in _AIR_OPTIONAL_CLARIFY
|
||
if key != "起运港" and not known(src, key)
|
||
)
|
||
if not required_items:
|
||
return ""
|
||
lines.append("")
|
||
lines.append(CLARIFY_PASTE_HINT)
|
||
seq = 0
|
||
origin_block = ""
|
||
other_optional_blocks: list[str] = []
|
||
for key, label, example in optional_pending:
|
||
if key == "起运港":
|
||
seq += 1
|
||
origin_block = _pending_field_block(
|
||
seq, label, example, optional=True
|
||
)
|
||
# 起运地(非必填)紧挨目的地上面:有则先写起运地,再写必填缺口。
|
||
if origin_block:
|
||
lines.append(origin_block)
|
||
for _key, label, example, unit in required_items:
|
||
seq += 1
|
||
lines.append(
|
||
_pending_field_block(seq, label, example, unit=unit)
|
||
)
|
||
for key, label, example in optional_pending:
|
||
if key == "起运港":
|
||
continue
|
||
seq += 1
|
||
other_optional_blocks.append(
|
||
_pending_field_block(seq, label, example, optional=True)
|
||
)
|
||
lines.extend(other_optional_blocks)
|
||
return "\n".join(lines)
|
||
|
||
|
||
def ask_missing(display_names: Iterable[str]) -> str:
|
||
"""兼容旧调用:仅有展示名时仍走补问,但没有已识别回显。"""
|
||
alias = {
|
||
"起运地": "起运港",
|
||
"目的地": "目的港",
|
||
"起运地(机场名或三字码)": "起运港",
|
||
"目的地(机场名或三字码)": "目的港",
|
||
"毛重": "毛重",
|
||
"重量": "毛重",
|
||
"重量(KG)": "毛重",
|
||
"重量(KG)": "毛重",
|
||
"体积": "体积",
|
||
"体积(CBM)": "体积",
|
||
"体积(CBM)": "体积",
|
||
"包装类型": "包装方式",
|
||
"包装方式": "包装方式",
|
||
"货物数量": "货量",
|
||
"货量": "货量",
|
||
"整柜或拼柜": "整柜或拼柜",
|
||
"箱型箱量": "箱型箱量",
|
||
}
|
||
keys: list[str] = []
|
||
for name in display_names:
|
||
key = alias.get(name, name)
|
||
if key and key not in keys and key != "报价日期":
|
||
keys.append(key)
|
||
return ask_clarify(facts={}, missing_keys=keys, transport_mode="空运")
|
||
|
||
|
||
def _keep_or_add_unit(raw: str, known: tuple[str, ...], default: str) -> str:
|
||
"""
|
||
销售侧单位原样回显。
|
||
|
||
写了 1.5KG 就回 1.5KG,禁止改成 1.5KGS。没写单位才补 default(可空)。
|
||
"""
|
||
text = (raw or "").strip()
|
||
if not text:
|
||
return "-"
|
||
upper = text.upper()
|
||
for unit in known:
|
||
if upper.endswith(unit.upper()):
|
||
return text
|
||
if default and not any(ch.isalpha() or ch in "立方件" for ch in text):
|
||
return f"{text}{default}"
|
||
return text
|
||
|
||
|
||
def _with_unit(raw: str, suffix: str) -> str:
|
||
"""兼容旧调用:已带单位则原样。重量不要走这里补 KGS。"""
|
||
if suffix.upper() == "KGS":
|
||
return _keep_or_add_unit(raw, ("KGS", "KG", "公斤", "千克"), "")
|
||
if suffix.upper() == "CBM":
|
||
return _keep_or_add_unit(raw, ("CBM", "M3", "M³", "立方米", "立方", "方"), "")
|
||
return _keep_or_add_unit(raw, (suffix,), suffix)
|
||
|
||
|
||
def inquiry_field_lines(
|
||
*,
|
||
work_order_no: str,
|
||
facts: dict[str, str],
|
||
transport_mode: str = "空运",
|
||
) -> list[str]:
|
||
"""
|
||
询价确认字段行(销售可见)。
|
||
|
||
起运/目的展示销售原文:说南京就南京,图上/原话是 SZX 就展示 SZX。
|
||
originCode 只给出站 TMS,不拿来改写成城市名再给销售看。
|
||
"""
|
||
origin = (facts.get("起运港") or facts.get("起运地") or "-").strip()
|
||
dest = (facts.get("目的港") or facts.get("目的地") or "-").strip()
|
||
cargo = (facts.get("品名") or "-").strip() or "-"
|
||
pack = (facts.get("包装方式") or facts.get("包装类型") or "-").strip() or "-"
|
||
from agent.schema.field_validate import is_blank_quote_date
|
||
|
||
quote_date = (facts.get("报价日期") or "").strip()
|
||
if is_blank_quote_date(quote_date):
|
||
quote_date = ""
|
||
mode = (transport_mode or "").strip()
|
||
if mode == "海运":
|
||
qty = (facts.get("货量") or facts.get("货物数量") or "-").strip() or "-"
|
||
load = (facts.get("整柜或拼柜") or "-").strip() or "-"
|
||
box = (facts.get("箱型箱量") or "-").strip() or "-"
|
||
trade = (facts.get("贸易条款") or "-").strip() or "-"
|
||
klass = (facts.get("运输分类") or "-").strip() or "-"
|
||
lines = [
|
||
f"工单号:{work_order_no}",
|
||
f"运输方式:{transport_mode}",
|
||
f"起运港:{origin}",
|
||
f"目的港:{dest}",
|
||
f"品名:{cargo}",
|
||
f"货物数量:{qty}",
|
||
f"整柜或拼柜:{load}",
|
||
]
|
||
# 拼柜不指定箱型:整行不出现,避免建单摘要/群摘要出现「箱型箱量:-」。
|
||
if not is_sea_lcl(load):
|
||
lines.append(f"箱型箱量:{box}")
|
||
lines.extend(
|
||
[
|
||
f"贸易条款:{trade}",
|
||
f"运输分类:{klass}",
|
||
f"报价日期:{quote_date or '-'}",
|
||
]
|
||
)
|
||
# 提货地址/派送地址非必填:识别到才多一行,没写不占位、不写成「-」。
|
||
for key, label in SEA_OPTIONAL_FIELDS:
|
||
val = (facts.get(key) or "").strip()
|
||
if not val or val in {"-", "—", "-"}:
|
||
continue
|
||
lines.append(f"{label}:{val}")
|
||
return lines
|
||
if mode == "陆运":
|
||
land_type = (facts.get("运输类型") or "").strip()
|
||
lines = [
|
||
f"工单号:{work_order_no}",
|
||
f"运输方式:{transport_mode}",
|
||
f"运输类型:{land_type or '-'}",
|
||
f"线路类别:{(facts.get('线路类别') or '-').strip() or '-'}",
|
||
f"运输分类:{(facts.get('运输分类') or '-').strip() or '-'}",
|
||
]
|
||
for key in _land_cargo_keys(land_type, facts):
|
||
label = LAND_DISPLAY.get(key, key)
|
||
val = (facts.get(key) or "-").strip() or "-"
|
||
lines.append(f"{label}:{val}")
|
||
if land_type in _LAND_CUSTOMS_TYPES:
|
||
lines.append(f"通关口岸:{(facts.get('通关口岸') or '').strip()}")
|
||
# 客户名称是询价非必填:有值才占行,没填不写「-」,避免销售以为漏填。
|
||
customer = (facts.get("客户名称") or "").strip()
|
||
if customer and customer not in {"-", "—", "-"}:
|
||
lines.append(f"客户名称:{customer}")
|
||
return lines
|
||
# 空运建单摘要:必填照常列;非必填(起运地/尺寸/比例/报价日期/增值服务)有值才占行。
|
||
# 与核对卡一致:确定后不能把已识别的尺寸/比例丢掉。
|
||
lines = [
|
||
f"工单号:{work_order_no}",
|
||
f"运输方式:{transport_mode}",
|
||
]
|
||
if origin and origin not in {"-", "—", "-"}:
|
||
lines.append(f"起运地:{origin}")
|
||
lines.extend(
|
||
[
|
||
f"目的地:{dest}",
|
||
f"品名:{cargo}",
|
||
f"件数:{_with_unit(str(facts.get('件数') or ''), '件')}",
|
||
f"重量(KG):{_with_unit(str(facts.get('毛重') or ''), 'KGS')}",
|
||
f"体积(CBM):{_with_unit(str(facts.get('体积') or ''), 'CBM')}",
|
||
f"包装类型:{pack}",
|
||
]
|
||
)
|
||
# 建单确认和空运群:有内容才多一行。没填不出现。
|
||
added = (facts.get("added_services") or facts.get("增值服务") or "").strip()
|
||
if added and added not in {"-", "—", "-"}:
|
||
lines.append(f"增值服务:{added}")
|
||
for key, label in AIR_OPTIONAL_FIELDS:
|
||
# 起运地已在上方;增值服务已按双键处理,避免重复行
|
||
if key in {"起运港", "added_services"}:
|
||
continue
|
||
opt_val = (facts.get(key) or "").strip()
|
||
if not opt_val or opt_val in {"-", "—", "-"}:
|
||
continue
|
||
lines.append(f"{label}:{opt_val}")
|
||
if quote_date:
|
||
lines.append(f"报价日期:{quote_date}")
|
||
return lines
|
||
|
||
|
||
def inquiry_card(
|
||
*,
|
||
work_order_no: str,
|
||
facts: dict[str, str],
|
||
first_or_same: str = "first",
|
||
transport_mode: str = "空运",
|
||
) -> str:
|
||
"""
|
||
建单后的询价确认(核对卡点「确定」后发出的那一条)。
|
||
|
||
产品口径(2026-11-22 真机反馈):确定后不再重复字段段,只保留
|
||
「工单号:WO…」+「工单已创建,正在查询TMS报价。」两行。
|
||
原因:销售在点确定前的核对卡上已经逐项核对过;建单后再列一遍,
|
||
销售会以为还有字段没确认,且手机上消息很长。字段展示逻辑仍保留在
|
||
`inquiry_field_lines`,核对卡、拉群摘要、报价方案卡继续用它。
|
||
|
||
无标题、无 [test]。facts / first_or_same / transport_mode 仅为兼容
|
||
旧调用方签名保留,不再参与这里的文案。
|
||
"""
|
||
_ = (facts, first_or_same, transport_mode)
|
||
no = (work_order_no or "").strip()
|
||
return "\n".join([f"工单号:{no}", "工单已创建,正在查询TMS报价。"])
|
||
|
||
|
||
def canonical_button(raw: str) -> str:
|
||
"""把销售点卡 / 原文 / 企微 EventKey 收成标准按钮文案;对不上返回空。"""
|
||
text = (raw or "").strip().strip("【】")
|
||
if text in {
|
||
BTN_SKIP_COLLAB,
|
||
BTN_SKIP_COLLAB_OLD,
|
||
BTN_SKIP_COLLAB_TMS,
|
||
BTN_SKIP_COLLAB_KEY,
|
||
}:
|
||
return BTN_SKIP_COLLAB
|
||
# 新卡 EventKey 带工单号:skip_collab:WO…,避免点旧卡落到最新单
|
||
if text.startswith(f"{BTN_SKIP_COLLAB_KEY}:"):
|
||
return BTN_SKIP_COLLAB
|
||
if text in {BTN_PULL_COLLAB, BTN_PULL_COLLAB_KEY} or text.startswith(
|
||
f"{BTN_PULL_COLLAB_KEY}:"
|
||
):
|
||
return BTN_PULL_COLLAB
|
||
if text in {BTN_HANDOFF_LAND, BTN_HANDOFF_LAND_KEY} or text.startswith(
|
||
f"{BTN_HANDOFF_LAND_KEY}:"
|
||
):
|
||
return BTN_HANDOFF_LAND
|
||
if text in {BTN_LAND_OPTIONS, BTN_LAND_OPTIONS_KEY} or text.startswith(
|
||
f"{BTN_LAND_OPTIONS_KEY}:"
|
||
):
|
||
return BTN_LAND_OPTIONS
|
||
if text in {BTN_ADOPT_EXCEL, BTN_ADOPT_EXCEL_CARD, BTN_ADOPT_EXCEL_KEY} or text.startswith(
|
||
f"{BTN_ADOPT_EXCEL_KEY}:"
|
||
):
|
||
return BTN_ADOPT_EXCEL
|
||
if text in {BTN_ADOPT_PDF, BTN_ADOPT_PDF_CARD, BTN_ADOPT_PDF_KEY} or text.startswith(
|
||
f"{BTN_ADOPT_PDF_KEY}:"
|
||
):
|
||
return BTN_ADOPT_PDF
|
||
if text in {BTN_REJECT_KEY} or text.startswith(f"{BTN_REJECT_KEY}:"):
|
||
return BTN_REJECT
|
||
if text in {BTN_COPY_TICKET, BTN_COPY_TICKET_KEY} or text.startswith(
|
||
f"{BTN_COPY_TICKET_KEY}:"
|
||
):
|
||
return BTN_COPY_TICKET
|
||
if text in {BTN_DEAL, BTN_DEAL_KEY} or text.startswith(f"{BTN_DEAL_KEY}:"):
|
||
return BTN_DEAL
|
||
if text in {BTN_LOST, BTN_LOST_KEY} or text.startswith(f"{BTN_LOST_KEY}:"):
|
||
return BTN_LOST
|
||
if text in {BTN_NEGOTIATE, BTN_NEGOTIATE_KEY} or text.startswith(
|
||
f"{BTN_NEGOTIATE_KEY}:"
|
||
):
|
||
return BTN_NEGOTIATE
|
||
if text == BTN_SKIP_COLLAB_DONE:
|
||
return ""
|
||
for btn in (BTN_ADOPT_EXCEL, BTN_ADOPT_PDF, BTN_REJECT, BTN_DEAL, BTN_LOST, BTN_NEGOTIATE):
|
||
if text == btn:
|
||
return btn
|
||
return ""
|
||
|
||
|
||
def extract_work_order_no(text: str) -> str:
|
||
"""从任意原话里抽出 WO/VT + 12 位数字;没有则空。不判断意图。"""
|
||
hit = _WO_NO.search(text or "")
|
||
return hit.group(0).upper() if hit else ""
|
||
|
||
|
||
def card_task_id(kind: str, work_order_no: str) -> str:
|
||
"""
|
||
每次出卡一个新 task_id。企微 42014:同一 task_id 不能再发,会整卡失败只剩文字。
|
||
|
||
工单号仍嵌在前缀里,点卡回调靠 work_order_from_card_meta 找回。
|
||
"""
|
||
stamp = f"{int(time.time() * 1000):x}{uuid.uuid4().hex[:4]}"
|
||
return f"{kind}-{work_order_no}-{stamp}"
|
||
|
||
|
||
def looks_like_continue_ticket(text: str) -> bool:
|
||
"""
|
||
无网夹具用的弱提示。正式环境必须由大模型判续办意图。
|
||
禁止把本函数当成生产唯一路由。
|
||
"""
|
||
raw = (text or "").strip()
|
||
if not raw:
|
||
return False
|
||
return any(token in raw for token in _CONTINUE_HINT)
|
||
|
||
|
||
def work_order_from_card_meta(
|
||
card_meta: dict[str, Any] | None = None,
|
||
*,
|
||
text: str = "",
|
||
) -> str:
|
||
"""
|
||
从企微点卡回调用找回「点的是哪一张卡当时的工单」。
|
||
|
||
优先 TaskId / EventKey 里的工单号(quote-WO…、scheme-adopt_excel-WO…)。
|
||
对不上返回空:调用方按该销售当前最新书签处理。
|
||
"""
|
||
src = card_meta or {}
|
||
for raw in (
|
||
str(src.get("task_id") or "").strip(),
|
||
str(src.get("event_key") or "").strip(),
|
||
(text or "").strip(),
|
||
):
|
||
no = _work_order_in_token(raw)
|
||
if no:
|
||
return no
|
||
return ""
|
||
|
||
|
||
def deal_card_version(card_meta: dict[str, Any] | None = None, *, text: str = "") -> int:
|
||
"""从成交卡 EventKey 取出 vN;没有则 0(旧私聊卡)。"""
|
||
src = card_meta or {}
|
||
for raw in (
|
||
str(src.get("event_key") or ""),
|
||
str(src.get("task_id") or ""),
|
||
text or "",
|
||
):
|
||
hit = re.search(r":v(\d+)\s*$", raw)
|
||
if hit:
|
||
return int(hit.group(1))
|
||
return 0
|
||
|
||
|
||
def _work_order_in_token(raw: str) -> str:
|
||
"""从 TaskId / EventKey 里抽出 WO 工单号;没有则空。"""
|
||
text = (raw or "").strip()
|
||
if not text:
|
||
return ""
|
||
for prefix in (
|
||
f"{BTN_SKIP_COLLAB_KEY}:",
|
||
f"{BTN_PULL_COLLAB_KEY}:",
|
||
f"{BTN_ADOPT_EXCEL_KEY}:",
|
||
f"{BTN_ADOPT_PDF_KEY}:",
|
||
f"{BTN_REJECT_KEY}:",
|
||
f"{BTN_COPY_TICKET_KEY}:",
|
||
f"{BTN_DEAL_KEY}:",
|
||
f"{BTN_LOST_KEY}:",
|
||
f"{BTN_NEGOTIATE_KEY}:",
|
||
):
|
||
if text.startswith(prefix):
|
||
rest = text[len(prefix) :].strip()
|
||
if rest.upper().startswith("WO"):
|
||
return extract_work_order_no(rest) or rest.split("-", 1)[0]
|
||
# 卡片 task_id 会带时间后缀(scheme-WO…-abc),只取 WO+12 位。
|
||
return extract_work_order_no(text)
|
||
|
||
|
||
_CURRENCY_PREFIX = re.compile(r"^[A-Za-z]{3}\b")
|
||
|
||
|
||
def money_text(amount: Any, currency: str = "") -> str:
|
||
"""
|
||
销售可见金额:TMS 有币种就写成「CNY 14669.60」。
|
||
|
||
金额已带三字码前缀则原样。没有币种不编造 CNY/USD。
|
||
"""
|
||
raw = str(amount if amount not in (None, "") else "").strip()
|
||
if not raw or raw == "-":
|
||
return raw or "-"
|
||
if _CURRENCY_PREFIX.match(raw):
|
||
return raw
|
||
code = str(currency or "").strip().upper()
|
||
if code == "RMB":
|
||
code = "CNY"
|
||
if not code:
|
||
return raw
|
||
return f"{code} {raw}"
|
||
|
||
|
||
# TMS 计价单位常见写法的归一表。
|
||
# 陆运 TMS 会把「KG / KGS / 公斤 / 元/KG」混着回,销售侧统一成 KG、CBM、票、台、柜、板、点;
|
||
# 认不出的单位原样返回,禁止在这里编造 TMS 没给的单位。
|
||
_UNIT_ALIASES = {
|
||
"KG": "KG",
|
||
"KGS": "KG",
|
||
"KGM": "KG",
|
||
"公斤": "KG",
|
||
"千克": "KG",
|
||
"CBM": "CBM",
|
||
"M3": "CBM",
|
||
"立方": "CBM",
|
||
"立方米": "CBM",
|
||
"票": "票",
|
||
"SHIPMENT": "票",
|
||
"BL": "票",
|
||
"台": "台",
|
||
"辆": "台",
|
||
"柜": "柜",
|
||
"板": "板",
|
||
"板数": "板",
|
||
"点": "点",
|
||
}
|
||
|
||
|
||
def fee_unit(raw: Any) -> str:
|
||
"""
|
||
TMS 计价单位归一成销售可读的一小段(KG / CBM / 票 / 台 …)。
|
||
|
||
参数:raw 为 TMS 费用行上的 unit / unitName / chargeUnit 原文。
|
||
返回:归一后的单位;空串表示 TMS 没给单位,调用方就不写「/单位」。
|
||
副作用:无。线程:纯函数。
|
||
「元/KG」「每公斤」「KGS」都收成 KG;认不出的去掉前导斜杠原样给,禁止编。
|
||
"""
|
||
text = str(raw if raw is not None else "").strip()
|
||
if not text or text in {"-", "None", "null"}:
|
||
return ""
|
||
token = text.upper().replace("/", "").replace("每", "").replace(" ", "")
|
||
# 「元/KG」「RMBCBM」这类带币种前缀的写法,币种不是单位,去掉再认。
|
||
token = token.replace("元", "").replace("RMB", "").replace("CNY", "")
|
||
if token in _UNIT_ALIASES:
|
||
return _UNIT_ALIASES[token]
|
||
# 长句里夹带单位(如「每票/KGS」)时按后缀兜底,仍只认常见计费单位。
|
||
for suffix, unit in (("KGS", "KG"), ("KG", "KG"), ("CBM", "CBM"), ("M3", "CBM")):
|
||
if token.endswith(suffix):
|
||
return unit
|
||
return text.lstrip("/").strip()
|
||
|
||
|
||
def fee_amount_text(
|
||
fee: dict[str, Any] | None,
|
||
header_ccy: str = "",
|
||
prefer_unit_price: bool = True,
|
||
) -> str:
|
||
"""
|
||
一条费用的销售可见金额,后面补 TMS 计价单位:CNY 13.00/KG。
|
||
|
||
参数:
|
||
fee TMS 费用行或已投影的 fee_lines 行(含 amount/currency/unit);
|
||
header_ccy 行上没有币种时的表头币种;
|
||
prefer_unit_price
|
||
行上同时有整段金额和单价时,是否改按单价展示。
|
||
TMS 陆运零担传 True:TMS 给的行金额是整段总额,
|
||
直接拼单位会变成「6500/KG」这种读不通的写法。
|
||
产品手工报价传 False:金额是产品自己敲的数,
|
||
必须原样展示(1000/车),不能被单价顶掉。
|
||
返回:金额文本;TMS 没给单位就与原来完全一致,只回金额。
|
||
为什么不自己乘除:主账已经把行金额和单价分开落库,文字侧只展示 TMS 给的数,
|
||
本地再算一遍容易和报价单对不上。
|
||
副作用:无。线程:纯函数。
|
||
"""
|
||
src = dict(fee or {})
|
||
amount = str(src.get("amount") if src.get("amount") is not None else src.get("value") or "").strip()
|
||
if not amount or amount == "-":
|
||
return amount or "-"
|
||
line_ccy = str(src.get("currency") or src.get("currencyCode") or header_ccy or "").strip()
|
||
# 费率/税率是百分比,加单位会变成「1%/KG」,不加。
|
||
name = str(src.get("name") or "").strip()
|
||
if "%" in amount or name.endswith("费率") or name in {"税率", "燃油附加费率"}:
|
||
return money_text(amount, line_ccy)
|
||
unit = fee_unit(src.get("unit") or src.get("unitName") or src.get("chargeUnit"))
|
||
if not unit:
|
||
return money_text(amount, line_ccy)
|
||
base = amount
|
||
unit_price = str(src.get("unit_price") or src.get("unitPrice") or "").strip()
|
||
if prefer_unit_price and unit_price and _differs_from_amount(unit_price, amount):
|
||
# 只有单价和总额确实不同才换成单价;相同时仍用金额原文,保留 TMS 给的 13.00 写法。
|
||
# 产品手工报价(prefer_unit_price=False)永远用自己敲的金额,不被单价顶掉。
|
||
base = unit_price
|
||
shown = money_text(base, line_ccy)
|
||
# 金额原文已经带了单位(如 TMS 回「13.00/KG」),不再重复拼一次。
|
||
if unit.upper() in shown.upper():
|
||
return shown
|
||
return f"{shown}/{unit}"
|
||
|
||
|
||
def _differs_from_amount(unit_price: str, amount: str) -> bool:
|
||
"""单价与行金额是否真的不同。认不出数字时不换,避免把 0 或怪值当单价。"""
|
||
try:
|
||
up = float(unit_price.replace(",", ""))
|
||
am = float(amount.replace(",", ""))
|
||
except (TypeError, ValueError):
|
||
return False
|
||
if up <= 0:
|
||
return False
|
||
return abs(up - am) > 0.0001
|
||
|
||
|
||
def _total_label(
|
||
quote: dict[str, Any] | None = None,
|
||
business_line: str = "",
|
||
transport_mode: str = "",
|
||
) -> str:
|
||
"""销售侧汇总行。企微卡片、文字明细、成交通知都叫费用合计。"""
|
||
src = dict(quote or {})
|
||
return total_price_label(
|
||
business_line or str(src.get("business_line") or ""),
|
||
transport_mode,
|
||
quote=src,
|
||
)
|
||
|
||
|
||
def _manual_land_default_unit(
|
||
quote: dict[str, Any],
|
||
*,
|
||
business_line: str = "",
|
||
transport_mode: str = "",
|
||
facts: dict[str, str] | None = None,
|
||
) -> str:
|
||
"""
|
||
人工报价只敲金额时,陆运整车补「车」;零担/拼车不补。
|
||
|
||
为什么只补整车(产品 2026-11-22 拍板):
|
||
* 整车口径唯一——一台车跑一趟一个价,「1000/车」不会理解错;
|
||
* 零担按公斤/立方/票/件计价的写法都常见,产品没写单位时系统猜哪一个都可能错,
|
||
把按公斤算出来的总额写成「1000/票」,客户按票下单就会亏。
|
||
认「整车」只看工单事实,不猜:运输类型/陆运业务类型落在整车枚举里,或有车型/数量。
|
||
分档单价、多车型价矩阵不补——那两种价格本身就带计量口径,再补「/车」会自相矛盾。
|
||
参数 facts 是工单询价字段;返回空串表示不该补。
|
||
副作用:无。线程:纯函数。
|
||
"""
|
||
src = dict(quote or {})
|
||
line = (business_line or "").strip().upper()
|
||
if line != "LAND" and scheme_transport_mode(transport_mode or "", fallback="") != "陆运":
|
||
return ""
|
||
if src.get("weightPrices") or src.get("vehicleRates") or src.get("vehicle_rates"):
|
||
return ""
|
||
fact = dict(facts or {})
|
||
for key in ("运输类型", "陆运业务类型", "运输分类"):
|
||
val = str(fact.get(key) or "").strip()
|
||
if val in _LAND_FTL_TYPES:
|
||
return "车"
|
||
if str(fact.get("车型/数量") or "").strip():
|
||
return "车"
|
||
return ""
|
||
|
||
|
||
def _manual_quote_lines(
|
||
manual: dict[str, Any],
|
||
*,
|
||
business_line: str = "",
|
||
transport_mode: str = "",
|
||
facts: dict[str, str] | None = None,
|
||
layout: str = "",
|
||
) -> list[str]:
|
||
"""
|
||
人工报价段:航司(有则先写)+ 费用行 + 总销售价,不写合计。
|
||
|
||
空运短句把航司写在报价对象上时,摘要必须露出,否则销售侧以为「TH 没识别」。
|
||
陆运整车且产品没写单位时,费用行补「/车」(产品 2026-11-22 拍板);
|
||
零担/拼车一律不补,宁可只显示金额也不把零担总额写成分车型价。
|
||
|
||
参数 layout="manual"(产品 2026-11-22 定的对外版式):
|
||
* 空运改走「价格方案如下:比例/重量等级/舱位信息 + 价格」,见 _air_manual_quote_lines;
|
||
* 陆运多车型价改成「车型/数量 + 运输费」竖排,见 _manual_land_vehicle_lines;
|
||
* 其余(海运、陆运单档价)行格式不变,只在末尾补一行「原始报价参考」。
|
||
销售调价不传 layout:调价是公司在原价上改的数字,不是产品原话,不摆「原始报价参考」。
|
||
"""
|
||
src = dict(manual or {})
|
||
mode = layout or ""
|
||
if mode == "manual":
|
||
if (business_line or "").strip().upper() == "AIR":
|
||
return _air_manual_quote_lines(src, facts=facts)
|
||
if _vehicle_rates_of(src):
|
||
return _manual_land_vehicle_lines(src)
|
||
lines: list[str] = []
|
||
airline = str(src.get("airlineCode") or src.get("airline") or "").strip()
|
||
if airline:
|
||
lines.append(f"航司:{airline}")
|
||
weight_break = str(src.get("weight_break") or "").strip()
|
||
if weight_break and not any(
|
||
weight_break in str(amount or "") for _name, amount in quote_fee_lines(src)
|
||
):
|
||
lines.append(f"重量分档:{weight_break}")
|
||
# 陆运多车型价:矩阵只在 landSegments/fee 之外,摘要不列出来销售就只看到一个合计,
|
||
# 会以为机器人把三档价合成了一档。
|
||
vehicle_rates = _vehicle_rates_of(src)
|
||
header_ccy = str(src.get("currency") or src.get("currencyCode") or "").strip()
|
||
if vehicle_rates:
|
||
shown = ";".join(
|
||
f"{model} {money_text(amount, header_ccy)}"
|
||
for model, amount in vehicle_rates.items()
|
||
)
|
||
lines.append(f"车型价:{shown}")
|
||
lines.extend(
|
||
f"{name}:{amount}"
|
||
for name, amount in quote_fee_lines(
|
||
src,
|
||
default_unit=_manual_land_default_unit(
|
||
src, business_line=business_line, transport_mode=transport_mode, facts=facts
|
||
),
|
||
)
|
||
)
|
||
remark = str(src.get("remark") or "").strip()
|
||
if remark and remark not in {"-", "—", "-"}:
|
||
lines.append(f"备注:{remark}")
|
||
# 陆运单档价/费用行:税与燃油口径也要在群摘要露出,销售才知道 3200 是否已乘燃油。
|
||
is_land = (business_line or "").strip().upper() == "LAND" or (
|
||
transport_mode or ""
|
||
).strip() in {"陆运", "LAND", "land"}
|
||
if is_land:
|
||
tax = str(src.get("tax_rate") or src.get("taxRate") or "").strip()
|
||
if tax and tax not in {"-", "—", "-"}:
|
||
lines.append(f"税率:{tax.rstrip('%')}")
|
||
fuel = str(src.get("fuel_surcharge_rate") or src.get("fuelSurchargeRate") or "").strip()
|
||
if fuel and fuel not in {"-", "—", "-"}:
|
||
lines.append(f"燃油费率:{fuel}")
|
||
total = money_text(str(src.get("total") or "-").strip() or "-", header_ccy)
|
||
if total and total != "-":
|
||
# 多车型价并存时合计只是一个展示值,必须注明,否则 13000 会被当成三档的总价。
|
||
suffix = "(分车型价见上,此处取第一档)" if vehicle_rates else ""
|
||
lines.append(f"{_total_label(src, business_line, transport_mode)}:{total}{suffix}")
|
||
elif not lines:
|
||
lines.append(f"{_total_label(src, business_line, transport_mode)}:{total or '-'}")
|
||
if mode == "manual":
|
||
# 原始报价参考只给空运/陆运人工报价(产品 2026-11-22 口径);
|
||
# 海运原话里常带码头/附加费等口语,不在这轮范围,保持原样。
|
||
if (business_line or "").strip().upper() in {"AIR", "LAND"}:
|
||
ref = _raw_quote_ref_line(src)
|
||
if ref:
|
||
lines.append(ref)
|
||
return lines
|
||
|
||
|
||
def _manual_layout_self_titled(quote: dict[str, Any], *, business_line: str = "") -> bool:
|
||
"""
|
||
新版式是否自带标题。
|
||
|
||
空运写「价格方案如下:」、陆运多车型写「车型/数量:」,标题已说明这是产品报价,
|
||
再叠一行「人工报价:」就是重复;海运与单档陆运的小标题保持原样。
|
||
注意:这个判断只在上面已经有 TMS 段时才成立。TMS 无价、段落被整段省掉时,
|
||
群摘要开头就是价格方案,调用方必须自己补「人工报价:」,不能靠本函数。
|
||
副作用:无。线程:纯函数。
|
||
"""
|
||
if (business_line or "").strip().upper() == "AIR":
|
||
return True
|
||
return bool(_vehicle_rates_of(quote or {}))
|
||
|
||
|
||
def _raw_quote_ref_line(quote: dict[str, Any]) -> str:
|
||
"""
|
||
「原始报价参考:<产品原话>」。
|
||
|
||
产品 2026-11-22 要求人工报价摘要必须附原话,销售才敢照抄给客户。
|
||
原话只在写入时留档(quote["raw_text"]);没有就不写这一行,禁止拿拼出来的摘要冒充原话。
|
||
2026-11-22 补充:@机器人 只是叫机器人的称呼,不是报价内容,展示前必须剥掉;
|
||
历史单子里已经存进了「@智价通AIQ」,所以在这里再剥一次,不必回改老数据。
|
||
副作用:无。线程:纯函数。
|
||
"""
|
||
src = dict(quote or {})
|
||
raw = str(src.get("raw_text") or src.get("rawText") or "").strip()
|
||
if not raw or raw in {"-", "—", "-"}:
|
||
return ""
|
||
from agent.policy.bot_name import strip_mention_keep_layout
|
||
|
||
raw = strip_mention_keep_layout(raw).strip()
|
||
# 称呼被剥掉后句首可能只剩标点(「@机器人,1:300…」):一并去掉,别让销售读到半句。
|
||
raw = raw.lstrip(",,;;、:: ")
|
||
if not raw or raw in {"-", "—", "-"}:
|
||
return ""
|
||
return f"原始报价参考:{raw}"
|
||
|
||
|
||
def _month_day_text(value: str) -> str:
|
||
"""
|
||
日期只去掉前导零:2026-11-02 → 2026-11-2。
|
||
|
||
为什么这样做:空运报价摘要照产品原话「11月2日」的写法给销售,月日不补零;
|
||
认不出标准日期就原样返回,不做任何猜测(可能本来就是「11月2日」)。
|
||
副作用:无。线程:纯函数。
|
||
"""
|
||
raw = str(value or "").strip()
|
||
hit = re.fullmatch(r"(\d{4})-(\d{1,2})-(\d{1,2})", raw)
|
||
if not hit:
|
||
return raw
|
||
return f"{hit.group(1)}-{int(hit.group(2))}-{int(hit.group(3))}"
|
||
|
||
|
||
def _air_manual_quote_lines(
|
||
manual: dict[str, Any],
|
||
*,
|
||
facts: dict[str, str] | None = None,
|
||
) -> list[str]:
|
||
"""
|
||
空运人工报价对外版式(产品 2026-11-22 定稿):
|
||
|
||
价格方案如下:
|
||
比例:1:300
|
||
重量等级:+500
|
||
舱位信息:TH,NKG-KUL,2026-11-2起(包含航司、线路、班期、舱位日期哪天起),具体根据放舱情况而定
|
||
|
||
价格
|
||
空运费:USD11/kg
|
||
报价有效期:2026-11-2
|
||
原始报价参考:<产品原话>
|
||
|
||
为什么这样排:比例/重量等级决定客户能不能用这档价,必须排在价格前面;
|
||
舱位信息把航司、线路、日期合成一句,销售可以直接复制给客户;
|
||
「具体根据放舱情况而定」是公司对外的固定口径,人工报价一律带上,避免销售乱承诺舱位。
|
||
缺哪个字段就不写哪一行,不拿横杠占位、不编日期。
|
||
副作用:无。线程:纯函数。
|
||
"""
|
||
src = dict(manual or {})
|
||
fact = dict(facts or {})
|
||
lines: list[str] = ["价格方案如下:"]
|
||
ratio = str(src.get("ratio") or fact.get("比例") or "").strip()
|
||
if ratio and ratio not in {"-", "—", "-"}:
|
||
lines.append(f"比例:{ratio}")
|
||
weight_break = str(src.get("weight_break") or fact.get("重量等级") or "").strip()
|
||
if weight_break and weight_break not in {"-", "—", "-"}:
|
||
lines.append(f"重量等级:{weight_break}")
|
||
chargeable = _air_chargeable_line(src, fact)
|
||
if chargeable:
|
||
lines.append(chargeable)
|
||
cabin = _air_cabin_line(src, fact)
|
||
if cabin:
|
||
lines.append(cabin)
|
||
lines.append("")
|
||
lines.append("价格")
|
||
lines.extend(_air_manual_price_rows(src))
|
||
valid = _air_manual_valid_date(src, fact)
|
||
if valid:
|
||
lines.append(f"报价有效期:{valid}")
|
||
ref = _raw_quote_ref_line(src)
|
||
if ref:
|
||
lines.append(ref)
|
||
return lines
|
||
|
||
|
||
def _air_chargeable_line(manual: dict[str, Any], facts: dict[str, str]) -> str:
|
||
"""
|
||
人工报价里的「计费重」一行(产品口径 2026-11-22 定稿,当晚修正)。
|
||
|
||
写法:
|
||
计费重:848.87KG
|
||
计费重:500KG
|
||
计费重:800KG ← 报价对象上已经有数(TMS/航线明说)
|
||
|
||
取数顺序:
|
||
1. 报价对象上的 chargeableWeightKg(航线自己写了就以他为准);
|
||
2. 事实里的「计费重」;
|
||
3. 按体积重(CBM×166.7)与毛重取大。
|
||
|
||
为什么不再写「(按体积重)/(按实重)」:
|
||
产品 2026-11-22 晚在空运协同群核对后确定,这一行只写数字;
|
||
销售对客报价和报价单格子都只需要 KG 数,括注会让客户追问计价依据。
|
||
依据(basis)仍由 air_chargeable 在内部返回,供排查/落库用,不上屏。
|
||
|
||
TMS 没报价时销售只看到「USD11/kg」,不知道整票多少钱;有计费重才敢照抄给客户。
|
||
读不到体积也读不到毛重时不写这一行,禁止编数字。
|
||
副作用:无。线程:纯函数。
|
||
"""
|
||
from agent.schema.air_chargeable import compute_chargeable, format_kg, with_kg_unit
|
||
|
||
src = dict(manual or {})
|
||
fact = dict(facts or {})
|
||
for key in ("chargeableWeightKg", "chargeable_weight_kg", "chargeableWeight"):
|
||
text = str(src.get(key) or "").strip()
|
||
if text and text not in {"-", "—", "-"}:
|
||
return f"计费重:{with_kg_unit(text)}"
|
||
computed = compute_chargeable(fact)
|
||
if computed is None:
|
||
return ""
|
||
return "计费重:" + format_kg(computed.get("chargeable_kg")) + "KG"
|
||
|
||
|
||
def _air_cabin_line(manual: dict[str, Any], facts: dict[str, str]) -> str:
|
||
"""
|
||
「舱位信息:TH,NKG-KUL,2026-11-2起(包含航司、线路、班期、舱位日期哪天起),具体根据放舱情况而定」。
|
||
|
||
括号里那句是产品 2026-11-22 明确要求保留的说明(截图核对过),
|
||
告诉客户这行涵盖航司、线路、班期、舱位日期四件事,不能省。
|
||
航司优先报价对象上的代码(TH),再退工单字段;线路用起运港-目的港;
|
||
班期(如 D2345)有才拼进去。日期取自报价日期,写成「...起」。
|
||
四者全缺时返回空串——只写半句说明和「具体根据放舱情况而定」没有信息量。
|
||
副作用:无。线程:纯函数。
|
||
"""
|
||
src = dict(manual or {})
|
||
fact = dict(facts or {})
|
||
airline = str(
|
||
src.get("airlineCode") or src.get("airline") or fact.get("航司") or ""
|
||
).strip()
|
||
origin = str(fact.get("起运港") or fact.get("起运地") or "").strip()
|
||
dest = str(fact.get("目的港") or fact.get("目的地") or "").strip()
|
||
route = ""
|
||
if origin and dest and origin not in {"-", "—", "-"} and dest not in {"-", "—", "-"}:
|
||
route = f"{origin}-{dest}"
|
||
if not route:
|
||
route = str(src.get("route_line") or src.get("route") or "").strip()
|
||
schedule = str(
|
||
src.get("schedule") or src.get("frequency") or fact.get("班期") or ""
|
||
).strip()
|
||
parts = [p for p in (airline, route, schedule) if p and p not in {"-", "—", "-"}]
|
||
date = _month_day_text(str(fact.get("报价日期") or "").strip())
|
||
if date and date not in {"-", "—", "-"}:
|
||
parts.append(f"{date}起")
|
||
if not parts:
|
||
return ""
|
||
# 括号说明固定跟在航司/线路/日期之后,不参与上面的「缺项就不写」判断。
|
||
head = ",".join(parts)
|
||
return f"舱位信息:{head}{_AIR_CABIN_SCOPE_NOTE},具体根据放舱情况而定"
|
||
|
||
|
||
def _air_manual_price_rows(manual: dict[str, Any]) -> list[str]:
|
||
"""
|
||
空运人工报价的「价格」行:空运费:USD11/kg。
|
||
|
||
为什么不用 quote_fee_lines:那边写成「单价USD11/KG(+500)」,是给 TMS 摘要用的;
|
||
产品定的版式是「费用名:币种单价/单位」,重量等级已经单独一行,不再重复括号。
|
||
单价优先于行金额(产品按公斤报价时 amount 和 unit_price 相同也能写对);
|
||
单位 KG/KGS 统一小写成 kg。没有费用行时退回总价一行。
|
||
副作用:无。线程:纯函数。
|
||
"""
|
||
src = dict(manual or {})
|
||
header_ccy = str(src.get("currency") or src.get("currencyCode") or "").strip()
|
||
raw = src.get("fee_lines") or src.get("fee_rows")
|
||
parsed: Any = raw
|
||
if isinstance(raw, str) and raw.strip():
|
||
try:
|
||
parsed = json.loads(raw)
|
||
except json.JSONDecodeError:
|
||
parsed = None
|
||
rows: list[str] = []
|
||
if isinstance(parsed, list):
|
||
for item in parsed:
|
||
if not isinstance(item, dict):
|
||
continue
|
||
name = str(item.get("name") or item.get("label") or "").strip() or "空运费"
|
||
amount = str(item.get("amount") or item.get("value") or "").strip()
|
||
ccy = str(item.get("currency") or item.get("currencyCode") or header_ccy).strip()
|
||
unit_price = str(item.get("unit_price") or item.get("unitPrice") or "").strip()
|
||
base = unit_price if unit_price else amount
|
||
if not base or base == "-":
|
||
continue
|
||
shown = f"{ccy}{base}" if ccy else base
|
||
unit = str(item.get("unit") or item.get("unitName") or "").strip()
|
||
token = unit.upper().replace("/", "").replace("每", "").replace(" ", "")
|
||
if token in {"KG", "KGS", "公斤", "千克"}:
|
||
shown = f"{shown}/kg"
|
||
elif unit:
|
||
shown = f"{shown}/{unit}"
|
||
rows.append(f"{name}:{shown}")
|
||
if rows:
|
||
return rows
|
||
total = str(src.get("total") or "").strip()
|
||
if total and total != "-":
|
||
return [f"空运费:{header_ccy}{total}" if header_ccy else f"空运费:{total}"]
|
||
return ["空运费:-"]
|
||
|
||
|
||
def _air_manual_valid_date(manual: dict[str, Any], facts: dict[str, str]) -> str:
|
||
"""
|
||
人工报价的「报价有效期」。
|
||
|
||
空运人工报价没有 TMS 的 validUntil,产品口头给的日期就是这一次的报价依据,
|
||
因此取报价对象上的 validUntil,没有就用工单「报价日期」。
|
||
两者都没有返回空串,摘要不写这一行——宁可不写,也不编一个有效期出来。
|
||
副作用:无。线程:纯函数。
|
||
"""
|
||
src = dict(manual or {})
|
||
fact = dict(facts or {})
|
||
for key in ("validUntil", "valid_until", "validUntilDate"):
|
||
val = str(src.get(key) or "").strip()
|
||
if val and val not in {"-", "—", "-"}:
|
||
return _month_day_text(val)
|
||
val = str(fact.get("报价日期") or "").strip()
|
||
if val and val not in {"-", "—", "-"}:
|
||
return _month_day_text(val)
|
||
return ""
|
||
|
||
|
||
def _manual_land_vehicle_lines(manual: dict[str, Any]) -> list[str]:
|
||
"""
|
||
陆运多车型人工报价竖排版式(产品 2026-11-22 定稿):
|
||
|
||
车型/数量:7.6米
|
||
运输费:CNY13000元
|
||
...
|
||
税率:0
|
||
燃油费率:13%
|
||
备注:未含燃油13%
|
||
原始报价参考:<产品原话>
|
||
|
||
为什么竖排:三档车型价横排成一行,销售在手机上要来回对照才敢报;竖排一段一价不会看串。
|
||
「税率」只在抽出确切点数时写(如「含0点税」= 0);含税/不含税看不出点数就不写这行。
|
||
副作用:无。线程:纯函数。
|
||
"""
|
||
src = dict(manual or {})
|
||
header_ccy = str(src.get("currency") or src.get("currencyCode") or "CNY").strip()
|
||
units = _vehicle_rate_units(src)
|
||
lines: list[str] = []
|
||
for model, amount in _vehicle_rates_of(src).items():
|
||
lines.append(f"车型/数量:{model}")
|
||
shown = _land_matrix_amount_text(amount, header_ccy)
|
||
# 产品原话写了「/车」就带着,销售才分得清整车价还是按公斤/按票。
|
||
unit = units.get(model) or ""
|
||
if unit and not shown.endswith(f"/{unit}"):
|
||
shown = f"{shown}/{unit}"
|
||
lines.append(f"运输费:{shown}")
|
||
tax = str(src.get("tax_rate") or src.get("taxRate") or "").strip()
|
||
if tax and tax not in {"-", "—", "-"}:
|
||
lines.append(f"税率:{tax.rstrip('%')}")
|
||
fuel = str(src.get("fuel_surcharge_rate") or src.get("fuelSurchargeRate") or "").strip()
|
||
if fuel and fuel not in {"-", "—", "-"}:
|
||
lines.append(f"燃油费率:{fuel}")
|
||
remark = str(src.get("remark") or "").strip()
|
||
# 「含0点税」这类备注和上面「税率:0」是同一句话说两遍;税率已写出来就不再单列。
|
||
# 「未含燃油13%」等真正的附加条件仍照常显示,不能被吞。
|
||
if remark and tax and _remark_restates_tax(remark):
|
||
remark = ""
|
||
if remark and remark not in {"-", "—", "-"}:
|
||
lines.append(f"备注:{remark}")
|
||
ref = _raw_quote_ref_line(src)
|
||
if ref:
|
||
lines.append(ref)
|
||
return lines
|
||
|
||
|
||
def _remark_restates_tax(remark: str) -> bool:
|
||
"""
|
||
备注是否只是把税率重复一遍(「含0税」/「含0点税」)。
|
||
|
||
陆运报价条件归一后,「含0点税」既写 税率=0 又写备注=含0税;
|
||
摘要里两行说的是同一件事,只保留税率。真正的条件(未含燃油13%)不在此列。
|
||
副作用:无。线程:纯函数。
|
||
"""
|
||
text = re.sub(r"\s+", "", remark or "")
|
||
return text in {"含0税", "含0点税"}
|
||
|
||
|
||
def _land_matrix_amount_text(amount: str, currency: str) -> str:
|
||
"""
|
||
车型价金额按产品原话写法:CNY13000元。
|
||
|
||
人民币加「元」,其他币种只写代码前缀(USD3200),不套用人民币量词。
|
||
币种缺失时只返回数字加「元」,不猜币种。
|
||
副作用:无。线程:纯函数。
|
||
"""
|
||
val = str(amount or "").strip()
|
||
if not val or val == "-":
|
||
return "-"
|
||
token = str(currency or "").strip().upper()
|
||
if not token:
|
||
return f"{val}元"
|
||
if token == "CNY":
|
||
return f"CNY{val}元"
|
||
return f"{token}{val}"
|
||
|
||
|
||
def _vehicle_rate_units(quote: dict[str, Any]) -> dict[str, str]:
|
||
"""
|
||
车型价各档自己写的计价单位 {车型: 单位},如 {"10T": "车"}。
|
||
|
||
只读报价里已抽好的 vehicle_fee_rows[].unit,认不出就空 dict;
|
||
禁止在这里猜「/车」——零担车型价按公斤/按票计价,猜错会写错口径。
|
||
副作用:无。线程:纯函数。
|
||
"""
|
||
src = dict(quote or {})
|
||
rows = src.get("vehicle_fee_rows")
|
||
if not isinstance(rows, list):
|
||
return {}
|
||
out: dict[str, str] = {}
|
||
for row in rows:
|
||
if not isinstance(row, dict):
|
||
continue
|
||
model = str(row.get("name") or "").strip()
|
||
unit = str(row.get("unit") or "").strip()
|
||
if model and unit:
|
||
out[model] = unit
|
||
return out
|
||
|
||
|
||
def _vehicle_rates_of(quote: dict[str, Any]) -> dict[str, str]:
|
||
"""
|
||
人工报价里的多车型价 {车型: 金额}。
|
||
|
||
先读报价顶层 vehicleRates/vehicle_rates,再退回 landSegments[0] 上的同名键;
|
||
字符串 JSON 也认(主账回读时可能是一段文本)。没有则空 dict。
|
||
副作用:无。
|
||
"""
|
||
src = dict(quote or {})
|
||
raw: Any = src.get("vehicleRates") or src.get("vehicle_rates")
|
||
if not raw:
|
||
segs = src.get("landSegments") or src.get("land_segments")
|
||
if isinstance(segs, list) and segs and isinstance(segs[0], dict):
|
||
raw = segs[0].get("vehicleRates") or segs[0].get("vehicle_rates")
|
||
if isinstance(raw, str) and raw.strip():
|
||
try:
|
||
raw = json.loads(raw)
|
||
except json.JSONDecodeError:
|
||
return {}
|
||
if not isinstance(raw, dict):
|
||
return {}
|
||
return {
|
||
str(key).strip(): str(val).strip()
|
||
for key, val in raw.items()
|
||
if str(key).strip() and str(val).strip()
|
||
}
|
||
|
||
|
||
def quote_fee_lines(
|
||
quote: dict[str, Any] | None,
|
||
default_unit: str = "",
|
||
) -> list[tuple[str, str]]:
|
||
"""
|
||
价格明细行:优先 TMS fee_lines,否则用运费/其它/合计兜底。
|
||
|
||
返回 (费用名, 金额) ,空金额不展示。金额带 TMS 币种,不编造。
|
||
有 unit + unit_price 且单位是 KG:展示「单价CNY11/KG」,与空运 TMS 摘要一致。
|
||
default_unit:行上没有任何单位时的兜底单位(只给陆运整车人工报价用,别处别传)。
|
||
"""
|
||
src = dict(quote or {})
|
||
header_ccy = str(src.get("currency") or src.get("currencyCode") or "").strip()
|
||
raw = src.get("fee_lines") or src.get("fee_rows")
|
||
parsed: Any = raw
|
||
if isinstance(raw, str) and raw.strip():
|
||
try:
|
||
parsed = json.loads(raw)
|
||
except json.JSONDecodeError:
|
||
parsed = None
|
||
lines: list[tuple[str, str]] = []
|
||
if isinstance(parsed, list):
|
||
for item in parsed:
|
||
if not isinstance(item, dict):
|
||
continue
|
||
name = str(item.get("name") or item.get("label") or "").strip()
|
||
amount = str(item.get("amount") or item.get("value") or "").strip()
|
||
line_ccy = str(item.get("currency") or item.get("currencyCode") or header_ccy).strip()
|
||
unit_raw = str(item.get("unit") or item.get("unitName") or "").strip() or default_unit
|
||
unit_price = str(item.get("unit_price") or item.get("unitPrice") or "").strip()
|
||
unit_token = unit_raw.upper().replace("/", "").replace("每", "").replace(" ", "")
|
||
weight_break = str(
|
||
item.get("weight_break") or src.get("weight_break") or ""
|
||
).strip()
|
||
if name and unit_price and unit_token in {"KG", "KGS", "公斤", "千克"}:
|
||
prefix = f"单价{line_ccy}{unit_price}" if line_ccy else f"单价{unit_price}"
|
||
shown = f"{prefix}/KG"
|
||
# +500 是重量分档,跟在单价后,禁止再单开「其它费用」。
|
||
if weight_break:
|
||
shown = f"{shown}({weight_break})"
|
||
lines.append((name, shown))
|
||
continue
|
||
if name and amount and amount != "-":
|
||
# 燃油附加费率 / 税率是百分比,禁止套成「CNY 1%」。
|
||
if "%" in amount or name.endswith("费率") or name in {"税率", "燃油附加费率"}:
|
||
shown = amount if "%" in amount else f"{amount}%"
|
||
lines.append((name, shown))
|
||
else:
|
||
# 产品文字报价里自己写了单位(1000/车、13元/KG)就带上;
|
||
# 没写单位不猜(TMS 也不给就无法知道是整车还是每公斤)。
|
||
# prefer_unit_price=False:行金额是产品敲的数,不能被单价顶掉。
|
||
lines.append(
|
||
(
|
||
name,
|
||
fee_amount_text(
|
||
{
|
||
"name": name,
|
||
"amount": amount,
|
||
"currency": line_ccy,
|
||
"unit": unit_raw,
|
||
},
|
||
line_ccy,
|
||
prefer_unit_price=False,
|
||
),
|
||
)
|
||
)
|
||
elif name and unit_price:
|
||
lines.append((name, money_text(unit_price, line_ccy)))
|
||
if lines:
|
||
return lines
|
||
freight = str(src.get("freight") or "").strip()
|
||
other = str(src.get("other_fee") or "").strip()
|
||
if freight and freight != "-":
|
||
lines.append(("空运费", money_text(freight, header_ccy)))
|
||
if other and other != "-":
|
||
lines.append(("其他附加费", money_text(other, header_ccy)))
|
||
return lines
|
||
|
||
|
||
_SEA_FEE_BUCKETS = (
|
||
("polCostItems", "始发港费用", "始发港费用小计"),
|
||
("oceanCostItems", "海运费", "海运费小计"),
|
||
("podCostItems", "目的港费用", "目的港费用小计"),
|
||
)
|
||
|
||
|
||
def _parse_fee_bucket(quote: dict[str, Any], key: str) -> list[dict[str, Any]]:
|
||
"""读 polCostItems / fee_rows_polCostItems,字符串 JSON 也认。"""
|
||
raw = quote.get(key) or quote.get("fee_rows_" + key)
|
||
if isinstance(raw, str) and raw.strip():
|
||
try:
|
||
raw = json.loads(raw)
|
||
except json.JSONDecodeError:
|
||
return []
|
||
if not isinstance(raw, list):
|
||
return []
|
||
return [row for row in raw if isinstance(row, dict)]
|
||
|
||
|
||
def sea_bucket_fee_lines(quote: dict[str, Any] | None) -> list[str]:
|
||
"""
|
||
海运三段正文。有任一段桶才用;否则返回空列表,调用方回退扁平 fee_lines。
|
||
|
||
费目按该条币种金额原样;小计用 fee_total_* + 表头结算币种。本地不折算。
|
||
"""
|
||
src = dict(quote or {})
|
||
header_ccy = str(src.get("currency") or src.get("currencyCode") or "").strip()
|
||
sections: list[str] = []
|
||
any_bucket = False
|
||
for key, title, subtotal_label in _SEA_FEE_BUCKETS:
|
||
rows = _parse_fee_bucket(src, key)
|
||
subtotal = str(src.get("fee_total_" + key) or "").strip()
|
||
if not rows and not subtotal:
|
||
continue
|
||
any_bucket = True
|
||
sections.append(title)
|
||
for row in rows:
|
||
name = str(row.get("name") or row.get("label") or row.get("feeName") or "").strip()
|
||
amount = str(row.get("amount") or row.get("value") or "").strip()
|
||
line_ccy = str(row.get("currency") or row.get("currencyCode") or header_ccy).strip()
|
||
if name and amount and amount != "-":
|
||
sections.append(f"{name}:{money_text(amount, line_ccy)}")
|
||
if subtotal and subtotal != "-":
|
||
sections.append(f"{subtotal_label}:{money_text(subtotal, header_ccy)}")
|
||
sections.append("")
|
||
if not any_bucket:
|
||
return []
|
||
while sections and sections[-1] == "":
|
||
sections.pop()
|
||
return sections
|
||
|
||
|
||
def _land_grouped_fee_lines(quote: dict[str, Any] | None) -> list[str]:
|
||
"""陆运:每段先写始发→目的,再写该段 TMS 费用名。
|
||
|
||
金额后补 TMS 计价单位(运输费:CNY 13.00/KG)。TMS 没回单位时与原来一致,
|
||
只写金额,禁止本地猜 KG/台。
|
||
口岸海关监管费 + 接驳费(东南亚整车境外段)不是路段运费,单独一行;
|
||
TMS 没回就不出现(非必填),名称和金额都照抄 TMS。
|
||
"""
|
||
from agent.schema.land_route_options import land_leg_fee_blocks, land_port_handling_fees
|
||
|
||
src = dict(quote or {})
|
||
header_ccy = str(src.get("currency") or src.get("currencyCode") or "").strip()
|
||
lines: list[str] = []
|
||
blocks = land_leg_fee_blocks(src)
|
||
for title, fees, eta in blocks:
|
||
lines.append(title)
|
||
for fee in fees:
|
||
name = str(fee.get("name") or "").strip()
|
||
amount = str(fee.get("amount") or "").strip()
|
||
if name and amount and amount != "-":
|
||
# 费用行带 TMS 计价单位时写成「CNY 13.00/KG」;没带就只写金额。
|
||
lines.append(f"{name}:{fee_amount_text(fee, header_ccy)}")
|
||
if eta:
|
||
lines.append(f"时效:{eta}")
|
||
# 附加费放在各段费用之后、合计之前,和模板「境外报价」表下方的固定行对应。
|
||
shown = [fee for _title, fees, _eta in blocks for fee in fees]
|
||
for fee in land_port_handling_fees(src, shown_fees=shown):
|
||
name = str(fee.get("name") or "").strip()
|
||
amount = str(fee.get("amount") or "").strip()
|
||
if name and amount and amount != "-":
|
||
lines.append(f"{name}:{fee_amount_text(fee, header_ccy)}")
|
||
return lines
|
||
|
||
|
||
def land_port_handling_note(
|
||
quote: dict[str, Any] | None,
|
||
facts: dict[str, Any] | None = None,
|
||
) -> str:
|
||
"""
|
||
口岸海关监管费+接驳费在文字价格明细里的一行(TMS 有则出,没有则空)。
|
||
|
||
三种情形:
|
||
1. TMS 回了独立费目(带金额):已经进各段费用和合计,这里返回空,避免重复;
|
||
2. TMS 只在费用备注里写了「口岸海关监管+接驳费:8T/1093元、…」:
|
||
按本单车型取对应金额,标明「另计」(TMS 合计不含这笔);
|
||
3. 备注里有价表但车型对不上:整句原文展示,销售按自己车型看,禁止替他挑数。
|
||
|
||
为什么放合计之后:TMS 回包 total 只含两段运输费(实测 WO202611220035
|
||
9000+9600=18600,不含接驳费),写在合计前会让销售以为已经算进总价。
|
||
副作用:无。线程:纯函数。
|
||
"""
|
||
from agent.schema.land_route_options import (
|
||
land_leg_fee_blocks,
|
||
land_port_handling_clause,
|
||
land_port_handling_fees,
|
||
land_port_handling_rate,
|
||
land_port_handling_title,
|
||
)
|
||
|
||
src = dict(quote or {})
|
||
blocks = land_leg_fee_blocks(src)
|
||
shown = [fee for _title, fees, _eta in blocks for fee in fees]
|
||
if land_port_handling_fees(src, shown_fees=shown):
|
||
return ""
|
||
title = land_port_handling_title(src)
|
||
if not title:
|
||
return ""
|
||
ccy = str(src.get("currency") or src.get("currencyCode") or "").strip()
|
||
rate = land_port_handling_rate(src, facts)
|
||
if rate:
|
||
token, amount = rate
|
||
return f"{title}:{money_text(amount, ccy)}({token},另计)"
|
||
clause = land_port_handling_clause(src)
|
||
if clause:
|
||
return f"{title}:{clause}(另计,按车型)"
|
||
return ""
|
||
|
||
|
||
def _is_land_surface(*, quote: dict[str, Any] | None = None, business_line: str = "", transport_mode: str = "") -> bool:
|
||
"""陆运销售面:有 landSegments / 陆运业务线就按段出明细。"""
|
||
src = dict(quote or {})
|
||
if src.get("landSegments") or src.get("land_segments") or src.get("selected_land_route"):
|
||
return True
|
||
raw_line = (business_line or "").strip()
|
||
if raw_line and scheme_transport_mode(raw_line, fallback="") == "陆运":
|
||
return True
|
||
mode = (transport_mode or "").strip()
|
||
return bool(mode) and scheme_transport_mode(mode, fallback="") == "陆运"
|
||
|
||
|
||
def quote_area_fee_text(
|
||
quote: dict[str, Any] | None,
|
||
business_line: str = "",
|
||
transport_mode: str = "",
|
||
facts: dict[str, str] | None = None,
|
||
) -> str:
|
||
"""
|
||
卡片引用条正文:费用行 + 总销售价。
|
||
|
||
必须走 quote_text。塞进 quote_area.title 会被企微整段藏掉。
|
||
facts 只用于「人工报价没写单位时整车补 /车」,TMS 价不受影响。
|
||
"""
|
||
src = dict(quote or {})
|
||
header_ccy = str(src.get("currency") or src.get("currencyCode") or "").strip()
|
||
total = money_text(str(src.get("total") or "-").strip() or "-", header_ccy)
|
||
bucket_lines = sea_bucket_fee_lines(src)
|
||
land_lines = _land_grouped_fee_lines(src) if _is_land_surface(
|
||
quote=src, business_line=business_line, transport_mode=transport_mode
|
||
) else []
|
||
if bucket_lines:
|
||
fee_lines = list(bucket_lines)
|
||
elif land_lines:
|
||
fee_lines = list(land_lines)
|
||
else:
|
||
# 人工报价(产品/销售价)没写单位时,陆运整车补 /车;TMS 价仍按 TMS 自带的单位。
|
||
default_unit = ""
|
||
if not quote_is_tms(src):
|
||
default_unit = _manual_land_default_unit(
|
||
src, business_line=business_line, transport_mode=transport_mode, facts=facts
|
||
)
|
||
fee_lines = [
|
||
f"{name}:{amount}" for name, amount in quote_fee_lines(src, default_unit=default_unit)
|
||
]
|
||
fee_lines.append(f"{_total_label(src, business_line, transport_mode)}:{total}")
|
||
# 口岸监管/接驳费 TMS 合计里没有,单独放在合计下面并标明另计。
|
||
if _is_land_surface(quote=src, business_line=business_line, transport_mode=transport_mode):
|
||
note = land_port_handling_note(src, facts)
|
||
if note:
|
||
fee_lines.append(note)
|
||
return "\n".join(fee_lines)
|
||
|
||
|
||
def quote_area_safe_text(
|
||
quote: dict[str, Any] | None,
|
||
business_line: str = "",
|
||
transport_mode: str = "",
|
||
) -> str:
|
||
"""
|
||
价格明细卡的引用条只留两行:费用合计、共几项费用。
|
||
|
||
空运、海运、陆运都这样。完整路段和逐项费用在上一条文字里。
|
||
引用条再塞路段名或费用明细,企微会裁成半句。
|
||
"""
|
||
src = dict(quote or {})
|
||
header_ccy = str(src.get("currency") or src.get("currencyCode") or "").strip()
|
||
total = money_text(str(src.get("total") or "-").strip() or "-", header_ccy)
|
||
n = len(quote_fee_lines(src))
|
||
if _is_land_surface(quote=src, business_line=business_line, transport_mode=transport_mode):
|
||
from agent.schema.land_route_options import (
|
||
land_leg_fee_blocks,
|
||
land_port_handling_fees,
|
||
)
|
||
|
||
blocks = land_leg_fee_blocks(src)
|
||
block_n = sum(len(fees) for _title, fees, _eta in blocks)
|
||
if block_n:
|
||
# 口岸监管/接驳费也在明细里单独一行,项数必须算上,否则和上一条正文对不上。
|
||
block_n += len(
|
||
land_port_handling_fees(
|
||
src,
|
||
shown_fees=[fee for _title, fees, _eta in blocks for fee in fees],
|
||
)
|
||
)
|
||
n = block_n
|
||
label = _total_label(src, business_line, transport_mode)
|
||
if n:
|
||
return f"{label}:{total}\n共{n}项费用,详见上一条明细"
|
||
return f"{label}:{total}"
|
||
|
||
|
||
def sea_quote_detail_text(
|
||
*,
|
||
work_order_no: str,
|
||
quote: dict[str, Any],
|
||
first_or_same: str = "first",
|
||
history_work_order_no: str = "",
|
||
status: str = "已报价",
|
||
business_line: str = "SEA",
|
||
facts: dict[str, str] | None = None,
|
||
) -> str:
|
||
"""
|
||
完整价格明细文本。海运/陆运仍是来源、工单号、费用行、总价。
|
||
|
||
空运改成货物信息 + 价格方案。航司、计费重、货型和费用来自 TMS,其余用工单字段。
|
||
陆运分段时效跟在各段费用后面,用 TMS 原文;报价有效期写在总成本价下面。
|
||
海运时效、报价有效期仍只在按钮卡副标题。
|
||
不受卡片引用条字数限制。first_or_same 只兼容调用方。
|
||
"""
|
||
_ = first_or_same, history_work_order_no, status
|
||
src = dict(quote or {})
|
||
line = (business_line or "").upper()
|
||
if line == "AIR":
|
||
from agent.policy.air_quote_copy import air_private_quote_text
|
||
|
||
return air_private_quote_text(
|
||
work_order_no=work_order_no,
|
||
facts=facts,
|
||
quote=src,
|
||
)
|
||
source = str(src.get("source_label") or "TMS标准报价").replace(" ", "")
|
||
lines = [source, f"{work_order_no}价格明细"]
|
||
if line == "LAND":
|
||
from agent.schema.land_route_options import land_leg_fee_blocks
|
||
|
||
# 只有一段时,费用前的「始发 → 目的」已经是这条线路。
|
||
# 再写横杠全程(上海-谅山…)就是同一条路出现两次。多段仍保留全程,方便看中转。
|
||
blocks = land_leg_fee_blocks(src)
|
||
land_line = str(src.get("route_line") or "").strip()
|
||
if land_line and len(blocks) != 1:
|
||
lines.append(land_line)
|
||
lines.append(quote_area_fee_text(src, business_line=business_line or "SEA", facts=facts))
|
||
# 陆运价格明细正文带有效期。海运仍放在按钮卡副标题。空运已在上面单独成文。
|
||
if line == "LAND":
|
||
lines.append(f"报价有效期:{tms_valid_until_display(src)}")
|
||
return "\n".join(lines)
|
||
|
||
|
||
def collab_hint(work_order_no: str) -> str:
|
||
"""
|
||
空运价格卡协同说明:紧挨「不需要协同,直接生成报价单」上方。
|
||
|
||
后台 inquiring.air_collab_hint;库内模板可用 {{ticket_no}}、{{bot_name}}。
|
||
无后台模板时用本默认句(已带工单号与当前显示名)。
|
||
"""
|
||
from agent.policy.bot_name import bot_display_name
|
||
|
||
no = (work_order_no or "").strip()
|
||
name = bot_display_name()
|
||
return _r(
|
||
"inquiring.air_collab_hint",
|
||
(
|
||
f"若需要咨询内部航线人员,请手动将相关方拉到空运原有协同群,"
|
||
f"并在空运协同群中发送“ {no}@{name}”,谢谢。"
|
||
),
|
||
ticket_no=no,
|
||
bot_name=name,
|
||
)
|
||
|
||
|
||
def _strip_leading_dash(raw: str) -> str:
|
||
"""去掉时效前导 '-' / '—',避免销售看到「-3-5天」或「-;3-5天」。"""
|
||
text = (raw or "").strip()
|
||
while text.startswith(("-", "—", "–")):
|
||
rest = text[1:].lstrip(" \t;;,,")
|
||
if not rest:
|
||
return ""
|
||
text = rest
|
||
return text
|
||
|
||
|
||
def tms_eta_display(quote: dict[str, Any] | None) -> str:
|
||
"""时效只取 TMS 回的 eta / 预计送达;没有就「-」。禁止本地编。前导横杠会剥掉。"""
|
||
src = dict(quote or {})
|
||
eta = _strip_leading_dash(str(src.get("eta") or ""))
|
||
if eta:
|
||
return eta
|
||
dated = _strip_leading_dash(
|
||
str(src.get("eta_date") or src.get("estimated_delivery") or "")
|
||
)
|
||
return dated or "-"
|
||
|
||
|
||
def tms_valid_until_display(quote: dict[str, Any] | None) -> str:
|
||
"""报价有效期只取 TMS 原文,没有就 "-"。"""
|
||
src = dict(quote or {})
|
||
raw = str(
|
||
src.get("valid_until")
|
||
or src.get("validUntil")
|
||
or src.get("quoteValidUntil")
|
||
or ""
|
||
).strip()
|
||
return raw if raw else "-"
|
||
|
||
|
||
def similar_inquiry_line(*, first_or_same: str, history_work_order_no: str = "") -> str:
|
||
"""
|
||
相同询价内容检索文案(主账仍会算,销售侧价格卡已隐藏,不再调用)。
|
||
"""
|
||
no = (history_work_order_no or "").strip()
|
||
if first_or_same == "same" and no:
|
||
return f"相同询价内容检索:引用历史工单号{no}"
|
||
return "相同询价内容检索:首次询价"
|
||
|
||
|
||
def quote_detail_lines(
|
||
quote: dict[str, Any] | None,
|
||
*,
|
||
first_or_same: str = "first",
|
||
history_work_order_no: str = "",
|
||
status: str = "已报价",
|
||
business_line: str = "",
|
||
transport_mode: str = "",
|
||
) -> list[str]:
|
||
"""
|
||
价格明细正文:TMS 费用、总销售价一行、有效期;海运/陆运再加时效。
|
||
|
||
空运业务确认没有时效,这里不写。禁止编时效/有效期。
|
||
总销售价只写一次。价格卡不展示工单状态,也不展示相同询价内容检索。
|
||
"""
|
||
_ = status, first_or_same, history_work_order_no
|
||
src = dict(quote or {})
|
||
header_ccy = str(src.get("currency") or src.get("currencyCode") or "").strip()
|
||
total = money_text(str(src.get("total") or "-").strip() or "-", header_ccy)
|
||
bucket_lines = sea_bucket_fee_lines(src) if (business_line or "").upper() == "SEA" or (
|
||
transport_mode or ""
|
||
).strip() == "海运" else []
|
||
land_lines = _land_grouped_fee_lines(src) if _is_land_surface(
|
||
quote=src, business_line=business_line, transport_mode=transport_mode
|
||
) else []
|
||
if bucket_lines:
|
||
lines = list(bucket_lines)
|
||
elif land_lines:
|
||
lines = list(land_lines)
|
||
else:
|
||
lines = [f"{name}:{amount}" for name, amount in quote_fee_lines(src)]
|
||
lines.append(f"{_total_label(src, business_line, transport_mode)}:{total}")
|
||
if _is_land_surface(quote=src, business_line=business_line, transport_mode=transport_mode):
|
||
# 口岸监管/接驳费在 TMS 合计之外,单独一行,避免销售误以为已含在总价里。
|
||
note = land_port_handling_note(src)
|
||
if note:
|
||
lines.append(note)
|
||
if not _is_air_surface(business_line=business_line, transport_mode=transport_mode):
|
||
lines.append(f"时效:{tms_eta_display(src)}")
|
||
lines.append(f"报价有效期:{tms_valid_until_display(src)}")
|
||
return lines
|
||
|
||
|
||
def tms_hit_card(
|
||
*,
|
||
work_order_no: str,
|
||
quote: dict[str, Any],
|
||
first_or_same: str = "first",
|
||
history_work_order_no: str = "",
|
||
status: str = "已报价",
|
||
) -> str:
|
||
"""
|
||
有价后的价格明细(文本兜底,模板卡发失败时仍给销售看)。
|
||
|
||
顺序:来源 → 价格明细 → 报价正文 → 协同说明 → 按钮。
|
||
"""
|
||
src = dict(quote or {})
|
||
source = str(src.get("source_label") or "TMS标准报价").replace(" ", "")
|
||
lines = [source, f"{work_order_no}价格明细"]
|
||
from agent.schema.air_route_options import route_codes
|
||
|
||
route = route_codes(None, {}, src) or str(src.get("route_line") or "").strip()
|
||
if route:
|
||
lines.append(route)
|
||
lines.extend(
|
||
quote_detail_lines(
|
||
src,
|
||
first_or_same=first_or_same,
|
||
history_work_order_no=history_work_order_no,
|
||
status=status,
|
||
business_line="AIR",
|
||
)
|
||
)
|
||
lines.append("")
|
||
lines.append(collab_hint(work_order_no))
|
||
lines.append(BTN_SKIP_COLLAB)
|
||
return "\n".join(lines)
|
||
|
||
|
||
def air_route_entry_card(
|
||
*,
|
||
work_order_no: str,
|
||
count: int,
|
||
url: str,
|
||
cabin_pick: bool = False,
|
||
) -> dict[str, Any]:
|
||
"""
|
||
入口卡:短文案跳转 H5,避免企微截断。
|
||
|
||
cabin_pick 为真时按舱位条数原文展示(1 条也发)。
|
||
说了报价日期的选线路仍至少写 2 条,和原来多线路入口一致。
|
||
"""
|
||
n = max(int(count or 0), 1 if cabin_pick else 2)
|
||
noun = "舱位" if cabin_pick else "线路"
|
||
fallback = "https://ai.ytd-scm.com/inquiry-form"
|
||
action_url = (url or "").strip() or fallback
|
||
return {
|
||
"msgtype": "template_card",
|
||
"template_card": {
|
||
"card_type": "text_notice",
|
||
"source": {"desc": "TMS舱位报价" if cabin_pick else "TMS线路报价", "desc_color": 0},
|
||
"main_title": {"title": f"{work_order_no}已查到{n}条{noun}"},
|
||
"sub_title_text": f"点击查看并选择一条{noun},选定后回企微看价格明细。",
|
||
"card_action": {"type": 1, "url": action_url},
|
||
"jump_list": [{"type": 1, "title": f"查看并选择{noun}", "url": action_url}],
|
||
},
|
||
}
|
||
|
||
|
||
def air_route_entry_text(*, work_order_no: str, count: int, cabin_pick: bool = False) -> str:
|
||
"""入口卡失败时的文字兜底。没填日期时 1 条舱位也提示去点选。"""
|
||
n = max(int(count or 0), 1 if cabin_pick else 2)
|
||
noun = "舱位" if cabin_pick else "线路"
|
||
return f"{work_order_no}已查到{n}条{noun},请点击卡片查看并选择。"
|
||
|
||
|
||
def tms_hit_wecom_payload(
|
||
*,
|
||
work_order_no: str,
|
||
quote: dict[str, Any],
|
||
first_or_same: str = "first",
|
||
history_work_order_no: str = "",
|
||
status: str = "已报价",
|
||
) -> dict[str, Any]:
|
||
"""
|
||
一张价格卡,销售侧顺序:
|
||
|
||
价格明细
|
||
┌ 引用条:空运费 / 其它费用 / 总销售价
|
||
报价有效期(空运不写时效)
|
||
协同说明(普通文字,贴按钮上方)
|
||
按钮
|
||
|
||
相同询价内容检索对销售隐藏;first_or_same 只兼容调用方,不展示。
|
||
企微 quote_area.title 只能很短,超长会被整段藏掉。报价必须放 quote_text。
|
||
不用 horizontal:它会排到副标题后面,把费用挤到按钮前。
|
||
"""
|
||
_ = first_or_same, history_work_order_no
|
||
src = dict(quote or {})
|
||
source = str(src.get("source_label") or "TMS标准报价").replace(" ", "")
|
||
meta = "\n".join(
|
||
[
|
||
f"报价有效期:{tms_valid_until_display(src)}",
|
||
"",
|
||
collab_hint(work_order_no),
|
||
]
|
||
)
|
||
return {
|
||
"msgtype": "template_card",
|
||
"template_card": {
|
||
"card_type": "button_interaction",
|
||
"source": {"desc": source, "desc_color": 0},
|
||
"main_title": {"title": f"{work_order_no}价格明细"},
|
||
"quote_area": {
|
||
"type": 0,
|
||
"quote_text": quote_area_safe_text(src, business_line="AIR"),
|
||
},
|
||
"sub_title_text": meta,
|
||
"task_id": card_task_id("quote", work_order_no),
|
||
"button_list": [
|
||
{
|
||
"text": BTN_SKIP_COLLAB,
|
||
"style": 1,
|
||
# EventKey 也带工单号:TaskId 偶发缺失时仍能找回当时那一单
|
||
"key": f"{BTN_SKIP_COLLAB_KEY}:{work_order_no}",
|
||
}
|
||
],
|
||
},
|
||
}
|
||
|
||
|
||
def skip_collab_card_update(*, response_code: str, replace_name: str = "") -> dict[str, Any]:
|
||
"""
|
||
点过模板卡后更新原卡:被点按钮改成已提交,整卡置灰不可再点。
|
||
|
||
必须带企微回调 ResponseCode;无码则不要入出站。
|
||
成交卡三个按钮共用这一条,replace_name 默认「已提交」。
|
||
"""
|
||
return {
|
||
"msgtype": "update_template_card",
|
||
"response_code": response_code,
|
||
"button": {"replace_name": (replace_name or BTN_SKIP_COLLAB_DONE).strip() or BTN_SKIP_COLLAB_DONE},
|
||
}
|
||
|
||
|
||
def tms_miss_reason(tms: dict[str, Any] | None = None) -> str:
|
||
"""
|
||
查价失败原因:优先 TMS 回包 remark 原文,其次 missingFields 的 reason,最后才用 message。
|
||
|
||
为什么要这个函数:主账已把 TMS 的 data.remark 原样透出(例如
|
||
「陆运报价条件无法匹配系统字典,请修正后重试」),销售卡片「查价结果」必须展示这句话,
|
||
否则只剩一句「暂无匹配报价」,销售看不出是哪个条件对不上,只好拉产品进群。
|
||
「需补充字段」「标准运价库无匹配报价」只是业务码标题,不含原因,不当原因展示。
|
||
本函数只读回包,不编造、不翻译、不推断。
|
||
|
||
@param tms 主账 /inquiry/agent/tms/query 的分类 envelope(或同结构 dict)
|
||
@return 可直接展示的中文原因;没有则空串,由调用方回落到原话术
|
||
"""
|
||
row = dict(tms or {})
|
||
for key in ("remark", "tmsRemark", "tms_remark", "missRemark"):
|
||
val = str(row.get(key) or "").strip()
|
||
if val and val not in {"-", "null", "None"}:
|
||
return val
|
||
missing = row.get("missingFields") or row.get("missing_fields")
|
||
if isinstance(missing, list):
|
||
reasons: list[str] = []
|
||
for item in missing:
|
||
if isinstance(item, dict):
|
||
reason = str(item.get("reason") or "").strip()
|
||
if reason and reason not in reasons:
|
||
reasons.append(reason)
|
||
else:
|
||
text = str(item or "").strip()
|
||
if text and text not in reasons:
|
||
reasons.append(text)
|
||
if reasons:
|
||
return ";".join(reasons)
|
||
# 业务码标题不算原因;其它 message 才当兜底。
|
||
msg = str(row.get("message") or "").strip()
|
||
if msg and msg not in {"需补充字段", "标准运价库无匹配报价", "参数格式错误"}:
|
||
return msg
|
||
return ""
|
||
|
||
|
||
def tms_miss_result_text(reason: str = "") -> str:
|
||
"""
|
||
卡片「查价结果」的取值:有 TMS 原文原因就展示原文,没有才回落到「暂无匹配报价」。
|
||
|
||
企微 horizontal_content_list 的 value 上限 100 字符,超长截断并补省略号,
|
||
避免整张卡因为一句长备注发不出去。
|
||
"""
|
||
text = (reason or "").strip()
|
||
if not text:
|
||
return "暂无匹配报价"
|
||
if len(text) > 95:
|
||
text = text[:94].rstrip() + "…"
|
||
return text
|
||
|
||
|
||
def tms_miss_quote_text(*, work_order_no: str) -> str:
|
||
"""请手动拉人进群并发送工单号。后台 inquiring.pull_group_manual。"""
|
||
no = (work_order_no or "").strip()
|
||
return _r(
|
||
"inquiring.pull_group_manual",
|
||
f"请手动拉相关人员和智能体到原有群,并在群聊发送工单编号{no}",
|
||
ticket_no=no,
|
||
)
|
||
|
||
|
||
def tms_miss_hint() -> str:
|
||
"""卡片标题下灰字:与现网图一致。"""
|
||
return "TMS 标准价库暂无匹配报价"
|
||
|
||
|
||
def tms_miss_body() -> str:
|
||
"""卡片中间说明:需求已建单。"""
|
||
return "需求已解析并创建工单。"
|
||
|
||
|
||
def tms_miss(*, work_order_no: str, reason: str = "") -> str:
|
||
"""
|
||
卡片发送失败时的短兜底。拉群句另发,不写在这里。
|
||
禁止在没打 TMS 前用本句冒充无价。
|
||
reason 有值时把 TMS 原文原因一起带给销售。
|
||
"""
|
||
no = (work_order_no or "").strip()
|
||
text = (reason or "").strip()
|
||
if text:
|
||
return f"询价工单{no}已建立,TMS暂无匹配报价:{text}"
|
||
return f"询价工单{no}已建立,TMS标准价库暂无匹配报价。"
|
||
|
||
|
||
def tms_miss_followup(*, work_order_no: str) -> str:
|
||
"""出卡后再发的独立一句:拉人进群并带上工单号。"""
|
||
return tms_miss_quote_text(work_order_no=work_order_no)
|
||
|
||
|
||
def copy_ticket_text(*, work_order_no: str) -> str:
|
||
"""
|
||
点「复制工单号」后回给销售的正文:只含工单号。
|
||
|
||
企微模板卡按钮不能写系统剪贴板。整条消息只有工单号,销售长按「复制」不会夹杂其它字。
|
||
"""
|
||
return (work_order_no or "").strip()
|
||
|
||
|
||
def tms_miss_wecom_payload(
|
||
*,
|
||
work_order_no: str,
|
||
action_url: str = "",
|
||
copy_url: str = "",
|
||
reason: str = "",
|
||
) -> dict[str, Any]:
|
||
"""
|
||
TMS 已查过、确认没价:一张文本通知卡(对齐现网图)。
|
||
|
||
询价工单号 / 灰字无价说明 / 建单说明 / 工单状态与查价结果。
|
||
不要「来源」行、不要按钮。企微 text_notice 必须带可信域名 card_action。
|
||
copy_url 忽略:不再跳复制页。
|
||
reason 为 TMS 回包 remark 原文(如「陆运报价条件无法匹配系统字典,请修正后重试」),
|
||
有值时「查价结果」直接展示它;没有才写「暂无匹配报价」。
|
||
"""
|
||
_ = copy_url
|
||
no = (work_order_no or "").strip()
|
||
card: dict[str, Any] = {
|
||
"card_type": "text_notice",
|
||
"source": {"desc": "TMS查价结果", "desc_color": 0},
|
||
"main_title": {
|
||
"title": f"询价工单 {no}",
|
||
"desc": tms_miss_hint(),
|
||
},
|
||
"sub_title_text": tms_miss_body(),
|
||
"horizontal_content_list": [
|
||
{"keyname": "工单状态", "value": "询价中"},
|
||
{"keyname": "查价结果", "value": tms_miss_result_text(reason)},
|
||
],
|
||
"task_id": card_task_id("miss", no),
|
||
}
|
||
url = (action_url or "").strip()
|
||
if url:
|
||
card["card_action"] = {"type": 1, "url": url}
|
||
return {"msgtype": "template_card", "template_card": card}
|
||
|
||
|
||
def tms_contract_gap(*, work_order_no: str) -> str:
|
||
"""已废弃:不再因字典对不上拦查价;保留以免旧单测/引用报错。"""
|
||
return tms_miss(work_order_no=work_order_no)
|
||
|
||
|
||
def tms_need_more(*, work_order_no: str, hint: str = "") -> str:
|
||
"""已废弃:TMS 回 1003 按合同收成暂无报价。保留以免旧引用报错。"""
|
||
_ = hint
|
||
return tms_miss(work_order_no=work_order_no)
|
||
|
||
|
||
def skip_collab_ack() -> str:
|
||
"""销售跳过内部协同。后台 inquiring.skip_collab_ack。"""
|
||
return _r(
|
||
"inquiring.skip_collab_ack",
|
||
"好的,已跳过内部协同。正在按 TMS 标准报价生成报价单草稿…",
|
||
)
|
||
|
||
|
||
def sea_collab_ask() -> str:
|
||
"""海运有价卡协同问句。"""
|
||
return "是否拉产品人员进群协同?"
|
||
|
||
|
||
def sea_miss_ask() -> str:
|
||
"""海运无价卡协同问句。"""
|
||
return "未查到合适报价,是否需要拉内部产品人员进群,进行手工协同报价?"
|
||
|
||
|
||
def sea_tms_hit_card(
|
||
*,
|
||
work_order_no: str,
|
||
quote: dict[str, Any],
|
||
first_or_same: str = "first",
|
||
history_work_order_no: str = "",
|
||
status: str = "已报价",
|
||
business_line: str = "SEA",
|
||
) -> str:
|
||
"""海运/陆运有价:价格明细 + 拉群问句 + 两个按钮。空运卡片函数保持不动。"""
|
||
src = dict(quote or {})
|
||
source = str(src.get("source_label") or "TMS标准报价").replace(" ", "")
|
||
lines = [source, f"{work_order_no}价格明细"]
|
||
lines.extend(
|
||
quote_detail_lines(
|
||
src,
|
||
first_or_same=first_or_same,
|
||
history_work_order_no=history_work_order_no,
|
||
status=status,
|
||
business_line=business_line or "SEA",
|
||
)
|
||
)
|
||
lines.append("")
|
||
lines.append(sea_collab_ask())
|
||
lines.append(BTN_PULL_COLLAB)
|
||
lines.append(BTN_SKIP_COLLAB_TMS)
|
||
return "\n".join(lines)
|
||
|
||
|
||
def sea_tms_hit_wecom_payload(
|
||
*,
|
||
work_order_no: str,
|
||
quote: dict[str, Any],
|
||
first_or_same: str = "first",
|
||
history_work_order_no: str = "",
|
||
status: str = "已报价",
|
||
business_line: str = "SEA",
|
||
) -> dict[str, Any]:
|
||
"""海运/陆运有价企微卡:价格明细 + 问句 + 拉群 / 跳过协同。"""
|
||
_ = first_or_same, history_work_order_no, status
|
||
src = dict(quote or {})
|
||
source = str(src.get("source_label") or "TMS标准报价").replace(" ", "")
|
||
meta = "\n".join(
|
||
[
|
||
f"时效:{tms_eta_display(src)}",
|
||
f"报价有效期:{tms_valid_until_display(src)}",
|
||
"",
|
||
sea_collab_ask(),
|
||
]
|
||
)
|
||
return {
|
||
"msgtype": "template_card",
|
||
"template_card": {
|
||
"card_type": "button_interaction",
|
||
"source": {"desc": source, "desc_color": 0},
|
||
"main_title": {"title": f"{work_order_no}价格明细"},
|
||
"quote_area": {
|
||
"type": 0,
|
||
"quote_text": quote_area_safe_text(src, business_line=business_line or "SEA"),
|
||
},
|
||
"sub_title_text": meta,
|
||
"task_id": card_task_id("quote", work_order_no),
|
||
"button_list": [
|
||
{
|
||
"text": BTN_PULL_COLLAB,
|
||
"style": 1,
|
||
"key": f"{BTN_PULL_COLLAB_KEY}:{work_order_no}",
|
||
},
|
||
{
|
||
"text": BTN_SKIP_COLLAB_TMS,
|
||
"style": 1,
|
||
"key": f"{BTN_SKIP_COLLAB_KEY}:{work_order_no}",
|
||
},
|
||
],
|
||
},
|
||
}
|
||
|
||
|
||
def sea_tms_miss(*, work_order_no: str, reason: str = "") -> str:
|
||
"""海运/陆运无价文字兜底:工单号、暂无匹配、询价中、拉群问句。"""
|
||
no = (work_order_no or "").strip()
|
||
result = tms_miss_result_text(reason)
|
||
return "\n".join(
|
||
[
|
||
f"询价工单 {no}",
|
||
f"暂无匹配报价:{result}" if (reason or "").strip() else "暂无匹配报价",
|
||
"工单状态:询价中",
|
||
sea_miss_ask(),
|
||
BTN_PULL_COLLAB,
|
||
]
|
||
)
|
||
|
||
|
||
def sea_tms_miss_wecom_payload(
|
||
*, work_order_no: str, action_url: str = "", reason: str = ""
|
||
) -> dict[str, Any]:
|
||
"""
|
||
海运/陆运无价一张卡:工单号、暂无匹配、询价中、拉群问句、一个按钮。
|
||
|
||
禁止再写「请手动拉人到原有群」。
|
||
reason 为 TMS 回包 remark 原文:有值时标题下与「查价结果」都展示它,
|
||
销售能直接看到「目的地无法匹配系统地址编码」这类原因,而不是只有一句无价。
|
||
"""
|
||
no = (work_order_no or "").strip()
|
||
result = tms_miss_result_text(reason)
|
||
card: dict[str, Any] = {
|
||
"card_type": "button_interaction",
|
||
"source": {"desc": "TMS查价结果", "desc_color": 0},
|
||
"main_title": {
|
||
"title": f"询价工单 {no}",
|
||
"desc": result,
|
||
},
|
||
"sub_title_text": "\n".join(
|
||
[
|
||
"需求已解析并创建工单。",
|
||
sea_miss_ask(),
|
||
]
|
||
),
|
||
"horizontal_content_list": [
|
||
{"keyname": "工单状态", "value": "询价中"},
|
||
{"keyname": "查价结果", "value": result},
|
||
],
|
||
"task_id": card_task_id("miss", no),
|
||
"button_list": [
|
||
{
|
||
"text": BTN_PULL_COLLAB,
|
||
"style": 1,
|
||
"key": f"{BTN_PULL_COLLAB_KEY}:{no}",
|
||
}
|
||
],
|
||
}
|
||
url = (action_url or "").strip()
|
||
if url:
|
||
card["card_action"] = {"type": 1, "url": url}
|
||
return {"msgtype": "template_card", "template_card": card}
|
||
|
||
|
||
MULTI_CONFIRM_TAIL = "请回复「确定」创建工单,内容有误时,请重新发内容。"
|
||
MULTI_SAME_MODE = "同一运输方式出现了两段,请拆开再发。"
|
||
# 核对卡已取消时效,不再对销售发送此句。
|
||
MULTI_CONFIRM_EXPIRED = "刚才的多段核对已过期,请把多段需求再发一次,出核对后再回复确定。"
|
||
|
||
|
||
def multi_air_manual_hint(*, work_order_no: str) -> str:
|
||
"""含空运的多段:不自动拉群。与 inquiring.pull_group_manual 同一条后台。"""
|
||
return tms_miss_quote_text(work_order_no=work_order_no)
|
||
|
||
|
||
def multi_pull_text(*, work_order_no: str) -> str:
|
||
"""不含空运:建单后的拉群文字兜底。不写 TMS、不写暂无报价。"""
|
||
no = (work_order_no or "").strip()
|
||
return "\n".join(
|
||
[
|
||
f"询价工单 {no}",
|
||
"工单已创建。",
|
||
"是否拉产品人员进群协同?",
|
||
BTN_PULL_COLLAB,
|
||
]
|
||
)
|
||
|
||
|
||
def multi_pull_wecom_payload(*, work_order_no: str) -> dict[str, Any]:
|
||
"""不含空运:一张卡、一个按钮,点下去才按各段线路建群。"""
|
||
no = (work_order_no or "").strip()
|
||
return {
|
||
"msgtype": "template_card",
|
||
"template_card": {
|
||
"card_type": "button_interaction",
|
||
"source": {"desc": "多段联运", "desc_color": 0},
|
||
"main_title": {"title": f"询价工单 {no}", "desc": "工单已创建。"},
|
||
"sub_title_text": "是否拉产品人员进群协同?",
|
||
"task_id": card_task_id("multi_pull", no or "multi"),
|
||
"button_list": [
|
||
{
|
||
"text": BTN_PULL_COLLAB,
|
||
"style": 1,
|
||
"key": f"{BTN_PULL_COLLAB_KEY}:{no}",
|
||
}
|
||
],
|
||
},
|
||
}
|
||
|
||
|
||
def handoff_air_hint(*, work_order_no: str) -> str:
|
||
"""空运转人工:只发文字,不建群。后台 transfer.air_hint。"""
|
||
no = (work_order_no or "").strip()
|
||
if no:
|
||
default = f"触发转人工,请手动把航线和智能体拉进原有空运群,并发送工单号 {no}。"
|
||
return _r("transfer.air_hint", default, ticket_no=no)
|
||
return _r(
|
||
"transfer.air_hint",
|
||
"触发转人工,请手动把航线和智能体拉进原有空运群,并发送工单号。",
|
||
ticket_no="",
|
||
)
|
||
|
||
|
||
def handoff_offer_text(*, reason: str, transport_mode: str) -> str:
|
||
"""海运/陆运转人工卡片的文字兜底(点卡或原样回复按钮均可)。"""
|
||
why = (reason or "").strip() or HANDOFF_INTENT_REASON
|
||
mode = (transport_mode or "").strip()
|
||
lines = [why]
|
||
if mode:
|
||
lines.append(f"运输方式:{mode}")
|
||
lines.append("是否拉产品人员进群协同?")
|
||
lines.append(BTN_PULL_COLLAB)
|
||
return "\n".join(lines)
|
||
|
||
|
||
def handoff_offer_wecom_payload(*, reason: str, task_token: str) -> dict[str, Any]:
|
||
"""海运/陆运一张卡、一个按钮:拉产品进群协同。"""
|
||
token = (task_token or "handoff").strip() or "handoff"
|
||
why = (reason or "").strip() or HANDOFF_INTENT_REASON
|
||
return {
|
||
"msgtype": "template_card",
|
||
"template_card": {
|
||
"card_type": "button_interaction",
|
||
"source": {"desc": "转人工", "desc_color": 0},
|
||
"main_title": {"title": why, "desc": "是否拉产品人员进群协同?"},
|
||
"task_id": card_task_id("handoff", token),
|
||
"button_list": [
|
||
{
|
||
"text": BTN_PULL_COLLAB,
|
||
"style": 1,
|
||
"key": f"{BTN_PULL_COLLAB_KEY}:{token}",
|
||
}
|
||
],
|
||
},
|
||
}
|
||
|
||
|
||
def handoff_land_card_text(*, reason: str) -> str:
|
||
"""陆运转人工:卡片发不出时的文字兜底(两组编号)。后台 transfer.land_card_text。"""
|
||
from agent.schema.handoff_options import text_fallback
|
||
|
||
why = (reason or "").strip() or HANDOFF_INTENT_REASON
|
||
return _r("transfer.land_card_text", text_fallback(why), reason=why)
|
||
|
||
|
||
def handoff_land_need_selection() -> str:
|
||
"""点提交但两项没选全:不建单、不建群,让销售补选。"""
|
||
return _r(
|
||
"transfer.land_need_selection",
|
||
"还差一项:业务类型和报价场景都要选。请重新点一遍「提交并转人工」。",
|
||
)
|
||
|
||
|
||
def handoff_land_session_expired() -> str:
|
||
"""点卡时私聊书签已过期:没有业务类型不知道拉谁,只能重说一次。"""
|
||
return _r(
|
||
"transfer.land_session_expired",
|
||
"这次选的卡已过期,请重新说「转人工」,我会再发一次业务类型和报价场景。",
|
||
)
|
||
|
||
|
||
def land_options_session_expired() -> str:
|
||
"""点陆运补问卡时会话已过期:已填字段找不回来,只能让销售重发一句。"""
|
||
return _r(
|
||
"inquiry.land_options_expired",
|
||
"这张选项卡已过期,请把这次陆运询价再发一句,我重新问一次。",
|
||
)
|
||
|
||
|
||
def land_options_incomplete() -> str:
|
||
"""两项没点全就提交:不往下走,重发卡让销售补齐。"""
|
||
return _r(
|
||
"inquiry.land_options_incomplete",
|
||
"还有没选的项,请把这张卡上的选项都选完再提交。",
|
||
)
|
||
|
||
|
||
def land_option_pair_mismatch(*, transport_type: str, route: str) -> str:
|
||
"""
|
||
运输类型与线路类别搭不上(例如中港整车 + 国内长途/零担)。
|
||
|
||
为什么不硬改:这两个字段决定拉哪个产品、出哪张报价模板,改错就是把单发给错的人。
|
||
合并卡之后销售只需要重选一个业务类型,类型和线路会一起换掉。
|
||
"""
|
||
return _r(
|
||
"inquiry.land_option_pair_mismatch",
|
||
f"运输类型「{transport_type}」和线路类别「{route}」不匹配,"
|
||
"请重新选择业务类型(车型、线路、地区随按钮一起确定)。",
|
||
transport_type=transport_type,
|
||
route=route,
|
||
)
|
||
|
||
|
||
def ask_land_cargo_name() -> str:
|
||
"""只剩货物品名时的短句。卡片收不了自由文字,只能请销售打字。"""
|
||
return _r(
|
||
"inquiry.ask_land_cargo_name",
|
||
"请回复货物品名(必填),例如:衣服。",
|
||
)
|
||
|
||
|
||
def handoff_land_group_created(
|
||
*,
|
||
work_order_no: str,
|
||
sales_name: str,
|
||
product_names: list[str],
|
||
biz_type_label: str = "",
|
||
quote_scenario: str = "",
|
||
deadline: str = "",
|
||
) -> str:
|
||
"""
|
||
陆运转人工已自动建群:私聊回执。
|
||
|
||
把销售刚选的业务类型、报价场景和报价时效原样带回,
|
||
让销售确认系统按他点的选项建了单,而不是按上一单的旧字段。
|
||
群内只发摘要:报价单同样要群里有人说「生成报价单」才出,
|
||
系统按工单业务类型直取对应模板,不自动推空白表。
|
||
"""
|
||
no = (work_order_no or "").strip()
|
||
sales_raw = (sales_name or "").strip()
|
||
sales = _role_label(sales_raw, "销售")
|
||
name_bits = [n.strip() for n in (product_names or []) if (n or "").strip()]
|
||
products = "、".join(_role_label(n, "产品") for n in name_bits) or "产品"
|
||
default = (
|
||
f"已转人工并自动建群,群名与工单号一致:{no}。"
|
||
f"已邀请:{sales}、{products}。"
|
||
f"业务类型:{biz_type_label or '-'};报价场景:{quote_scenario or '-'}"
|
||
+ (f"({deadline})" if deadline else "")
|
||
+ "。群内已发询价摘要,产品可直接回文字报价或发报价附件;"
|
||
"需要报价单时在群里说「生成报价单」。"
|
||
)
|
||
sales_var = sales_raw[2:] if sales_raw.startswith("销售") else sales_raw
|
||
prod_var = "、".join((n[2:] if n.startswith("产品") else n) for n in name_bits) or "产品"
|
||
return _r(
|
||
"transfer.land_group_created",
|
||
default,
|
||
ticket_no=no,
|
||
sales_name=sales_var,
|
||
product_names=prod_var,
|
||
biz_type=biz_type_label or "-",
|
||
quote_scenario=quote_scenario or "-",
|
||
deadline=deadline or "-",
|
||
)
|
||
|
||
|
||
def handoff_land_create_failed() -> str:
|
||
"""陆运转人工建正式单失败:不建群,可重试。"""
|
||
return _r(
|
||
"transfer.land_create_failed",
|
||
"转人工建单没成功,暂时没有建群。请稍后重试;急的话可先在后台新建工单再转人工。",
|
||
)
|
||
|
||
|
||
def handoff_group_brief(
|
||
*,
|
||
work_order_no: str,
|
||
facts: dict[str, str] | None,
|
||
business_line: str,
|
||
product_names: list[str] | None = None,
|
||
sales_name: str = "",
|
||
airline_names: list[str] | None = None,
|
||
biz_type_label: str = "",
|
||
quote_scenario: str = "",
|
||
quote_deadline: str = "",
|
||
) -> str:
|
||
"""
|
||
转人工群摘要:@人 +「工单号转人工」+ 已整理询价;未整理与协同全部为 -。
|
||
不写 TMS、不写模板。
|
||
|
||
biz_type_label / quote_scenario:陆运卡片带下来的业务类型与报价场景。
|
||
这两项只有陆运卡才有:海运/空运摘要里出现「业务类型:-、报价场景:-」,
|
||
产品会以为是漏填,所以非陆运一律不输出这两行(2026-11 真机反馈)。
|
||
quote_deadline:陆运卡片的报价时效(如「2小时」);海空运不传,不显示该行。
|
||
"""
|
||
from agent.policy.handoff_human import collab_keys_for_handoff, transport_label
|
||
from agent.schema.field_validate import collab_display_name
|
||
|
||
no = (work_order_no or "").strip()
|
||
line = (business_line or "").strip().upper()
|
||
mode = transport_label(line) or "海运"
|
||
names: list[str] = []
|
||
if line == "AIR":
|
||
if (sales_name or "").strip():
|
||
names.append(sales_name.strip())
|
||
for n in airline_names or []:
|
||
if (n or "").strip() and n.strip() not in names:
|
||
names.append(n.strip())
|
||
else:
|
||
for n in product_names or []:
|
||
if (n or "").strip() and n.strip() not in names:
|
||
names.append(n.strip())
|
||
at = " ".join(f"@{n}" for n in names)
|
||
src = dict(facts or {})
|
||
field_lines = inquiry_field_lines(work_order_no=no, facts=src, transport_mode=mode)
|
||
body = field_lines[1:] if field_lines else []
|
||
lines: list[str] = []
|
||
if at:
|
||
lines.append(at)
|
||
lines.append(f"{no}转人工")
|
||
lines.append("询价字段:")
|
||
lines.extend(body)
|
||
if line == "LAND":
|
||
# 只有陆运卡片才有业务类型/报价场景;海运、空运出这两行会让产品以为漏填。
|
||
lines.append("")
|
||
lines.append(f"业务类型:{(biz_type_label or '').strip() or '-'}")
|
||
lines.append(f"报价场景:{(quote_scenario or '').strip() or '-'}")
|
||
if (quote_deadline or "").strip():
|
||
lines.append(f"报价时效:{quote_deadline.strip()}")
|
||
lines.append("")
|
||
lines.append("协同补充字段:")
|
||
for key in collab_keys_for_handoff(src, line):
|
||
label = collab_display_name(key, line)
|
||
lines.append(f"{label}:-")
|
||
return "\n".join(lines)
|
||
|
||
|
||
def _role_label(name: str, role: str) -> str:
|
||
"""姓名前加岗位,避免「销售甲、乙」被读成两人都是销售。已带岗位前缀的不再重复加。"""
|
||
text = (name or "").strip()
|
||
if not text:
|
||
return role
|
||
if text.startswith(role):
|
||
return text
|
||
return f"{role}{text}"
|
||
|
||
|
||
def sea_group_created(*, work_order_no: str, sales_name: str, product_names: list[str]) -> str:
|
||
"""拉群成功后的私聊。后台 inquiring.group_created。已邀请写成「销售某某、产品某某」。"""
|
||
no = (work_order_no or "").strip()
|
||
sales_raw = (sales_name or "").strip()
|
||
sales = _role_label(sales_raw, "销售")
|
||
name_bits = [n.strip() for n in (product_names or []) if (n or "").strip()]
|
||
products = "、".join(_role_label(n, "产品") for n in name_bits) or "产品"
|
||
default = (
|
||
f"协同群已创建,群名与工单号一致:{no}。"
|
||
f"已邀请:{sales}、{products}。"
|
||
"群内已发询价摘要,请在群聊中继续确认报价。"
|
||
)
|
||
sales_var = sales_raw[2:] if sales_raw.startswith("销售") else sales_raw
|
||
prod_var = "、".join(
|
||
(n[2:] if n.startswith("产品") else n) for n in name_bits
|
||
) or "产品"
|
||
return _r(
|
||
"inquiring.group_created",
|
||
default,
|
||
ticket_no=no,
|
||
sales_name=sales_var,
|
||
product_names=prod_var,
|
||
)
|
||
|
||
|
||
def sea_group_exists(*, work_order_no: str) -> str:
|
||
"""同一工单已有群,不再新建。后台 inquiring.group_exists。"""
|
||
no = (work_order_no or "").strip()
|
||
return _r(
|
||
"inquiring.group_exists",
|
||
f"协同群已在,群名与工单号一致:{no}。请到该群继续确认报价。",
|
||
ticket_no=no,
|
||
)
|
||
|
||
|
||
def handoff_sea_group_created(
|
||
*,
|
||
work_order_no: str,
|
||
sales_name: str,
|
||
product_names: list[str],
|
||
) -> str:
|
||
"""
|
||
海运转人工已自动建群:私聊回执。
|
||
|
||
与自动协同群的区别要说清楚:本群是转人工直接建的,群内只发询价摘要,
|
||
报价单也要在群里说「生成报价单」才出(与自动协同群同一口径,不自动推空白表)。
|
||
产品回文字价或发附件即可落账;成交仍由销售在后台确认,群里不会自动记成交。
|
||
"""
|
||
no = (work_order_no or "").strip()
|
||
sales_raw = (sales_name or "").strip()
|
||
sales = _role_label(sales_raw, "销售")
|
||
name_bits = [n.strip() for n in (product_names or []) if (n or "").strip()]
|
||
products = "、".join(_role_label(n, "产品") for n in name_bits) or "产品"
|
||
default = (
|
||
f"已转人工并自动建群,群名与工单号一致:{no}。"
|
||
f"已邀请:{sales}、{products}。"
|
||
"群内已发询价摘要,产品可直接回文字报价或发报价附件;"
|
||
"需要报价单时在群里说「生成报价单」。"
|
||
)
|
||
sales_var = sales_raw[2:] if sales_raw.startswith("销售") else sales_raw
|
||
prod_var = "、".join((n[2:] if n.startswith("产品") else n) for n in name_bits) or "产品"
|
||
return _r(
|
||
"transfer.sea_group_created",
|
||
default,
|
||
ticket_no=no,
|
||
sales_name=sales_var,
|
||
product_names=prod_var,
|
||
)
|
||
|
||
|
||
def handoff_sea_create_failed() -> str:
|
||
"""
|
||
海运转人工建正式单失败:不建群。
|
||
|
||
群名必须等于正式工单号,建单没成功就建群,产品报的价挂不到任何一张单上,
|
||
比不建群更麻烦。这里只让销售稍后重试或先到后台建单。
|
||
"""
|
||
return _r(
|
||
"transfer.sea_create_failed",
|
||
"转人工建单没成功,暂时没有建群。请稍后再说「转人工」;急的话可先在后台新建工单再转人工。",
|
||
)
|
||
|
||
|
||
def sea_group_no_staff() -> str:
|
||
"""没有可拉的产品人员,不建空群。后台 inquiring.sea_no_staff。"""
|
||
return _r(
|
||
"inquiring.sea_no_staff",
|
||
"没有可拉的产品人员。请先在后台「员工管理」维护本线路的海运产品岗后再试。",
|
||
)
|
||
|
||
|
||
def group_sheet_send_fail() -> str:
|
||
"""模板已填好但没发进群。"""
|
||
return "报价单已生成,发到群里失败,请稍后重试。"
|
||
|
||
|
||
def group_sheet_generating(*, kind: str = "Excel") -> str:
|
||
"""群内按指令开始生成报价单。"""
|
||
label = "PDF" if str(kind or "").upper() == "PDF" else "Excel"
|
||
return f"好的,正在生成{label}报价单,请稍候。"
|
||
|
||
|
||
def group_sheet_need_quote(
|
||
*,
|
||
work_order_no: str = "",
|
||
business_line: str = "",
|
||
airline_names: list[str] | None = None,
|
||
) -> str:
|
||
"""
|
||
完全没有报价数据、无法出空白表时的兜底提示。
|
||
|
||
2026-11-17 起:单业务线/多段只要有段数据,无价也按空白报价单出表,不再走这里;
|
||
仅多段工单连 segments 都没有(没有模板维度可分)才保留提示。
|
||
|
||
空运协同群点名本线路航线·空运(姓名来自员工管理);查不到人时回落「@航线人员」。
|
||
"""
|
||
no = (work_order_no or "").strip()
|
||
head = f"工单 {no}:" if no else ""
|
||
line = (business_line or "").strip().upper()
|
||
if line == "AIR":
|
||
names = [str(n or "").strip() for n in (airline_names or []) if str(n or "").strip()]
|
||
at = " ".join(f"@{n}" for n in names) if names else "@航线人员"
|
||
return f"{head}当前还没有可用报价,请航线{at}先报价或确认 TMS的报价 后再生成报价单。"
|
||
return f"{head}当前还没有可用报价,请产品/航线先报价或确认 TMS 后再生成报价单。"
|
||
|
||
|
||
def group_sheet_request_not_allowed() -> str:
|
||
"""
|
||
历史文案:曾经用来挡「非销售/商务/航线」喊生成报价单。
|
||
|
||
产品口径 2026-11-22 起群内任何成员都能要表,正常流程不会再走到这里;
|
||
保留函数只为兼容可能残留的调用/历史测试,不要在新代码里再引用。
|
||
"""
|
||
return "只有销售、商务或航线可以说「生成报价单」;产品请直接在群里报价格。"
|
||
|
||
|
||
def sea_group_create_fail(*, reason: str = "") -> str:
|
||
"""建群失败,按钮保持可点。后台 inquiring.group_create_fail(无原因短句)。"""
|
||
extra = (reason or "").strip()
|
||
default = f"建群失败:{extra}。请稍后重试。" if extra else "建群失败,请稍后重试。"
|
||
return _r("inquiring.group_create_fail", default, reason=extra)
|
||
|
||
|
||
def quote_is_product(quote: dict[str, Any] | None) -> bool:
|
||
"""来源是产品/航线人工,不能写进 TMS 报价段。"""
|
||
src = dict(quote or {})
|
||
code = str(src.get("source") or "").strip().upper()
|
||
if code == "PRODUCT":
|
||
return True
|
||
if code in {"TMS", "SALES_ADJUST"}:
|
||
return False
|
||
label = str(src.get("source_label") or src.get("sourceLabel") or src.get("source") or "")
|
||
return "产品" in label or "航线" in label
|
||
|
||
|
||
def quote_is_sales_adjust(quote: dict[str, Any] | None) -> bool:
|
||
"""销售调过价的这一版,禁止再写进 TMS 报价段。"""
|
||
src = dict(quote or {})
|
||
code = str(src.get("source") or "").strip().upper()
|
||
if code == "SALES_ADJUST":
|
||
return True
|
||
if code in {"TMS", "PRODUCT"}:
|
||
return False
|
||
label = str(src.get("source_label") or src.get("sourceLabel") or "").replace(" ", "")
|
||
return "销售调价" in label
|
||
|
||
|
||
def quote_is_tms(quote: dict[str, Any] | None) -> bool:
|
||
"""
|
||
只有 TMS 原价能进 TMS 段。
|
||
|
||
产品报价、销售调价即使金额还在,也不能冒充 TMS。
|
||
"""
|
||
src = dict(quote or {})
|
||
if not src:
|
||
return False
|
||
if quote_is_product(src) or quote_is_sales_adjust(src):
|
||
return False
|
||
code = str(src.get("source") or "").strip().upper()
|
||
if code == "TMS":
|
||
return True
|
||
label = str(src.get("source_label") or src.get("sourceLabel") or src.get("source") or "")
|
||
return "TMS" in label.upper() or "标准报价" in label or not label.strip()
|
||
|
||
|
||
def remember_tms_quote(sess: Any, quote: dict[str, Any] | None) -> None:
|
||
"""
|
||
书签里单独记住 TMS 原价。调价只改 sess.quote,不准覆盖这一份。
|
||
|
||
副作用:写 sess.tms_quote。调用方在 TMS 命中或调价前调用。
|
||
"""
|
||
if sess is None:
|
||
return
|
||
src = dict(quote or {})
|
||
if not quote_is_tms(src):
|
||
return
|
||
sess.tms_quote = dict(src)
|
||
|
||
|
||
def resolve_tms_quote(sess: Any = None, ticket: Any = None) -> dict[str, Any]:
|
||
"""
|
||
拉群/群摘要用的 TMS 原价:书签 tms_quote → 主账 tms_quote → 当前价若仍是 TMS。
|
||
|
||
调后最新一条是销售调价,不能拿来当 TMS。
|
||
"""
|
||
candidates: list[dict[str, Any]] = []
|
||
if sess is not None:
|
||
candidates.append(dict(getattr(sess, "tms_quote", None) or {}))
|
||
view = dict(ticket or {}) if isinstance(ticket, dict) else {}
|
||
if view:
|
||
candidates.append(dict(view.get("tms_quote") or view.get("tmsQuote") or {}))
|
||
elif ticket is not None:
|
||
candidates.append(dict(getattr(ticket, "tms_quote", None) or {}))
|
||
if sess is not None:
|
||
candidates.append(dict(getattr(sess, "quote", None) or {}))
|
||
if view:
|
||
candidates.append(dict(view.get("quote") or {}))
|
||
for item in candidates:
|
||
if quote_is_tms(item) and (item.get("total") or item.get("fee_rows") or item.get("fee_lines")):
|
||
return item
|
||
return {}
|
||
|
||
|
||
def _land_quote_is_selected(quote: dict[str, Any] | None) -> bool:
|
||
"""已收成一条陆运线路:有选定段,或只剩一条且写了线路名。"""
|
||
src = dict(quote or {})
|
||
selected = src.get("selected_land_route")
|
||
if isinstance(selected, dict) and list(selected.get("legs") or []):
|
||
return True
|
||
if not str(src.get("route_line") or "").strip():
|
||
return False
|
||
from agent.schema.land_route_options import needs_route_pick
|
||
|
||
return not needs_route_pick(src)
|
||
|
||
|
||
def land_group_tms_quote(
|
||
tms: dict[str, Any] | None,
|
||
current: dict[str, Any] | None,
|
||
) -> dict[str, Any]:
|
||
"""
|
||
群摘要陆运 TMS 段用已选线路,不用还没选的整包。
|
||
|
||
选定前书签里的 TMS 原价含全部备选费用,总成本价和时效都还没汇总。
|
||
当前价已是选定投影时用当前价。当前价已是人工报价或调价时,仍用传入的 TMS 价。
|
||
"""
|
||
raw = dict(tms or {})
|
||
cur = dict(current or {})
|
||
if not quote_is_tms(cur) or not _land_quote_is_selected(cur):
|
||
return raw
|
||
from agent.schema.land_route_options import needs_route_pick
|
||
|
||
if _land_quote_is_selected(raw) and not needs_route_pick(raw):
|
||
return raw
|
||
return cur
|
||
|
||
|
||
def sea_group_brief(
|
||
*,
|
||
work_order_no: str,
|
||
facts: dict[str, str],
|
||
quote: dict[str, Any] | None,
|
||
collab_facts: dict[str, str] | None,
|
||
sales_name: str,
|
||
product_names: list[str],
|
||
has_price: bool,
|
||
tms_quote: dict[str, Any] | None = None,
|
||
product_quote: dict[str, Any] | None = None,
|
||
transport_mode: str = "海运",
|
||
) -> str:
|
||
"""
|
||
群里自动摘要:询价 + 协同 + TMS 报价 + 人工报价。
|
||
|
||
TMS 有价:TMS 段照常出明细。
|
||
TMS 无价但产品已手工报价:整段「TMS 暂无匹配报价 + 请另报」省掉,直接出「人工报价:」明细
|
||
(产品 2026-11-22 口径:销售眼前已有价,再说一遍无价会让销售以为报价没生效)。
|
||
TMS 无价且没人报价:保留「TMS 暂无匹配报价」并提示用文字或 Excel 另报。
|
||
产品报的价单独放人工报价,销售调价另起一段。
|
||
禁止把调后价写进 TMS 段。陆运传入 transport_mode=陆运,始发站/货物字段跟核对卡同一套。
|
||
"""
|
||
no = (work_order_no or "").strip()
|
||
sales = (sales_name or "").strip() or "销售"
|
||
products = [n.strip() for n in (product_names or []) if (n or "").strip()]
|
||
at_bits = [f"@{sales}"] + [f"@{n}" for n in products]
|
||
mode = (transport_mode or "海运").strip() or "海运"
|
||
lines = [" ".join(at_bits), f"工单号:{no}", "询价字段:"]
|
||
lines.extend(
|
||
inquiry_field_lines(work_order_no=no, facts=facts, transport_mode=mode)[1:]
|
||
)
|
||
lines.append("")
|
||
lines.append("协同补充字段:")
|
||
collab = dict(collab_facts or {})
|
||
line = "LAND" if mode == "陆运" else "SEA"
|
||
collab_keys = collab_fields_for_ticket(facts, line) or SEA_COLLAB_FIELDS
|
||
for key in collab_keys:
|
||
label = collab_display_name(key, line)
|
||
val = (collab.get(key) or collab.get(label) or "").strip() or "-"
|
||
lines.append(f"{label}:{val}")
|
||
tms, manual, adjust = _split_brief_quotes(
|
||
quote=quote,
|
||
tms_quote=tms_quote,
|
||
product_quote=product_quote,
|
||
has_price=has_price,
|
||
)
|
||
if mode == "陆运":
|
||
# 拉群时书签仍可能是整包。当前价已选定时换成那一条,和私聊明细对齐。
|
||
tms = land_group_tms_quote(tms, quote)
|
||
has_tms_price = bool(tms and (tms.get("total") or quote_fee_lines(tms)))
|
||
manual_priced = _brief_quote_has_price(manual)
|
||
# 产品 2026-11-22 拍板:TMS 已无价、产品又手工报了价时,整段「TMS报价:TMS 暂无匹配报价
|
||
# + 请用文字/Excel 另报」是空话,销售眼前已经有价,再读一遍无价只会以为报价没生效。
|
||
# 所以这种情况整段省掉,直接给「人工报价:」+ 明细。
|
||
# TMS 有价时照旧保留 TMS 段;TMS 无价又没人报价时也要保留,好让产品知道该补价。
|
||
if has_tms_price or not manual_priced:
|
||
lines.append("")
|
||
lines.append("TMS报价:")
|
||
if has_tms_price:
|
||
if mode == "陆运":
|
||
# 与私聊价格明细同一套:线路名、分段费用、各段时效、总成本价、有效期。
|
||
# 不用 quote_detail_lines,那会再补一行合并时效,也会在未选定时摊开全部费用。
|
||
# 群里已有「TMS报价:」,不要再叠「TMS标准报价」和「工单号价格明细」。
|
||
detail = sea_quote_detail_text(
|
||
work_order_no=no,
|
||
quote=tms,
|
||
business_line="LAND",
|
||
).splitlines()
|
||
source = str(tms.get("source_label") or "TMS标准报价").replace(" ", "")
|
||
title = f"{no}价格明细"
|
||
lines.extend(ln for ln in detail if ln.strip() not in {source, title, "TMS标准报价"})
|
||
else:
|
||
lines.extend(quote_detail_lines(tms, transport_mode=mode))
|
||
lines.append(group_tms_confirm_hint(business_line="LAND" if mode == "陆运" else "SEA"))
|
||
else:
|
||
lines.append("TMS 暂无匹配报价")
|
||
lines.append(group_no_tms_quote_hint(business_line="LAND" if mode == "陆运" else "SEA"))
|
||
if manual_priced:
|
||
lines.append("")
|
||
# 新版式(陆运多车型竖排)自带「车型/数量」段、「空运」自带「价格方案如下:」,
|
||
# 已经有 TMS 段在上面时不再套「人工报价:」;TMS 段整段省掉后上面什么都没有,
|
||
# 必须补一行小标题,销售才知道下面是产品报的价而不是系统查出来的价。
|
||
if not _manual_layout_self_titled(manual, business_line=line) or not has_tms_price:
|
||
lines.append("人工报价:")
|
||
lines.extend(
|
||
_manual_quote_lines(
|
||
manual,
|
||
business_line=("LAND" if mode == "陆运" else "SEA"),
|
||
transport_mode=mode,
|
||
facts=facts,
|
||
layout="manual",
|
||
)
|
||
)
|
||
if adjust and (quote_fee_lines(adjust) or str(adjust.get("total") or "").strip() not in {"", "-"}):
|
||
lines.append("")
|
||
lines.append("销售调价:")
|
||
lines.extend(_manual_quote_lines(adjust, transport_mode=mode))
|
||
return "\n".join(lines)
|
||
|
||
|
||
def _brief_quote_has_price(quote: dict[str, Any] | None) -> bool:
|
||
"""
|
||
群摘要判定「这一段到底有没有价」:有费用行,或合计不是空/横杠。
|
||
|
||
为什么要单独一个函数:TMS 段和人工报价段用的是两套数据,判定口径必须一致;
|
||
任何一边写成「有空字典就算有价」都会让摘要出现一个只有标题没有金额的空段。
|
||
副作用:无。线程:纯函数。
|
||
"""
|
||
src = dict(quote or {})
|
||
if quote_fee_lines(src):
|
||
return True
|
||
return str(src.get("total") or "").strip() not in {"", "-", "—", "-"}
|
||
|
||
|
||
def _split_brief_quotes(
|
||
*,
|
||
quote: dict[str, Any] | None,
|
||
tms_quote: dict[str, Any] | None,
|
||
product_quote: dict[str, Any] | None,
|
||
has_price: bool,
|
||
) -> tuple[dict[str, Any], dict[str, Any], dict[str, Any]]:
|
||
"""
|
||
群摘要三段价:TMS 原价 / 人工 / 销售调价。调后价不准掉进 TMS。
|
||
|
||
tms_quote 显式传入(含空字典)时不再用当前 quote 兜底。
|
||
"""
|
||
src = dict(quote or {})
|
||
tms = dict(tms_quote) if tms_quote is not None else {}
|
||
manual = dict(product_quote or {})
|
||
if tms_quote is None:
|
||
if has_price and src and quote_is_tms(src):
|
||
tms = src
|
||
elif quote_is_product(src):
|
||
manual = manual or src
|
||
if quote_is_sales_adjust(tms) or quote_is_product(tms):
|
||
tms = {}
|
||
if quote_is_sales_adjust(manual):
|
||
manual = {}
|
||
adjust = src if quote_is_sales_adjust(src) else {}
|
||
if quote_is_sales_adjust(product_quote) and not adjust:
|
||
adjust = dict(product_quote or {})
|
||
return tms, manual, adjust
|
||
|
||
|
||
def scheme_card(
|
||
*,
|
||
work_order_no: str,
|
||
status: str,
|
||
facts: dict[str, str],
|
||
quote: dict[str, Any],
|
||
transport_mode: str = "空运",
|
||
) -> str:
|
||
"""
|
||
报价方案文本兜底(模板卡发失败时仍给销售看)。
|
||
|
||
与卡片同一套字段,禁止漏项。不再展示工单状态。
|
||
"""
|
||
_ = status
|
||
src = dict(quote or {})
|
||
# 方案卡不展示询价「报价日期」,改展示 TMS 报价有效期
|
||
body = "\n".join(
|
||
line
|
||
for line in inquiry_field_lines(
|
||
work_order_no=work_order_no, facts=facts, transport_mode=transport_mode
|
||
)
|
||
if not line.startswith("报价日期:")
|
||
)
|
||
eta = tms_eta_display(src)
|
||
valid = tms_valid_until_display(src)
|
||
lines = [
|
||
f"【{work_order_no}报价方案】",
|
||
body,
|
||
"价格明细",
|
||
quote_area_fee_text(src, transport_mode=transport_mode),
|
||
]
|
||
if not _is_air_surface(transport_mode=transport_mode):
|
||
lines.append(f"时效:{eta or '-'}")
|
||
lines.extend(
|
||
[
|
||
f"报价有效期:{valid}",
|
||
"请确认是否采用该报价方案:",
|
||
f"【{BTN_ADOPT_EXCEL_CARD}】",
|
||
f"【{BTN_ADOPT_PDF_CARD}】",
|
||
f"【{BTN_REJECT}】",
|
||
]
|
||
)
|
||
return "\n".join(lines)
|
||
|
||
|
||
def _clip(raw: str, limit: int) -> str:
|
||
"""企微列表格有字数建议;超长才裁,避免空白。"""
|
||
text = (raw or "").strip() or "-"
|
||
return text if len(text) <= limit else text[: limit - 1] + "…"
|
||
|
||
|
||
def _air_scheme_route(quote: dict[str, Any], facts: dict[str, str]) -> str:
|
||
"""方案卡航线:空运ZUH→中转→CRK。没有三字码才退回地名。"""
|
||
from agent.schema.air_route_options import scheme_route_codes
|
||
|
||
return scheme_route_codes(quote, facts)
|
||
|
||
|
||
def scheme_transport_mode(business_line: str = "", fallback: str = "空运") -> str:
|
||
"""会话业务线 → 方案卡运输方式。海运不得再写成空运。"""
|
||
raw = (business_line or "").strip()
|
||
line = raw.upper()
|
||
if line == "SEA" or raw == "海" or "海运" in raw:
|
||
return "海运"
|
||
if line == "LAND" or raw == "陆" or "陆运" in raw:
|
||
return "陆运"
|
||
if line == "AIR" or raw == "空" or "空运" in raw:
|
||
return "空运"
|
||
return (fallback or "空运").strip() or "空运"
|
||
|
||
|
||
def _is_air_surface(*, business_line: str = "", transport_mode: str = "") -> bool:
|
||
"""
|
||
空运销售面:业务确认没有时效,展示层一律不写。
|
||
|
||
海运/陆运仍保留时效。两边都空时不算空运,避免误藏海运/陆运卡。
|
||
"""
|
||
raw_line = (business_line or "").strip()
|
||
if raw_line and scheme_transport_mode(raw_line, fallback="") == "空运":
|
||
return True
|
||
mode = (transport_mode or "").strip()
|
||
return bool(mode) and scheme_transport_mode(mode, fallback="") == "空运"
|
||
|
||
|
||
def scheme_horizontal_rows(
|
||
*,
|
||
work_order_no: str,
|
||
facts: dict[str, str],
|
||
quote: dict[str, Any],
|
||
transport_mode: str = "空运",
|
||
) -> list[dict[str, str]]:
|
||
"""
|
||
卡片字段:一行一项,每格独立字数,不会像副标题整段截成 8CB…。
|
||
|
||
企微 horizontal 最多 6 条。运输方式写进行程行,避免丢字段。
|
||
海运用货物数量/整柜或拼柜/箱型箱量,禁止套空运件数毛重体积包装。
|
||
陆运用运输类型/线路类别/体积重量或车型数量,禁止套海运箱型或空运件数包装。
|
||
不用 sub_title 堆字段(它会排到列表前面,且超长必截断)。
|
||
"""
|
||
src = facts or {}
|
||
q = dict(quote or {})
|
||
origin = (src.get("起运港") or "-").strip() or "-"
|
||
dest = (src.get("目的港") or "-").strip() or "-"
|
||
cargo = (src.get("品名") or "-").strip() or "-"
|
||
mode = (transport_mode or "空运").strip() or "空运"
|
||
if mode == "海运":
|
||
qty = (src.get("货量") or src.get("货物数量") or "-").strip() or "-"
|
||
load = (src.get("整柜或拼柜") or "-").strip() or "-"
|
||
# 拼柜不指定箱型:方案卡「箱型」格只写整柜或拼柜,不补「-」。
|
||
box = "" if is_sea_lcl(load) else (src.get("箱型箱量") or "").strip()
|
||
box_cell = f"{load};{box}" if box else load
|
||
return [
|
||
{"keyname": "工单号", "value": _clip(work_order_no, 26)},
|
||
{"keyname": "航线", "value": _clip(f"海运 {origin}→{dest}", 26)},
|
||
{"keyname": "货物", "value": _clip(f"{cargo};{qty}", 26)},
|
||
{"keyname": "箱型", "value": _clip(box_cell, 26)},
|
||
{"keyname": "时效", "value": _clip(tms_eta_display(q), 26)},
|
||
{"keyname": "报价有效期", "value": _clip(tms_valid_until_display(q), 26)},
|
||
]
|
||
if mode == "陆运":
|
||
return [
|
||
{"keyname": "工单号", "value": _clip(work_order_no, 26)},
|
||
{"keyname": "线路", "value": _clip(f"陆运 {origin}→{dest}", 26)},
|
||
{"keyname": "类型", "value": _clip(_land_scheme_type_line(src), 26)},
|
||
{"keyname": "货物", "value": _clip(_land_scheme_goods_line(src), 26)},
|
||
{"keyname": "时效", "value": _clip(tms_eta_display(q), 26)},
|
||
{"keyname": "报价有效期", "value": _clip(tms_valid_until_display(q), 26)},
|
||
]
|
||
pieces = _with_unit(str(src.get("件数") or ""), "件")
|
||
weight = _with_unit(str(src.get("毛重") or ""), "KGS")
|
||
volume = _with_unit(str(src.get("体积") or ""), "CBM")
|
||
pack = (src.get("包装方式") or src.get("包装类型") or "-").strip() or "-"
|
||
return [
|
||
{"keyname": "工单号", "value": _clip(work_order_no, 26)},
|
||
{"keyname": "航线", "value": _clip(_air_scheme_route(q, src), 26)},
|
||
{"keyname": "货物", "value": _clip(f"{cargo};{pieces};{weight};{volume}", 26)},
|
||
{"keyname": "包装", "value": _clip(pack, 26)},
|
||
{"keyname": "报价有效期", "value": _clip(tms_valid_until_display(q), 26)},
|
||
]
|
||
|
||
|
||
def _land_scheme_type_line(facts: dict[str, str]) -> str:
|
||
"""方案卡「类型」格:运输类型、线路类别、运输分类。"""
|
||
src = facts or {}
|
||
land_type = (src.get("运输类型") or "-").strip() or "-"
|
||
bits = [land_type]
|
||
route = (src.get("线路类别") or "-").strip() or "-"
|
||
klass = (src.get("运输分类") or "-").strip() or "-"
|
||
bits.extend([route, klass])
|
||
return ";".join(bits)
|
||
|
||
|
||
def _land_scheme_goods_line(facts: dict[str, str]) -> str:
|
||
"""方案卡「货物」格:品名 + 当前类型的体积重量或车型/数量。"""
|
||
src = facts or {}
|
||
land_type = (src.get("运输类型") or "").strip()
|
||
bits: list[str] = [(src.get("品名") or "-").strip() or "-"]
|
||
for key in _land_cargo_keys(land_type, src):
|
||
if key in {"起运港", "目的港", "品名", "报价日期"}:
|
||
continue
|
||
val = (src.get(key) or "").strip()
|
||
if val:
|
||
bits.append(val)
|
||
if land_type in _LAND_CUSTOMS_TYPES:
|
||
customs = (src.get("通关口岸") or "").strip()
|
||
if customs:
|
||
bits.append(customs)
|
||
return ";".join(bits)
|
||
|
||
|
||
def scheme_wecom_payload(
|
||
*,
|
||
work_order_no: str,
|
||
facts: dict[str, str],
|
||
quote: dict[str, Any],
|
||
status: str = "已报价",
|
||
transport_mode: str = "空运",
|
||
) -> dict[str, Any]:
|
||
"""
|
||
一张报价方案卡:标题带工单号、引用条、字段、三个短按钮。
|
||
|
||
按钮文案必须短:生成Excel / 生成PDF / 不采用。长文案横排会被裁。
|
||
海运/空运/陆运字段分开,禁止陆运卡再出箱型或空运件数包装。
|
||
"""
|
||
_ = status
|
||
src = dict(quote or {})
|
||
source = str(src.get("source_label") or "TMS标准报价").replace(" ", "")
|
||
return {
|
||
"msgtype": "template_card",
|
||
"template_card": {
|
||
"card_type": "button_interaction",
|
||
"source": {"desc": source, "desc_color": 0},
|
||
"main_title": {"title": f"{work_order_no}报价方案"},
|
||
"quote_area": {
|
||
"type": 0,
|
||
# 空运/海运/陆运引用条一律只留总价,费用在上一条明细。
|
||
"quote_text": quote_area_safe_text(src, transport_mode=transport_mode),
|
||
},
|
||
"horizontal_content_list": scheme_horizontal_rows(
|
||
work_order_no=work_order_no,
|
||
facts=facts,
|
||
quote=src,
|
||
transport_mode=transport_mode,
|
||
),
|
||
"task_id": card_task_id("scheme", work_order_no),
|
||
"button_list": [
|
||
{
|
||
"text": BTN_ADOPT_EXCEL_CARD,
|
||
"style": 1,
|
||
"key": f"{BTN_ADOPT_EXCEL_KEY}:{work_order_no}",
|
||
},
|
||
{
|
||
"text": BTN_ADOPT_PDF_CARD,
|
||
"style": 1,
|
||
"key": f"{BTN_ADOPT_PDF_KEY}:{work_order_no}",
|
||
},
|
||
{
|
||
"text": BTN_REJECT,
|
||
"style": 2,
|
||
"key": f"{BTN_REJECT_KEY}:{work_order_no}",
|
||
},
|
||
],
|
||
},
|
||
}
|
||
|
||
|
||
def reject_ack() -> str:
|
||
"""销售选择不采用。后台 quoted.reject_ack。"""
|
||
return _r(
|
||
"quoted.reject_ack",
|
||
"请问如何调整报价单,以满足您的需求?请说明要调哪一项费用,例如「提货费加 10%」。",
|
||
)
|
||
|
||
|
||
def adjust_confirm(applied: list[dict[str, Any]]) -> str:
|
||
"""识别成功确认。applied 每项含 name、how。"""
|
||
bits = [f"{x.get('name')}在当前报价基础上{x.get('how')}" for x in applied if x.get("name")]
|
||
body = ";".join(bits) or "已识别调价指令"
|
||
return f"已识别调价指令:{body}。正在刷新报价"
|
||
|
||
|
||
def adjust_ledger_remark(applied: list[dict[str, Any]]) -> str:
|
||
"""
|
||
主账状态流转备注:每次销售调价一条,记下当时改了哪几项。
|
||
|
||
后台「状态流转与备注」原样展示;不含利润点。
|
||
"""
|
||
bits = [f"{x.get('name')}{x.get('how')}" for x in applied if x.get("name")]
|
||
return "销售调价:" + (";".join(bits) if bits else "已改价")
|
||
|
||
|
||
def adjust_need_fee() -> str:
|
||
return "请说明要调哪一项费用明细,例如「提货费加 10%」。只说报价或总价,系统不会改价。"
|
||
|
||
|
||
def adjust_need_lines() -> str:
|
||
return "请分别对明细价格进行调价,系统不分摊合计。"
|
||
|
||
|
||
def adjust_currency_hint() -> str:
|
||
return "请分别对不同币种进行调价,系统不做汇率换算。"
|
||
|
||
|
||
def adjust_unmatched(missing: list[str], available: list[str]) -> str:
|
||
miss = "、".join(x for x in missing if x) or "这些费用"
|
||
have = "、".join(available) if available else "(当前没有费用明细)"
|
||
return f"卡上没有{miss}。当前能调的费用:{have}"
|
||
|
||
|
||
def adjust_ambiguous(name: str, hits: list[str]) -> str:
|
||
return f"「{name}」对上了多项({'、'.join(hits)}),请说明要调哪一项。"
|
||
|
||
|
||
def adjust_ineligible(reason: str) -> str:
|
||
if reason == "already_collab":
|
||
return "这张单已经拉群协同,请到群里发送调价指令。"
|
||
if reason == "closed":
|
||
return "这张单已经结案,不能再按这套调价。"
|
||
return "当前工单还没有可调的报价,不能按这套调价。"
|
||
|
||
|
||
def adjust_only_sales(*, air_room: bool = False) -> str:
|
||
"""
|
||
群里不是这张单的销售在发调价。价格不改。
|
||
|
||
空运群称呼航线;海运、陆运称呼产品。两句用句号分开,避免连成一句。
|
||
"""
|
||
who = "航线" if air_room else "产品"
|
||
return f"只有这张单的销售能调价。{who}请继续报价,价格未改。"
|
||
|
||
|
||
def adjust_group_body(quote: dict[str, Any] | None, business_line: str = "") -> str:
|
||
"""
|
||
群里调后全量明细,利润点不写给销售。
|
||
|
||
海运必须带三段标题与小计(始发港/海运/目的港),禁止只摊平 fee_lines 把小计弄丢。
|
||
"""
|
||
src = dict(quote or {})
|
||
text = quote_area_fee_text(src, business_line=business_line)
|
||
return "调价后价格:\n" + text
|
||
|
||
|
||
def adopt_excel_ack() -> str:
|
||
"""采用并生成 Excel。后台 quoted.adopt_excel。"""
|
||
return _r("quoted.adopt_excel", "已确认采用该方案,正在生成 Excel 最终报价单…")
|
||
|
||
|
||
def adopt_pdf_ack() -> str:
|
||
"""采用并生成 PDF。后台 quoted.adopt_pdf。"""
|
||
return _r("quoted.adopt_pdf", "已确认采用该方案,正在生成PDF最终报价单…")
|
||
|
||
|
||
QUOTE_TEMPLATE_MISSING = "生成报价单失败,请联系IT运维维护后台报价模板。"
|
||
|
||
|
||
def quote_template_missing() -> str:
|
||
"""关键词与默认模板都未维护。后台 quoted.template_missing。"""
|
||
return _r("quoted.template_missing", QUOTE_TEMPLATE_MISSING)
|
||
|
||
|
||
def file_ready(*, kind: str, work_order_no: str, filename: str) -> str:
|
||
"""最终报价单生成完成。后台 quoted.file_ready。"""
|
||
return _r(
|
||
"quoted.file_ready",
|
||
f"已生成{kind}最终报价单:{filename}(工单 {work_order_no})",
|
||
kind=kind,
|
||
filename=filename,
|
||
ticket_no=work_order_no,
|
||
)
|
||
|
||
|
||
# 未成交原因说明跟在未成交后,不跟在协商中后。验收脚本认这个开关。
|
||
DEAL_LOST_REASON_BEFORE_NEGOTIATE = True
|
||
DEAL_HINT = "咨询成交状态:请输入成交 / 未成交 (未成交需补充原因)/ 协商中。"
|
||
DEAL_SUBTITLE = "报价单已发送,请跟进成交状态"
|
||
|
||
|
||
def deal_horizontal_rows(
|
||
*,
|
||
quote: dict[str, Any] | None,
|
||
facts: dict[str, str] | None = None,
|
||
business_line: str = "",
|
||
transport_mode: str = "",
|
||
) -> list[dict[str, str]]:
|
||
"""
|
||
成交跟进卡字段:费用合计、报价有效期;海运/陆运再加时效。
|
||
|
||
空运业务确认没有时效,这里不写。
|
||
不要历史报价 / 工单状态 / 来源。来源只留卡片顶部红字。
|
||
"""
|
||
_ = facts
|
||
src = dict(quote or {})
|
||
header_ccy = str(src.get("currency") or src.get("currencyCode") or "").strip()
|
||
total = money_text(str(src.get("total") or "-").strip() or "-", header_ccy)
|
||
rows = [
|
||
{"keyname": _total_label(src, business_line, transport_mode), "value": _clip(total, 26)},
|
||
]
|
||
if not _is_air_surface(business_line=business_line, transport_mode=transport_mode):
|
||
rows.append({"keyname": "时效", "value": _clip(tms_eta_display(src), 26)})
|
||
rows.append({"keyname": "报价有效期", "value": _clip(tms_valid_until_display(src), 26)})
|
||
return rows
|
||
|
||
|
||
def deal_card(
|
||
*,
|
||
work_order_no: str,
|
||
status: str = "",
|
||
summary: str = "",
|
||
quote: dict[str, Any] | None = None,
|
||
facts: dict[str, str] | None = None,
|
||
business_line: str = "",
|
||
) -> str:
|
||
"""
|
||
成交跟进文字兜底(模板卡发失败时仍给销售看)。
|
||
|
||
与卡片同一套字段;status / summary 只兼容旧调用,不再展示。
|
||
"""
|
||
_ = status, summary
|
||
no = (work_order_no or "").strip()
|
||
rows = deal_horizontal_rows(quote=quote, facts=facts, business_line=business_line)
|
||
lines = [
|
||
f"询价工单 {no}",
|
||
DEAL_SUBTITLE,
|
||
DEAL_HINT,
|
||
]
|
||
lines.extend(f"{row['keyname']}:{row['value']}" for row in rows)
|
||
lines.append(f"【{BTN_DEAL}】【{BTN_LOST}】【{BTN_NEGOTIATE}】")
|
||
return "\n".join(lines)
|
||
|
||
|
||
def deal_wecom_payload(
|
||
*,
|
||
work_order_no: str,
|
||
quote: dict[str, Any] | None = None,
|
||
facts: dict[str, str] | None = None,
|
||
card_version: int = 0,
|
||
business_line: str = "",
|
||
) -> dict[str, Any]:
|
||
"""
|
||
成交跟进卡:对齐现网 button_interaction。
|
||
|
||
顶部来源红字;标题询价工单号;副标题跟进成交;三个按钮。
|
||
字段:费用合计、报价有效期;海运/陆运再加时效。空运不写时效。
|
||
"""
|
||
src = dict(quote or {})
|
||
source = str(src.get("source_label") or "TMS标准报价").replace(" ", "")
|
||
no = (work_order_no or "").strip()
|
||
ver = int(card_version or 0)
|
||
suffix = f":v{ver}" if ver > 0 else ""
|
||
return {
|
||
"msgtype": "template_card",
|
||
"template_card": {
|
||
"card_type": "button_interaction",
|
||
"source": {"desc": source, "desc_color": 0},
|
||
"main_title": {
|
||
"title": f"询价工单 {no}",
|
||
"desc": DEAL_SUBTITLE,
|
||
},
|
||
"sub_title_text": DEAL_HINT,
|
||
"horizontal_content_list": deal_horizontal_rows(
|
||
quote=src, facts=facts, business_line=business_line
|
||
),
|
||
"task_id": card_task_id("deal", no),
|
||
"button_list": [
|
||
{
|
||
"text": BTN_DEAL,
|
||
"style": 1,
|
||
"key": f"{BTN_DEAL_KEY}:{no}{suffix}",
|
||
},
|
||
{
|
||
"text": BTN_LOST,
|
||
"style": 1,
|
||
"key": f"{BTN_LOST_KEY}:{no}{suffix}",
|
||
},
|
||
{
|
||
"text": BTN_NEGOTIATE,
|
||
"style": 1,
|
||
"key": f"{BTN_NEGOTIATE_KEY}:{no}{suffix}",
|
||
},
|
||
],
|
||
},
|
||
}
|
||
|
||
|
||
def ticket_locked(*, work_order_no: str, status: str) -> str:
|
||
"""已成交/未成交/已关闭后,群里改字段或再报价。"""
|
||
no = (work_order_no or "").strip() or "该工单"
|
||
st = (status or "").strip() or "已关闭"
|
||
return f"工单{no}{st},不能修改工单信息"
|
||
|
||
|
||
def ticket_locked_file(*, work_order_no: str, status: str) -> str:
|
||
"""终态工单再发 Excel/PDF。"""
|
||
no = (work_order_no or "").strip() or "该工单"
|
||
st = (status or "").strip() or "已关闭"
|
||
return f"工单{no}{st},不能再上传附件"
|
||
|
||
|
||
def group_only_excel_pdf() -> str:
|
||
return "群里只收 Excel 和 PDF,其它文件不会存入工单"
|
||
|
||
|
||
def group_only_product_can_quote() -> str:
|
||
"""非产品人员在群里报价。后台 inquiring.group_only_product。"""
|
||
return _r(
|
||
"inquiring.group_only_product",
|
||
"只有产品能报价格,请产品在群里发价格或 Excel/PDF",
|
||
)
|
||
|
||
|
||
_GROUP_QUOTE_TAIL = (
|
||
"亦可通过“纯文字”告知新报价,或直接将新报价excel表发给智能体。\n"
|
||
"(注:附件报价单仅作为文件存储于后台,智能体不读取且不将报价内容写进系统)"
|
||
)
|
||
|
||
|
||
def group_tms_confirm_hint(*, business_line: str = "") -> str:
|
||
"""
|
||
有 TMS 价时写在报价有效期下方:谁可以确认沿用,以及怎么另报。
|
||
|
||
空运喊航线人员,海运/陆运喊产品人员。附件只当存档,不读进报价。
|
||
"""
|
||
line = (business_line or "").strip().upper()
|
||
who = "航线人员" if line == "AIR" else "产品人员"
|
||
return f"请{who}回复“确认报价”确定采用TMS报价。\n{_GROUP_QUOTE_TAIL}"
|
||
|
||
|
||
def multi_group_field_hint(*, includes_air: bool) -> str:
|
||
"""
|
||
多段群里每次带出各段询价字段时,贴在最后一行字段下面。
|
||
|
||
任一段是空运,喊航线或产品;没有空运只喊产品。成交跟进不走这里。
|
||
注里说的是附件报价单只存档,不改报价写入规则。
|
||
"""
|
||
who = "航线或产品人员" if includes_air else "产品人员"
|
||
return (
|
||
f"{who}可通过“纯文字”告知新报价,或直接将新报价excel表发给智能体。\n"
|
||
"(注:附件报价单仅作为文件存储于后台,智能体不读取且不将报价内容写进系统)"
|
||
)
|
||
|
||
|
||
def group_no_tms_quote_hint(*, business_line: str = "") -> str:
|
||
"""
|
||
TMS 无价时写在「暂无匹配」下方:谁可以用文字或 Excel 另报。
|
||
|
||
空运喊航线人员,海运/陆运喊产品人员。附件只当存档,不读进报价。
|
||
"""
|
||
line = (business_line or "").strip().upper()
|
||
who = "航线人员" if line == "AIR" else "产品人员"
|
||
return (
|
||
f"{who}可通过“纯文字”告知新报价,或直接将新报价excel表发给智能体。\n"
|
||
"(注:附件报价单仅作为文件存储于后台,智能体不读取且不将报价内容写进系统)"
|
||
)
|
||
|
||
|
||
def quote_attachment_saved_app(work_order_no: str, count: int = 1) -> str:
|
||
"""
|
||
群里告诉大家:模板或产品附件已进后台。
|
||
|
||
后台 inquiring.group_file_saved。默认句带工单号和份数;后台改成短句则原样用。
|
||
"""
|
||
no = (work_order_no or "").strip() or "WO"
|
||
n = count if count > 0 else 1
|
||
return _r(
|
||
"inquiring.group_file_saved",
|
||
f"工单{no}的{n}个附件已保存到后台工单",
|
||
ticket_no=no,
|
||
count=n,
|
||
)
|
||
|
||
|
||
def group_file_saved(*, work_order_no: str = "", count: int = 1) -> str:
|
||
"""群附件已保存。与 quote_attachment_saved_app 同一条后台编码。"""
|
||
return quote_attachment_saved_app(work_order_no, count)
|
||
|
||
|
||
def group_ask_tms_or_product() -> str:
|
||
return (
|
||
"本单已有 TMS 报价。\n"
|
||
"1. 要在 TMS 基础上改,请回复:在TMS基础上改(或直接回 1 / 第一个)\n"
|
||
"2. 不要 TMS 报价、只用你刚说的价格,请回复:不用TMS报价(或直接回 2 / 第二个)"
|
||
)
|
||
|
||
|
||
def group_tms_choice_unclear() -> str:
|
||
return "还没确定怎么用 TMS 报价。请回复:在TMS基础上改 / 不用TMS报价,也可以直接回 1 或 2。"
|
||
|
||
|
||
def group_need_price_before_choice() -> str:
|
||
return "请产品先发价格,再说在TMS基础上改或不用TMS报价"
|
||
|
||
|
||
def group_confirm_need_tms() -> str:
|
||
return "本单还没有可沿用的 TMS 报价,请直接回复协同报价或上传报价附件。"
|
||
|
||
|
||
def group_quote_not_recognized() -> str:
|
||
return "没有识别到报价,请产品再发一次价格或 Excel/PDF"
|
||
|
||
|
||
def group_fields_not_recognized(
|
||
keys: tuple[str, ...] | None = None,
|
||
business_line: str = "",
|
||
) -> str:
|
||
"""没认出协同字段。陆运列出本单可补项;海运列出协同五项(不含贸易条款)。"""
|
||
if keys:
|
||
line = (business_line or "").strip().upper() or "SEA"
|
||
names = [collab_display_name(k, line) for k in keys]
|
||
return "听到了。请直接说" + "、".join(names) + "。"
|
||
return "听到了。请直接说货好时间、商品海关编码、是否含油/含电/含磁。"
|
||
|
||
|
||
def group_deal_stale(*, work_order_no: str) -> str:
|
||
"""点了上一版成交卡。"""
|
||
no = (work_order_no or "").strip()
|
||
return f"这张成交确认已过期。工单{no}已有更新的报价,请点击群里最新一条成交跟进。"
|
||
|
||
|
||
def group_deal_need_quote(*, work_order_no: str = "") -> str:
|
||
"""
|
||
群里确认成交/未成交时,主账还没有可确认报价。
|
||
|
||
空运 WO027:询价中且无 inq_quote,旧句「主账状态更新失败」不说明原因。
|
||
"""
|
||
no = (work_order_no or "").strip()
|
||
if no:
|
||
return (
|
||
f"工单{no}状态更新失败,原因:没有报价。\n"
|
||
f"请先报价,然后再更新工单状态。"
|
||
)
|
||
return "工单状态更新失败,原因:没有报价。\n请先报价,然后再更新工单状态。"
|
||
|
||
|
||
def deal_fail_no_quote(*, work_order_no: str = "") -> str:
|
||
"""成交三态写入失败且判定为没有报价时的销售可见说明。与群门禁同句。"""
|
||
return group_deal_need_quote(work_order_no=work_order_no)
|
||
|
||
|
||
def group_deal_sales_only() -> str:
|
||
return "只有负责该工单的销售可以确认成交状态。"
|
||
|
||
|
||
def group_dissolved(*, work_order_no: str) -> str:
|
||
no = (work_order_no or "").strip()
|
||
return f"群已解散。工单{no}协同已结束。"
|
||
|
||
|
||
def group_cannot_recreate(*, work_order_no: str) -> str:
|
||
"""解散后再点拉群。"""
|
||
no = (work_order_no or "").strip() or "该工单"
|
||
return f"工单{no}协同已结束,不能再拉群。"
|
||
|
||
|
||
def group_deal_followup(*, sales_name: str, work_order_no: str) -> str:
|
||
"""
|
||
群里文字成交跟进。只提示输入,不含报价合计、不含点选按钮。
|
||
|
||
私聊成交卡仍走 deal_card,不改这里。
|
||
"""
|
||
name = (sales_name or "").strip() or "销售"
|
||
no = (work_order_no or "").strip()
|
||
return (
|
||
f"@{name}\n"
|
||
f"询价工单 {no}\n"
|
||
"报价单已发送,请确认成交状态\n"
|
||
"咨询成交状态:请输入成交 / 未成交 (未成交需补充原因)/ 协商中"
|
||
)
|
||
|
||
|
||
def group_deal_mention(*, sales_name: str, work_order_no: str) -> str:
|
||
return group_deal_followup(sales_name=sales_name, work_order_no=work_order_no)
|
||
|
||
|
||
def deal_ack(
|
||
outcome: str,
|
||
work_order_no: str,
|
||
next_followup_at: str = "",
|
||
lost_reason: str = "",
|
||
followup_hours: float | None = None,
|
||
business_line: str = "",
|
||
) -> str:
|
||
"""
|
||
成交三态回执。后台 deal.recorded_won / lost.ask_reason / negotiating.recorded。
|
||
|
||
多段联运不锁舱:已成交只回短确认,不要后台模板里「空运可锁舱」括号说明。
|
||
"""
|
||
no = (work_order_no or "").strip()
|
||
line = (business_line or "").strip().upper()
|
||
if outcome == "已成交":
|
||
if line == "MULTI":
|
||
return f"工单 {no} 已记录为「已成交」。"
|
||
return _r(
|
||
"deal.recorded_won",
|
||
f"已记录为「已成交」。工单 {no} 已关闭智能体跟进。",
|
||
ticket_no=no,
|
||
)
|
||
if outcome == "未成交":
|
||
reason = (lost_reason or "").strip()
|
||
if reason:
|
||
return f"已记录为「未成交」。{lost_reason_ack(reason, work_order_no=no)}"
|
||
return _r("lost.ask_reason", "已记录为「未成交」。请补充未成交原因。", ticket_no=no)
|
||
from agent.policy.event_exception import format_timeout_hours
|
||
|
||
# 小时缺省按后台默认 48;0 表示运营关掉再问,话术仍说明已记录协商中
|
||
hours_val = 48.0 if followup_hours is None else float(followup_hours)
|
||
hours_text = format_timeout_hours(hours_val if hours_val > 0 else 48.0)
|
||
when = _followup_when_text(next_followup_at) or f"{hours_text} 个工作小时后"
|
||
return _r(
|
||
"negotiating.recorded",
|
||
f"已记录为「协商中」。已顺延 {hours_text} 个工作小时,将于 {when} 再次发起成交状态询问。",
|
||
followup_when=when,
|
||
followup_hours=hours_text,
|
||
ticket_no=no,
|
||
)
|
||
|
||
|
||
def _followup_month_day(raw: str) -> str:
|
||
"""把主账再问时刻收成「9月24日」(旧模板兼容)。"""
|
||
when = _followup_when_text(raw)
|
||
if "日" in when:
|
||
return when.split("日", 1)[0] + "日"
|
||
return when
|
||
|
||
|
||
def _followup_when_text(raw: str) -> str:
|
||
"""把主账再问时刻收成「9月24日15:30」。"""
|
||
from datetime import datetime
|
||
|
||
text = (raw or "").strip()
|
||
if not text:
|
||
return ""
|
||
for fmt, n in (("%Y-%m-%d %H:%M:%S", 19), ("%Y-%m-%d %H:%M", 16), ("%Y-%m-%d", 10)):
|
||
try:
|
||
dt = datetime.strptime(text[:n], fmt)
|
||
if n <= 10:
|
||
return f"{dt.month}月{dt.day}日"
|
||
return f"{dt.month}月{dt.day}日{dt.hour}:{dt.minute:02d}"
|
||
except ValueError:
|
||
continue
|
||
return ""
|
||
|
||
|
||
def lost_reason_ack(reason: str = "", work_order_no: str = "") -> str:
|
||
"""
|
||
收下未成交原因后的回执。后台 lost.reason_ack。
|
||
|
||
后台模板里的 {{lost_reason}}、{{ticket_no}} 都在这里填。
|
||
不传工单号时占位留空串,避免群里再露出 {{ticket_no}}。
|
||
"""
|
||
text = (reason or "").strip()
|
||
no = (work_order_no or "").strip()
|
||
if text.endswith(("。", ".", "!", "!", "?", "?")):
|
||
default = f"已记下未成交原因:{text}"
|
||
else:
|
||
default = f"已记下未成交原因:{text}。" if text else "已记下未成交原因。"
|
||
return _r("lost.reason_ack", default, lost_reason=text, ticket_no=no)
|
||
|
||
|
||
def deal_notify_card(
|
||
*,
|
||
work_order_no: str,
|
||
sales_name: str,
|
||
facts: dict[str, str] | None,
|
||
quote: dict[str, Any] | None,
|
||
business_line: str,
|
||
) -> str:
|
||
"""成交通知文字兜底:方案卡压缩。不写销售,避免标题出现「销售 销售」。"""
|
||
no = (work_order_no or "").strip()
|
||
_ = sales_name
|
||
mode = scheme_transport_mode(business_line)
|
||
rows = scheme_horizontal_rows(
|
||
work_order_no=no,
|
||
facts=dict(facts or {}),
|
||
quote=dict(quote or {}),
|
||
transport_mode=mode,
|
||
)
|
||
lines = [f"成交通知 工单 {no}"]
|
||
lines.extend(f"{row['keyname']}:{row['value']}" for row in rows)
|
||
return "\n".join(lines)
|
||
|
||
|
||
def site_card_url() -> str:
|
||
"""
|
||
整张卡片点击后打开的后台地址。
|
||
|
||
事件异常、系统异常、成交通知都走这里,点开是同一个网站。
|
||
不用企业微信授权页,也不要用 work.weixin.qq.com。
|
||
"""
|
||
from agent.config import get_settings
|
||
|
||
base = str(getattr(get_settings(), "public_base_url", "") or "").strip().rstrip("/")
|
||
return base or "https://ai.ytd-scm.com"
|
||
|
||
|
||
def deal_notify_wecom_payload(
|
||
*,
|
||
work_order_no: str,
|
||
sales_name: str,
|
||
facts: dict[str, str] | None,
|
||
quote: dict[str, Any] | None,
|
||
business_line: str,
|
||
) -> dict[str, Any]:
|
||
"""
|
||
成交通知卡:走询价消息服务,不走询价小助手。
|
||
|
||
字段按报价方案卡压缩,避免超过 6 格。标题不写销售。
|
||
"""
|
||
no = (work_order_no or "").strip()
|
||
_ = sales_name
|
||
mode = scheme_transport_mode(business_line)
|
||
rows = scheme_horizontal_rows(
|
||
work_order_no=no,
|
||
facts=dict(facts or {}),
|
||
quote=dict(quote or {}),
|
||
transport_mode=mode,
|
||
)
|
||
action_url = site_card_url()
|
||
return {
|
||
"msgtype": "template_card",
|
||
"wecom_app": "notify",
|
||
"template_card": {
|
||
"card_type": "text_notice",
|
||
"source": {"desc": "成交通知", "desc_color": 0},
|
||
"main_title": {
|
||
"title": f"成交通知 工单 {no}",
|
||
},
|
||
"horizontal_content_list": rows,
|
||
"card_action": {"type": 1, "url": action_url},
|
||
},
|
||
}
|
||
|
||
|
||
def air_need_work_order() -> str:
|
||
"""
|
||
询价/空运协同群既没识别到工单号、本群也没有已激活当前单时的提示。
|
||
|
||
能对上工单(句中带 WO 号,或本群已绑定/激活过当前单)不走这句;
|
||
群里出现非 @BOT 文字会断开当前单关联,之后 @BOT 但没带号时就要用这句,
|
||
只教销售一种写法:先写「已成交WO****」,再 @ 机器人。
|
||
显示名取后台 system.bot_display_name(企微改名后仍能对上),禁止写死旧名。
|
||
后台可改:reply_code=inquiring.air_need_wo。
|
||
"""
|
||
from agent.policy.bot_name import at_bot, bot_display_name
|
||
|
||
bot = bot_display_name()
|
||
at = at_bot()
|
||
default = f"请以“已成交WO****{at}”方式给{bot}下达任务"
|
||
return _r("inquiring.air_need_wo", default)
|
||
|
||
|
||
def air_lock_need_work_order() -> str:
|
||
"""锁舱/释放必须自带工单号。后台 inquiring.air_lock_need_wo。"""
|
||
return _cabin_copy(
|
||
"inquiring.air_lock_need_wo",
|
||
"锁舱或取消舱位请带上工单号,例如 WO202609160001取消舱位。",
|
||
)
|
||
|
||
|
||
def air_ticket_not_found(work_order_no: str) -> str:
|
||
"""空运群找不到工单。后台 inquiring.air_ticket_not_found。"""
|
||
no = (work_order_no or "").strip()
|
||
return _r("inquiring.air_ticket_not_found", f"找不到工单{no},请核对工单号后再试。", ticket_no=no)
|
||
|
||
|
||
def air_ticket_not_air(work_order_no: str) -> str:
|
||
"""工单不是空运单。后台 inquiring.air_ticket_not_air。"""
|
||
no = (work_order_no or "").strip()
|
||
return _r(
|
||
"inquiring.air_ticket_not_air",
|
||
f"工单{no}不是空运单,不能在空运群里跟。",
|
||
ticket_no=no,
|
||
)
|
||
|
||
|
||
def air_ticket_closed(work_order_no: str, status: str) -> str:
|
||
"""空运工单已是终态仍要激活。后台 closed.air_cannot_activate。"""
|
||
no = (work_order_no or "").strip()
|
||
st = (status or "").strip() or "已关闭"
|
||
return _r(
|
||
"closed.air_cannot_activate",
|
||
f"工单{no}已是「{st}」,不能再激活。",
|
||
ticket_no=no,
|
||
status=st,
|
||
)
|
||
|
||
|
||
def air_ticket_occupied(work_order_no: str) -> str:
|
||
"""空运工单已在另一个群。后台 inquiring.air_ticket_occupied。"""
|
||
no = (work_order_no or "").strip()
|
||
return _r(
|
||
"inquiring.air_ticket_occupied",
|
||
f"工单{no}已经在另一个群跟了,请到那个群继续。",
|
||
ticket_no=no,
|
||
)
|
||
|
||
|
||
def air_group_brief(
|
||
*,
|
||
work_order_no: str,
|
||
facts: dict[str, str],
|
||
quote: dict[str, Any] | None,
|
||
collab_facts: dict[str, str] | None,
|
||
has_price: bool,
|
||
tms_quote: dict[str, Any] | None = None,
|
||
product_quote: dict[str, Any] | None = None,
|
||
sales_name: str = "",
|
||
airline_names: list[str] | None = None,
|
||
) -> str:
|
||
"""
|
||
空运群激活摘要。
|
||
|
||
有 TMS 报价:货物信息(含航司、计费重、货型)+ 协同补充 + 新价格段。
|
||
没有 TMS 报价、也没人手工报价:仍是「询价字段」+「TMS 暂无匹配报价」。
|
||
TMS 无价但航线已手工报价:省掉 TMS 无价段,直接出「人工报价:」(产品 2026-11-22 口径)。
|
||
品名正好是普货且没人补过时,含电含磁仍写「否」。
|
||
人工报价、销售调价有则接在后面,写法不改。
|
||
"""
|
||
no = (work_order_no or "").strip()
|
||
at_bits: list[str] = []
|
||
sales = (sales_name or "").strip()
|
||
if sales:
|
||
at_bits.append(f"@{sales}")
|
||
for name in airline_names or []:
|
||
token = f"@{(name or '').strip()}"
|
||
if (name or "").strip() and token not in at_bits:
|
||
at_bits.append(token)
|
||
tms, manual, adjust = _split_brief_quotes(
|
||
quote=quote,
|
||
tms_quote=tms_quote,
|
||
product_quote=product_quote,
|
||
has_price=has_price,
|
||
)
|
||
if tms:
|
||
from agent.schema.air_route_options import project_group_air_quote
|
||
|
||
tms = project_group_air_quote(tms, facts)
|
||
has_tms_price = bool(tms and (tms.get("total") or quote_fee_lines(tms)))
|
||
if has_tms_price:
|
||
# 有 TMS 报价才换成货物信息。没报价仍走下面的「询价字段」。
|
||
from agent.policy.air_quote_copy import air_cargo_lines
|
||
|
||
lines = ([(" ".join(at_bits))] if at_bits else [])
|
||
lines.extend(air_cargo_lines(work_order_no=no, facts=facts, quote=tms))
|
||
else:
|
||
lines = ([(" ".join(at_bits))] if at_bits else []) + [f"工单号:{no}", "询价字段:"]
|
||
lines.extend(
|
||
inquiry_field_lines(work_order_no=no, facts=facts, transport_mode="空运")[1:]
|
||
)
|
||
lines.append("")
|
||
lines.append("协同补充字段:")
|
||
collab = dict(collab_facts or {})
|
||
from agent.schema.field_validate import air_general_cargo_collab_defaults
|
||
|
||
# 品名是普货时,摘要里不能再把没写的含电/含磁显示成横杠。
|
||
for key, val in air_general_cargo_collab_defaults(facts).items():
|
||
current = (collab.get(key) or "").strip()
|
||
if current in {"", "-", "—", "-"}:
|
||
collab[key] = val
|
||
for key in AIR_COLLAB_FIELDS:
|
||
val = (collab.get(key) or "").strip()
|
||
if val in {"", "-", "—", "-"}:
|
||
val = "-"
|
||
lines.append(f"{key}:{val}")
|
||
manual_priced = _brief_quote_has_price(manual)
|
||
# 产品 2026-11-22:TMS 无价、航线已手工报价时,不再重复「TMS 暂无匹配报价」空话,
|
||
# 整段 TMS 省掉直接给人工报价;TMS 有价或还没人报价时仍保留 TMS 段。
|
||
if has_tms_price or not manual_priced:
|
||
lines.append("")
|
||
lines.append("TMS报价:")
|
||
if has_tms_price:
|
||
from agent.policy.air_quote_copy import air_price_lines
|
||
|
||
lines.extend(air_price_lines(tms, facts))
|
||
lines.append(group_tms_confirm_hint(business_line="AIR"))
|
||
else:
|
||
lines.append("TMS 暂无匹配报价")
|
||
lines.append(group_no_tms_quote_hint(business_line="AIR"))
|
||
if manual_priced:
|
||
lines.append("")
|
||
# 空运人工报价新版式自带「价格方案如下:」标题;只有上面还有 TMS 段时才靠它区分,
|
||
# TMS 段整段省掉后必须补「人工报价:」,否则群摘要一开头就是价格方案分不清谁报的。
|
||
if not _manual_layout_self_titled(manual, business_line="AIR") or not has_tms_price:
|
||
lines.append("人工报价:")
|
||
lines.extend(
|
||
_manual_quote_lines(
|
||
manual, business_line="AIR", facts=facts, layout="manual"
|
||
)
|
||
)
|
||
if adjust and (quote_fee_lines(adjust) or str(adjust.get("total") or "").strip() not in {"", "-"}):
|
||
lines.append("")
|
||
lines.append("销售调价:")
|
||
lines.extend(_manual_quote_lines(adjust, business_line="AIR"))
|
||
return "\n".join(lines)
|
||
|
||
|
||
def air_activate_summary(
|
||
*,
|
||
work_order_no: str,
|
||
facts: dict,
|
||
status: str = "",
|
||
wait_hint: str = "",
|
||
quote: dict[str, Any] | None = None,
|
||
collab_facts: dict[str, str] | None = None,
|
||
) -> str:
|
||
"""兼容旧名:激活成功改走海运同款群摘要。"""
|
||
_ = status, wait_hint
|
||
src = dict(quote or {})
|
||
return air_group_brief(
|
||
work_order_no=work_order_no,
|
||
facts=dict(facts or {}),
|
||
quote=src,
|
||
collab_facts=collab_facts,
|
||
has_price=bool(src.get("total") or src.get("quoteId")),
|
||
tms_quote=src if quote_is_tms(src) else {},
|
||
product_quote=src if quote_is_product(src) else {},
|
||
)
|
||
|
||
|
||
def air_wait_hint(*, status: str, has_quote: bool, pending_lock: bool = False) -> str:
|
||
"""激活摘要里的「现在等到哪一步」。"""
|
||
if pending_lock:
|
||
return "等待航线选择要锁的舱位选项"
|
||
st = (status or "").strip()
|
||
if st == "协商中":
|
||
return "协商中,可继续改价或确认成交"
|
||
if has_quote or st == "已报价":
|
||
return "已有报价,可补含电/含磁、航线改价、锁舱或确认成交"
|
||
return "等待航线报价或锁舱(须先有 TMS 报价)"
|
||
|
||
|
||
def air_airline_only_quote() -> str:
|
||
return "只有岗位为「航线·空运」的人能报价格。"
|
||
|
||
|
||
def air_airline_only_cabin() -> str:
|
||
"""只有航线能锁舱或释放。后台 inquiring.air_airline_only_cabin。"""
|
||
return _cabin_copy(
|
||
"inquiring.air_airline_only_cabin",
|
||
"只有岗位为「航线·空运」的人能锁舱或取消舱位。",
|
||
)
|
||
|
||
|
||
def air_sales_only_deal() -> str:
|
||
return "只有负责该工单的销售可以确认成交状态。"
|
||
|
||
|
||
def air_fields_not_recognized() -> str:
|
||
return "听到了。空运群里可补充是否含电、是否含磁,或报价日期。"
|
||
|
||
|
||
def air_quote_not_recognized() -> str:
|
||
return "没有识别到报价,请航线再发一次价格或 Excel/PDF。"
|
||
|
||
|
||
def handoff_deal_in_backend() -> str:
|
||
"""
|
||
转人工群里出现成交/未成交话术时的统一口径。
|
||
|
||
为什么不让 AI 直接落成交:产品说「这个价能做」常被模型当成成交,
|
||
销售还没跟客户确认就被动关单。转人工阶段只收报价,成交由销售在后台确认。
|
||
"""
|
||
return "转人工单不自动记成交/未成交,请销售在后台确认。报价可以继续发在群里。"
|
||
|
||
|
||
def handoff_raw_quote_saved(*, work_order_no: str) -> str:
|
||
"""
|
||
转人工群里产品发的是非模板话术:原话已留档,但没有拆成标准报价。
|
||
|
||
不报错、不催重发,避免群里有火药味;同时把口径说清楚——工单仍是询价中,
|
||
要正式报价可以按模板再发一次。
|
||
"""
|
||
no = (work_order_no or "").strip() or "WO"
|
||
return (
|
||
f"工单{no}已记录产品报价原话。\n"
|
||
"这句没能拆成标准报价,工单仍为询价中;如需入正式报价,请按报价项再发一次。"
|
||
)
|
||
|
||
|
||
def air_fields_denied() -> str:
|
||
return "只有这张单的销售或航线可以补含电/含磁或报价日期。"
|
||
|
||
|
||
def air_no_tms_quote_to_lock(work_order_no: str) -> str:
|
||
return air_lock_need_quote(work_order_no)
|
||
|
||
|
||
def air_lock_need_params(missing=None) -> str:
|
||
"""
|
||
舱位编号、航司、航班号(及报价日期空时)没齐时,只请航线补全。
|
||
|
||
这时还没锁舱,不能说 TMS 锁舱失败,也不能通知 IT。
|
||
缺哪项就点哪项;报价日期也空时示例末尾带日期。
|
||
"""
|
||
labels = [str(x).strip() for x in (missing or []) if str(x).strip()]
|
||
if not labels:
|
||
labels = ["舱位编号", "航司", "航班号"]
|
||
joined = "、".join(labels)
|
||
# 报价日期空:产品指定示例带日期,避免航线补完舱位三项后再被单独拦一次。
|
||
if "报价日期" in labels:
|
||
example = "AIR-OPT01,南航,航班CZ327,2026-10-18"
|
||
else:
|
||
example = "AIR-OPT01,南航,航班CZ327"
|
||
return f"锁舱参数不完整,缺少:{joined}。请补充后再锁,例如:{example}。"
|
||
|
||
|
||
def air_lock_need_quote(work_order_no: str) -> str:
|
||
"""没有任何报价时的锁舱拒绝。后台 inquiring.air_lock_need_quote。"""
|
||
no = (work_order_no or "").strip()
|
||
return _r(
|
||
"inquiring.air_lock_need_quote",
|
||
f"工单{no}还没有报价,不能锁舱。",
|
||
ticket_no=no,
|
||
)
|
||
|
||
|
||
def air_ask_lock_option(options: list) -> str:
|
||
lines = ["这张单有多条舱位选项,请回复要锁哪一条,例如 AIR-OPT-01:"]
|
||
for item in options:
|
||
no = str(item.get("optionNo") or item.get("option_no") or "").strip()
|
||
airline = str(item.get("airlineName") or item.get("airlineCode") or "").strip()
|
||
if no and airline:
|
||
lines.append(f"{no} {airline}")
|
||
elif no:
|
||
lines.append(no)
|
||
return "\n".join(lines)
|
||
|
||
|
||
def air_ask_manual_lock_fields(
|
||
*,
|
||
need_option: bool = False,
|
||
need_airline: bool = False,
|
||
need_chargeable: bool = False,
|
||
need_flight: bool = False,
|
||
options: list | None = None,
|
||
) -> str:
|
||
"""
|
||
手动报价锁舱缺 TMS MANUAL 必填项时,群里补问,禁止空壳出站。
|
||
|
||
例:请回复「AIR-OPT01 CZ 航班CZ327」。
|
||
"""
|
||
bits: list[str] = []
|
||
if need_option:
|
||
bits.append("舱位编号(如 AIR-OPT01)")
|
||
if need_airline:
|
||
bits.append("航司两位码(如 CZ)")
|
||
if need_flight:
|
||
bits.append("航班号(如 CZ327)")
|
||
if need_chargeable:
|
||
bits.append("计费重(公斤)")
|
||
need = "、".join(bits) if bits else "舱位编号、航司和航班号"
|
||
lines = [
|
||
f"锁舱还缺:{need}。",
|
||
"请按「AIR-OPT01 CZ 航班CZ327」回复;",
|
||
]
|
||
if need_chargeable:
|
||
lines.append("若还缺计费重,请再写「计费重xx公斤」。")
|
||
else:
|
||
lines.append("例如:AIR-OPT01 CZ 航班CZ327")
|
||
opts = options or []
|
||
shown = False
|
||
for item in opts:
|
||
if not isinstance(item, dict):
|
||
continue
|
||
no = str(item.get("optionNo") or item.get("option_no") or "").strip()
|
||
airline = str(item.get("airlineCode") or item.get("airlineName") or "").strip()
|
||
if not no:
|
||
continue
|
||
if not shown:
|
||
lines.append("可选舱位:")
|
||
shown = True
|
||
lines.append(f"{no} {airline}".strip() if airline else no)
|
||
return "\n".join(lines)
|
||
|
||
|
||
def air_lock_ok(
|
||
*,
|
||
work_order_no: str,
|
||
operator: str,
|
||
option_no: str,
|
||
exec_no: str,
|
||
quote_no: str,
|
||
origin: str = "",
|
||
destination: str = "",
|
||
airline: str = "",
|
||
flight_date: str = "",
|
||
) -> str:
|
||
"""
|
||
空运锁舱成功。后台 inquiring.air_lock_ok。
|
||
|
||
回执在报价编号下补起运地、目的地、航司、航班日期。
|
||
后台旧模板没有这四行时,这里补上,避免被旧句盖掉。
|
||
不编造空值;没取到就留空。
|
||
"""
|
||
origin_text = (origin or "").strip()
|
||
destination_text = (destination or "").strip()
|
||
airline_text = (airline or "").strip()
|
||
flight_text = (flight_date or "").strip()
|
||
route = (
|
||
f"起运地:{origin_text}\n"
|
||
f"目的地:{destination_text}\n"
|
||
f"航司:{airline_text}\n"
|
||
f"航班日期:{flight_text}"
|
||
)
|
||
default = (
|
||
"TMS空运锁舱成功。\n"
|
||
f"操作人:{operator}\n"
|
||
f"舱位选项:{option_no}\n"
|
||
f"执行编号:{exec_no}\n"
|
||
f"报价编号:{quote_no}\n"
|
||
f"{route}"
|
||
)
|
||
text = _r(
|
||
"inquiring.air_lock_ok",
|
||
default,
|
||
ticket_no=work_order_no,
|
||
operator=operator,
|
||
option_no=option_no,
|
||
exec_no=exec_no,
|
||
quote_no=quote_no,
|
||
origin=origin_text,
|
||
destination=destination_text,
|
||
airline=airline_text,
|
||
flight_date=flight_text,
|
||
)
|
||
body = (text or "").rstrip()
|
||
if "起运地:" in body and "航班日期:" in body:
|
||
return body
|
||
return f"{body}\n{route}" if body else route
|
||
|
||
|
||
# 多段里有空运段时,航线锁舱的失败原因。产品暂不支持,不当 TMS 故障。
|
||
MULTI_LOCK_UNSUPPORTED = "多段询价,暂时不支持锁舱"
|
||
# 后台锁舱失败模板末尾常有「已通知IT…请稍等」。多段这条要整句拿掉。
|
||
_IT_NOTICE_LINE = re.compile(r"已通知\s*IT[^\n。]*。?")
|
||
|
||
|
||
def multi_air_lock_unsupported(*, work_order_no: str, operator: str) -> str:
|
||
"""
|
||
多段含空运、航线锁舱时的群回复。
|
||
|
||
抬头、操作人、失败原因仍走锁舱失败模板,只去掉通知 IT 那句。
|
||
不打主账、不标系统异常。
|
||
"""
|
||
text = air_lock_fail(
|
||
work_order_no=work_order_no,
|
||
operator=operator,
|
||
reason=MULTI_LOCK_UNSUPPORTED,
|
||
)
|
||
kept: list[str] = []
|
||
for line in text.splitlines():
|
||
cleaned = _IT_NOTICE_LINE.sub("", line).strip()
|
||
if cleaned:
|
||
kept.append(cleaned)
|
||
return "\n".join(kept).strip()
|
||
|
||
|
||
def air_lock_fail(*, work_order_no: str, operator: str, reason: str) -> str:
|
||
"""空运锁舱失败(接口/系统类)。后台 inquiring.air_lock_fail。"""
|
||
why = (reason or "").strip() or "TMS未返回原因"
|
||
default = (
|
||
f"工单{work_order_no} 空运锁舱失败。\n"
|
||
f"操作人:{operator}\n"
|
||
f"锁舱失败的原因:{why}\n"
|
||
"已通知IT运维排查,请稍等。"
|
||
)
|
||
return _r(
|
||
"inquiring.air_lock_fail",
|
||
default,
|
||
ticket_no=work_order_no,
|
||
operator=operator,
|
||
reason=why,
|
||
)
|
||
|
||
|
||
def air_text_quote_for_lock_example(work_order_no: str = "") -> str:
|
||
"""
|
||
需要锁舱时,请航线用纯文字报价的示例句。
|
||
|
||
附件报价不拆金额、常锁不成;产品口径改为引导航线改发文字价。
|
||
示例必须带计费单位(空运常见 KG),避免航线漏写单位导致文字价无法落库。
|
||
"""
|
||
from agent.policy.bot_name import at_bot
|
||
|
||
no = (work_order_no or "").strip() or "工单号"
|
||
return f"{no},空运段,KG,空运费 单价CNY99.00,数量100 ,金额CNY9900.00{at_bot()}"
|
||
|
||
|
||
def air_attachment_quote_lock_tip(*, work_order_no: str) -> str:
|
||
"""
|
||
航线刚发完 Excel/PDF 附件报价后的提示。
|
||
|
||
附件只存档、不拆费用;若还要锁舱,必须再发一版纯文字报价。
|
||
"""
|
||
no = (work_order_no or "").strip() or "WO"
|
||
example = air_text_quote_for_lock_example(no)
|
||
return (
|
||
f"工单{no}附件报价已收。\n"
|
||
"如需锁舱,请航线人员改用纯文字报价(不要只发附件),例如:\n"
|
||
f"{example}"
|
||
)
|
||
|
||
|
||
def air_lock_need_text_quote(*, work_order_no: str, operator: str = "", reason: str = "") -> str:
|
||
"""
|
||
附件/手工报价锁舱走不通时:引导航线先发文字报价,不算系统异常、不通知 IT。
|
||
|
||
故意不回主账/TMS 原文「原因」(如 AIR-OPT / 运价库无匹配),只留文字报价指引。
|
||
reason 参数保留兼容旧调用,出站话术不用。
|
||
"""
|
||
_ = reason
|
||
no = (work_order_no or "").strip() or "WO"
|
||
who = (operator or "").strip()
|
||
lines = [f"工单{no} 还不能锁舱。"]
|
||
if who:
|
||
lines.append(f"操作人:{who}")
|
||
lines.append("如需锁舱,请航线人员先发纯文字报价,例如:")
|
||
lines.append(air_text_quote_for_lock_example(no))
|
||
lines.append("文字报价写入后再发锁舱口令(可带舱位、航司、航班号)。")
|
||
return "\n".join(lines)
|
||
|
||
|
||
def air_lock_incomplete(*, work_order_no: str, operator: str, reason: str = "") -> str:
|
||
"""
|
||
锁舱参数不齐(纯手工价、运价库无舱位等):业务补问,不算系统异常,不惊动 IT。
|
||
|
||
有 TMS / 非附件报价:仍补舱位三项。附件报价的「请发纯文字」只由
|
||
air_group_ops 在 source_label=航线附件报价 时走 air_lock_need_text_quote,
|
||
禁止按「无匹配报价」关键字在这里拐过去(会误伤 TMS 锁舱)。
|
||
"""
|
||
why = (reason or "").strip()
|
||
lines = [
|
||
f"工单{work_order_no} 还不能锁舱。",
|
||
f"操作人:{operator}",
|
||
]
|
||
if why:
|
||
lines.append(f"原因:{why}")
|
||
else:
|
||
lines.append("原因:本单没有可引用的 TMS 舱位(手工价或运价库无匹配)。")
|
||
lines.append("请直接回复「AIR-OPT01 CZ 航班CZ327」(换成实际舱位、航司、航班号)后再锁。")
|
||
return "\n".join(lines)
|
||
|
||
|
||
def air_cabin_fail_reason(*, tms_reason: str = "", flight_date: str = "", today: str = "") -> str:
|
||
"""
|
||
锁舱/释放失败原因:原样回 TMS 的 failReason。
|
||
禁止改写成「报价选项不存在」或「航班已起飞」。
|
||
flight_date / today 只为兼容旧调用,不再用来改写原文。
|
||
"""
|
||
_ = flight_date
|
||
_ = today
|
||
return (tms_reason or "").strip()
|
||
|
||
|
||
def air_release_ok(
|
||
*,
|
||
work_order_no: str,
|
||
operator: str,
|
||
option_no: str,
|
||
exec_no: str,
|
||
quote_no: str,
|
||
) -> str:
|
||
"""空运释放成功。后台 inquiring.air_release_ok。"""
|
||
default = (
|
||
f"工单{work_order_no} 已通知TMS执行空运取消舱位成功\n"
|
||
f"操作人:{operator}\n"
|
||
f"舱位选项:{option_no}\n"
|
||
f"执行编号:{exec_no}\n"
|
||
f"报价编号:{quote_no}"
|
||
)
|
||
return _cabin_copy(
|
||
"inquiring.air_release_ok",
|
||
default,
|
||
ticket_no=work_order_no,
|
||
operator=operator,
|
||
option_no=option_no,
|
||
exec_no=exec_no,
|
||
quote_no=quote_no,
|
||
)
|
||
|
||
|
||
def air_release_fail(*, work_order_no: str, operator: str, reason: str) -> str:
|
||
"""空运释放失败。后台 inquiring.air_release_fail。"""
|
||
why = (reason or "").strip() or "TMS未返回原因"
|
||
default = (
|
||
f"工单{work_order_no} 空运取消舱位失败。\n"
|
||
f"操作人:{operator}\n"
|
||
f"取消舱位失败的原因:{why}\n"
|
||
"已通知IT运维排查,请稍等。"
|
||
)
|
||
return _cabin_copy(
|
||
"inquiring.air_release_fail",
|
||
default,
|
||
ticket_no=work_order_no,
|
||
operator=operator,
|
||
reason=why,
|
||
)
|
||
|
||
|
||
def air_lock_ask_airline(*, work_order_no: str, airline_names: list[str] | None = None) -> str:
|
||
"""
|
||
已成交后点名航线去锁舱。只在空运群另发,不改共用的「已记录为已成交」。
|
||
|
||
后台 inquiring.air_lock_ask_airline。姓名来自岗位「航线·空运」。
|
||
"""
|
||
no = (work_order_no or "").strip()
|
||
names = [str(n or "").strip() for n in (airline_names or []) if str(n or "").strip()]
|
||
at = " ".join(f"@{n}" for n in names) if names else "@航线人员"
|
||
return _cabin_copy(
|
||
"inquiring.air_lock_ask_airline",
|
||
f"{no}请锁舱{at}",
|
||
ticket_no=no,
|
||
airline_names=at,
|
||
)
|
||
|
||
|
||
def air_lock_need_deal(*, sales_name: str) -> str:
|
||
"""
|
||
销售还没回复成交、未成交或协商中,航线就要锁。点名本单销售。
|
||
|
||
后台 inquiring.air_lock_need_deal。
|
||
"""
|
||
name = (sales_name or "").strip() or "销售"
|
||
return _cabin_copy(
|
||
"inquiring.air_lock_need_deal",
|
||
f"请销售@{name}先确认工单「已成交」",
|
||
sales_name=name,
|
||
)
|
||
|
||
|
||
def air_lock_blocked_negotiating(work_order_no: str) -> str:
|
||
"""协商中已经回过,不能再当成没回复。后台 inquiring.air_lock_negotiating。"""
|
||
no = (work_order_no or "").strip()
|
||
return _cabin_copy(
|
||
"inquiring.air_lock_negotiating",
|
||
f"工单{no}还在协商中,成交后才能锁舱。",
|
||
ticket_no=no,
|
||
)
|
||
|
||
|
||
def air_lock_blocked_lost(work_order_no: str) -> str:
|
||
"""未成交不能再请销售改口确认已成交。后台 inquiring.air_lock_lost。"""
|
||
no = (work_order_no or "").strip()
|
||
return _cabin_copy(
|
||
"inquiring.air_lock_lost",
|
||
f"工单{no}已是「未成交」,不能锁舱。",
|
||
ticket_no=no,
|
||
)
|
||
|
||
|
||
def air_release_deal_blocked(work_order_no: str) -> str:
|
||
"""
|
||
旧句:已成交不能取消舱位。
|
||
|
||
现口径是成交并锁舱之后仍可取消,群里不再发这句。后台编码保留,避免旧配置对不上。
|
||
"""
|
||
no = (work_order_no or "").strip()
|
||
return _cabin_copy(
|
||
"inquiring.air_release_deal_blocked",
|
||
f"取消舱位失败,工单{no}已成交。",
|
||
ticket_no=no,
|
||
)
|
||
|
||
|
||
def air_release_need_lock(work_order_no: str) -> str:
|
||
"""没有有效锁舱不能释放。后台 inquiring.air_release_need_lock。"""
|
||
no = (work_order_no or "").strip()
|
||
return _cabin_copy(
|
||
"inquiring.air_release_need_lock",
|
||
f"工单{no}还没有有效锁舱,不能取消舱位。",
|
||
ticket_no=no,
|
||
)
|
||
|
||
|
||
# —— 系统异常(UC024):销售/群兜底与恢复;告警卡只给有权限的人 ——
|
||
TYPE_LLM = "大模型异常"
|
||
TYPE_TMS = "TMS异常"
|
||
TYPE_GROUP = "建群异常"
|
||
|
||
STEP_LLM_NO_TICKET = "llm_no_ticket"
|
||
STEP_LLM = "llm"
|
||
STEP_TMS_QUOTE = "tms_quote"
|
||
STEP_TMS_CABIN = "tms_cabin"
|
||
STEP_TMS_LOCK = "tms_lock"
|
||
STEP_TMS_RELEASE = "tms_release"
|
||
STEP_GROUP = "group"
|
||
|
||
SYS_EXC_LLM_FALLBACK = "大模型服务异常,IT排查中..."
|
||
SYS_EXC_TMS_FALLBACK = "TMS接口异常,IT运维排查中..."
|
||
SYS_EXC_RECOVERY_TMS = "TMS已恢复正常使用,感谢您的耐心等待。"
|
||
SYS_EXC_RECOVERY_LLM = "大模型已恢复正常使用,感谢您的耐心等待。"
|
||
SYS_EXC_RECOVERY_CABIN = "空运舱位接口已恢复正常使用,感谢您的耐心等待。"
|
||
SYS_EXC_RECOVERY_GROUP = "建群已恢复正常,感谢您的耐心等待。"
|
||
|
||
IMPACT_LLM_NO_TICKET = "尚未建单,不落后台工单"
|
||
IMPACT_LLM = "当前识别暂停,已有字段与工单状态保持不变"
|
||
IMPACT_TMS_QUOTE = "当前询价流程暂停,识别字段与原业务节点保持不变"
|
||
IMPACT_TMS_CABIN = "当前锁舱/取消舱位暂停,工单状态保持不变"
|
||
IMPACT_TMS_LOCK = "锁舱失败,工单状态保持不变"
|
||
IMPACT_TMS_RELEASE = "取消舱位失败,工单状态保持不变"
|
||
|
||
|
||
def air_release_ask() -> str:
|
||
"""航线说了和舱位有关的「释放」时先问,不直接取消。"""
|
||
return "是否需要取消舱位?"
|
||
|
||
|
||
def air_release_ask_dropped() -> str:
|
||
"""航线否认这次取消。不打 TMS。"""
|
||
return "已取消这次操作"
|
||
IMPACT_GROUP = "当前建群/加人暂停,已建群不新建第二个群"
|
||
|
||
|
||
def system_exception_fallback(exception_type: str, *, names: list[str] | None = None) -> str:
|
||
"""判定系统异常后,给卡住的会话的兜底话术。后台 sys.llm_fail / sys.tms_fail / sys.group_add_fail。"""
|
||
kind = (exception_type or "").strip()
|
||
if kind == TYPE_LLM:
|
||
return _r("sys.llm_fail", SYS_EXC_LLM_FALLBACK)
|
||
if kind == TYPE_GROUP:
|
||
people = [str(x).strip() for x in (names or []) if str(x).strip()]
|
||
joined = "、".join(people)
|
||
if joined:
|
||
default = f"无法添加 {joined} 同事,请联系管理员检查企业微信配置。"
|
||
else:
|
||
default = "无法添加同事,请联系管理员检查企业微信配置。"
|
||
text = _r("sys.group_add_fail", default, staff_name=joined)
|
||
if not joined:
|
||
text = re.sub(r"无法添加\s+同事", "无法添加同事", text)
|
||
return text
|
||
return _r("sys.tms_fail", SYS_EXC_TMS_FALLBACK)
|
||
|
||
|
||
def system_exception_recovery(step: str) -> str:
|
||
"""后台点重试成功后,先发这一句,再补没做完的事。"""
|
||
key = (step or "").strip()
|
||
if key == STEP_LLM or key == STEP_LLM_NO_TICKET:
|
||
return _r("sys.llm_recovery", SYS_EXC_RECOVERY_LLM)
|
||
if key in {STEP_TMS_CABIN, STEP_TMS_LOCK, STEP_TMS_RELEASE}:
|
||
return _r("sys.cabin_recovery", SYS_EXC_RECOVERY_CABIN)
|
||
if key == STEP_GROUP:
|
||
return _r("sys.group_recovery", SYS_EXC_RECOVERY_GROUP)
|
||
return _r("sys.tms_recovery", SYS_EXC_RECOVERY_TMS)
|
||
|
||
|
||
def system_exception_impact(step: str) -> str:
|
||
"""告警卡「影响范围」,按卡住的步骤。"""
|
||
key = (step or "").strip()
|
||
if key == STEP_LLM_NO_TICKET:
|
||
return IMPACT_LLM_NO_TICKET
|
||
if key == STEP_LLM:
|
||
return IMPACT_LLM
|
||
if key == STEP_TMS_LOCK:
|
||
return IMPACT_TMS_LOCK
|
||
if key == STEP_TMS_RELEASE:
|
||
return IMPACT_TMS_RELEASE
|
||
if key == STEP_TMS_CABIN:
|
||
return IMPACT_TMS_CABIN
|
||
if key == STEP_GROUP:
|
||
return IMPACT_GROUP
|
||
return IMPACT_TMS_QUOTE
|
||
|
||
|
||
def system_exception_alert_payload(
|
||
*,
|
||
work_order_no: str,
|
||
exception_type: str,
|
||
service: str,
|
||
error_code: str,
|
||
step: str,
|
||
occurred_at: str,
|
||
test_prefix: bool = False,
|
||
) -> dict[str, Any]:
|
||
"""
|
||
系统异常通知卡:走询价消息服务,不走询价小助手。
|
||
|
||
五行:异常类型、接口/服务、报错原因、影响范围、发生时间。
|
||
标题不加 [test]:运维要看的就是这张卡本身,test_prefix 仅保留兼容入参。
|
||
"""
|
||
no = (work_order_no or "").strip() or "无"
|
||
_ = test_prefix
|
||
title = "系统异常通知"
|
||
rows = [
|
||
{"keyname": "异常类型", "value": (exception_type or "").strip() or "-"},
|
||
{"keyname": "接口/服务", "value": (service or "").strip() or "-"},
|
||
{"keyname": "报错原因", "value": (error_code or "").strip() or "-"},
|
||
{"keyname": "影响范围", "value": system_exception_impact(step)},
|
||
{"keyname": "发生时间", "value": (occurred_at or "").strip() or "-"},
|
||
]
|
||
action_url = site_card_url()
|
||
return {
|
||
"msgtype": "template_card",
|
||
"wecom_app": "notify",
|
||
"template_card": {
|
||
"card_type": "text_notice",
|
||
"source": {"desc": "询价机器人系统告警", "desc_color": 0},
|
||
"main_title": {
|
||
"title": title,
|
||
"desc": f"关联工单 {no}",
|
||
},
|
||
"horizontal_content_list": rows,
|
||
"card_action": {"type": 1, "url": action_url},
|
||
},
|
||
}
|
||
|
||
|
||
def system_exception_alert_text(
|
||
*,
|
||
work_order_no: str,
|
||
exception_type: str,
|
||
service: str,
|
||
error_code: str,
|
||
step: str,
|
||
occurred_at: str,
|
||
test_prefix: bool = False,
|
||
) -> str:
|
||
"""告警卡纯文本兜底(通道降级时仍能看懂)。标题同样不加 [test]。"""
|
||
no = (work_order_no or "").strip() or "无"
|
||
_ = test_prefix
|
||
head = "系统异常通知"
|
||
lines = [
|
||
"询价机器人系统告警",
|
||
head,
|
||
f"关联工单 {no}",
|
||
f"异常类型:{(exception_type or '').strip() or '-'}",
|
||
f"接口/服务:{(service or '').strip() or '-'}",
|
||
f"报错原因:{(error_code or '').strip() or '-'}",
|
||
f"影响范围:{system_exception_impact(step)}",
|
||
f"发生时间:{(occurred_at or '').strip() or '-'}",
|
||
]
|
||
return "\n".join(lines)
|
||
|
||
|
||
def event_exception_session_text(
|
||
*,
|
||
exception_type: str,
|
||
work_order_no: str,
|
||
names: list[str] | None,
|
||
hours: float,
|
||
mention: bool,
|
||
test_prefix: bool = False,
|
||
) -> str:
|
||
"""
|
||
事件异常会话两行话术。
|
||
|
||
mention=True 时姓名前加 @,多人空格拼接。小时数用后台规则值。
|
||
测服也不加 [test]:这是销售/航线/产品会话,不是运维告警。
|
||
"""
|
||
from agent.policy.event_exception import format_timeout_hours
|
||
|
||
_ = test_prefix
|
||
no = (work_order_no or "").strip() or "-"
|
||
kind = (exception_type or "").strip() or "事件异常"
|
||
hour_text = format_timeout_hours(hours)
|
||
people = [str(n).strip() for n in (names or []) if str(n).strip()]
|
||
if mention:
|
||
tagged = " ".join(f"@{n}" for n in people) if people else "相关人员"
|
||
else:
|
||
tagged = " ".join(people) if people else "您"
|
||
body = (
|
||
f"关联询价工单 {no}。{tagged}已超过 {hour_text} 工作小时 无响应,"
|
||
f"事件异常已记录工单,请及时处理。"
|
||
)
|
||
return f"{kind}\n{body}"
|
||
|
||
|
||
def event_exception_alert_payload(
|
||
*,
|
||
card_title: str,
|
||
work_order_no: str,
|
||
hours: float,
|
||
test_prefix: bool = False,
|
||
) -> dict[str, Any]:
|
||
"""
|
||
事件异常通知卡:走询价消息服务,不走询价小助手。
|
||
|
||
对齐现网「询价机器人事件告警」:标题用响应异常,正文用「您已超过」。
|
||
测服标题也不加 [test];test_prefix 仅保留兼容入参。
|
||
"""
|
||
from agent.policy.event_exception import format_timeout_hours
|
||
|
||
_ = test_prefix
|
||
no = (work_order_no or "").strip() or "-"
|
||
title = (card_title or "").strip() or "事件响应异常"
|
||
hour_text = format_timeout_hours(hours)
|
||
action_url = site_card_url()
|
||
return {
|
||
"msgtype": "template_card",
|
||
"wecom_app": "notify",
|
||
"template_card": {
|
||
"card_type": "text_notice",
|
||
"source": {"desc": "询价机器人事件告警", "desc_color": 0},
|
||
"main_title": {
|
||
"title": title,
|
||
"desc": f"关联询价工单 {no}",
|
||
},
|
||
"sub_title_text": f"您已超过 {hour_text} 工作小时无响应,事件异常已记录工单,请及时处理。",
|
||
"card_action": {"type": 1, "url": action_url},
|
||
},
|
||
}
|
||
|
||
|
||
def event_exception_alert_text(
|
||
*,
|
||
card_title: str,
|
||
work_order_no: str,
|
||
hours: float,
|
||
test_prefix: bool = False,
|
||
) -> str:
|
||
"""应用卡片纯文本兜底。"""
|
||
payload = event_exception_alert_payload(
|
||
card_title=card_title,
|
||
work_order_no=work_order_no,
|
||
hours=hours,
|
||
test_prefix=test_prefix,
|
||
)
|
||
card = payload.get("template_card") or {}
|
||
main = card.get("main_title") or {}
|
||
lines = [
|
||
str((card.get("source") or {}).get("desc") or "询价机器人事件告警"),
|
||
str(main.get("title") or ""),
|
||
str(main.get("desc") or ""),
|
||
str(card.get("sub_title_text") or ""),
|
||
]
|
||
return "\n".join(x for x in lines if x)
|