feat(shipping-notice): 增强订单抽取与审计功能,支持取消与保留策略

- 在环境配置中新增进度细化、订单提取取消与审计导出相关的开关
- 实现订单抽取运行的取消功能,允许管理员在运行中进行操作
- 引入审计事件的保留策略,支持三年保留期的自动清理
- 更新相关模型与服务,确保新功能的完整性与可追溯性
- 增强文档与代码注释,反映新的业务逻辑与审计要求
This commit is contained in:
hugo committed 2026-07-30 14:52:21 +08:00
1 parent c85997884b
commit 95cdfe66b0
158 files changed
+19005 -1201

No files matched your search

+45 -3
View File
@@ -2,16 +2,19 @@
backend/app/models/shipping_audit_event.py
用途:定义货代领域统一、仅追加的审计事件。
依赖:SQLAlchemy Base、货代 Run/Task/Intake 的稳定业务编号。
依赖:SQLAlchemy Base、货代 Run/Task/Intake/会话 PDF 工作项的稳定业务编号。
逻辑:各业务服务通过 audit_service 追加事件;事件不依赖可级联删除的业务外键,保留跨生命周期追溯事实。
进度审计阶段扩展 operation_id、对象关联、白名单前后快照、证据引用、结果与保留到期时间;
会话工作项阶段追加 owner_user_id、session_item_id 与脱敏 client_ip_hash/user_agent_hash;
禁止存储 PDF/邮件正文、模型原文、对象存储 URL、原始 IP/UA 或密钥。
"""
from __future__ import annotations
from datetime import datetime
from typing import Any
from typing import Any, Optional
from sqlalchemy import CheckConstraint, DateTime, Index, Integer, JSON, String, event
from sqlalchemy import CheckConstraint, DateTime, Index, Integer, JSON, String, Text, event
from sqlalchemy.orm import Mapped, mapped_column
from app.models.base import Base
@@ -27,9 +30,23 @@ class ShippingAuditEvent(Base):
"actor_type IN ('user', 'system', 'rpa', 'chatflow', 'admin')",
name='ck_shipping_audit_events_actor_type',
),
CheckConstraint(
"result IS NULL OR result IN ('success', 'failure', 'rejected', 'ignored')",
name='ck_shipping_audit_events_result',
),
Index('ix_shipping_audit_events_aggregate_created', 'aggregate_type', 'aggregate_id', 'created_at'),
Index('ix_shipping_audit_events_run_created', 'run_no', 'created_at'),
Index('ix_shipping_audit_events_task_created', 'task_no', 'created_at'),
Index('ix_shipping_audit_events_operation_created', 'operation_id', 'created_at'),
Index('ix_shipping_audit_events_request_created', 'request_id', 'created_at'),
Index('ix_shipping_audit_events_conversation_created', 'conversation_id', 'created_at'),
Index('ix_shipping_audit_events_document_created', 'document_id', 'created_at'),
Index('ix_shipping_audit_events_unit_created', 'unit_id', 'created_at'),
Index('ix_shipping_audit_events_actor_created', 'actor_type', 'actor_id', 'created_at'),
Index('ix_shipping_audit_events_event_type_created', 'event_type', 'created_at'),
Index('ix_shipping_audit_events_retention_expires', 'retention_expires_at', 'id'),
Index('ix_shipping_audit_events_session_item_created', 'session_item_id', 'created_at'),
Index('ix_shipping_audit_events_owner_created', 'owner_user_id', 'created_at'),
)
id: Mapped[int] = mapped_column(primary_key=True)
@@ -41,12 +58,37 @@ class ShippingAuditEvent(Base):
intake_no: Mapped[str | None] = mapped_column(String(64), nullable=True)
actor_type: Mapped[str] = mapped_column(String(16), nullable=False, default='system', server_default='system')
actor_id: Mapped[str | None] = mapped_column(String(128), nullable=True)
actor_role: Mapped[str | None] = mapped_column(String(64), nullable=True)
request_id: Mapped[str | None] = mapped_column(String(64), nullable=True)
operation_id: Mapped[str | None] = mapped_column(String(64), nullable=True)
correlation_id: Mapped[str | None] = mapped_column(String(64), nullable=True)
source: Mapped[str | None] = mapped_column(String(32), nullable=True)
result: Mapped[str | None] = mapped_column(String(16), nullable=True)
error_code: Mapped[str | None] = mapped_column(String(64), nullable=True)
conversation_id: Mapped[str | None] = mapped_column(String(100), nullable=True)
unit_id: Mapped[str | None] = mapped_column(String(64), nullable=True)
document_id: Mapped[str | None] = mapped_column(String(64), nullable=True)
review_case_no: Mapped[str | None] = mapped_column(String(64), nullable=True)
# 会话 PDF 工作项稳定编号(item_no);软引用,避免级联删除抹除审计。
session_item_id: Mapped[str | None] = mapped_column(String(64), nullable=True)
# 业务属主(非操作者);代操作场景与 actor_id 区分。
owner_user_id: Mapped[str | None] = mapped_column(String(64), nullable=True)
# 仅存哈希,禁止写入原始 IP / User-Agent。
client_ip_hash: Mapped[str | None] = mapped_column(String(64), nullable=True)
user_agent_hash: Mapped[str | None] = mapped_column(String(64), nullable=True)
attempt_no: Mapped[int | None] = mapped_column(Integer, nullable=True)
action: Mapped[str | None] = mapped_column(String(64), nullable=True)
reason_code: Mapped[str | None] = mapped_column(String(128), nullable=True)
reason: Mapped[str | None] = mapped_column(Text, nullable=True)
before_hash: Mapped[str | None] = mapped_column(String(64), nullable=True)
after_hash: Mapped[str | None] = mapped_column(String(64), nullable=True)
before_snapshot_json: Mapped[Optional[dict[str, Any]]] = mapped_column(JSON, nullable=True)
after_snapshot_json: Mapped[Optional[dict[str, Any]]] = mapped_column(JSON, nullable=True)
evidence_refs_json: Mapped[Optional[list[dict[str, Any]]]] = mapped_column(JSON, nullable=True)
artifact_refs_json: Mapped[Optional[list[dict[str, Any]]]] = mapped_column(JSON, nullable=True)
context_refs_json: Mapped[Optional[dict[str, Any]]] = mapped_column(JSON, nullable=True)
metadata_json: Mapped[dict[str, Any]] = mapped_column(JSON, nullable=False, default=dict)
retention_expires_at: Mapped[Optional[datetime]] = mapped_column(DateTime, nullable=True)
created_at: Mapped[datetime] = mapped_column(DateTime, default=datetime.utcnow, nullable=False)