feat(shipping_notice): 新增货代订单全流程业务模块及配套基建

本次提交新增货代订单处理全流程的完整业务能力、数据库迁移与测试用例:
1.  统一追加式审计事件体系,支持全链路不可篡改的业务追溯
2.  货代任务撤销、版本替代管控逻辑,规范已上传订单的合规版本更替
3.  订单抽取路由审计、模板自动冻结资格、路由计数等核心业务基建
4.  EML订单补充表解析契约与合并逻辑,完善订单补全能力
5.  Chatflow只读能力令牌校验机制,保障工具调用安全
6.  人工复核证据校验服务,规范人工审核的证据留存
所有变更均配套完整的单元测试与迁移契约测试,支持受控回退
This commit is contained in:
hugo committed 2026-07-28 08:56:58 +08:00
1 parent 90f77634d6
commit 1f3ecb87b7
128 files changed
+3635 -2074

No files matched your search

@@ -0,0 +1,73 @@
"""add order extraction route decisions
Revision ID: 06ef1a2b3c4d
Revises: 05df1e2f3a4b
Create Date: 2026-07-27 21:10:00.000000
用途:为订单抽取 Unit 的每一次模板分流保留不可变、脱敏的审计事实。
依赖:shipping_order_extraction_runs、shipping_order_extraction_units、jobs、Profile。
逻辑:决策在 Worker 分流前追加;重试以 unit/job/attempt 唯一。统一长期审计由后继 shipping_audit_events 保存。
"""
from alembic import op
import sqlalchemy as sa
revision = '06ef1a2b3c4d'
down_revision = '05df1e2f3a4b'
branch_labels = None
depends_on = None
def upgrade() -> None:
"""创建 Unit 路由决策审计表及按 Run/Unit 查询的索引。"""
op.create_table(
'shipping_order_extraction_route_decisions',
sa.Column('id', sa.Integer(), nullable=False),
sa.Column('run_id', sa.Integer(), nullable=False),
sa.Column('unit_id', sa.Integer(), nullable=False),
sa.Column('job_id', sa.Integer(), nullable=False),
sa.Column('attempt_no', sa.Integer(), nullable=False),
sa.Column('decision', sa.String(length=16), nullable=False),
sa.Column('match_status', sa.String(length=32), nullable=False),
sa.Column('selected_profile_id', sa.Integer(), nullable=True),
sa.Column('selected_profile_code', sa.String(length=128), nullable=True),
sa.Column('selected_profile_version', sa.Integer(), nullable=True),
sa.Column('selected_profile_checksum', sa.String(length=64), nullable=True),
sa.Column('auto_create_approved', sa.Boolean(), nullable=True),
sa.Column('candidate_count', sa.Integer(), nullable=False, server_default='0'),
sa.Column('table_count', sa.Integer(), nullable=False, server_default='0'),
sa.Column('approved_table_count', sa.Integer(), nullable=False, server_default='0'),
sa.Column('candidate_summary_json', sa.JSON(), nullable=False),
sa.Column('fallback_reason_codes_json', sa.JSON(), nullable=False),
sa.Column('created_at', sa.DateTime(), nullable=False),
sa.CheckConstraint(
"decision IN ('deterministic', 'review_required', 'skipped')",
name='ck_shipping_order_extraction_route_decision',
),
sa.CheckConstraint(
"match_status IN ('unique_match', 'no_match', 'ambiguous', 'layout_drift', 'evidence_insufficient')",
name='ck_shipping_order_extraction_route_match_status',
),
sa.ForeignKeyConstraint(['job_id'], ['jobs.id']),
sa.ForeignKeyConstraint(['run_id'], ['shipping_order_extraction_runs.id'], ondelete='CASCADE'),
sa.ForeignKeyConstraint(['selected_profile_id'], ['shipping_order_document_profiles.id']),
sa.ForeignKeyConstraint(['unit_id'], ['shipping_order_extraction_units.id'], ondelete='CASCADE'),
sa.PrimaryKeyConstraint('id'),
sa.UniqueConstraint('unit_id', 'job_id', 'attempt_no', name='uq_shipping_order_extraction_route_decision_attempt'),
)
op.create_index(
'ix_shipping_order_extraction_route_decisions_run_unit',
'shipping_order_extraction_route_decisions', ['run_id', 'unit_id', 'id'],
)
op.create_index(
'ix_shipping_order_extraction_route_decisions_unit_created',
'shipping_order_extraction_route_decisions', ['unit_id', 'created_at'],
)
def downgrade() -> None:
"""删除路由决策审计表及其索引。"""
op.drop_index('ix_shipping_order_extraction_route_decisions_unit_created', table_name='shipping_order_extraction_route_decisions')
op.drop_index('ix_shipping_order_extraction_route_decisions_run_unit', table_name='shipping_order_extraction_route_decisions')
op.drop_table('shipping_order_extraction_route_decisions')
@@ -0,0 +1,58 @@
"""add shipping audit events
Revision ID: 07f01a2b3c4d
Revises: 06ef1a2b3c4d
Create Date: 2026-07-27 23:15:00.000000
用途:建立不依赖业务外键的货代统一追加式审计表。
依赖:订单抽取 Run/Unit 的稳定业务编号;不级联删除任何审计事实。
逻辑:状态变化、模板路由与后续履约动作统一写入该表;仅保存哈希和脱敏元数据。
"""
from alembic import op
import sqlalchemy as sa
revision = '07f01a2b3c4d'
down_revision = '06ef1a2b3c4d'
branch_labels = None
depends_on = None
def upgrade() -> None:
"""创建统一审计表和按聚合、Run、Task 查询的索引。"""
op.create_table(
'shipping_audit_events',
sa.Column('id', sa.Integer(), nullable=False),
sa.Column('event_type', sa.String(length=64), nullable=False),
sa.Column('aggregate_type', sa.String(length=64), nullable=False),
sa.Column('aggregate_id', sa.String(length=128), nullable=False),
sa.Column('run_no', sa.String(length=64), nullable=True),
sa.Column('task_no', sa.String(length=64), nullable=True),
sa.Column('intake_no', sa.String(length=64), nullable=True),
sa.Column('actor_type', sa.String(length=16), nullable=False, server_default='system'),
sa.Column('actor_id', sa.String(length=128), nullable=True),
sa.Column('request_id', sa.String(length=64), nullable=True),
sa.Column('correlation_id', sa.String(length=64), nullable=True),
sa.Column('reason_code', sa.String(length=128), nullable=True),
sa.Column('before_hash', sa.String(length=64), nullable=True),
sa.Column('after_hash', sa.String(length=64), nullable=True),
sa.Column('metadata_json', sa.JSON(), nullable=False),
sa.Column('created_at', sa.DateTime(), nullable=False),
sa.CheckConstraint(
"actor_type IN ('user', 'system', 'rpa', 'chatflow', 'admin')",
name='ck_shipping_audit_events_actor_type',
),
sa.PrimaryKeyConstraint('id'),
)
op.create_index('ix_shipping_audit_events_aggregate_created', 'shipping_audit_events', ['aggregate_type', 'aggregate_id', 'created_at'])
op.create_index('ix_shipping_audit_events_run_created', 'shipping_audit_events', ['run_no', 'created_at'])
op.create_index('ix_shipping_audit_events_task_created', 'shipping_audit_events', ['task_no', 'created_at'])
def downgrade() -> None:
"""删除审计表;仅用于受控回滚,业务运行不得删除已有审计记录。"""
op.drop_index('ix_shipping_audit_events_task_created', table_name='shipping_audit_events')
op.drop_index('ix_shipping_audit_events_run_created', table_name='shipping_audit_events')
op.drop_index('ix_shipping_audit_events_aggregate_created', table_name='shipping_audit_events')
op.drop_table('shipping_audit_events')
@@ -0,0 +1,70 @@
"""remove order extraction provider fields
Revision ID: 08f12a3b4c5d
Revises: 07f01a2b3c4d
Create Date: 2026-07-27 23:40:00.000000
用途:移除订单解析的 Runtime Binding/Provider 持久化依赖,冻结确定性解析版本和结果。
依赖:shipping_order_extraction_runs、订单抽取阶段 0 审计契约。
逻辑:破坏性迁移不保留历史 Provider 快照;新 Run 不需要 Dify 或内部 Workflow 配置。
"""
from alembic import op
import sqlalchemy as sa
revision = '08f12a3b4c5d'
down_revision = '07f01a2b3c4d'
branch_labels = None
depends_on = None
def upgrade() -> None:
"""替换 Provider 字段为确定性解析器版本、执行结果和人工冻结策略。"""
inspector = sa.inspect(op.get_bind())
existing_columns = {
column['name']
for column in inspector.get_columns('shipping_order_extraction_runs')
}
existing_indexes = {
index['name']
for index in inspector.get_indexes('shipping_order_extraction_runs')
}
existing_foreign_keys = {
foreign_key['name']
for foreign_key in inspector.get_foreign_keys('shipping_order_extraction_runs')
if foreign_key.get('name')
}
with op.batch_alter_table('shipping_order_extraction_runs') as batch:
for index in ('ix_shipping_order_extraction_runs_runtime_binding_id', 'ix_shipping_order_extraction_runs_provider_agent_id'):
if index in existing_indexes:
batch.drop_index(index)
for constraint in ('fk_shipping_order_extraction_runs_runtime_binding_id', 'fk_shipping_order_extraction_runs_provider_agent_id'):
if constraint in existing_foreign_keys:
batch.drop_constraint(constraint, type_='foreignkey')
for column in (
'runtime_binding_id', 'provider_agent_id', 'quality_batch_id', 'provider_config_json',
'provider_snapshot_json', 'dify_workflow_run_id', 'provider_result_json', 'provider_completed_at',
):
if column in existing_columns:
batch.drop_column(column)
batch.add_column(sa.Column('parser_engine_version', sa.String(length=64), nullable=False, server_default='deterministic-order-parser.v1'))
batch.add_column(sa.Column('parser_policy_version', sa.String(length=64), nullable=False, server_default='deterministic-order-policy.v1'))
batch.add_column(sa.Column('auto_freeze_policy_json', sa.JSON(), nullable=False, server_default=sa.text("'{}'")))
batch.add_column(sa.Column('execution_result_json', sa.JSON(), nullable=True))
batch.add_column(sa.Column('extraction_completed_at', sa.DateTime(), nullable=True))
def downgrade() -> None:
"""恢复旧列结构,仅用于受控回滚;不会恢复已删除 Provider 内容。"""
with op.batch_alter_table('shipping_order_extraction_runs') as batch:
for column in ('parser_engine_version', 'parser_policy_version', 'auto_freeze_policy_json', 'execution_result_json', 'extraction_completed_at'):
batch.drop_column(column)
batch.add_column(sa.Column('runtime_binding_id', sa.Integer(), nullable=True))
batch.add_column(sa.Column('provider_agent_id', sa.Integer(), nullable=True))
batch.add_column(sa.Column('quality_batch_id', sa.Integer(), nullable=True))
batch.add_column(sa.Column('provider_config_json', sa.JSON(), nullable=True))
batch.add_column(sa.Column('provider_snapshot_json', sa.JSON(), nullable=True))
batch.add_column(sa.Column('dify_workflow_run_id', sa.String(length=128), nullable=True))
batch.add_column(sa.Column('provider_result_json', sa.JSON(), nullable=True))
batch.add_column(sa.Column('provider_completed_at', sa.DateTime(), nullable=True))
@@ -0,0 +1,33 @@
"""add order route table counters
Revision ID: 09a23b4c5d6e
Revises: 08f12a3b4c5d
Create Date: 2026-07-27 23:55:00.000000
用途:细分订单模板路由的文档、业务、匹配与解析表计数。
依赖:shipping_order_extraction_route_decisions 阶段 0 路由审计表。
逻辑:只保存计数,不保存订单值、正文或坐标载荷。
"""
from alembic import op
import sqlalchemy as sa
revision = '09a23b4c5d6e'
down_revision = '08f12a3b4c5d'
branch_labels = None
depends_on = None
def upgrade() -> None:
"""为 Route Decision 增加四类不可逆业务计数。"""
with op.batch_alter_table('shipping_order_extraction_route_decisions') as batch:
for column in ('document_table_count', 'business_table_count', 'matched_table_count', 'parsed_table_count'):
batch.add_column(sa.Column(column, sa.Integer(), nullable=False, server_default='0'))
def downgrade() -> None:
"""回退阶段 3 路由计数列。"""
with op.batch_alter_table('shipping_order_extraction_route_decisions') as batch:
for column in ('parsed_table_count', 'matched_table_count', 'business_table_count', 'document_table_count'):
batch.drop_column(column)
@@ -0,0 +1,53 @@
"""add independent order profile auto-freeze qualifications
Revision ID: 0aa34b5c6d7e
Revises: 09a23b4c5d6e
Create Date: 2026-07-28 00:20:00.000000
用途:将订单模板发布生命周期与自动冻结资格分离。
依赖:订单 Profile、Schema、盲测套件和用户表。
逻辑:资格按 Profile 版本/checksum 冻结,首版仅写 manual_confirmation,不会反向打开自动建单。
"""
from alembic import op
import sqlalchemy as sa
revision = '0aa34b5c6d7e'
down_revision = '09a23b4c5d6e'
branch_labels = None
depends_on = None
def upgrade() -> None:
"""创建模板版本级自动冻结资格表。"""
op.create_table(
'shipping_document_profile_auto_freeze_qualifications',
sa.Column('id', sa.Integer(), primary_key=True),
sa.Column('profile_id', sa.Integer(), sa.ForeignKey('shipping_order_document_profiles.id'), nullable=False),
sa.Column('profile_version', sa.Integer(), nullable=False),
sa.Column('profile_checksum', sa.String(64), nullable=False),
sa.Column('schema_version_id', sa.Integer(), sa.ForeignKey('shipping_order_schema_versions.id'), nullable=False),
sa.Column('parser_version', sa.String(64), nullable=False),
sa.Column('quality_suite_id', sa.Integer(), sa.ForeignKey('shipping_document_profile_test_suites.id'), nullable=True),
sa.Column('status', sa.String(32), nullable=False, server_default='manual_confirmation'),
sa.Column('approved_by_id', sa.Integer(), sa.ForeignKey('users.id'), nullable=True),
sa.Column('approved_at', sa.DateTime(), nullable=True),
sa.Column('suspended_by_id', sa.Integer(), sa.ForeignKey('users.id'), nullable=True),
sa.Column('suspended_at', sa.DateTime(), nullable=True),
sa.Column('suspension_reason', sa.Text(), nullable=True),
sa.Column('created_at', sa.DateTime(), nullable=False),
sa.Column('updated_at', sa.DateTime(), nullable=False),
sa.UniqueConstraint('profile_id', 'profile_version', 'profile_checksum', name='uq_shipping_document_profile_auto_freeze_qualification_version'),
sa.CheckConstraint("status IN ('manual_confirmation', 'auto_freeze_approved', 'auto_freeze_suspended')", name='ck_shipping_document_profile_auto_freeze_qualification_status'),
)
op.create_index(
'ix_ship_doc_prof_auto_freeze_profile_status',
'shipping_document_profile_auto_freeze_qualifications', ['profile_id', 'status'],
)
def downgrade() -> None:
"""删除独立资格表,不修改模板发布记录。"""
op.drop_index('ix_ship_doc_prof_auto_freeze_profile_status', table_name='shipping_document_profile_auto_freeze_qualifications')
op.drop_table('shipping_document_profile_auto_freeze_qualifications')
@@ -0,0 +1,33 @@
"""add snapshot task uniqueness and freeze source manifest
Revision ID: 1bb45c6d7e8f
Revises: 0aa34b5c6d7e
Create Date: 2026-07-28 00:30:00.000000
用途:冻结并发时保证一个 Task 只有一个 Snapshot,并固化冻结字段来源清单。
"""
from alembic import op
import sqlalchemy as sa
revision = '1bb45c6d7e8f'
down_revision = '0aa34b5c6d7e'
branch_labels = None
depends_on = None
def upgrade() -> None:
"""为冻结快照增加并发唯一约束和来源 JSON。"""
with op.batch_alter_table('shipping_order_snapshots') as batch_op:
batch_op.add_column(sa.Column('source_manifest_json', sa.JSON(), nullable=False, server_default=sa.text("'{}'")))
batch_op.create_unique_constraint('uq_shipping_order_snapshots_task', ['task_id'])
with op.batch_alter_table('shipping_order_snapshots') as batch_op:
batch_op.alter_column('source_manifest_json', server_default=None)
def downgrade() -> None:
"""回滚阶段六快照字段与任务唯一约束。"""
with op.batch_alter_table('shipping_order_snapshots') as batch_op:
batch_op.drop_constraint('uq_shipping_order_snapshots_task', type_='unique')
batch_op.drop_column('source_manifest_json')
@@ -0,0 +1,21 @@
"""add freight cancellation records for task supersession
Revision ID: 2cc56d7e8f9a
Revises: 1bb45c6d7e8f
Create Date: 2026-07-28 00:40:00.000000
"""
from alembic import op
import sqlalchemy as sa
revision = '2cc56d7e8f9a'
down_revision = '1bb45c6d7e8f'
branch_labels = None
depends_on = None
def upgrade() -> None:
op.create_table('shipping_freight_cancellations', sa.Column('id', sa.Integer(), primary_key=True), sa.Column('old_task_id', sa.Integer(), sa.ForeignKey('shipping_tasks.id'), nullable=False), sa.Column('new_task_id', sa.Integer(), sa.ForeignKey('shipping_tasks.id'), nullable=False), sa.Column('status', sa.String(32), nullable=False), sa.Column('requested_by_id', sa.Integer(), sa.ForeignKey('users.id')), sa.Column('requested_at', sa.DateTime(), nullable=False), sa.Column('completed_by_id', sa.Integer(), sa.ForeignKey('users.id')), sa.Column('completed_at', sa.DateTime()), sa.Column('freight_receipt_no', sa.String(128)), sa.Column('receipt_asset_id', sa.Integer(), sa.ForeignKey('file_assets.id')), sa.Column('reason', sa.Text(), nullable=False), sa.Column('failure_reason', sa.Text()), sa.UniqueConstraint('old_task_id', 'new_task_id', name='uq_shipping_freight_cancellations_version_pair'))
op.create_index('ix_shipping_freight_cancellations_old_status', 'shipping_freight_cancellations', ['old_task_id', 'status'])
def downgrade() -> None:
op.drop_index('ix_shipping_freight_cancellations_old_status', table_name='shipping_freight_cancellations')
op.drop_table('shipping_freight_cancellations')
@@ -0,0 +1,18 @@
"""enforce one active shipping notice chatflow
Revision ID: 3dd67e8f9a0b
Revises: 2cc56d7e8f9a
Create Date: 2026-07-28 01:00:00.000000
"""
from alembic import op
revision = '3dd67e8f9a0b'
down_revision = '2cc56d7e8f9a'
branch_labels = None
depends_on = None
def upgrade() -> None:
op.create_index('uq_agents_one_active_shipping_notice', 'agents', ['agent_code'], unique=True, postgresql_where="agent_code = 'shipping_notice' AND is_active = true", sqlite_where="agent_code = 'shipping_notice' AND is_active = 1")
def downgrade() -> None:
op.drop_index('uq_agents_one_active_shipping_notice', table_name='agents')