- 更新发货通知工具 API,剥离不可信的操作人身份字段,确保审计安全 - 引入审计事件的哈希链校验机制,支持数据完整性验证 - 增强订单抽取与审计功能,支持敏感快照的访问控制 - 更新相关模型与服务,确保新功能的完整性与可追溯性 - 完善文档与代码注释,反映新的业务逻辑与审计要求
312 lines
14 KiB
Python
312 lines
14 KiB
Python
"""
|
||
backend/app/models/shipping_notice_workflow.py
|
||
|
||
用途:定义货代发货通知 Workflow / Item / Version / Event 四层事实源。
|
||
依赖:users、agents、shipping_notice_documents、shipping_order_extraction_runs、
|
||
shipping_order_review_cases、shipping_tasks。
|
||
逻辑:一次上传批次 = Workflow;每个 PDF 创建时即有 WorkflowItem;
|
||
重识别只新增 ItemVersion 并切换 current 指针;用户可见节点写入 WorkflowEvent;
|
||
审计通过业务编号软引用本表,禁止级联删除抹除追溯。
|
||
"""
|
||
|
||
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 ShippingNoticeWorkflow(Base):
|
||
"""一次上传/识别批次;重新打开对话复用未关闭 Workflow,不新建。"""
|
||
|
||
__tablename__ = 'shipping_notice_workflows'
|
||
__table_args__ = (
|
||
UniqueConstraint('workflow_no', name='uq_shipping_notice_workflows_no'),
|
||
CheckConstraint(
|
||
"status IN ("
|
||
"'open', 'completed', 'cancelled', 'failed'"
|
||
")",
|
||
name='ck_shipping_notice_workflows_status',
|
||
),
|
||
CheckConstraint('lock_version >= 1', name='ck_shipping_notice_workflows_lock_version'),
|
||
Index(
|
||
'ix_shipping_notice_workflows_conversation_status',
|
||
'conversation_id',
|
||
'status',
|
||
'updated_at',
|
||
),
|
||
Index(
|
||
'ix_shipping_notice_workflows_owner_status',
|
||
'owner_user_id',
|
||
'status',
|
||
'updated_at',
|
||
),
|
||
Index(
|
||
'ix_shipping_notice_workflows_open_conversation',
|
||
'conversation_id',
|
||
'updated_at',
|
||
postgresql_where=text("status = 'open'"),
|
||
sqlite_where=text("status = 'open'"),
|
||
),
|
||
)
|
||
|
||
id: Mapped[int] = mapped_column(primary_key=True)
|
||
workflow_no: Mapped[str] = mapped_column(String(64), nullable=False)
|
||
conversation_id: Mapped[str] = mapped_column(String(100), nullable=False, index=True)
|
||
owner_user_id: Mapped[int] = mapped_column(ForeignKey('users.id'), nullable=False, index=True)
|
||
owner_agent_id: Mapped[Optional[int]] = mapped_column(ForeignKey('agents.id'), nullable=True, index=True)
|
||
status: Mapped[str] = mapped_column(String(32), nullable=False, default='open', server_default='open')
|
||
current_stage: Mapped[str] = mapped_column(
|
||
String(64), nullable=False, default='queued', server_default='queued',
|
||
)
|
||
last_activity_at: Mapped[Optional[datetime]] = mapped_column(DateTime, nullable=True)
|
||
closed_reason: Mapped[Optional[str]] = mapped_column(String(64), nullable=True)
|
||
closed_at: Mapped[Optional[datetime]] = mapped_column(DateTime, nullable=True)
|
||
lock_version: Mapped[int] = mapped_column(Integer, nullable=False, default=1, server_default='1')
|
||
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,
|
||
)
|
||
|
||
|
||
class ShippingNoticeWorkflowItem(Base):
|
||
"""PDF 级稳定实体;识别开始前即存在,覆盖解析到 Excel 全阶段。"""
|
||
|
||
__tablename__ = 'shipping_notice_workflow_items'
|
||
__table_args__ = (
|
||
UniqueConstraint('item_no', name='uq_shipping_notice_workflow_items_no'),
|
||
UniqueConstraint(
|
||
'workflow_id',
|
||
'root_document_id',
|
||
name='uq_shipping_notice_workflow_items_workflow_root_doc',
|
||
),
|
||
UniqueConstraint(
|
||
'current_case_id',
|
||
name='uq_shipping_notice_workflow_items_current_case',
|
||
),
|
||
CheckConstraint(
|
||
"lifecycle_status IN ("
|
||
"'queued', 'layout_parsing', 'extracting', 'pending_review', "
|
||
"'building_order', 'inventory_matching', 'generating_excel', "
|
||
"'manual_upload_pending', 'reparse_failed', "
|
||
"'cancelled', 'failed', 'closed'"
|
||
")",
|
||
name='ck_shipping_notice_workflow_items_lifecycle',
|
||
),
|
||
CheckConstraint(
|
||
"closed_reason IS NULL OR closed_reason IN ("
|
||
"'confirmed', 'non_order', 'user_cancelled', 'admin_terminated', 'failed'"
|
||
")",
|
||
name='ck_shipping_notice_workflow_items_closed_reason',
|
||
),
|
||
CheckConstraint('lock_version >= 1', name='ck_shipping_notice_workflow_items_lock_version'),
|
||
CheckConstraint(
|
||
'current_version_no >= 1',
|
||
name='ck_shipping_notice_workflow_items_version_no',
|
||
),
|
||
Index(
|
||
'ix_shipping_notice_workflow_items_workflow_lifecycle',
|
||
'workflow_id',
|
||
'lifecycle_status',
|
||
'updated_at',
|
||
),
|
||
Index(
|
||
'ix_shipping_notice_workflow_items_conversation_lifecycle',
|
||
'conversation_id',
|
||
'lifecycle_status',
|
||
'updated_at',
|
||
),
|
||
Index(
|
||
'ix_shipping_notice_workflow_items_owner_lifecycle',
|
||
'owner_user_id',
|
||
'lifecycle_status',
|
||
'updated_at',
|
||
),
|
||
Index(
|
||
'ix_shipping_notice_workflow_items_open_conversation',
|
||
'conversation_id',
|
||
'updated_at',
|
||
postgresql_where=text(
|
||
"lifecycle_status NOT IN ('closed', 'cancelled')"
|
||
),
|
||
sqlite_where=text(
|
||
"lifecycle_status NOT IN ('closed', 'cancelled')"
|
||
),
|
||
),
|
||
)
|
||
|
||
id: Mapped[int] = mapped_column(primary_key=True)
|
||
item_no: Mapped[str] = mapped_column(String(64), nullable=False)
|
||
workflow_id: Mapped[int] = mapped_column(
|
||
ForeignKey('shipping_notice_workflows.id'), nullable=False, index=True,
|
||
)
|
||
workflow_no: Mapped[str] = mapped_column(String(64), nullable=False, index=True)
|
||
conversation_id: Mapped[str] = mapped_column(String(100), nullable=False, index=True)
|
||
owner_user_id: Mapped[int] = mapped_column(ForeignKey('users.id'), nullable=False, index=True)
|
||
# 同源 PDF 稳定身份:首次纳入时的 Document;替换文件不改变本字段。
|
||
root_document_id: Mapped[int] = mapped_column(
|
||
ForeignKey('shipping_notice_documents.id'), nullable=False, index=True,
|
||
)
|
||
current_document_id: Mapped[int] = mapped_column(
|
||
ForeignKey('shipping_notice_documents.id'), nullable=False, index=True,
|
||
)
|
||
# 软指针:避免与 Version.workflow_item_id 形成循环外键;由 ledger 保证一致性。
|
||
current_version_id: Mapped[Optional[int]] = mapped_column(Integer, nullable=True, index=True)
|
||
current_version_no: Mapped[int] = mapped_column(Integer, nullable=False, default=1, server_default='1')
|
||
current_run_id: Mapped[Optional[int]] = mapped_column(
|
||
ForeignKey('shipping_order_extraction_runs.id'), nullable=True, index=True,
|
||
)
|
||
current_run_no: Mapped[Optional[str]] = mapped_column(String(64), nullable=True)
|
||
current_case_id: Mapped[Optional[int]] = mapped_column(
|
||
ForeignKey('shipping_order_review_cases.id'), nullable=True,
|
||
)
|
||
current_case_no: Mapped[Optional[str]] = mapped_column(String(64), nullable=True)
|
||
current_task_no: Mapped[Optional[str]] = mapped_column(String(64), nullable=True)
|
||
lifecycle_status: Mapped[str] = mapped_column(
|
||
String(32), nullable=False, default='queued', server_default='queued',
|
||
)
|
||
current_stage: Mapped[str] = mapped_column(
|
||
String(64), nullable=False, default='queued', server_default='queued',
|
||
)
|
||
closed_reason: Mapped[Optional[str]] = mapped_column(String(32), nullable=True)
|
||
closed_at: Mapped[Optional[datetime]] = mapped_column(DateTime, nullable=True)
|
||
last_activity_at: Mapped[Optional[datetime]] = mapped_column(DateTime, nullable=True)
|
||
lock_version: Mapped[int] = mapped_column(Integer, nullable=False, default=1, server_default='1')
|
||
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,
|
||
)
|
||
|
||
|
||
class ShippingNoticeWorkflowItemVersion(Base):
|
||
"""重识别版本链;关联 Run / Case / Task / Excel,旧版本只读留存。"""
|
||
|
||
__tablename__ = 'shipping_notice_workflow_item_versions'
|
||
__table_args__ = (
|
||
UniqueConstraint(
|
||
'workflow_item_id',
|
||
'version_no',
|
||
name='uq_shipping_notice_workflow_item_versions_item_no',
|
||
),
|
||
CheckConstraint('version_no >= 1', name='ck_shipping_notice_workflow_item_versions_no'),
|
||
CheckConstraint(
|
||
"status IN ("
|
||
"'active', 'superseded', 'failed', 'cancelled'"
|
||
")",
|
||
name='ck_shipping_notice_workflow_item_versions_status',
|
||
),
|
||
Index(
|
||
'ix_shipping_notice_workflow_item_versions_item_created',
|
||
'workflow_item_id',
|
||
'created_at',
|
||
),
|
||
Index(
|
||
'ix_shipping_notice_workflow_item_versions_run',
|
||
'run_id',
|
||
),
|
||
Index(
|
||
'ix_shipping_notice_workflow_item_versions_case',
|
||
'case_id',
|
||
),
|
||
)
|
||
|
||
id: Mapped[int] = mapped_column(primary_key=True)
|
||
workflow_item_id: Mapped[int] = mapped_column(
|
||
ForeignKey('shipping_notice_workflow_items.id'), nullable=False, index=True,
|
||
)
|
||
item_no: Mapped[str] = mapped_column(String(64), nullable=False, index=True)
|
||
workflow_id: Mapped[int] = mapped_column(
|
||
ForeignKey('shipping_notice_workflows.id'), nullable=False, index=True,
|
||
)
|
||
workflow_no: Mapped[str] = mapped_column(String(64), nullable=False, index=True)
|
||
version_no: Mapped[int] = mapped_column(Integer, nullable=False, default=1, server_default='1')
|
||
document_id: Mapped[int] = mapped_column(
|
||
ForeignKey('shipping_notice_documents.id'), nullable=False, index=True,
|
||
)
|
||
run_id: Mapped[Optional[int]] = mapped_column(
|
||
ForeignKey('shipping_order_extraction_runs.id'), nullable=True, index=True,
|
||
)
|
||
run_no: Mapped[Optional[str]] = mapped_column(String(64), nullable=True)
|
||
case_id: Mapped[Optional[int]] = mapped_column(
|
||
ForeignKey('shipping_order_review_cases.id'), nullable=True,
|
||
)
|
||
case_no: Mapped[Optional[str]] = mapped_column(String(64), nullable=True)
|
||
task_id: Mapped[Optional[int]] = mapped_column(
|
||
ForeignKey('shipping_tasks.id'), nullable=True,
|
||
)
|
||
task_no: Mapped[Optional[str]] = mapped_column(String(64), nullable=True)
|
||
excel_artifact_no: Mapped[Optional[str]] = mapped_column(String(64), nullable=True)
|
||
status: Mapped[str] = mapped_column(
|
||
String(32), nullable=False, default='active', server_default='active',
|
||
)
|
||
is_current: Mapped[bool] = mapped_column(Boolean, nullable=False, default=True, server_default='true')
|
||
is_readonly: Mapped[bool] = mapped_column(Boolean, nullable=False, default=False, server_default='false')
|
||
superseded_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,
|
||
)
|
||
|
||
|
||
class ShippingNoticeWorkflowEvent(Base):
|
||
"""用户可见、按 Workflow 顺序递增的节点事件(与审计账本并存)。"""
|
||
|
||
__tablename__ = 'shipping_notice_workflow_events'
|
||
__table_args__ = (
|
||
UniqueConstraint(
|
||
'workflow_id',
|
||
'seq_no',
|
||
name='uq_shipping_notice_workflow_events_seq',
|
||
),
|
||
Index(
|
||
'ix_shipping_notice_workflow_events_workflow_occurred',
|
||
'workflow_id',
|
||
'occurred_at',
|
||
),
|
||
Index(
|
||
'ix_shipping_notice_workflow_events_item_occurred',
|
||
'workflow_item_id',
|
||
'occurred_at',
|
||
),
|
||
)
|
||
|
||
id: Mapped[int] = mapped_column(primary_key=True)
|
||
workflow_id: Mapped[int] = mapped_column(
|
||
ForeignKey('shipping_notice_workflows.id'), nullable=False, index=True,
|
||
)
|
||
workflow_no: Mapped[str] = mapped_column(String(64), nullable=False, index=True)
|
||
workflow_item_id: Mapped[Optional[int]] = mapped_column(
|
||
ForeignKey('shipping_notice_workflow_items.id'), nullable=True, index=True,
|
||
)
|
||
item_no: Mapped[Optional[str]] = mapped_column(String(64), nullable=True, index=True)
|
||
workflow_item_version_id: Mapped[Optional[int]] = mapped_column(
|
||
ForeignKey('shipping_notice_workflow_item_versions.id'), nullable=True,
|
||
)
|
||
version_no: Mapped[Optional[int]] = mapped_column(Integer, nullable=True)
|
||
seq_no: Mapped[int] = mapped_column(Integer, nullable=False)
|
||
event_code: Mapped[str] = mapped_column(String(64), nullable=False)
|
||
stage_code: Mapped[Optional[str]] = mapped_column(String(64), nullable=True)
|
||
message_code: Mapped[Optional[str]] = mapped_column(String(128), nullable=True)
|
||
actor_type: Mapped[str] = mapped_column(String(16), nullable=False, default='system', server_default='system')
|
||
actor_id: Mapped[Optional[str]] = mapped_column(String(128), nullable=True)
|
||
payload_json: Mapped[Optional[dict[str, Any]]] = mapped_column(JSON, nullable=True)
|
||
reason: Mapped[Optional[str]] = mapped_column(Text, nullable=True)
|
||
occurred_at: Mapped[datetime] = mapped_column(DateTime, default=datetime.utcnow, nullable=False)
|
||
created_at: Mapped[datetime] = mapped_column(DateTime, default=datetime.utcnow, nullable=False)
|