feat(shipping-notice): 添加识别定义和规则集管理功能,支持客户和站点的可配置识别规则

This commit is contained in:
hugo committed 2026-07-20 19:28:17 +08:00
1 parent 88408490d7
commit 34d489a2d1
167 files changed
+10365 -7978

No files matched your search

@@ -2,12 +2,12 @@
services/shipping_notice/query_service.py
用途:发货通知读模型 — 任务/批次查询、排序与序列化投影(含版本链与重复决策字段)。
依赖:models.ShippingBatch、ShippingCase、ShippingNoticeCandidate、ShippingTask、ShippingTaskEvent、task_line_store。
依赖:models.ShippingBatch、ShippingCase、ShippingNoticeCandidate、ShippingTask、ShippingTaskEvent、动态订单字段编辑器。
逻辑:workspace_shipping_notice 与 shipping_notice_tools 的读路径统一经本模块;
tools.py 通过 TaskProjection / BatchProjection 委托序列化与任务解析,写路径逐步下沉;
SUMMARY/DETAIL 经 _task_version_chain_fields 暴露 document_version / supersedes / duplicate_*;
DETAIL 另投影 parse_validation / process_field_gate / source_candidate 供持久只读展示;
订单行经 task_line_store.read_task_order_lines,支持 shadow 差异观测。
订单行仅从任务锁定 Extraction 的 FieldValue 投影,未抽取任务返回空行集。
"""
from __future__ import annotations
@@ -27,7 +27,10 @@ from app.models import (
ShippingNoticeIntake,
ShippingTask,
)
from app.services.shipping_notice.task_line_store import read_task_order_lines
from app.services.shipping_notice.task_order_field_editor import (
current_header_field_values_or_empty,
current_line_field_values_or_empty,
)
EXPORT_MIME_TYPE = "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet"
@@ -355,6 +358,7 @@ def _task_tool_payload(task: ShippingTask) -> dict[str, Any]:
from app.services.shipping_notice.rpa import rpa_runtime_mode
step_payload = task.step_payload_json or {}
header = current_header_field_values_or_empty(task)
payload: dict[str, Any] = {
"task_no": task.task_no,
"shipping_case_id": task.shipping_case_id,
@@ -365,17 +369,17 @@ def _task_tool_payload(task: ShippingTask) -> dict[str, Any]:
"review_status": task.review_status,
"match_status": task.match_status,
"pending_human_reason": task.pending_human_reason,
"customer_name": task.customer_name,
"customer_code": task.customer_code,
"customer_name": header.get('customer_name') or header.get('bill_to_customer'),
"customer_code": header.get('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,
"lines": read_task_order_lines(task),
"ship_to": header.get('ship_to') or header.get('ship_to_address'),
"po_number": header.get('po_number'),
"required_date": header.get('required_date'),
"lines": current_line_field_values_or_empty(task),
"shortage": task.shortage_json,
"line_results": task.line_results_json,
"export_file_name": task.export_file_name,
@@ -504,6 +508,7 @@ def serialize_task(task: ShippingTask, projection: TaskProjection) -> dict[str,
return _task_tool_payload(task)
if projection == TaskProjection.SUMMARY:
step_payload = task.step_payload_json or {}
header = current_header_field_values_or_empty(task)
data = {
"id": task.id,
"task_no": task.task_no,
@@ -516,7 +521,7 @@ def serialize_task(task: ShippingTask, projection: TaskProjection) -> dict[str,
"source_file_type": task.source_file_type,
"batch_index": task.batch_index,
"batch_total": task.batch_total,
"customer_name": task.customer_name,
"customer_name": header.get('customer_name') or header.get('bill_to_customer'),
"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,
@@ -531,6 +536,11 @@ def serialize_task(task: ShippingTask, projection: TaskProjection) -> dict[str,
"upload_attempt": task.upload_attempt,
"pending_human_reason": task.pending_human_reason,
"error_message": task.error_message or task.upload_error_message,
"lifecycle_status": str(getattr(task, 'lifecycle_status', 'active') or 'active'),
"deleted_at": task.deleted_at.isoformat() if getattr(task, 'deleted_at', None) else None,
"voided_at": task.voided_at.isoformat() if getattr(task, 'voided_at', None) else None,
"void_reason": getattr(task, 'void_reason', None),
"archived_at": task.archived_at.isoformat() if getattr(task, 'archived_at', None) else None,
"conversation_id": task.conversation_id,
"export_file_name": task.export_file_name,
"freight_ref_no": task.freight_ref_no,
@@ -558,20 +568,32 @@ def serialize_task(task: ShippingTask, projection: TaskProjection) -> dict[str,
"updated_at": task.updated_at.isoformat() if task.updated_at else None,
}
data.update(_task_version_chain_fields(task))
from app.services.shipping_notice.task_disposition_service import task_disposition_capability
data['disposition_capabilities'] = {
action: {
'allowed': result.allowed,
'code': result.code,
'reason': result.reason,
}
for action in ('delete', 'void', 'archive', 'restore')
for result in (task_disposition_capability(task, action),)
}
return data
if projection == TaskProjection.DETAIL:
step_payload = task.step_payload_json if isinstance(task.step_payload_json, dict) else {}
header = current_header_field_values_or_empty(task)
data = serialize_task(task, TaskProjection.SUMMARY)
data.update({
"customer_code": task.customer_code,
"ship_to": task.ship_to,
"po_number": task.po_number,
"required_date": task.required_date,
"customer_code": header.get('customer_code'),
"ship_to": header.get('ship_to') or header.get('ship_to_address'),
"po_number": header.get('po_number'),
"required_date": header.get('required_date'),
"step_payload": step_payload,
"upload_receipt": task.upload_receipt_json or {},
"export_version": task.export_version,
"idempotency_key": task.idempotency_key,
"lines": read_task_order_lines(task),
"lines": current_line_field_values_or_empty(task),
"shortage": task.shortage_json,
"line_results": task.line_results_json or [],
"parse_confidence": task.parse_confidence,
@@ -755,6 +777,67 @@ def list_tasks_for_user(
return [serialize_task_summary(c) for c in rows]
def list_tasks_page_for_user(
user_id: int,
*,
page: int = 1,
page_size: int = 20,
batch_no: str | None = None,
process_status: str | None = None,
lifecycle_status: str | None = None,
) -> dict[str, Any]:
"""
分页列出当前用户可见的发货任务摘要。
工作台任务量增长后必须由数据库分页,不能先拉取固定上限再在浏览器切页;
`items` 仅表示当前页,`total` 用于计算稳定的页码与空页回退。
"""
normalized_page = max(1, int(page))
normalized_page_size = max(1, min(int(page_size), 100))
q = ShippingTask.query.filter(
(ShippingTask.user_id == user_id) | (ShippingTask.user_id.is_(None))
)
if batch_no:
batch = get_batch_for_user(batch_no, user_id)
if not batch:
return {"items": [], "page": normalized_page, "page_size": normalized_page_size, "total": 0}
q = q.filter(ShippingTask.batch_id == batch.id)
if process_status:
if process_status == "shortage_review":
q = q.filter(
(ShippingTask.review_status == "shortage_review")
| (ShippingTask.pending_human_reason.in_(("shortage", "shortage_review")))
)
else:
q = q.filter(ShippingTask.process_status == process_status)
if lifecycle_status:
normalized_lifecycle = str(lifecycle_status).strip().lower()
if normalized_lifecycle == 'deleted':
q = q.filter(ShippingTask.deleted_at.is_not(None))
elif normalized_lifecycle == 'active':
q = q.filter(ShippingTask.deleted_at.is_(None), ShippingTask.lifecycle_status == 'active')
elif normalized_lifecycle == 'voided':
# 作废后移入回收站的任务仍须出现在“已作废”视图,便于业务审计与追溯。
q = q.filter(ShippingTask.lifecycle_status == normalized_lifecycle)
else:
q = q.filter(ShippingTask.deleted_at.is_(None), ShippingTask.lifecycle_status == normalized_lifecycle)
else:
q = q.filter(ShippingTask.deleted_at.is_(None), ShippingTask.lifecycle_status == 'active')
total = q.order_by(None).count()
rows = (
q.order_by(desc(ShippingTask.created_at))
.offset((normalized_page - 1) * normalized_page_size)
.limit(normalized_page_size)
.all()
)
return {
"items": [serialize_task_summary(item) for item in rows],
"page": normalized_page,
"page_size": normalized_page_size,
"total": total,
}
def get_task_for_user(task_no: str, user_id: int) -> ShippingTask | None:
"""按 task_no 查找任务并校验 user_id 归属。"""
task = get_task_by_ref(task_no)