refactor(shipping-notice): 重构货代发货通知状态管理,移除废弃的process_status字段

统一货代发货通知的业务阶段为四个标准阶段:接收解析、订单复核、发货准备、上传完成
设置 ui_state 为前端唯一状态数据源,不再依赖 process_status 驱动业务流程
删除 shipping_notices 表的废弃 process_status 列及对应复合索引,新增数据库迁移脚本
替换全栈代码中所有对 process_status 的引用为生命周期、上传状态等领域字段
更新相关文档、测试用例与前端展示逻辑适配新的状态管理规则
This commit is contained in:
hugo committed 2026-08-15 10:19:10 +08:00
1 parent 6684e7181a
commit 5ef8329bd8
178 files changed
+5091 -1734

No files matched your search

@@ -324,7 +324,6 @@ def human_options_for_task_or_checkpoint(
def resolve_task_ui_state_from_fields(
*,
process_status: str | None,
review_status: str | None,
pending_human_reason: str | None,
shipping_case_id: int | None,
@@ -352,13 +351,16 @@ def resolve_task_ui_state_from_fields(
return upload_req_options_for_task(upload_req_task)
return list(CHECKPOINT_REGISTRY[CP_UPLOAD_REQ])
# [STEP 1] 终态:已上传、已关闭、已驳回 — 无可用按钮。
if process_status == "uploaded":
upload_status = str(getattr(upload_req_task, "upload_status", None) if upload_req_task is not None else "").strip().lower()
has_export = bool(str(export_file_path or "").strip())
# [STEP 1] 终态:上传结果事实或生命周期处置 — 无履约写按钮。
if upload_status in {"uploaded", "succeeded", "success"}:
return _task_ui_state(CP_UPLOADED, [])
if process_status == "closed":
if upload_req_task is not None and str(getattr(upload_req_task, "lifecycle_status", "") or "").strip().lower() in {"archived", "deleted"}:
return _task_ui_state(CP_COMPLETED, [])
if process_status == "rejected" or review_status == "rejected":
if review_status == "rejected":
return _task_ui_state(CP_REJECTED, [])
# [STEP 2] 先完成订单解析复核,再暴露客户确认节点。
@@ -380,30 +382,34 @@ def resolve_task_ui_state_from_fields(
if not order_snapshot_id and has_dynamic_order_lines:
return _task_ui_state(CP_ORDER_FREEZE, list(CHECKPOINT_REGISTRY[CP_ORDER_FREEZE]))
# Excel 已生成即进入通知完成前的人工上传登记节点,即使历史任务未落 match_status,
# 也不能退回“处理中”或丢失下载/登记动作。
if has_export:
return _task_ui_state(CP_EXPORT_READY, list(CHECKPOINT_REGISTRY[CP_EXPORT_READY]))
# [STEP 4] 客户确认并冻结订单后,才进入库存和履约节点。
if reason == PH_RPA_EXHAUSTED:
return _task_ui_state(CP_UPLOAD_REQ, _upload_opts())
if (
process_status == "parsed"
and not review_status
and not pending_human_reason
and not inventory_lines_json
):
return _task_ui_state(CP_INVENTORY_PENDING, list(CHECKPOINT_REGISTRY[CP_INVENTORY_PENDING]))
if (
process_status == "inventory_ready"
inventory_lines_json
and not review_status
and not pending_human_reason
and not match_status
and has_dynamic_order_lines
):
return _task_ui_state(CP_READY_MATCH, list(CHECKPOINT_REGISTRY[CP_READY_MATCH]))
if (
not inventory_lines_json
and not review_status
and not pending_human_reason
and has_dynamic_order_lines
):
return _task_ui_state(CP_INVENTORY_PENDING, list(CHECKPOINT_REGISTRY[CP_INVENTORY_PENDING]))
# [STEP 5] 匹配/导出阶段 — matched 后区分待生成与已生成 Excel(STP 停点)。
if process_status == "matched":
if export_file_path:
if match_status in {"fulfillable", "partial", "unfulfillable", "matched"}:
if has_export:
return _task_ui_state(CP_EXPORT_READY, list(CHECKPOINT_REGISTRY[CP_EXPORT_READY]))
return _task_ui_state(CP_READY_EXPORT, list(CHECKPOINT_REGISTRY[CP_READY_EXPORT]))
@@ -417,18 +423,16 @@ def resolve_task_ui_state_from_fields(
)
if reason == PH_SHORTAGE or review_status == RS_SHORTAGE_REVIEW:
return _task_ui_state(RS_SHORTAGE_REVIEW, list(CHECKPOINT_REGISTRY[RS_SHORTAGE_REVIEW]))
if reason == PH_UPLOAD_RPA_FAILED or process_status == "upload_failed":
if reason == PH_UPLOAD_RPA_FAILED or upload_status == "failed":
return _task_ui_state(CP_UPLOAD_FAILED, list(CHECKPOINT_REGISTRY[CP_UPLOAD_FAILED]))
if reason == PH_UPLOAD_RESULT_UNKNOWN:
return _task_ui_state(
CP_UPLOAD_RESULT_UNKNOWN,
list(CHECKPOINT_REGISTRY[CP_UPLOAD_RESULT_UNKNOWN]),
)
if reason == PH_RPA_PENDING or (
process_status == "waiting_inventory" and reason == PH_RPA_PENDING
):
if reason == PH_RPA_PENDING:
return _task_ui_state(CP_UPLOAD_REQ, _upload_opts())
if reason == PH_UPLOAD_RPA_PENDING or process_status == "waiting_upload":
if reason == PH_UPLOAD_RPA_PENDING or upload_status in {"pending", "uploading"}:
return _task_ui_state(CP_WAITING_UPLOAD, list(CHECKPOINT_REGISTRY[CP_WAITING_UPLOAD]))
return _task_ui_state(None, None)
@@ -439,10 +443,9 @@ def resolve_task_ui_state(task: ShippingNotice) -> TaskUiState:
核心逻辑:委托 resolve_task_ui_state_from_fields;订单行动态存在性按需查库;
upload_req 按钮仅在命中对应分支时惰性解析(兼容测试用轻量 stub)。
履约写操作由任务处理工作台消费 action_buttons;Chat 不再作为写入口。
履约写操作由任务处理工作台消费 ui_state.actions;Chat 不再作为写入口。
"""
return resolve_task_ui_state_from_fields(
process_status=task.process_status,
review_status=task.review_status,
pending_human_reason=task.pending_human_reason,
shipping_case_id=task.shipping_case_id,
@@ -471,12 +474,14 @@ def build_checkpoint_catalog() -> dict[str, Any]:
checkpoints = []
for checkpoint_id, option_ids in CHECKPOINT_REGISTRY.items():
buttons = build_action_buttons(list(option_ids))
primary = next((button for button in buttons if button.get('variant') == 'primary'), None)
secondary = [button for button in buttons if button is not primary]
checkpoints.append(
{
"id": checkpoint_id,
"checkpoint_kind": str(checkpoint_kind_for(checkpoint_id)),
"human_options": list(option_ids),
"action_buttons": buttons,
"actions": {"primary": primary, "secondary": secondary},
}
)
return {
@@ -513,7 +518,6 @@ _UNSET = object()
@dataclass(frozen=True)
class _TransitionSpec:
process_status: Any = _UNSET
review_status: Any = _UNSET
pending_human_reason: Any = _UNSET
clear_error_message: bool = False
@@ -546,69 +550,56 @@ class TaskTransition(StrEnum):
TASK_TRANSITIONS: dict[TaskTransition, _TransitionSpec] = {
TaskTransition.PARSED: _TransitionSpec(
process_status="parsed",
review_status=None,
pending_human_reason=None,
),
TaskTransition.PARSE_NEEDS_REVIEW: _TransitionSpec(
process_status="pending_review",
review_status=RS_PARSE_REVIEW,
pending_human_reason=PH_LOW_CONF,
),
TaskTransition.INVENTORY_READY: _TransitionSpec(
process_status="inventory_ready",
review_status=None,
pending_human_reason=None,
clear_error_message=True,
),
TaskTransition.RPA_EXHAUSTED: _TransitionSpec(
process_status="waiting_inventory",
review_status=None,
pending_human_reason=PH_RPA_EXHAUSTED,
),
TaskTransition.RPA_PENDING: _TransitionSpec(
process_status="waiting_inventory",
review_status=None,
pending_human_reason=PH_RPA_PENDING,
),
TaskTransition.INV_UNAVAIL: _TransitionSpec(
process_status="waiting_inventory",
review_status=None,
pending_human_reason=PH_INV_UNAVAIL,
),
TaskTransition.INVENTORY_SOURCE_REVIEW: _TransitionSpec(
process_status="pending_review",
review_status=None,
pending_human_reason=PH_INVENTORY_SOURCE_UNRESOLVED,
),
TaskTransition.RSV_REQUIRED: _TransitionSpec(
process_status="pending_review",
review_status=RS_RSV_REQ,
pending_human_reason=PH_RSV_REQ,
),
TaskTransition.MATCHED_FULFILLABLE: _TransitionSpec(
process_status="matched",
review_status=RS_CONFIRMED,
pending_human_reason=None,
),
TaskTransition.MATCH_SHORTAGE: _TransitionSpec(
process_status="pending_review",
review_status=RS_SHORTAGE_REVIEW,
pending_human_reason=PH_SHORTAGE,
),
TaskTransition.REJECTED: _TransitionSpec(
process_status="rejected",
review_status=RS_REJECTED,
pending_human_reason=None,
),
TaskTransition.CLOSED: _TransitionSpec(
process_status="closed",
review_status=None,
pending_human_reason=None,
clear_error_message=True,
),
TaskTransition.CONFIRM_PARSE: _TransitionSpec(
process_status="parsed",
review_status=None,
pending_human_reason=None,
),
@@ -617,27 +608,22 @@ TASK_TRANSITIONS: dict[TaskTransition, _TransitionSpec] = {
pending_human_reason=None,
),
TaskTransition.NOTICE_TEXT_EXTRACTION_FAILED: _TransitionSpec(
process_status="pending_review",
review_status=None,
pending_human_reason=PH_NOTICE_TEXT_EXTRACTION_FAILED,
),
TaskTransition.REMATCH_REQUESTED: _TransitionSpec(
process_status="inventory_ready",
review_status=None,
pending_human_reason=None,
clear_error_message=True,
),
TaskTransition.WAITING_UPLOAD: _TransitionSpec(process_status="waiting_upload"),
TaskTransition.WAITING_UPLOAD: _TransitionSpec(),
TaskTransition.UPLOAD_RPA_PENDING: _TransitionSpec(
process_status="waiting_upload",
pending_human_reason=PH_UPLOAD_RPA_PENDING,
),
TaskTransition.UPLOAD_FAILED: _TransitionSpec(
process_status="upload_failed",
pending_human_reason=PH_UPLOAD_RPA_FAILED,
),
TaskTransition.UPLOADED: _TransitionSpec(
process_status="uploaded",
pending_human_reason=None,
clear_error_message=True,
),
@@ -646,7 +632,7 @@ TASK_TRANSITIONS: dict[TaskTransition, _TransitionSpec] = {
def apply_task_transition(task: ShippingNotice, transition: TaskTransition) -> None:
"""
将命名状态迁移写入 ShippingNotice 的 process_status / review_status / pending_human_reason。
将命名业务事实迁移写入 ShippingNotice 的领域结果字段与 review/pending 事实。
核心逻辑:查 TASK_TRANSITIONS 表驱动字段更新;所有 Tool 写库须经本函数,禁止散落赋值。
库存相关迁移后同步挂接 OT 的 stage(waiting_inventory → inventory_fetching 等)。
@@ -656,8 +642,6 @@ def apply_task_transition(task: ShippingNotice, transition: TaskTransition) -> N
transition: TaskTransition 枚举值。
"""
spec = TASK_TRANSITIONS[transition]
if spec.process_status is not _UNSET:
task.process_status = spec.process_status
if spec.review_status is not _UNSET:
task.review_status = spec.review_status
if spec.pending_human_reason is not _UNSET: