feat(shipping-notice): 完成货代发货通知全流程开发与代码重构

- 新增PO号提取、预约ID解析(支持`PR@xxx`格式)工具,完善库存匹配逻辑
- 新增库存映射缓存、匹配/解析结果格式化摘要,优化用户提示
- 修复数据库迁移脚本未检查表存在的问题,避免全新部署失败
- 新增路径工具类统一管理相对路径,优化配置文件处理逻辑
- 归档旧文档,将部署、Dify配置合并到`shipping_notice.md`
- 新增完整单元测试与集成测试覆盖核心流程
- 更新前端国际化与会话管理逻辑,优化界面交互
This commit is contained in:
hugo committed 2026-06-04 15:26:34 +08:00
1 parent b62082758b
commit 8deae3146b
65 files changed
+6477 -1937

No files matched your search

+64 -7
View File
@@ -3,7 +3,7 @@ from __future__ import annotations
from typing import Any
from app.services.shipping_notice.reservation_id import normalize_reservation_id
from app.services.shipping_notice.reservation_id import extract_reservation_id
def _norm(value: Any) -> str:
@@ -11,7 +11,7 @@ def _norm(value: Any) -> str:
def _norm_reservation(value: Any) -> str:
return normalize_reservation_id(value)
return extract_reservation_id(value)
def _line_key(sku: str, reservation_id: str | None) -> tuple[str, str]:
@@ -25,11 +25,14 @@ def _aggregate_inventory(inventory_lines: list[dict[str, Any]]) -> dict[tuple[st
sku = _norm(row.get("sku"))
if not sku:
continue
reservation_id = _norm_reservation(row.get("reservation_id"))
if not reservation_id:
continue
try:
qty = float(row.get("available_qty") or 0)
except (TypeError, ValueError):
qty = 0.0
key = _line_key(sku, row.get("reservation_id"))
key = _line_key(sku, reservation_id)
totals[key] = totals.get(key, 0.0) + qty
return totals
@@ -41,10 +44,9 @@ def _available_for_notice_line(
) -> float:
sku_n = _norm(sku)
rid = _norm_reservation(reservation_id)
if rid:
return inv_totals.get((sku_n, rid), 0.0)
# 订单未带 Reservation ID 时,按 SKU 汇总所有 reservation 的可用量
return sum(qty for (s, _r), qty in inv_totals.items() if s == sku_n)
if not rid:
return 0.0
return inv_totals.get((sku_n, rid), 0.0)
def match_notice_lines(
@@ -102,3 +104,58 @@ def match_notice_lines(
match_status = "fulfillable"
return match_status, line_results, {"lines": shortages}
def _fmt_qty(value: float) -> str:
if value == int(value):
return str(int(value))
return f"{value:g}"
def format_match_summary(
match_status: str,
line_results: list[dict[str, Any]],
) -> str:
"""Human-readable order qty vs inventory qty for match_inventory responses."""
if not line_results:
return "无订单明细,无法比对数量。"
lines = ["匹配明细(订单数量 vs 库存可用量):"]
for i, row in enumerate(line_results, 1):
sku = str(row.get("sku") or "-")
reservation_id = str(row.get("reservation_id") or "-")
try:
required = float(row.get("required_qty") or 0)
except (TypeError, ValueError):
required = 0.0
try:
available = float(row.get("available_qty") or 0)
except (TypeError, ValueError):
available = 0.0
try:
shortage = float(row.get("shortage_qty") or 0)
except (TypeError, ValueError):
shortage = 0.0
status = str(row.get("line_status") or "")
status_label = {"ok": "充足", "partial": "部分满足", "shortage": "不足"}.get(
status, status or "未知"
)
detail = (
f"{i}. SKU {sku} | Reservation ID {reservation_id} | "
f"订单 {_fmt_qty(required)} / 库存 {_fmt_qty(available)} | {status_label}"
)
if shortage > 0:
detail += f"(缺口 {_fmt_qty(shortage)})"
lines.append(detail)
lines.append("")
if match_status == "fulfillable":
lines.append("结论:库存充足,可生成发货 Excel 并上传货代系统。")
elif match_status == "partial":
lines.append("结论:部分行库存不足,请复核缺口后决定是否部分发货。")
elif match_status == "unfulfillable":
lines.append("结论:库存均不足,无法整单发货。")
else:
lines.append(f"结论:match_status={match_status}")
return "\n".join(lines)