Files
syops/backend/app/services/shipping_notice/i18n.py
T
hugo c18c0394d0 feat(shipping_notice): 完成货代发货通知全流程功能迭代与重构
主要变更:
1.  移除废弃的发货通知字段抽取独立Workflow Agent相关代码
2.  重构Dify服务调用,新增inputs参数支持注入环境变量配置的工具API地址与密钥
3.  新增auto_progress工具API与checkpoint catalog接口,支持流程自动推进
4.  统一字段命名,将column_mapping_json改为map_json并兼容旧有调用
5.  重构库存匹配、解析结果的展示格式,改为标准化多语言Markdown表格
6.  新增ODM ID库存字段支持,优化库存解析与映射逻辑
7.  新增LibreOffice转换隔离Profile配置,避免多实例运行冲突
8.  新增完整环境变量配置、数据库迁移脚本与测试用例
9.  适配前端代码,更新字段命名与聊天跳转逻辑
10. 更新OpenAPI规范适配Dify工具调用,删除已归档的旧部署文档
2026-06-08 16:07:20 +08:00

209 lines
12 KiB
Python
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
from __future__ import annotations
from typing import Any
from flask import has_request_context, request
SUPPORTED_LANGS = ("zh", "en", "th")
_MESSAGES: dict[str, dict[str, str]] = {
"zh": {
"parse.title": "【发货通知解析确认】任务编号:{task_no}",
"parse.no_lines": "未解析到订单明细行,请重新上传或重新解析。",
"parse.low_conf": "解析置信度较低,请重点核对料号、预订单号、数量、单价与金额。",
"parse.confirm": "请确认以上信息是否正确?",
"parse.hint": "回复「确认」进入库存检查;「重新解析」重试;「驳回」终止本任务。",
"parse.hint_btn": "",
"align.title": "【库存字段对齐确认 — 确认后再匹配】任务编号:{task_no}",
"align.match_rule": "匹配规则:{rule}{note}",
"align.table.sys_field": "系统字段",
"align.table.inv_col": "库存 Excel 列",
"align.table.notice_field": "订单字段",
"align.table.display_only": "unit_price / amount(展示用,不参与匹配)",
"align.sample.notice": "订单样例:",
"align.sample.inventory": "库存样例:",
"align.stats": "统计:{stats}",
"align.source.llm": "(LLM 推断)",
"align.source.fallback": "(后端动态兜底)",
"align.source.other": "({source})",
"align.confirm": "请确认以上字段对齐是否正确。",
"align.hint": "回复「确认」进入匹配;「更正映射」重选列;「重新上传」换库存文件;「驳回」终止本任务。",
"align.hint_btn": "",
"common.tips": "提示:",
"table.field": "字段",
"table.value": "值",
"field.po": "采购单号",
"field.sku": "料号",
"field.reservation_id": "预订单号",
"field.qty": "数量",
"field.unit_price": "单价",
"field.amount": "金额",
"field.required_date": "需求日期",
"field.ship_to": "收货方",
"field.vendor_part_number": "供应商料号",
"field.line_no": "项次",
"field.available_qty": "可用数量",
"field.aws_sku": "料号",
"match.title": "【库存匹配结果】任务编号:{task_no}",
"match.subtitle": "匹配明细(订单数量 vs 库存可用量)",
"match.col.order_qty": "订单需求",
"match.col.inv_qty": "库存可用",
"match.col.gap": "缺口",
"match.col.status": "状态",
"match.status.ok": "✅ 充足",
"match.status.partial": "⚠️ 部分满足",
"match.status.shortage": "❌ 不足",
"match.status.unknown": "未知",
"match.conclusion.fulfillable": "**结论**:库存充足,可生成发货 Excel 并上传货代系统。",
"match.conclusion.partial": "**结论**:部分行库存不足,请复核缺口后决定是否部分发货。",
"match.conclusion.unfulfillable": "**结论**:库存均不足,无法整单发货。",
"match.conclusion.other": "**结论**:match_status={status}",
"match.no_lines": "无订单明细,无法比对数量。",
},
"en": {
"parse.title": "[Shipping notice parsed] Task: {task_no}",
"parse.no_lines": "No order lines found. Please re-upload or re-parse.",
"parse.low_conf": "Low confidence. Please double-check SKU, Qty, Reservation ID, Unit Price and Amount.",
"parse.confirm": "Please confirm the above information.",
"parse.hint": 'Reply "Confirm" to proceed; "Re-parse" to retry; "Reject" to stop this task.',
"parse.hint_btn": "",
"align.title": "[Inventory mapping confirmation] Task: {task_no}",
"align.match_rule": "Match rule: {rule}{note}",
"align.table.sys_field": "System field",
"align.table.inv_col": "Inventory column",
"align.table.notice_field": "Order field",
"align.table.display_only": "unit_price / amount (display only)",
"align.sample.notice": "Order samples:",
"align.sample.inventory": "Inventory samples:",
"align.stats": "Stats: {stats}",
"align.source.llm": " (LLM)",
"align.source.fallback": " (backend fallback)",
"align.source.other": " ({source})",
"align.confirm": "Please confirm the field alignment above.",
"align.hint": 'Reply "Confirm" to match; "Fix mapping" to reselect columns; "Re-upload" to change the inventory file; "Reject" to stop.',
"align.hint_btn": "",
"common.tips": "Notes:",
"table.field": "Field",
"table.value": "Value",
"field.po": "PO",
"field.sku": "SKU",
"field.reservation_id": "Reservation ID",
"field.qty": "Qty",
"field.unit_price": "Unit Price",
"field.amount": "Amount",
"field.required_date": "Required date",
"field.ship_to": "Ship to",
"field.vendor_part_number": "Vendor PN",
"field.line_no": "Line",
"field.available_qty": "Available qty",
"field.aws_sku": "AWS Sku",
"match.title": "[Inventory match result] Task: {task_no}",
"match.subtitle": "Match details (order qty vs inventory available)",
"match.col.order_qty": "Order qty",
"match.col.inv_qty": "Available",
"match.col.gap": "Gap",
"match.col.status": "Status",
"match.status.ok": "✅ OK",
"match.status.partial": "⚠️ Partial",
"match.status.shortage": "❌ Short",
"match.status.unknown": "Unknown",
"match.conclusion.fulfillable": "**Result**: Inventory is sufficient. You may generate the shipment Excel and upload.",
"match.conclusion.partial": "**Result**: Some lines are short. Review gaps before partial shipment.",
"match.conclusion.unfulfillable": "**Result**: Insufficient inventory for all lines.",
"match.conclusion.other": "**Result**: match_status={status}",
"match.no_lines": "No order lines to compare.",
},
"th": {
"parse.title": "【ยืนยันการแยกใบแจ้งส่งสินค้า】งาน: {task_no}",
"parse.no_lines": "ไม่พบบรรทัดรายการสั่งซื้อ กรุณาอัปโหลดใหม่หรือแยกใหม่อีกครั้ง",
"parse.low_conf": "ความมั่นใจต่ำ กรุณาตรวจสอบ SKU, จำนวน, Reservation ID, ราคาต่อหน่วย และจำนวนเงิน",
"parse.confirm": "กรุณายืนยันว่าข้อมูลด้านบนถูกต้องหรือไม่",
"parse.hint": 'พิมพ์ "ยืนยัน" เพื่อไปต่อ; "แยกใหม่" เพื่อลองใหม่; "ปฏิเสธ" เพื่อยุติงานนี้',
"parse.hint_btn": "",
"align.title": "【ยืนยันการจับคู่คอลัมน์สต็อก】งาน: {task_no}",
"align.match_rule": "กฎการจับคู่: {rule}{note}",
"align.table.sys_field": "ฟิลด์ระบบ",
"align.table.inv_col": "คอลัมน์สต็อก",
"align.table.notice_field": "ฟิลด์คำสั่งซื้อ",
"align.table.display_only": "unit_price / amount (แสดงผลเท่านั้น)",
"align.sample.notice": "ตัวอย่างคำสั่งซื้อ:",
"align.sample.inventory": "ตัวอย่างสต็อก:",
"align.stats": "สถิติ: {stats}",
"align.source.llm": " (LLM)",
"align.source.fallback": " (สำรองโดยระบบ)",
"align.source.other": " ({source})",
"align.confirm": "กรุณายืนยันว่าการจับคู่ฟิลด์ด้านบนถูกต้อง",
"align.hint": 'พิมพ์ "ยืนยัน" เพื่อจับคู่; "แก้ไขการจับคู่" เพื่อเลือกคอลัมน์ใหม่; "อัปโหลดใหม่" เพื่อเปลี่ยนไฟล์สต็อก; "ปฏิเสธ" เพื่อยุติ',
"align.hint_btn": "",
"common.tips": "หมายเหตุ:",
"table.field": "ฟิลด์",
"table.value": "ค่า",
"field.po": "PO",
"field.sku": "SKU",
"field.reservation_id": "Reservation ID",
"field.qty": "จำนวน",
"field.unit_price": "ราคาต่อหน่วย",
"field.amount": "จำนวนเงิน",
"field.required_date": "วันที่ต้องการ",
"field.ship_to": "ผู้รับ",
"field.vendor_part_number": "หมายเลขชิ้นส่วนผู้ขาย",
"field.line_no": "รายการ",
"field.available_qty": "จำนวนคงเหลือ",
"field.aws_sku": "AWS Sku",
"match.title": "【ผลการจับคู่สต็อก】งาน: {task_no}",
"match.subtitle": "รายละเอียดการจับคู่ (จำนวนสั่งซื้อ vs สต็อกคงเหลือ)",
"match.col.order_qty": "จำนวนสั่ง",
"match.col.inv_qty": "สต็อกคงเหลือ",
"match.col.gap": "ส่วนต่าง",
"match.col.status": "สถานะ",
"match.status.ok": "✅ เพียงพอ",
"match.status.partial": "⚠️ บางส่วน",
"match.status.shortage": "❌ ไม่พอ",
"match.status.unknown": "ไม่ทราบ",
"match.conclusion.fulfillable": "**สรุป**: สต็อกเพียงพอ สามารถสร้าง Excel และอัปโหลดได้",
"match.conclusion.partial": "**สรุป**: บางรายการสต็อกไม่พอ กรุณาตรวจสอบส่วนต่าง",
"match.conclusion.unfulfillable": "**สรุป**: สต็อกไม่เพียงพอทั้งหมด",
"match.conclusion.other": "**สรุป**: match_status={status}",
"match.no_lines": "ไม่มีรายการสั่งซื้อสำหรับเปรียบเทียบ",
},
}
def normalize_lang(raw: Any) -> str:
text = str(raw or "").strip().lower()
if not text:
return "zh"
if text in {"zh-cn", "zh_hans", "zh-hans", "cn", "zh"}:
return "zh"
if text in {"en-us", "en_us", "en"}:
return "en"
if text in {"th-th", "th_th", "th"}:
return "th"
for prefix in SUPPORTED_LANGS:
if text.startswith(prefix):
return prefix
return "zh"
def resolve_lang(payload: dict[str, Any] | None = None) -> str:
if isinstance(payload, dict):
for key in ("lang", "locale", "language"):
if key in payload:
val = str(payload.get(key) or "").strip()
if val:
return normalize_lang(val)
if has_request_context():
hdr = str(request.headers.get("X-Lang") or "").strip()
if hdr:
return normalize_lang(hdr)
accept = str(request.headers.get("Accept-Language") or "").strip()
if accept:
return normalize_lang(accept.split(",")[0].strip())
return "zh"
def t(key: str, *, lang: str | None = None) -> str:
resolved = normalize_lang(lang) if lang else "zh"
return _MESSAGES.get(resolved, {}).get(key) or _MESSAGES["zh"].get(key) or key