feat(freight-rpa): 库存/收料改名在手/入站,报表归属RPA,类名统一FreightRpa

把 shipping 侧 inventory/receipt 词根迁到 onhand/inbound,29 张表与
枚举/权限/前端/i18n 同步改名;在手与入站报表配置及文件改由 freight_rpa
持有,发货与调拨仅按 source_code 消费;ORM 类加 FreightRpa 前缀,
上传动作改为 freight_rpa_upload。含类名与上传动作改名规格文档。

Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
hugoandCursor committed 2026-10-19 10:46:06 +08:00
1 parent ffc0d4a430
commit 39fe3c05f2
453 files changed
+17945 -8646

No files matched your search

@@ -3,7 +3,7 @@ services/shipping_notice/query_service.py
用途:发货通知读模型 — 任务查询、排序与序列化投影(含版本链、待办分类与任务中心字段)。
依赖:ShippingOrderCandidate、ShippingNotice、ShippingTaskEvent、
ShippingReservation、ShippingNoticeLine、ShippingNoticeLineInventoryMatch、ShippingOrderReviewCase、ShippingOrderProcessingRun、
ShippingReservation、ShippingNoticeLine、ShippingNoticeLineOnhandMatch、ShippingOrderReviewCase、ShippingOrderProcessingRun、
ShippingOrderIntake / ShippingOrderProcessingRunIntake / FileAsset、state_machine、动态订单字段编辑器、
order_review_projection(任务中心 STP retry 动作投影)。
逻辑:workspace_shipping_notice 与 shipping_notice_tools 的读路径统一经本模块;
@@ -43,8 +43,8 @@ from app.models import (
FileAsset,
ShippingExportArtifact,
ShippingExportArtifactLine,
ShippingInventoryVerification,
ShippingInventoryFile,
ShippingOnhandVerification,
FreightRpaOnhandReportFile,
ShippingOrderCandidate,
ShippingOrderDocument,
ShippingOrderIntake,
@@ -64,9 +64,9 @@ from app.models import (
ShippingCustomerShadowResolution,
ShippingProcessExecution,
ShippingProcessGate,
ShippingTaskInventoryLink,
ShippingTaskOnhandLink,
ShippingNoticeLine,
ShippingNoticeLineInventoryMatch,
ShippingNoticeLineOnhandMatch,
ShippingTaskEvent,
ShippingUploadAttempt,
)
@@ -125,9 +125,9 @@ _REVIEW_TODO_GATE_CODES = frozenset({
'reservation_validation',
})
_EXCEPTION_TODO_GATE_CODES = frozenset({
'inventory_sufficiency',
'onhand_sufficiency',
'freight_upload_rpa',
'inventory_source_routing',
'onhand_source_routing',
'excel_readiness',
})
_RUNNING_EXECUTION_STATUSES = frozenset({'active', 'waiting_external'})
@@ -281,8 +281,8 @@ class TaskProjection(str, Enum):
def _fulfillment_timeline(task: ShippingNotice) -> dict[str, list[dict[str, Any]]]:
"""为工作台投影库存核验、滚动 Artifact 与上传 Attempt,所有事实只读且按时间排序。"""
inventory_task_id = int(getattr(task, 'order_task_id', None) or task.id)
verifications = ShippingInventoryVerification.query.filter_by(task_id=inventory_task_id).order_by(
ShippingInventoryVerification.created_at, ShippingInventoryVerification.id,
verifications = ShippingOnhandVerification.query.filter_by(task_id=inventory_task_id).order_by(
ShippingOnhandVerification.created_at, ShippingOnhandVerification.id,
).all()
artifacts = []
if isinstance(task, ShippingNotice):
@@ -307,7 +307,7 @@ def _fulfillment_timeline(task: ShippingNotice) -> dict[str, list[dict[str, Any]
).order_by(ShippingUploadAttempt.created_at, ShippingUploadAttempt.id).all() if artifact_ids else []
return {
'inventory_verifications': [{
'id': item.id, 'status': item.status, 'inventory_snapshot_id': item.inventory_snapshot_id,
'id': item.id, 'status': item.status, 'inventory_snapshot_id': item.onhand_snapshot_id,
'source_config_version': item.source_config_version, 'created_at': item.created_at.isoformat() if item.created_at else None,
'lines': [{
'reservation_id': reservation_ids.get(int(line.reservation_id), str(line.reservation_id)), 'required_qty': str(line.required_qty),
@@ -486,7 +486,7 @@ def _task_tool_payload(task: ShippingNotice) -> dict[str, Any]:
"""Tool data 基础字段(裁剪前)。"""
if isinstance(task, ShippingOrderTask):
header, lines, _field_display = _order_values_for_projection(task)
from app.services.shipping_notice.line_inventory_match_projection import load_current_match_facts_by_task
from app.services.shipping_notice.line_onhand_match_projection import load_current_match_facts_by_task
match_facts = list(load_current_match_facts_by_task(task.id).values())
customer = _order_task_customer_projection(task)
@@ -517,7 +517,7 @@ def _task_tool_payload(task: ShippingNotice) -> dict[str, Any]:
'conversation_id': task.conversation_id,
'step_payload': task.step_payload_json or {},
**inventory,
'inventory_snapshot': _serialize_inventory_snapshot(task),
'onhand_snapshot': _serialize_inventory_snapshot(task),
}
from app.services.shipping_notice.notice_upload import max_notice_chars
from app.services.freight_rpa.common.rpa_runtime import rpa_runtime_mode
@@ -560,9 +560,9 @@ def _task_tool_payload(task: ShippingNotice) -> dict[str, Any]:
}
raw_content = str(getattr(workflow_owner, 'raw_content', None) or "").strip()
lines = payload["lines"]
from app.services.shipping_notice.inventory_snapshot_store import serialize_inventory_snapshot_link
from app.services.shipping_notice.onhand_snapshot_store import serialize_inventory_snapshot_link
payload["inventory_snapshot"] = serialize_inventory_snapshot_link(task)
payload["onhand_snapshot"] = serialize_inventory_snapshot_link(task)
# Dify 画布 LLM 字段抽取需要正文;已落库订单行后不再重复下发全文。
if raw_content and not lines:
limit = max_notice_chars()
@@ -574,7 +574,7 @@ def _task_tool_payload(task: ShippingNotice) -> dict[str, Any]:
def _current_match_projection(task: ShippingNotice | ShippingOrderTask) -> dict[str, Any]:
"""从关联 OT 的行级 Match 生成通知准备读模型,禁止读取 Notice 聚合缓存。"""
from app.services.shipping_notice.line_inventory_match_projection import load_current_match_facts_by_task
from app.services.shipping_notice.line_onhand_match_projection import load_current_match_facts_by_task
task_id = getattr(task, 'order_task_id', None) or getattr(task, 'id', None)
rows = list(load_current_match_facts_by_task(task_id).values())
@@ -652,7 +652,7 @@ def _order_values_for_projection(task: ShippingOrderTask | ShippingNotice) -> tu
def _serialize_inventory_snapshot(task: ShippingNotice) -> dict[str, Any] | None:
"""工作台任务摘要中的库存快照投影。"""
from app.services.shipping_notice.inventory_snapshot_store import serialize_inventory_snapshot_link
from app.services.shipping_notice.onhand_snapshot_store import serialize_inventory_snapshot_link
return serialize_inventory_snapshot_link(task)
@@ -675,7 +675,7 @@ def _order_task_customer_projection(task: ShippingOrderTask) -> dict[str, Any]:
def _order_task_inventory_projection(task: ShippingOrderTask) -> dict[str, Any]:
"""从 CandidateSet 与行级 Match 读取 OT 库存路由投影,禁止读 OT 旧列。"""
from app.services.shipping_notice.inventory_candidate_projection import project_inventory_candidate_facts
from app.services.shipping_notice.onhand_candidate_projection import project_inventory_candidate_facts
facts = project_inventory_candidate_facts(task)
sources = list(facts.get('sources') or [])
@@ -816,7 +816,7 @@ def serialize_task(task: ShippingNotice, projection: TaskProjection) -> dict[str
for result in (task_disposition_capability(task, action),)
}
step_payload = task.step_payload_json or {}
from app.services.shipping_notice.line_inventory_match_projection import load_current_match_facts_by_task
from app.services.shipping_notice.line_onhand_match_projection import load_current_match_facts_by_task
match_facts = list(load_current_match_facts_by_task(task.id).values())
from app.services.shipping_notice.process_gate_projection import task_blocking_reason
@@ -852,7 +852,7 @@ def serialize_task(task: ShippingNotice, projection: TaskProjection) -> dict[str
'destination_country': header.get('destination_country'),
'warehouse_hint': header.get('warehouse_hint'),
**inventory,
'inventory_snapshot': _serialize_inventory_snapshot(task),
'onhand_snapshot': _serialize_inventory_snapshot(task),
'shortage': {'lines': [row for row in match_facts if str(row.get('shortage_qty') or '').strip() not in {'', '0', '0.0', '0.00'}]},
'line_results': match_facts,
'order_snapshot_id': getattr(workflow_owner, 'order_snapshot_id', None),
@@ -864,13 +864,13 @@ def serialize_task(task: ShippingNotice, projection: TaskProjection) -> dict[str
workflow_payload = getattr(workflow_owner, 'step_payload_json', None) or {}
step_payload = workflow_payload
header = current_header_field_values_or_empty(workflow_owner)
from app.services.shipping_notice.inventory_candidate_projection import active_inventory_runtime_facts
from app.services.shipping_notice.stp_inventory_freeze import task_frozen_inventory_source_codes
from app.services.shipping_notice.onhand_candidate_projection import active_inventory_runtime_facts
from app.services.shipping_notice.stp_onhand_freeze import task_frozen_onhand_source_codes
inventory_facts = active_inventory_runtime_facts(workflow_owner)
source_codes = task_frozen_inventory_source_codes(workflow_owner)
source_codes = task_frozen_onhand_source_codes(workflow_owner)
routing = (getattr(inventory_facts.get('candidate_set'), 'routing_rule_version_json', None) or {})
from app.services.shipping_notice.line_inventory_match_projection import load_current_match_facts_by_task
from app.services.shipping_notice.line_onhand_match_projection import load_current_match_facts_by_task
match_task_id = getattr(workflow_owner, 'id', None) or task.id
match_facts = list(load_current_match_facts_by_task(match_task_id).values())
match_status_projection = (
@@ -931,7 +931,7 @@ def serialize_task(task: ShippingNotice, projection: TaskProjection) -> dict[str
"inventory_source_method": routing.get('adjudication_method'),
"inventory_source_config_version": inventory_facts.get('source_config_version'),
"inventory_routing_fingerprint": routing.get('routing_fingerprint'),
"inventory_snapshot": _serialize_inventory_snapshot(workflow_owner),
"onhand_snapshot": _serialize_inventory_snapshot(workflow_owner),
"shortage": {'lines': [row for row in match_facts if str(row.get('shortage_qty') or '').strip() not in {'', '0', '0.0', '0.00'}]},
"line_results": match_facts,
"created_at": task.created_at.isoformat() if task.created_at else None,
@@ -1424,9 +1424,9 @@ def _notice_line_summary_for_task(task: ShippingNotice) -> list[dict[str, Any]]:
if snapshot is not None:
matches_by_snapshot_line_id = {
int(item.snapshot_line_id): item
for item in ShippingNoticeLineInventoryMatch.query.filter(
ShippingNoticeLineInventoryMatch.task_id == int(owner.id),
ShippingNoticeLineInventoryMatch.is_current.is_(True),
for item in ShippingNoticeLineOnhandMatch.query.filter(
ShippingNoticeLineOnhandMatch.task_id == int(owner.id),
ShippingNoticeLineOnhandMatch.is_current.is_(True),
).all()
if item.snapshot_line_id is not None
}
@@ -1434,9 +1434,9 @@ def _notice_line_summary_for_task(task: ShippingNotice) -> list[dict[str, Any]]:
# 必须来自本 Notice 的 ShippingNoticeLine,避免不同通知波次串行。
matches_by_snapshot_line_id = {
int(item.snapshot_line_id): item
for item in ShippingNoticeLineInventoryMatch.query.filter(
ShippingNoticeLineInventoryMatch.task_id == int(task.id),
ShippingNoticeLineInventoryMatch.is_current.is_(True),
for item in ShippingNoticeLineOnhandMatch.query.filter(
ShippingNoticeLineOnhandMatch.task_id == int(task.id),
ShippingNoticeLineOnhandMatch.is_current.is_(True),
).all()
if item.snapshot_line_id is not None
}
@@ -1453,7 +1453,7 @@ def _notice_line_summary_for_task(task: ShippingNotice) -> list[dict[str, Any]]:
if item.snapshot_line_id is not None and matches_by_snapshot_line_id.get(int(item.snapshot_line_id)) is not None else None,
'shortage_qty': matches_by_snapshot_line_id.get(int(item.snapshot_line_id)).shortage_qty
if item.snapshot_line_id is not None and matches_by_snapshot_line_id.get(int(item.snapshot_line_id)) is not None else None,
'inventory_status': matches_by_snapshot_line_id.get(int(item.snapshot_line_id)).inventory_status
'inventory_status': matches_by_snapshot_line_id.get(int(item.snapshot_line_id)).onhand_status
if item.snapshot_line_id is not None and matches_by_snapshot_line_id.get(int(item.snapshot_line_id)) is not None else None,
} for item in rows]
@@ -2175,15 +2175,15 @@ def _order_task_todo_category_sql_expr():
reason = db.func.lower(db.func.coalesce(ShippingOrderTask.pending_human_reason, ""))
ready_file = exists(
select(1)
.select_from(ShippingTaskInventoryLink)
.join(ShippingInventoryFile, ShippingInventoryFile.id == ShippingTaskInventoryLink.inventory_file_id)
.select_from(ShippingTaskOnhandLink)
.join(FreightRpaOnhandReportFile, FreightRpaOnhandReportFile.id == ShippingTaskOnhandLink.onhand_file_id)
.where(
ShippingTaskInventoryLink.task_id == ShippingOrderTask.id,
ShippingTaskInventoryLink.is_active.is_(True),
ShippingTaskInventoryLink.invalidated_at.is_(None),
ShippingInventoryFile.snapshot_status == 'ready',
ShippingInventoryFile.invalidated_at.is_(None),
ShippingInventoryFile.line_count > 0,
ShippingTaskOnhandLink.task_id == ShippingOrderTask.id,
ShippingTaskOnhandLink.is_active.is_(True),
ShippingTaskOnhandLink.invalidated_at.is_(None),
FreightRpaOnhandReportFile.snapshot_status == 'ready',
FreightRpaOnhandReportFile.invalidated_at.is_(None),
FreightRpaOnhandReportFile.line_count > 0,
)
)
incomplete_match = exists(
@@ -2192,24 +2192,24 @@ def _order_task_todo_category_sql_expr():
.where(
ShippingOrderSnapshotLine.snapshot_id == ShippingOrderTask.order_snapshot_id,
~exists(
select(1).select_from(ShippingNoticeLineInventoryMatch).where(
ShippingNoticeLineInventoryMatch.task_id == ShippingOrderTask.id,
ShippingNoticeLineInventoryMatch.snapshot_line_id == ShippingOrderSnapshotLine.id,
ShippingNoticeLineInventoryMatch.is_current.is_(True),
ShippingNoticeLineInventoryMatch.match_result != 'preparing',
select(1).select_from(ShippingNoticeLineOnhandMatch).where(
ShippingNoticeLineOnhandMatch.task_id == ShippingOrderTask.id,
ShippingNoticeLineOnhandMatch.snapshot_line_id == ShippingOrderSnapshotLine.id,
ShippingNoticeLineOnhandMatch.is_current.is_(True),
ShippingNoticeLineOnhandMatch.match_result != 'preparing',
)
),
)
)
has_shortage = exists(
select(1).select_from(ShippingNoticeLineInventoryMatch).where(
ShippingNoticeLineInventoryMatch.task_id == ShippingOrderTask.id,
ShippingNoticeLineInventoryMatch.is_current.is_(True),
select(1).select_from(ShippingNoticeLineOnhandMatch).where(
ShippingNoticeLineOnhandMatch.task_id == ShippingOrderTask.id,
ShippingNoticeLineOnhandMatch.is_current.is_(True),
db.or_(
ShippingNoticeLineInventoryMatch.match_result != 'matched',
ShippingNoticeLineOnhandMatch.match_result != 'matched',
db.and_(
ShippingNoticeLineInventoryMatch.shortage_qty.isnot(None),
ShippingNoticeLineInventoryMatch.shortage_qty.notin_(('', '0', '0.0', '0.00', '0.00000000')),
ShippingNoticeLineOnhandMatch.shortage_qty.isnot(None),
ShippingNoticeLineOnhandMatch.shortage_qty.notin_(('', '0', '0.0', '0.00', '0.00000000')),
),
),
)
@@ -2242,7 +2242,7 @@ def _order_task_todo_category_sql_expr():
),
literal("in_progress"),
),
(stage.in_(("inventory_source_resolving", "notification_preparation", "inventory_fetching", "inventory_matching", "order_creating")), literal("in_progress")),
(stage.in_(("onhand_source_resolving", "notification_preparation", "onhand_fetching", "onhand_matching", "order_creating")), literal("in_progress")),
else_=literal("exception"),
)
@@ -2623,10 +2623,10 @@ def _todo_category_sql_filter(allowed: frozenset[str]):
clauses.append(
db.or_(
workflow_current_step.isnot(None),
exists(select(1).select_from(ShippingNoticeLineInventoryMatch).where(
ShippingNoticeLineInventoryMatch.task_id == ShippingNotice.order_task_id,
ShippingNoticeLineInventoryMatch.is_current.is_(True),
ShippingNoticeLineInventoryMatch.inventory_snapshot_id.isnot(None),
exists(select(1).select_from(ShippingNoticeLineOnhandMatch).where(
ShippingNoticeLineOnhandMatch.task_id == ShippingNotice.order_task_id,
ShippingNoticeLineOnhandMatch.is_current.is_(True),
ShippingNoticeLineOnhandMatch.onhand_snapshot_id.isnot(None),
)),
artifact_exists,
upload_pending,
@@ -2705,9 +2705,9 @@ def _task_todo_category_sql_expr():
artifact_exists = exists(select(1).select_from(ShippingExportArtifact).where(ShippingExportArtifact.task_id == ShippingNotice.id, ShippingExportArtifact.status.in_(("ready", "uploading", "uploaded"))))
upload_success = _verified_upload_success_sql_expr()
upload_pending = exists(select(1).select_from(ShippingUploadAttempt).join(ShippingExportArtifact, ShippingUploadAttempt.artifact_id == ShippingExportArtifact.id).where(ShippingExportArtifact.task_id == ShippingNotice.id, ShippingUploadAttempt.status.in_(("pending", "running", "failed", "unknown"))))
match_exists = exists(select(1).select_from(ShippingNoticeLineInventoryMatch).where(
ShippingNoticeLineInventoryMatch.task_id == ShippingNotice.order_task_id,
ShippingNoticeLineInventoryMatch.is_current.is_(True),
match_exists = exists(select(1).select_from(ShippingNoticeLineOnhandMatch).where(
ShippingNoticeLineOnhandMatch.task_id == ShippingNotice.order_task_id,
ShippingNoticeLineOnhandMatch.is_current.is_(True),
))
return case(
(upload_success, literal("done")),
@@ -2847,7 +2847,7 @@ def _todo_tasks_base_query(
workflow_review_status = select(ShippingOrderTask.review_status).where(ShippingOrderTask.id == ShippingNotice.order_task_id).scalar_subquery()
artifact_exists = exists(select(1).select_from(ShippingExportArtifact).where(ShippingExportArtifact.task_id == ShippingNotice.id, ShippingExportArtifact.status.in_(('ready', 'uploading', 'uploaded'))))
upload_success = _verified_upload_success_sql_expr()
match_exists = exists(select(1).select_from(ShippingNoticeLineInventoryMatch).where(ShippingNoticeLineInventoryMatch.task_id == ShippingNotice.order_task_id, ShippingNoticeLineInventoryMatch.is_current.is_(True)))
match_exists = exists(select(1).select_from(ShippingNoticeLineOnhandMatch).where(ShippingNoticeLineOnhandMatch.task_id == ShippingNotice.order_task_id, ShippingNoticeLineOnhandMatch.is_current.is_(True)))
if task_no:
q = q.filter(ShippingNotice.task_no == str(task_no).strip())
if customer_needle:
@@ -3259,7 +3259,7 @@ def count_my_todos(user_id: int, *, is_admin: bool = False) -> dict[str, Any]:
workflow_review_status = select(ShippingOrderTask.review_status).where(ShippingOrderTask.id == ShippingNotice.order_task_id).scalar_subquery()
artifact_exists = exists(select(1).select_from(ShippingExportArtifact).where(ShippingExportArtifact.task_id == ShippingNotice.id, ShippingExportArtifact.status.in_(('ready', 'uploading', 'uploaded'))))
upload_success = _verified_upload_success_sql_expr()
match_exists = exists(select(1).select_from(ShippingNoticeLineInventoryMatch).where(ShippingNoticeLineInventoryMatch.task_id == ShippingNotice.order_task_id, ShippingNoticeLineInventoryMatch.is_current.is_(True)))
match_exists = exists(select(1).select_from(ShippingNoticeLineOnhandMatch).where(ShippingNoticeLineOnhandMatch.task_id == ShippingNotice.order_task_id, ShippingNoticeLineOnhandMatch.is_current.is_(True)))
pending = _visible_open_review_cases_query(user_id, is_admin=is_admin).count()
pending += _visible_orphan_intakes_query(user_id, is_admin=is_admin).count()
category_expr = _task_todo_category_sql_expr()