feat(shipping-notice): 收敛复核阶段同源投影与 OT 后履约推进

统一 Case/Progress 阶段 label_key、修复 Job 成功未推进 OT 的可重试失败投影与写路径,并落地 OT 后行级匹配/波次履约与版面诊断开关。

Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
hugoandCursor committed 2026-08-07 13:11:14 +08:00
1 parent 74270adc22
commit fd6a23f870
100 files changed
+12138 -1017

No files matched your search

+4
View File
@@ -81,6 +81,8 @@ from app.models.shipping_order_review import (
)
from app.models.shipping_order_review_line_decision import ShippingOrderReviewLineDecision
from app.models.shipping_order_review_wave_line import ShippingOrderReviewWaveLine
from app.models.shipping_order_review_wave_idempotency import ShippingOrderReviewWaveIdempotency
from app.models.shipping_notice_line_inventory_match import ShippingNoticeLineInventoryMatch
from app.models.shipping_order_review_suggestion import ShippingOrderReviewSuggestion
from app.models.shipping_order_extraction_run import (
ShippingOrderExtractionDiagnosticAudit,
@@ -220,6 +222,8 @@ __all__ = [
'ShippingOrderReviewConfirmedRevision',
'ShippingOrderReviewLineDecision',
'ShippingOrderReviewWaveLine',
'ShippingOrderReviewWaveIdempotency',
'ShippingNoticeLineInventoryMatch',
'ShippingOrderReviewSuggestion',
'ShippingOrderProcessingRun',
'ShippingOrderProcessingRunIntake',
@@ -0,0 +1,112 @@
"""
models/shipping_notice_line_inventory_match.py
用途:正式 OT(ShippingNotice)行级库存匹配事实持久化——可用数量/短缺/match_result 的真相源。
依赖:shipping_notices、shipping_inventory_files、shipping_order_review_cases(可选关联)。
逻辑:由 inventory_matching 完成后写入;同一 OT+行+快照唯一;新快照新增行不覆盖历史;
is_current 标记投影用的当前事实;禁止用 Job payload / line_projections 充当正式真相。
"""
from __future__ import annotations
from datetime import datetime
from typing import Any, Optional
from sqlalchemy import (
Boolean,
CheckConstraint,
DateTime,
ForeignKey,
Index,
Integer,
JSON,
String,
Text,
UniqueConstraint,
text,
)
from sqlalchemy.orm import Mapped, mapped_column
from app.models.base import Base
class ShippingNoticeLineInventoryMatch(Base):
"""一条 OT 明细在某库存快照下的匹配事实(不可原地覆盖历史快照结果)。"""
__tablename__ = 'shipping_notice_line_inventory_matches'
__table_args__ = (
UniqueConstraint(
'task_id',
'line_key',
'inventory_snapshot_id',
name='uq_shipping_notice_line_inv_match_task_line_snap',
),
# 同一 OT 行同一时刻仅一条当前事实(PostgreSQL partial unique)。
Index(
'uq_shipping_notice_line_inv_match_current',
'task_id',
'line_key',
unique=True,
postgresql_where=text('is_current IS TRUE'),
),
Index('ix_shipping_notice_line_inv_match_task', 'task_id'),
Index('ix_shipping_notice_line_inv_match_case', 'review_case_id'),
Index('ix_shipping_notice_line_inv_match_snapshot', 'inventory_snapshot_id'),
CheckConstraint(
"match_result IN ("
"'matched', 'not_found', 'result_missing', 'preparing', "
"'source_conflict', 'invalid_key', 'mapping_failed', 'unavailable'"
")",
name='ck_shipping_notice_line_inv_match_result',
),
CheckConstraint(
"inventory_status IN ("
"'ready', 'preparing', 'mapping_failed', 'source_conflict', "
"'unavailable', 'invalid_key'"
")",
name='ck_shipping_notice_line_inv_match_status',
),
)
id: Mapped[int] = mapped_column(primary_key=True)
task_id: Mapped[int] = mapped_column(
ForeignKey('shipping_notices.id', ondelete='CASCADE'),
nullable=False,
)
review_case_id: Mapped[Optional[int]] = mapped_column(
ForeignKey('shipping_order_review_cases.id', ondelete='SET NULL'),
nullable=True,
)
line_key: Mapped[str] = mapped_column(String(64), nullable=False)
line_index: Mapped[Optional[int]] = mapped_column(Integer, nullable=True)
inventory_snapshot_id: Mapped[Optional[int]] = mapped_column(
ForeignKey('shipping_inventory_files.id', ondelete='SET NULL'),
nullable=True,
)
match_result: Mapped[str] = mapped_column(String(32), nullable=False)
inventory_status: Mapped[str] = mapped_column(String(32), nullable=False)
required_qty: Mapped[Optional[str]] = mapped_column(String(32), nullable=True)
available_qty: Mapped[Optional[str]] = mapped_column(String(32), nullable=True)
shortage_qty: Mapped[Optional[str]] = mapped_column(String(32), nullable=True)
reservation_id: Mapped[Optional[str]] = mapped_column(String(128), nullable=True)
part_number: Mapped[Optional[str]] = mapped_column(String(128), nullable=True)
unit: Mapped[Optional[str]] = mapped_column(String(32), nullable=True)
matching_rule_version: Mapped[Optional[str]] = mapped_column(String(64), nullable=True)
inventory_source_code: Mapped[Optional[str]] = mapped_column(String(64), nullable=True)
is_current: Mapped[bool] = mapped_column(Boolean, nullable=False, default=True, server_default='true')
matched_at: Mapped[datetime] = mapped_column(DateTime, nullable=False, default=datetime.utcnow)
detail_json: Mapped[dict[str, Any]] = mapped_column(JSON, nullable=False, default=dict)
# BE-7:自动履约失败独立存储(非 decision_code 枚举)
auto_fulfill_failed: Mapped[bool] = mapped_column(
Boolean, nullable=False, default=False, server_default='false',
)
auto_fulfill_error_code: Mapped[Optional[str]] = mapped_column(String(128), nullable=True)
auto_fulfill_error_message: Mapped[Optional[str]] = mapped_column(Text, nullable=True)
auto_fulfill_failed_at: Mapped[Optional[datetime]] = mapped_column(DateTime, nullable=True)
created_at: Mapped[datetime] = mapped_column(DateTime, default=datetime.utcnow, nullable=False)
updated_at: Mapped[datetime] = mapped_column(
DateTime,
default=datetime.utcnow,
onupdate=datetime.utcnow,
nullable=False,
)
@@ -1,10 +1,11 @@
"""
models/shipping_order_review_line_decision.py
用途:建单前复核 Case 的行级履约决定(版本化、可追溯)。
用途:复核 Case 的行级履约决定(版本化、可追溯);含 OT 后系统/人工来源。
依赖:ShippingOrderReviewCase、ShippingNotice、users。
逻辑:由 order_review_fulfillment_decision_service 写入;同一 (case, line_key, version) 唯一;
活动决定不可覆盖,重开生成新版本;Task 波次通过 task_id 关联。
活动决定不可覆盖,重开生成新版本;Task 波次通过 task_id 关联;
decision_source 区分 system/user(历史可空)。
"""
from __future__ import annotations
@@ -43,6 +44,10 @@ class ShippingOrderReviewLineDecision(Base):
")",
name='ck_shipping_review_line_decisions_status',
),
CheckConstraint(
"decision_source IS NULL OR decision_source IN ('system', 'user', 'unknown')",
name='ck_shipping_review_line_decisions_source',
),
CheckConstraint('decision_version >= 1', name='ck_shipping_review_line_decisions_version'),
Index('ix_shipping_review_line_decisions_case_line', 'review_case_id', 'line_key'),
Index('ix_shipping_review_line_decisions_case_active', 'review_case_id', 'is_active'),
@@ -67,6 +72,8 @@ class ShippingOrderReviewLineDecision(Base):
part_number: Mapped[Optional[str]] = mapped_column(String(128), nullable=True)
task_id: Mapped[Optional[int]] = mapped_column(ForeignKey('shipping_notices.id'), nullable=True)
wave_no: Mapped[Optional[int]] = mapped_column(Integer, nullable=True)
# system=自动履约;user=人工异常/显式履约;unknown=历史无法判定。
decision_source: Mapped[Optional[str]] = mapped_column(String(16), nullable=True)
actor_user_id: Mapped[Optional[int]] = mapped_column(ForeignKey('users.id'), nullable=True)
snapshot_json: Mapped[dict[str, Any]] = mapped_column(JSON, nullable=False, default=dict)
created_at: Mapped[datetime] = mapped_column(DateTime, default=datetime.utcnow, nullable=False)
@@ -0,0 +1,67 @@
"""
models/shipping_order_review_wave_idempotency.py
用途:post-OT 履约 wave 请求幂等表(idempotency_key → wave 身份),无 wave 主表时提供强约束。
依赖:shipping_order_review_cases、shipping_notices。
逻辑:create_post_ot_fulfillment_wave 写入/回放;同 key 参数不一致 → 409;
progress_* 记录本 wave Excel/RPA 推进状态(失败不回滚已提交 wave)。
"""
from __future__ import annotations
from datetime import datetime
from typing import Any, Optional
from sqlalchemy import DateTime, ForeignKey, Index, Integer, JSON, String, Text, UniqueConstraint
from sqlalchemy.orm import Mapped, mapped_column
from app.models.base import Base
class ShippingOrderReviewWaveIdempotency(Base):
"""一次 post-OT wave 创建请求的幂等记录。"""
__tablename__ = 'shipping_order_review_wave_idempotency'
__table_args__ = (
UniqueConstraint(
'idempotency_key',
name='uq_shipping_review_wave_idempotency_key',
),
Index('ix_shipping_review_wave_idempotency_case', 'review_case_id'),
Index('ix_shipping_review_wave_idempotency_task', 'task_id'),
Index('ix_shipping_review_wave_idempotency_wave', 'review_case_id', 'wave_no'),
)
id: Mapped[int] = mapped_column(primary_key=True)
idempotency_key: Mapped[str] = mapped_column(String(191), nullable=False)
review_case_id: Mapped[int] = mapped_column(
ForeignKey('shipping_order_review_cases.id', ondelete='CASCADE'),
nullable=False,
)
task_id: Mapped[int] = mapped_column(
ForeignKey('shipping_notices.id', ondelete='CASCADE'),
nullable=False,
)
task_no: Mapped[str] = mapped_column(String(64), nullable=False)
wave_no: Mapped[Optional[int]] = mapped_column(Integer, nullable=True)
decision_source: Mapped[str] = mapped_column(String(16), nullable=False)
# 稳定排序后的 line_key JSON 数组字符串指纹(冲突比对)。
line_keys_json: Mapped[list[Any]] = mapped_column(JSON, nullable=False, default=list)
line_keys_hash: Mapped[str] = mapped_column(String(64), nullable=False)
# 行集合共享快照 ID;多快照时为空,靠 fingerprint 比对。
inventory_snapshot_id: Mapped[Optional[int]] = mapped_column(Integer, nullable=True)
inventory_snapshot_fingerprint: Mapped[str] = mapped_column(String(256), nullable=False, default='')
status: Mapped[str] = mapped_column(String(32), nullable=False, default='succeeded')
result_json: Mapped[dict[str, Any]] = mapped_column(JSON, nullable=False, default=dict)
# 本 wave 下游推进(Excel/RPA);失败不回滚 wave 创建事实。
progress_status: Mapped[str] = mapped_column(String(32), nullable=False, default='pending')
progress_job_id: Mapped[Optional[int]] = mapped_column(Integer, nullable=True)
progress_error: Mapped[Optional[str]] = mapped_column(Text, nullable=True)
progress_idempotency_key: Mapped[Optional[str]] = mapped_column(String(191), nullable=True)
created_at: Mapped[datetime] = mapped_column(DateTime, default=datetime.utcnow, nullable=False)
updated_at: Mapped[datetime] = mapped_column(
DateTime,
default=datetime.utcnow,
onupdate=datetime.utcnow,
nullable=False,
)