perf(shipping-notice): 优化发货任务性能,修复上传流程与终态问题

This commit is contained in:
hugo committed 2026-08-27 19:25:16 +08:00
1 parent 434c04ed2d
commit bcdd8285ae
29 files changed
+1074 -128

No files matched your search

@@ -923,7 +923,6 @@ def _enrich_task_center_fields(
Returns:
同一 data 引用(已 enrich)。
"""
data["todo_category"] = task_todo_category(task)
data["item_kind"] = "task"
data["todo_ref"] = task.task_no
data["created_by"] = task.user_id
@@ -935,7 +934,15 @@ def _enrich_task_center_fields(
# [STEP] 懒导入避免与 ui_state ↔ query_service 循环依赖。
from app.services.shipping_notice.ui_state import build_task_ui_state
data["ui_state"] = build_task_ui_state(task, include_stage_context=include_stage_context)
ui_state = build_task_ui_state(task, include_stage_context=include_stage_context)
data["ui_state"] = ui_state
# [PERF] todo_category 与 ui_state 同源(均由 resolve_task_ui_state 推导):直接在
# 已构建的 ui_state 上推导,避免 task_todo_category 在请求内对同一任务再算一次
# resolve_task_ui_state(每次含 has_dynamic_order_lines 的取数),削减重复查询。
data["todo_category"] = _todo_category_from_ui(
ui_state.get("checkpoint_code"),
CheckpointKind(str(ui_state.get("checkpoint_kind"))),
)
_inject_unified_processing_fields(task, data)
# [BE-06] WorkflowItem 账本已下线;固定为 None,禁止前端再当 OT 身份源。
data['workflow_item'] = None
@@ -968,6 +975,7 @@ def serialize_task_detail(
viewer_user_id: int | None = None,
is_admin: bool = False,
include_stage_context: bool = False,
view: str = 'full',
) -> dict[str, Any]:
"""
工作台任务详情用的 DETAIL 投影(含 lines、events 与任务中心 enrich)。
@@ -977,8 +985,21 @@ def serialize_task_detail(
viewer_user_id: 当前查看者;用于 can_handle。
is_admin: 是否管理员视图。
include_stage_context: 是否让内嵌 ui_state 附带 stage_context(避免调用方重复 build)。
view: 投影视图。``full`` 返回完整详情(含复核编辑器数据);``workbench`` 为
订单生命周期工作台首屏裁剪版:工作台只消费 ui_state + 进度/产物/行摘要,
不读取行明细/订单头/事件/字段冲突等复核编辑器重字段。把这些字段从响应剔除,
避免每次进入工作台都拉取 67KB 级详情(对应 /shipping-tasks/:task_no 的大响应)。
"""
data = serialize_task(task, TaskProjection.DETAIL)
if view == 'workbench':
# [PERF] 工作台首屏不消费以下复核编辑器字段,剔除后显著缩小响应体与序列化耗时。
# 仅剔除编辑重字段;ui_state(含 stage_context)、notice_line_summary、
# artifact_summary、_fulfillment_timeline(export_artifacts) 与进度字段原样保留。
for key in (
'lines', 'order_header', 'order_header_fields', 'order_line_fields',
'events', 'field_conflicts',
):
data.pop(key, None)
return _enrich_task_center_fields(
task, data, viewer_user_id=viewer_user_id, is_admin=is_admin,
include_stage_context=include_stage_context,
@@ -1146,12 +1167,15 @@ def list_freight_notice_groups_for_task(
# 轻量投影仅返回动作码,避免逐张 Notice 跑完整 build_task_ui_state 造成 N 倍放大。
from app.services.shipping_notice.ui_state import project_notice_available_actions
available_actions = project_notice_available_actions(notice)
# [STEP] upload_mode/upload_attempt 从 ShippingUploadAttempt 权威事实解析。
# denormalized Notice.upload_mode 恒为 None,不能作为「人工/RPA」展示来源。
upload_mode, upload_attempt = _task_upload_attempt_facts(notice.id)
items.append({
'business_no': business_no,
'po_number': po_number,
'upload_status': notice.upload_status,
'upload_mode': notice.upload_mode,
'upload_attempt': notice.upload_attempt,
'upload_mode': upload_mode,
'upload_attempt': upload_attempt,
'upload_error_code': notice.upload_error_code,
# 逐单失败原因:优先取最近一次上传的错误正文,回退到任务级 error_message。
'upload_error_message': (notice.upload_error_message or notice.error_message or '')[:500] or None,
@@ -1167,6 +1191,38 @@ def list_freight_notice_groups_for_task(
}
def _task_upload_attempt_facts(task_id: int | None) -> tuple[str | None, int]:
"""从 ShippingUploadAttempt 权威事实解析任务上传方式与尝试次数。
读取侧不再消费 denormalized 的 Notice.upload_mode / upload_attempt(这两个列从未被
写入真实值,恒为 None/0)。成功态的 mode 优先(通知完成面以此区分人工/RPA),
否则回退最近一条 attempt 的 mode,与 ui_state._build_stage_context 的判定保持一致。
Args:
task_id: 正式 Notice 主键;为空时返回 (None, 0)。
Returns:
tuple[str | None, int]: (mode, attempt_count);无任何 attempt 时返回 (None, 0)。
"""
if not task_id:
return None, 0
attempts = (
ShippingUploadAttempt.query
.join(ShippingExportArtifact, ShippingUploadAttempt.artifact_id == ShippingExportArtifact.id)
.filter(ShippingExportArtifact.task_id == int(task_id))
.order_by(ShippingUploadAttempt.created_at.desc(), ShippingUploadAttempt.id.desc())
.all()
)
if not attempts:
return None, 0
succeeded = next(
(item for item in attempts if str(item.status or '').strip().lower() == 'succeeded'),
None,
)
anchor = succeeded or attempts[0]
return (anchor.mode if anchor is not None else None), len(attempts)
def _notice_po_number(notice: ShippingNotice) -> str:
"""读取 Notice 的正式客户 PO;动态 Schema、冻结快照和兼容投影按稳定顺序回退。"""
header = current_header_field_values_or_empty(notice)