feat(shipping-notice): 新增货代客户管理功能,支持客户导入、识别证据管理与客户确认

This commit is contained in:
hugo committed 2026-07-16 08:16:19 +08:00
1 parent b595e157e9
commit 4fbaed2b4c
176 files changed
+14011 -994

No files matched your search

@@ -2,7 +2,7 @@
services/shipping_notice/query_service.py
用途:发货通知读模型 — 任务/批次查询、排序与序列化投影(含版本链与重复决策字段)。
依赖:models.ShippingBatch、ShippingTask、ShippingTaskEvent。
依赖:models.ShippingBatch、ShippingCase、ShippingTask、ShippingTaskEvent。
逻辑:workspace_shipping_notice 与 shipping_notice_tools 的读路径统一经本模块;
tools.py 通过 TaskProjection / BatchProjection 委托序列化与任务解析,写路径逐步下沉;
SUMMARY/DETAIL 经 _task_version_chain_fields 暴露 document_version / supersedes / duplicate_*。
@@ -18,11 +18,18 @@ from typing import Any
from sqlalchemy import asc, desc
from app import db
from app.models import ShippingBatch, ShippingTask
from app.models import (
ShippingBatch,
ShippingCase,
ShippingNoticeCandidate,
ShippingNoticeIntake,
ShippingTask,
)
EXPORT_MIME_TYPE = "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet"
_TASK_NO_RE = re.compile(r"SN-\d{8}-[A-F0-9]{8}", re.IGNORECASE)
_BUSINESS_NO_RE = re.compile(r"(?<![A-Z0-9])[A-Z]{2,6}\d{8}(?![A-Z0-9])", re.IGNORECASE)
class TaskProjection(str, Enum):
@@ -61,13 +68,33 @@ def sort_batch_tasks(tasks: list[ShippingTask]) -> list[ShippingTask]:
def get_task_by_ref(task_ref: str) -> ShippingTask | None:
"""按 task_no 或数字主键查找任务(Tool API / 内部引用解析)。"""
"""按 task_no、business_no 或数字主键查找任务;作废原号跟随替代链返回当前最新版本。"""
value = str(task_ref or "").strip()
if not value:
return None
task = ShippingTask.query.filter_by(task_no=value).first()
if task:
return task
case = db.session.query(ShippingCase).filter_by(business_no=value.upper()).first()
if case is not None:
visited: set[int] = set()
while case.id not in visited:
visited.add(int(case.id))
replacement = (
db.session.query(ShippingCase)
.filter_by(replaces_case_id=case.id)
.order_by(ShippingCase.id.desc())
.first()
)
if replacement is None:
break
case = replacement
return (
db.session.query(ShippingTask)
.filter_by(shipping_case_id=case.id)
.order_by(ShippingTask.document_version.desc(), ShippingTask.id.desc())
.first()
)
if value.isdigit():
return db.session.get(ShippingTask, int(value))
return None
@@ -81,6 +108,71 @@ def get_batch_by_no(batch_no: str) -> ShippingBatch | None:
return ShippingBatch.query.filter_by(batch_no=value).first()
def resolve_order_extraction_ui_target(
*,
user_id: int,
conversation_id: str,
intake_nos: list[str],
) -> tuple[ShippingBatch | None, ShippingTask | None]:
"""
按本轮 Intake 消费关系解析订单抽取完成后的 UI State 目标。
核心逻辑:先查当前用户、当前会话、指定 Intake 直接消费的新 Batch;没有新 Batch 时,
再沿 Candidate.consumed_task_id 查找打开的既有 Task。禁止回退到“用户最近批次”,避免并发会话串单。
Args:
user_id: 当前聊天用户 ID。
conversation_id: 当前 HXOPS ChatSession.session_id。
intake_nos: 本轮 agent_runtime_context.refs.intake_nos。
Returns:
`(batch, task)`;新建批次时 batch 非空,只有 open_existing 时 task 非空。
"""
normalized = sorted({str(item or '').strip().upper() for item in intake_nos if str(item or '').strip()})
session_id = str(conversation_id or '').strip()
if not normalized or not session_id:
return None, None
intakes = (
db.session.query(ShippingNoticeIntake)
.filter(
ShippingNoticeIntake.user_id == int(user_id),
ShippingNoticeIntake.conversation_id == session_id,
ShippingNoticeIntake.intake_no.in_(normalized),
)
.order_by(ShippingNoticeIntake.id.asc())
.all()
)
if {item.intake_no for item in intakes} != set(normalized):
return None, None
batch_ids = [int(item.consumed_batch_id) for item in intakes if item.consumed_batch_id is not None]
if batch_ids:
batch = (
db.session.query(ShippingBatch)
.filter(
ShippingBatch.id.in_(batch_ids),
ShippingBatch.user_id == int(user_id),
ShippingBatch.conversation_id == session_id,
)
.order_by(ShippingBatch.created_at.desc(), ShippingBatch.id.desc())
.first()
)
if batch is not None:
return batch, None
task = (
db.session.query(ShippingTask)
.join(ShippingNoticeCandidate, ShippingNoticeCandidate.consumed_task_id == ShippingTask.id)
.filter(
ShippingNoticeCandidate.intake_id.in_([int(item.id) for item in intakes]),
ShippingTask.user_id == int(user_id),
)
.order_by(ShippingNoticeCandidate.id.asc(), ShippingTask.id.asc())
.first()
)
return None, task
def get_task_in_batch(batch_no: str, task_no: str) -> ShippingTask | None:
"""查找属于指定批次的任务;批次不存在或任务不在批次内时返回 None。"""
batch = get_batch_by_no(batch_no)
@@ -197,8 +289,9 @@ def compute_conversation_workspace_summary(batches: list[ShippingBatch]) -> dict
def extract_task_no_from_text(text: str) -> str | None:
"""从自然语言文本中提取 SN- 任务编号。"""
match = _TASK_NO_RE.search(str(text or ""))
"""从自然语言文本中提取 SN 技术号或客户业务号。"""
raw = str(text or "")
match = _TASK_NO_RE.search(raw) or _BUSINESS_NO_RE.search(raw)
return match.group(0).upper() if match else None
@@ -253,6 +346,9 @@ def _task_tool_payload(task: ShippingTask) -> dict[str, Any]:
step_payload = task.step_payload_json or {}
payload: dict[str, Any] = {
"task_no": task.task_no,
"shipping_case_id": task.shipping_case_id,
"case_no": task.shipping_case.case_no if task.shipping_case else None,
"business_no": task.shipping_case.business_no if task.shipping_case else None,
"process_status": task.process_status,
"current_step": task.current_step,
"review_status": task.review_status,
@@ -260,6 +356,11 @@ def _task_tool_payload(task: ShippingTask) -> dict[str, Any]:
"pending_human_reason": task.pending_human_reason,
"customer_name": task.customer_name,
"customer_code": task.customer_code,
"customer_resolution_status": task.customer_resolution_status,
"customer_resolution_method": task.customer_resolution_method,
"customer_id": task.shipping_case.customer_id if task.shipping_case else None,
"customer_name_snapshot": task.shipping_case.customer_name_snapshot if task.shipping_case else None,
"customer_short_code_snapshot": task.shipping_case.customer_short_code_snapshot if task.shipping_case else None,
"ship_to": task.ship_to,
"po_number": task.po_number,
"required_date": task.required_date,
@@ -343,6 +444,9 @@ def serialize_task(task: ShippingTask, projection: TaskProjection) -> dict[str,
data = {
"id": task.id,
"task_no": task.task_no,
"shipping_case_id": task.shipping_case_id,
"case_no": task.shipping_case.case_no if task.shipping_case else None,
"business_no": task.shipping_case.business_no if task.shipping_case else None,
"batch_no": _task_batch_no(task),
"source_type": task.source_type,
"source_file_name": task.source_file_name,
@@ -350,6 +454,11 @@ def serialize_task(task: ShippingTask, projection: TaskProjection) -> dict[str,
"batch_index": task.batch_index,
"batch_total": task.batch_total,
"customer_name": task.customer_name,
"customer_resolution_status": task.customer_resolution_status,
"customer_resolution_method": task.customer_resolution_method,
"customer_id": task.shipping_case.customer_id if task.shipping_case else None,
"customer_name_snapshot": task.shipping_case.customer_name_snapshot if task.shipping_case else None,
"customer_short_code_snapshot": task.shipping_case.customer_short_code_snapshot if task.shipping_case else None,
"current_step": task.current_step,
"process_status": task.process_status,
"review_status": task.review_status,
@@ -371,7 +480,7 @@ def serialize_task(task: ShippingTask, projection: TaskProjection) -> dict[str,
"destination_country": task.destination_country,
"warehouse_hint": task.warehouse_hint,
"inventory_source_code": task.inventory_source_code,
"inventory_source_type": task.inventory_source_type,
"inventory_connector_type": task.inventory_connector_type,
"inventory_site_id": task.inventory_site_id,
"inventory_warehouse_id": task.inventory_warehouse_id,
"inventory_source_status": task.inventory_source_status,