feat(freight-rpa): 增加出站报表配置与回调支持,重构相关服务

- 将 RPA 管理 API 扩展至出站报表配置,新增出站相关的回调接口。
- 重构 freight_rpa_report_config_service,支持入站与出站报表配置的管理。
- 更新 ORM 模型,增加出站报表相关的持久化模型。
- 统一命名规范,确保在手、入站与出站的概念一致性。
This commit is contained in:
hugo committed 2026-10-19 18:45:16 +08:00
1 parent 39fe3c05f2
commit 911afa0f73
152 files changed
+9861 -2984

No files matched your search

@@ -0,0 +1,275 @@
"""
migrations/versions/s19a0b1c2d3e4_freight_rpa_outbound_and_inbound_slim.py
用途:新建出站 RPA 三表,并拆除入站 per-file 企微投递表
freight_rpa_inbound_notification_deliveries(Task 2 已删 Python 模型)。
依赖:alembic.op、SQLAlchemy;down_revision 为实施时单 head s18d1e2f3a4b5。
逻辑:autogenerate 噪声过大(无关 drop / 历史索引改名),故手写本 revision。
升级先 drop 投递表及其历史索引,再按 ORM 同构建
configs → unit_runs → files;降级先拆出站三表,再按 s17b 后的现行结构
还原投递表(不恢复投递数据)。
"""
from __future__ import annotations
import sqlalchemy as sa
from alembic import op
revision = "s19a0b1c2d3e4"
down_revision = "s18d1e2f3a4b5"
branch_labels = None
depends_on = None
_DELIVERIES_TABLE = "freight_rpa_inbound_notification_deliveries"
_DELIVERIES_INDEXES = (
"ix_fr_inbound_notif_deliveries_recipient_user_id",
"ix_shipping_receipt_delivery_file_recipient",
"ix_shipping_receipt_delivery_unit_created",
)
def upgrade() -> None:
"""Drop inbound notification deliveries, then create outbound config / run / file tables."""
# [STEP 1] 入站投递账本:索引名含 s17b 未改的 shipping_receipt_delivery_* 与补建短名。
for index_name in _DELIVERIES_INDEXES:
op.drop_index(index_name, table_name=_DELIVERIES_TABLE)
op.drop_table(_DELIVERIES_TABLE)
# [STEP 2] 出站配置:站点一对一,字段与入站配置同构,无独立启停列。
op.create_table(
"freight_rpa_outbound_report_configs",
sa.Column("id", sa.Integer(), nullable=False),
sa.Column("source_id", sa.Integer(), nullable=False),
sa.Column("parsing_profile_id", sa.Integer(), nullable=True),
sa.Column("report_path_json", sa.JSON(), server_default=sa.text("'{}'"), nullable=False),
sa.Column("query_parameters_json", sa.JSON(), server_default=sa.text("'[]'"), nullable=False),
sa.Column("config_version", sa.Integer(), server_default=sa.text("1"), nullable=False),
sa.Column("created_by", sa.Integer(), nullable=True),
sa.Column("updated_by", sa.Integer(), nullable=True),
sa.Column("created_at", sa.DateTime(), nullable=False),
sa.Column("updated_at", sa.DateTime(), nullable=False),
sa.ForeignKeyConstraint(["created_by"], ["users.id"]),
sa.ForeignKeyConstraint(["parsing_profile_id"], ["freight_rpa_parsing_profiles.id"]),
sa.ForeignKeyConstraint(["source_id"], ["freight_rpa_sources.id"]),
sa.ForeignKeyConstraint(["updated_by"], ["users.id"]),
sa.PrimaryKeyConstraint("id"),
sa.UniqueConstraint("source_id", name="uq_freight_rpa_outbound_report_configs_source"),
)
op.create_index(
"ix_freight_rpa_outbound_report_configs_parsing_profile_id",
"freight_rpa_outbound_report_configs",
["parsing_profile_id"],
unique=False,
)
op.create_index(
"ix_freight_rpa_outbound_report_configs_source_id",
"freight_rpa_outbound_report_configs",
["source_id"],
unique=False,
)
# [STEP 3] 出站执行单元:回调幂等;schedule_run_id 可空以支持按需触发。
op.create_table(
"freight_rpa_outbound_rpa_unit_runs",
sa.Column("id", sa.Integer(), nullable=False),
sa.Column("schedule_run_id", sa.Integer(), nullable=True),
sa.Column("source_id", sa.Integer(), nullable=False),
sa.Column("retry_of_id", sa.Integer(), nullable=True),
sa.Column("source_code", sa.String(length=64), nullable=False),
sa.Column("connector_type", sa.String(length=32), nullable=False),
sa.Column("warehouse_id", sa.String(length=64), nullable=True),
sa.Column("callback_id", sa.String(length=128), nullable=False),
sa.Column("status", sa.String(length=32), server_default="queued", nullable=False),
sa.Column("config_snapshot_json", sa.JSON(), nullable=False),
sa.Column("trigger_snapshot_json", sa.JSON(), nullable=False),
sa.Column("result_json", sa.JSON(), nullable=False),
sa.Column("error_code", sa.String(length=64), nullable=True),
sa.Column("error_message", sa.Text(), nullable=True),
sa.Column("started_at", sa.DateTime(), nullable=True),
sa.Column("finished_at", sa.DateTime(), nullable=True),
sa.Column("created_at", sa.DateTime(), nullable=False),
sa.CheckConstraint(
"status IN ('queued', 'awaiting_rpa', 'processing', 'success', 'empty', 'failed', 'cancelled')",
name="ck_freight_rpa_outbound_rpa_unit_runs_status",
),
sa.ForeignKeyConstraint(["retry_of_id"], ["freight_rpa_outbound_rpa_unit_runs.id"]),
sa.ForeignKeyConstraint(["schedule_run_id"], ["schedule_runs.id"]),
sa.ForeignKeyConstraint(["source_id"], ["freight_rpa_sources.id"]),
sa.PrimaryKeyConstraint("id"),
sa.UniqueConstraint("callback_id", name="uq_freight_rpa_outbound_rpa_unit_runs_callback"),
)
op.create_index(
"ix_freight_rpa_outbound_rpa_unit_runs_retry_of_id",
"freight_rpa_outbound_rpa_unit_runs",
["retry_of_id"],
unique=False,
)
op.create_index(
"ix_freight_rpa_outbound_rpa_unit_runs_schedule_run_id",
"freight_rpa_outbound_rpa_unit_runs",
["schedule_run_id"],
unique=False,
)
op.create_index(
"ix_freight_rpa_outbound_rpa_unit_runs_source_id",
"freight_rpa_outbound_rpa_unit_runs",
["source_id"],
unique=False,
)
op.create_index(
"ix_freight_rpa_outbound_unit_run_schedule_status",
"freight_rpa_outbound_rpa_unit_runs",
["schedule_run_id", "status"],
unique=False,
)
op.create_index(
"ix_freight_rpa_outbound_unit_run_source_created",
"freight_rpa_outbound_rpa_unit_runs",
["source_id", "created_at"],
unique=False,
)
# [STEP 4] 出站归档文件:含解析失败分层列,不建 notification_deliveries。
op.create_table(
"freight_rpa_outbound_report_files",
sa.Column("id", sa.Integer(), nullable=False),
sa.Column("unit_run_id", sa.Integer(), nullable=False),
sa.Column("storage_provider", sa.String(length=32), nullable=False),
sa.Column("storage_bucket", sa.String(length=128), nullable=False),
sa.Column("storage_key", sa.String(length=1024), nullable=False),
sa.Column("original_filename", sa.String(length=255), nullable=False),
sa.Column("content_type", sa.String(length=128), nullable=True),
sa.Column("file_size", sa.Integer(), nullable=False),
sa.Column("sha256", sa.String(length=64), nullable=False),
sa.Column("parse_failed_at", sa.DateTime(), nullable=True),
sa.Column("parse_error_code", sa.String(length=64), nullable=True),
sa.Column("parse_error_message", sa.Text(), nullable=True),
sa.Column("archive_expires_at", sa.DateTime(), nullable=True),
sa.Column("archive_deleted_at", sa.DateTime(), nullable=True),
sa.Column("archive_delete_result_json", sa.JSON(), nullable=False),
sa.Column("created_at", sa.DateTime(), nullable=False),
sa.CheckConstraint("file_size >= 0", name="ck_freight_rpa_outbound_report_files_size"),
sa.ForeignKeyConstraint(["unit_run_id"], ["freight_rpa_outbound_rpa_unit_runs.id"]),
sa.PrimaryKeyConstraint("id"),
sa.UniqueConstraint("unit_run_id", "sha256", name="uq_freight_rpa_outbound_report_files_unit_sha256"),
)
op.create_index(
"ix_freight_rpa_outbound_report_files_expires",
"freight_rpa_outbound_report_files",
["archive_expires_at", "archive_deleted_at"],
unique=False,
)
op.create_index(
"ix_freight_rpa_outbound_report_files_unit",
"freight_rpa_outbound_report_files",
["unit_run_id"],
unique=False,
)
op.create_index(
"ix_freight_rpa_outbound_report_files_unit_run_id",
"freight_rpa_outbound_report_files",
["unit_run_id"],
unique=False,
)
def downgrade() -> None:
"""Drop outbound tables, then restore the inbound deliveries table structure (empty)."""
op.drop_index(
"ix_freight_rpa_outbound_report_files_unit_run_id",
table_name="freight_rpa_outbound_report_files",
)
op.drop_index(
"ix_freight_rpa_outbound_report_files_unit",
table_name="freight_rpa_outbound_report_files",
)
op.drop_index(
"ix_freight_rpa_outbound_report_files_expires",
table_name="freight_rpa_outbound_report_files",
)
op.drop_table("freight_rpa_outbound_report_files")
op.drop_index(
"ix_freight_rpa_outbound_unit_run_source_created",
table_name="freight_rpa_outbound_rpa_unit_runs",
)
op.drop_index(
"ix_freight_rpa_outbound_unit_run_schedule_status",
table_name="freight_rpa_outbound_rpa_unit_runs",
)
op.drop_index(
"ix_freight_rpa_outbound_rpa_unit_runs_source_id",
table_name="freight_rpa_outbound_rpa_unit_runs",
)
op.drop_index(
"ix_freight_rpa_outbound_rpa_unit_runs_schedule_run_id",
table_name="freight_rpa_outbound_rpa_unit_runs",
)
op.drop_index(
"ix_freight_rpa_outbound_rpa_unit_runs_retry_of_id",
table_name="freight_rpa_outbound_rpa_unit_runs",
)
op.drop_table("freight_rpa_outbound_rpa_unit_runs")
op.drop_index(
"ix_freight_rpa_outbound_report_configs_source_id",
table_name="freight_rpa_outbound_report_configs",
)
op.drop_index(
"ix_freight_rpa_outbound_report_configs_parsing_profile_id",
table_name="freight_rpa_outbound_report_configs",
)
op.drop_table("freight_rpa_outbound_report_configs")
# 还原投递表结构供降级路径;不回填 Task 2 之后的投递行。
op.create_table(
_DELIVERIES_TABLE,
sa.Column("id", sa.Integer(), nullable=False),
sa.Column("unit_run_id", sa.Integer(), nullable=True),
sa.Column("report_file_id", sa.Integer(), nullable=True),
sa.Column("recipient_user_id", sa.Integer(), nullable=False),
sa.Column("message_type", sa.String(length=16), nullable=False),
sa.Column("idempotency_key", sa.String(length=255), nullable=False),
sa.Column("status", sa.String(length=16), server_default=sa.text("'pending'"), nullable=False),
sa.Column("response_summary_json", sa.JSON(), server_default=sa.text("'{}'"), nullable=False),
sa.Column("error_code", sa.String(length=64), nullable=True),
sa.Column("error_message", sa.Text(), nullable=True),
sa.Column("sent_at", sa.DateTime(), nullable=True),
sa.Column("created_at", sa.DateTime(), nullable=False),
sa.Column("updated_at", sa.DateTime(), nullable=False),
sa.CheckConstraint(
"message_type IN ('file', 'summary')",
name="ck_freight_rpa_inbound_notification_deliveries_type",
),
sa.CheckConstraint(
"status IN ('pending', 'sending', 'success', 'failed')",
name="ck_freight_rpa_inbound_notification_deliveries_status",
),
sa.ForeignKeyConstraint(["unit_run_id"], ["freight_rpa_inbound_rpa_unit_runs.id"]),
sa.ForeignKeyConstraint(["report_file_id"], ["freight_rpa_inbound_report_files.id"]),
sa.ForeignKeyConstraint(["recipient_user_id"], ["users.id"]),
sa.PrimaryKeyConstraint("id"),
sa.UniqueConstraint(
"idempotency_key",
name="uq_freight_rpa_inbound_notification_deliveries_idempotency",
),
)
op.create_index(
"ix_fr_inbound_notif_deliveries_recipient_user_id",
_DELIVERIES_TABLE,
["recipient_user_id"],
unique=False,
)
op.create_index(
"ix_shipping_receipt_delivery_file_recipient",
_DELIVERIES_TABLE,
["report_file_id", "recipient_user_id"],
unique=False,
)
op.create_index(
"ix_shipping_receipt_delivery_unit_created",
_DELIVERIES_TABLE,
["unit_run_id", "created_at"],
unique=False,
)