refactor(shipping-review): 统一复核投影与正式订单事实来源

复核投影统一读取 ShippingOrderTask、ShippingOrderSnapshot,清理 line_key 血缘投影
This commit is contained in:
hugo committed 2026-09-04 20:20:22 +08:00
1 parent cd2967748b
commit 761a06e21a
5 files changed
+132 -58

No files matched your search

@@ -202,7 +202,7 @@ def _initial_draft_payload(
order = orders[0]
header = {'machine_fields': list(order.get('header_fields') or [])}
lines = [
{'line_key': f'machine-{index + 1}', 'machine_fields': list(line.get('fields') or [])}
{'draft_line_id': f'machine-{index + 1}', 'machine_fields': list(line.get('fields') or [])}
for index, line in enumerate(order.get('lines') or []) if isinstance(line, dict)
]
_restore_machine_source_case(header, lines)
@@ -499,7 +499,7 @@ def build_llm_suggestion_request_context(
user_id: int,
case_no: str,
field_id: str,
line_key: str | None = None,
draft_line_id: str | None = None,
) -> dict[str, Any]:
"""
为 LLM 建议请求绑定 Case、Document 与冻结 Schema 字段上下文。
@@ -510,7 +510,7 @@ def build_llm_suggestion_request_context(
user_id: Case 属主。
case_no: 复核 Case 编号。
field_id: 目标 field_id。
line_key: 明细行键。
draft_line_id: 明细行键。
Returns:
含 case_no、document_id、draft_version、field 的上下文 dict。
@@ -536,6 +536,6 @@ def build_llm_suggestion_request_context(
'field_id': str(field_id),
'field_code': str(field.get('field_code') or field_id),
'scope': str(field.get('scope') or ''),
'line_key': line_key,
'draft_line_id': draft_line_id,
'schema_version_id': int(case.run.schema_version_id),
}
@@ -18,7 +18,7 @@ from datetime import datetime
from typing import Any
from app import db
from app.models import ShippingNotice, ShippingOrderReviewCase
from app.models import ShippingNotice, ShippingOrderReviewCase, ShippingOrderTask
# 正式 OT 创建后不得再作为「当前人工阻断」的建单前 Gate。
_PRE_OT_BLOCKING_GATE_CODES: frozenset[str] = frozenset({
@@ -65,7 +65,7 @@ def resolve_formal_ot(case: ShippingOrderReviewCase) -> dict[str, Any]:
"""
判定 Case 是否已绑定可读的正式 OT。
核心逻辑:唯一判据为 confirmed_task_id 指向存在且 task_no 有效的 ShippingNotice;
核心逻辑:唯一判据为 confirmed_task_id 指向存在且 task_no 有效的 ShippingOrderTask;
Case.status=confirmed 不能单独证明 OT 存在。
Args:
@@ -75,7 +75,7 @@ def resolve_formal_ot(case: ShippingOrderReviewCase) -> dict[str, Any]:
{
has_formal_ot: bool,
task_missing: bool,
task: ShippingNotice | None,
task: ShippingOrderTask | None,
task_no: str | None,
task_id: int | None,
}
@@ -89,7 +89,7 @@ def resolve_formal_ot(case: ShippingOrderReviewCase) -> dict[str, Any]:
'task_no': None,
'task_id': None,
}
task = db.session.get(ShippingNotice, int(task_id))
task = db.session.get(ShippingOrderTask, int(task_id))
if task is None:
return {
'has_formal_ot': False,
@@ -441,17 +441,33 @@ def project_post_ot_automation_from_job(task: ShippingNotice | None) -> dict[str
}
task_no = str(getattr(task, 'task_no', None) or '').strip().upper() or None
from app.services.shipping_notice.upload_attempt_service import has_verified_upload_success
from app.services.shipping_notice.upload_attempt_service import (
has_verified_upload_success,
latest_export_artifact,
latest_upload_attempt,
)
verified_upload_success = has_verified_upload_success(task)
upload_status = getattr(task, 'upload_status', None)
from app.services.shipping_notice.upload_attempt_service import latest_upload_attempt
upload_status = getattr(latest_upload_attempt(task), 'status', None)
upload_status = upload_status.strip().lower() if isinstance(upload_status, str) else ''
export_file_path = getattr(task, 'export_file_path', None)
match_status = getattr(task, 'match_status', None)
inventory_lines = getattr(task, 'inventory_lines_json', None)
from app.services.shipping_notice.upload_attempt_service import latest_export_artifact
export_file_path = getattr(latest_export_artifact(task), 'file_path', None)
if isinstance(task, ShippingOrderTask):
from app.services.shipping_notice.inventory_file_store import (
current_inventory_matches_for_task,
has_ready_inventory_file_for_task,
)
has_match = bool(current_inventory_matches_for_task(task))
has_inventory = has_ready_inventory_file_for_task(task)
else:
from app.services.shipping_notice.line_inventory_match_projection import aggregate_match_status_for_task
from app.services.shipping_notice.inventory_file_store import has_ready_inventory_file_for_task
match_status = aggregate_match_status_for_task(getattr(task, 'order_task_id', None) or getattr(task, 'id', None))
has_match = bool(match_status)
has_inventory = has_ready_inventory_file_for_task(task)
has_export = isinstance(export_file_path, str) and bool(export_file_path.strip())
has_match = isinstance(match_status, str) and bool(match_status.strip())
has_inventory = isinstance(inventory_lines, (list, dict)) and bool(inventory_lines)
progress_state = (
'uploaded' if verified_upload_success
else 'export_ready' if has_export
@@ -642,21 +658,37 @@ def project_stage_for_task(
from app.services.shipping_notice.processing_projection_invariants import (
map_notice_to_processing_status_stage,
)
from app.services.shipping_notice.inventory_file_store import has_ready_inventory_file_for_task
from app.services.shipping_notice.upload_attempt_service import has_verified_upload_success
from app.services.shipping_notice.upload_attempt_service import (
has_verified_upload_success,
latest_export_artifact,
latest_upload_attempt,
)
# 业务完成不能仅信任 Notice 的历史聚合字段。复核工作台必须与批次投影
# 复用同一成功谓词,否则同一任务会在不同入口出现“已完成/待上传”分叉。
owner = task
if not isinstance(task, ShippingOrderTask) and getattr(task, 'order_task_id', None):
owner = db.session.get(ShippingOrderTask, int(task.order_task_id)) or task
verified_upload_success = has_verified_upload_success(task)
raw_upload_status = str(getattr(task, 'upload_status', None) or '').strip().lower()
raw_upload_status = str(getattr(latest_upload_attempt(task), 'status', None) or '').strip().lower()
if isinstance(owner, ShippingOrderTask):
from app.services.shipping_notice.line_inventory_match_projection import load_current_match_facts_by_task
match_facts = list(load_current_match_facts_by_task(getattr(owner, 'id', None)).values())
has_task_match = bool(match_facts)
else:
from app.services.shipping_notice.line_inventory_match_projection import aggregate_match_status_for_task
has_task_match = bool(aggregate_match_status_for_task(getattr(task, 'order_task_id', None) or getattr(task, 'id', None)))
progress_state = (
'uploaded' if verified_upload_success
else 'export_ready' if getattr(task, 'export_file_path', None)
else 'matched' if getattr(task, 'match_status', None)
else 'inventory_ready' if getattr(task, 'inventory_lines_json', None)
else 'export_ready' if getattr(latest_export_artifact(task), 'file_path', None)
else 'matched' if has_task_match
else 'inventory_ready' if has_ready_inventory_file_for_task(task)
else 'not_started'
)
current_step = str(getattr(task, 'current_step', None) or '').strip() or None
current_step = str(getattr(owner, 'current_step', None) or '').strip() or None
def _with_label(code: str, *, source: str) -> dict[str, Any]:
# order_creating 技术阶段码保留;用户可见文案键固定 waiting_system_start。
@@ -685,12 +717,6 @@ def project_stage_for_task(
if has_app_context():
if getattr(task, 'order_task_id', None):
order_task = db.session.get(ShippingOrderTask, int(task.order_task_id))
if order_task is None and getattr(task, 'id', None):
order_task = (
ShippingOrderTask.query.filter_by(shipping_notice_id=int(task.id))
.order_by(ShippingOrderTask.id.desc())
.first()
)
except Exception: # noqa: BLE001 — 单元测试无 Flask 上下文时跳过 OT 查询
order_task = None
ot_stage = str(getattr(order_task, 'stage', None) or '').strip().lower() if order_task else ''