把 shipping 侧 inventory/receipt 词根迁到 onhand/inbound,29 张表与 枚举/权限/前端/i18n 同步改名;在手与入站报表配置及文件改由 freight_rpa 持有,发货与调拨仅按 source_code 消费;ORM 类加 FreightRpa 前缀, 上传动作改为 freight_rpa_upload。含类名与上传动作改名规格文档。 Co-authored-by: Cursor <cursoragent@cursor.com>
274 lines
12 KiB
Python
274 lines
12 KiB
Python
"""
|
|
models/shipping_process_gate.py
|
|
|
|
用途:门禁级中断恢复的持久化模型——Execution / Gate / Attempt。
|
|
依赖:SQLAlchemy Base、ReviewCase、ShippingOrderTask、Job、shipping_onhand_candidate_sets;状态枚举与 process_gate_contract 对齐。
|
|
逻辑:Execution 为业务恢复真相;Gate 保存当前门禁状态;Attempt 不可变执行尝试并承载租约。
|
|
由领域服务独占写入;Job 仅调度。
|
|
co8e9f0a1b2c:已移除对 shipping_notice_workflow_* 的硬外键(无历史兼容)。
|
|
"""
|
|
|
|
from __future__ import annotations
|
|
|
|
from datetime import datetime
|
|
from typing import Any, Optional
|
|
|
|
from sqlalchemy import (
|
|
CheckConstraint,
|
|
DateTime,
|
|
ForeignKey,
|
|
Index,
|
|
Integer,
|
|
JSON,
|
|
String,
|
|
Text,
|
|
UniqueConstraint,
|
|
text,
|
|
)
|
|
from sqlalchemy.orm import Mapped, mapped_column, relationship
|
|
|
|
from app.models.base import Base
|
|
|
|
|
|
def _utc_now() -> datetime:
|
|
"""返回 naive UTC 时间,与仓库现有模型一致。"""
|
|
return datetime.utcnow()
|
|
|
|
|
|
class ShippingProcessExecution(Base):
|
|
"""业务执行实例;门禁恢复状态唯一真相,不保存大体量订单原文。"""
|
|
|
|
__tablename__ = 'shipping_process_executions'
|
|
__table_args__ = (
|
|
UniqueConstraint('execution_no', name='uq_shipping_process_executions_no'),
|
|
CheckConstraint(
|
|
"status IN ('active', 'blocked', 'waiting_external', 'completed', 'cancelled', 'failed', 'superseded')",
|
|
name='ck_shipping_process_executions_status',
|
|
),
|
|
CheckConstraint('lock_version >= 1', name='ck_shipping_process_executions_lock_version'),
|
|
Index(
|
|
'ix_shipping_process_executions_status_updated',
|
|
'status',
|
|
'updated_at',
|
|
),
|
|
Index(
|
|
'ix_shipping_process_executions_current_gate',
|
|
'current_gate_code',
|
|
'status',
|
|
'updated_at',
|
|
),
|
|
Index(
|
|
'ix_shipping_process_executions_review_case',
|
|
'review_case_id',
|
|
),
|
|
Index(
|
|
'ix_shipping_process_executions_task',
|
|
'task_id',
|
|
),
|
|
Index('ix_shipping_process_executions_candidate_set', 'candidate_set_id', 'status'),
|
|
)
|
|
|
|
id: Mapped[int] = mapped_column(primary_key=True)
|
|
execution_no: Mapped[str] = mapped_column(String(64), nullable=False)
|
|
review_case_id: Mapped[Optional[int]] = mapped_column(
|
|
ForeignKey('shipping_order_review_cases.id'), nullable=True,
|
|
)
|
|
task_id: Mapped[Optional[int]] = mapped_column(
|
|
ForeignKey('shipping_order_tasks.id'), nullable=True,
|
|
)
|
|
# 绑定候选版本,防止旧 callback/job 写入新候选链。
|
|
candidate_set_id: Mapped[Optional[int]] = mapped_column(
|
|
ForeignKey('shipping_onhand_candidate_sets.id'), nullable=True,
|
|
)
|
|
execution_revision: Mapped[int] = mapped_column(
|
|
Integer, nullable=False, default=1, server_default='1',
|
|
)
|
|
status: Mapped[str] = mapped_column(
|
|
String(32), nullable=False, default='active', server_default='active',
|
|
)
|
|
current_gate_code: Mapped[Optional[str]] = mapped_column(String(64), nullable=True)
|
|
last_passed_gate_code: Mapped[Optional[str]] = mapped_column(String(64), nullable=True)
|
|
primary_next_action_code: Mapped[Optional[str]] = mapped_column(String(64), nullable=True)
|
|
responsible_party: Mapped[Optional[str]] = mapped_column(String(32), nullable=True)
|
|
lock_version: Mapped[int] = mapped_column(Integer, nullable=False, default=1, server_default='1')
|
|
created_at: Mapped[datetime] = mapped_column(DateTime, default=_utc_now, nullable=False)
|
|
updated_at: Mapped[datetime] = mapped_column(
|
|
DateTime, default=_utc_now, onupdate=_utc_now, nullable=False,
|
|
)
|
|
|
|
gates: Mapped[list['ShippingProcessGate']] = relationship(
|
|
back_populates='execution',
|
|
cascade='all, delete-orphan',
|
|
order_by='ShippingProcessGate.sequence_no',
|
|
)
|
|
|
|
|
|
class ShippingProcessGate(Base):
|
|
"""单门禁实例;保存状态、输入指纹、阻断原因与最新 Attempt 指针。"""
|
|
|
|
__tablename__ = 'shipping_process_gates'
|
|
__table_args__ = (
|
|
UniqueConstraint(
|
|
'execution_id',
|
|
'gate_code',
|
|
name='uq_shipping_process_gates_execution_code',
|
|
),
|
|
UniqueConstraint(
|
|
'execution_id',
|
|
'sequence_no',
|
|
name='uq_shipping_process_gates_execution_sequence',
|
|
),
|
|
CheckConstraint(
|
|
"status IN ("
|
|
"'pending', 'queued', 'running', 'passed', 'blocked_retryable', "
|
|
"'blocked_manual', 'waiting_external', 'failed_terminal', 'invalidated', 'skipped', 'cancelled'"
|
|
")",
|
|
name='ck_shipping_process_gates_status',
|
|
),
|
|
CheckConstraint('sequence_no >= 1', name='ck_shipping_process_gates_sequence'),
|
|
CheckConstraint('evaluation_version >= 1', name='ck_shipping_process_gates_eval_version'),
|
|
CheckConstraint('lock_version >= 1', name='ck_shipping_process_gates_lock_version'),
|
|
Index(
|
|
'ix_shipping_process_gates_execution_status',
|
|
'execution_id',
|
|
'status',
|
|
'updated_at',
|
|
),
|
|
Index(
|
|
'ix_shipping_process_gates_code_status',
|
|
'gate_code',
|
|
'status',
|
|
'updated_at',
|
|
),
|
|
)
|
|
|
|
id: Mapped[int] = mapped_column(primary_key=True)
|
|
execution_id: Mapped[int] = mapped_column(
|
|
ForeignKey('shipping_process_executions.id', ondelete='CASCADE'), nullable=False,
|
|
)
|
|
gate_code: Mapped[str] = mapped_column(String(64), nullable=False)
|
|
sequence_no: Mapped[int] = mapped_column(Integer, nullable=False)
|
|
status: Mapped[str] = mapped_column(
|
|
String(32), nullable=False, default='pending', server_default='pending',
|
|
)
|
|
evaluation_version: Mapped[int] = mapped_column(Integer, nullable=False, default=1, server_default='1')
|
|
input_fingerprint: Mapped[Optional[str]] = mapped_column(String(64), nullable=True)
|
|
blocking_reason_code: Mapped[Optional[str]] = mapped_column(String(64), nullable=True)
|
|
blocking_reason_json: Mapped[Optional[dict[str, Any]]] = mapped_column(JSON, nullable=True)
|
|
recommendation_json: Mapped[Optional[dict[str, Any]]] = mapped_column(JSON, nullable=True)
|
|
effective_result_json: Mapped[Optional[dict[str, Any]]] = mapped_column(JSON, nullable=True)
|
|
next_action_code: Mapped[Optional[str]] = mapped_column(String(64), nullable=True)
|
|
lock_version: Mapped[int] = mapped_column(Integer, nullable=False, default=1, server_default='1')
|
|
# 软指针:避免与 Attempt.gate_id 循环外键;由领域服务保证一致性。
|
|
latest_attempt_id: Mapped[Optional[int]] = mapped_column(Integer, nullable=True)
|
|
passed_at: Mapped[Optional[datetime]] = mapped_column(DateTime, nullable=True)
|
|
invalidated_at: Mapped[Optional[datetime]] = mapped_column(DateTime, nullable=True)
|
|
created_at: Mapped[datetime] = mapped_column(DateTime, default=_utc_now, nullable=False)
|
|
updated_at: Mapped[datetime] = mapped_column(
|
|
DateTime, default=_utc_now, onupdate=_utc_now, nullable=False,
|
|
)
|
|
|
|
execution: Mapped[ShippingProcessExecution] = relationship(back_populates='gates')
|
|
attempts: Mapped[list['ShippingProcessGateAttempt']] = relationship(
|
|
back_populates='gate',
|
|
cascade='all, delete-orphan',
|
|
order_by='ShippingProcessGateAttempt.attempt_no',
|
|
foreign_keys='ShippingProcessGateAttempt.gate_id',
|
|
)
|
|
|
|
|
|
class ShippingProcessGateAttempt(Base):
|
|
"""不可变门禁执行尝试;成功或失败后业务结果不得被后续重试覆盖。"""
|
|
|
|
__tablename__ = 'shipping_process_gate_attempts'
|
|
__table_args__ = (
|
|
UniqueConstraint(
|
|
'gate_id',
|
|
'attempt_no',
|
|
name='uq_shipping_process_gate_attempts_gate_no',
|
|
),
|
|
UniqueConstraint(
|
|
'idempotency_key',
|
|
name='uq_shipping_process_gate_attempts_idempotency',
|
|
),
|
|
CheckConstraint(
|
|
"status IN ('queued', 'running', 'succeeded', 'failed', 'cancelled', 'superseded')",
|
|
name='ck_shipping_process_gate_attempts_status',
|
|
),
|
|
CheckConstraint('attempt_no >= 1', name='ck_shipping_process_gate_attempts_no'),
|
|
CheckConstraint(
|
|
"trigger_type IN ("
|
|
"'auto', 'user_retry', 'user_manual', 'worker_takeover', "
|
|
"'upstream_invalidation', 'system_requeue'"
|
|
")",
|
|
name='ck_shipping_process_gate_attempts_trigger',
|
|
),
|
|
CheckConstraint(
|
|
"actor_type IN ('user', 'system', 'rpa', 'chatflow', 'admin', 'worker')",
|
|
name='ck_shipping_process_gate_attempts_actor_type',
|
|
),
|
|
# 同一 Gate 同时最多一个 queued/running Attempt。
|
|
Index(
|
|
'uq_shipping_process_gate_attempts_one_active',
|
|
'gate_id',
|
|
unique=True,
|
|
postgresql_where=text("status IN ('queued', 'running')"),
|
|
sqlite_where=text("status IN ('queued', 'running')"),
|
|
),
|
|
Index(
|
|
'ix_shipping_process_gate_attempts_lease',
|
|
'lease_token',
|
|
'lease_expires_at',
|
|
),
|
|
Index(
|
|
'ix_shipping_process_gate_attempts_job',
|
|
'job_id',
|
|
),
|
|
Index(
|
|
'ix_shipping_process_gate_attempts_gate_status',
|
|
'gate_id',
|
|
'status',
|
|
'created_at',
|
|
),
|
|
)
|
|
|
|
id: Mapped[int] = mapped_column(primary_key=True)
|
|
gate_id: Mapped[int] = mapped_column(
|
|
ForeignKey('shipping_process_gates.id', ondelete='CASCADE'), nullable=False,
|
|
)
|
|
attempt_no: Mapped[int] = mapped_column(Integer, nullable=False)
|
|
status: Mapped[str] = mapped_column(
|
|
String(32), nullable=False, default='queued', server_default='queued',
|
|
)
|
|
trigger_type: Mapped[str] = mapped_column(String(32), nullable=False, default='auto', server_default='auto')
|
|
actor_type: Mapped[str] = mapped_column(String(16), nullable=False, default='system', server_default='system')
|
|
actor_id: Mapped[Optional[str]] = mapped_column(String(64), nullable=True)
|
|
idempotency_key: Mapped[str] = mapped_column(String(128), nullable=False)
|
|
input_snapshot_json: Mapped[Optional[dict[str, Any]]] = mapped_column(JSON, nullable=True)
|
|
input_snapshot_hash: Mapped[Optional[str]] = mapped_column(String(64), nullable=True)
|
|
rule_snapshot_json: Mapped[Optional[dict[str, Any]]] = mapped_column(JSON, nullable=True)
|
|
rule_snapshot_hash: Mapped[Optional[str]] = mapped_column(String(64), nullable=True)
|
|
output_snapshot_json: Mapped[Optional[dict[str, Any]]] = mapped_column(JSON, nullable=True)
|
|
output_snapshot_hash: Mapped[Optional[str]] = mapped_column(String(64), nullable=True)
|
|
job_id: Mapped[Optional[int]] = mapped_column(ForeignKey('jobs.id'), nullable=True)
|
|
worker_id: Mapped[Optional[str]] = mapped_column(String(64), nullable=True)
|
|
lease_token: Mapped[Optional[str]] = mapped_column(String(64), nullable=True)
|
|
lease_expires_at: Mapped[Optional[datetime]] = mapped_column(DateTime, nullable=True)
|
|
heartbeat_at: Mapped[Optional[datetime]] = mapped_column(DateTime, nullable=True)
|
|
started_at: Mapped[Optional[datetime]] = mapped_column(DateTime, nullable=True)
|
|
finished_at: Mapped[Optional[datetime]] = mapped_column(DateTime, nullable=True)
|
|
error_code: Mapped[Optional[str]] = mapped_column(String(64), nullable=True)
|
|
error_summary: Mapped[Optional[str]] = mapped_column(Text, nullable=True)
|
|
recovery_plan_id: Mapped[Optional[int]] = mapped_column(
|
|
ForeignKey('shipping_process_recovery_plans.id', use_alter=True), nullable=True, index=True,
|
|
)
|
|
created_at: Mapped[datetime] = mapped_column(DateTime, default=_utc_now, nullable=False)
|
|
|
|
gate: Mapped[ShippingProcessGate] = relationship(
|
|
back_populates='attempts',
|
|
foreign_keys=[gate_id],
|
|
)
|
|
recovery_plan = relationship(
|
|
'ShippingProcessRecoveryPlan', back_populates='attempts', foreign_keys=[recovery_plan_id],
|
|
)
|