- 调度整合:退役 in_transit_receipt_file_job,收料归档收敛 shipping_receipt_rpa_archive,排程注册表统一 - 自动调拨 Job:调拨前确保各来源当日收料文件就绪(缺失自动触发收料 RPA),严格等量匹配后生成 ERP 调拨单 - worker 手动唤醒:排程工作台支持手动唤醒与 runs 透视,heartbeat 协议同步 - 收料缺失幸存 T2:parse_failed 状态落库(迁移 q4c5) - 来源错误分层与 stale 时钟换源:改挂 inventory_checked_at 库存查得时刻,展示阈值与执行门禁解耦(迁移 t5a6) - 新增 wecom_sender 企微通知:每非空 Run 汇总一条,通知失败不影响 ERP 结果 - 同步排程工作台/调拨卡片前端、chat/admin 文案四语言、RPA 脚本与测试全家
169 lines
10 KiB
Python
169 lines
10 KiB
Python
"""
|
|
收料报表 RPA 的配置、运行、归档与通知投递持久化模型。
|
|
|
|
用途:为独立收料排程保存站点级配置、每站点/仓库执行单元、原样归档文件及企微投递事实。
|
|
依赖:库存站点、排程运行、SQLAlchemy Base;完整操作追溯统一写入 ShippingAuditEvent。
|
|
逻辑:模型只复用库存站点的连接资料,通过独立外键与唯一约束隔离库存快照、匹配和旧 RPA Worker 链路。
|
|
"""
|
|
|
|
from __future__ import annotations
|
|
|
|
from datetime import datetime
|
|
from typing import Any, Optional
|
|
|
|
from sqlalchemy import CheckConstraint, DateTime, ForeignKey, Index, Integer, JSON, String, Text, UniqueConstraint, text
|
|
from sqlalchemy.orm import Mapped, mapped_column, relationship
|
|
|
|
from app.models.base import Base
|
|
|
|
|
|
class ShippingReceiptReportConfig(Base):
|
|
"""库存站点的一对一收料报表配置;路径和条件始终独立于库存配置。"""
|
|
|
|
__tablename__ = 'shipping_receipt_report_configs'
|
|
__table_args__ = (
|
|
UniqueConstraint('inventory_source_id', name='uq_shipping_receipt_report_configs_source'),
|
|
)
|
|
|
|
id: Mapped[int] = mapped_column(primary_key=True)
|
|
inventory_source_id: Mapped[int] = mapped_column(
|
|
ForeignKey('shipping_inventory_sources.id'), nullable=False, index=True,
|
|
)
|
|
# 收料不设独立启停:是否运行统一由库存站点 enabled 与本配置是否存在决定。
|
|
# 收料文件解析规则(purpose='movement');定死领域字段端点。空则无法解析收料文件。
|
|
parsing_profile_id: Mapped[Optional[int]] = mapped_column(
|
|
ForeignKey('shipping_inventory_parsing_profiles.id'), nullable=True, index=True,
|
|
)
|
|
report_path_json: Mapped[dict[str, Any]] = mapped_column(
|
|
JSON, default=dict, server_default=text("'{}'"), nullable=False,
|
|
)
|
|
query_parameters_json: Mapped[list[dict[str, Any]]] = mapped_column(
|
|
JSON, default=list, server_default=text("'[]'"), nullable=False,
|
|
)
|
|
# 与库存同构:查询条件原样下发 Worker;空条件表示页面默认。排程一源一单元,不按仓拆分。
|
|
config_version: Mapped[int] = mapped_column(Integer, default=1, server_default=text('1'), nullable=False)
|
|
created_by: Mapped[Optional[int]] = mapped_column(ForeignKey('users.id'), nullable=True)
|
|
updated_by: Mapped[Optional[int]] = mapped_column(ForeignKey('users.id'), 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)
|
|
|
|
inventory_source: Mapped['ShippingInventorySource'] = relationship(back_populates='receipt_report_config')
|
|
parsing_profile: Mapped[Optional['ShippingInventoryParsingProfile']] = relationship()
|
|
|
|
|
|
class ShippingReceiptRpaUnitRun(Base):
|
|
"""一次收料执行的站点或站点-Warehouse 不可重复执行单元。
|
|
|
|
schedule_run_id 指向排程 run;调拨按需触发(重试/预检补抓)无排程,保持 NULL。
|
|
"""
|
|
|
|
__tablename__ = 'shipping_receipt_rpa_unit_runs'
|
|
__table_args__ = (
|
|
UniqueConstraint('callback_id', name='uq_shipping_receipt_rpa_unit_runs_callback'),
|
|
CheckConstraint(
|
|
"status IN ('queued', 'awaiting_rpa', 'processing', 'success', 'empty', 'failed', 'cancelled')",
|
|
name='ck_shipping_receipt_rpa_unit_runs_status',
|
|
),
|
|
Index('ix_shipping_receipt_unit_run_schedule_status', 'schedule_run_id', 'status'),
|
|
Index('ix_shipping_receipt_unit_run_source_created', 'inventory_source_id', 'created_at'),
|
|
)
|
|
|
|
id: Mapped[int] = mapped_column(primary_key=True)
|
|
schedule_run_id: Mapped[Optional[int]] = mapped_column(ForeignKey('schedule_runs.id'), nullable=True, index=True)
|
|
inventory_source_id: Mapped[int] = mapped_column(ForeignKey('shipping_inventory_sources.id'), nullable=False, index=True)
|
|
retry_of_id: Mapped[Optional[int]] = mapped_column(
|
|
ForeignKey('shipping_receipt_rpa_unit_runs.id'), nullable=True, index=True,
|
|
)
|
|
source_code: Mapped[str] = mapped_column(String(64), nullable=False)
|
|
connector_type: Mapped[str] = mapped_column(String(32), nullable=False)
|
|
warehouse_id: Mapped[Optional[str]] = mapped_column(String(64), nullable=True)
|
|
callback_id: Mapped[str] = mapped_column(String(128), nullable=False)
|
|
status: Mapped[str] = mapped_column(String(32), nullable=False, default='queued', server_default='queued')
|
|
config_snapshot_json: Mapped[dict[str, Any]] = mapped_column(JSON, nullable=False, default=dict)
|
|
trigger_snapshot_json: Mapped[dict[str, Any]] = mapped_column(JSON, nullable=False, default=dict)
|
|
result_json: Mapped[dict[str, Any]] = mapped_column(JSON, nullable=False, default=dict)
|
|
error_code: Mapped[Optional[str]] = mapped_column(String(64), nullable=True)
|
|
error_message: Mapped[Optional[str]] = mapped_column(Text, nullable=True)
|
|
started_at: Mapped[Optional[datetime]] = mapped_column(DateTime, nullable=True)
|
|
finished_at: Mapped[Optional[datetime]] = mapped_column(DateTime, nullable=True)
|
|
created_at: Mapped[datetime] = mapped_column(DateTime, default=datetime.utcnow, nullable=False)
|
|
|
|
schedule_run: Mapped[Optional['ScheduleRun']] = relationship(foreign_keys=[schedule_run_id])
|
|
inventory_source: Mapped['ShippingInventorySource'] = relationship()
|
|
retry_of: Mapped[Optional['ShippingReceiptRpaUnitRun']] = relationship(remote_side=[id])
|
|
files: Mapped[list['ShippingReceiptReportFile']] = relationship(
|
|
back_populates='unit_run', cascade='all, delete-orphan',
|
|
)
|
|
|
|
|
|
class ShippingReceiptReportFile(Base):
|
|
"""收料原样归档文件;保留文件身份和对象清理事实,不关联库存快照。"""
|
|
|
|
__tablename__ = 'shipping_receipt_report_files'
|
|
__table_args__ = (
|
|
UniqueConstraint('unit_run_id', 'sha256', name='uq_shipping_receipt_report_files_unit_sha256'),
|
|
CheckConstraint("file_size >= 0", name='ck_shipping_receipt_report_files_size'),
|
|
Index('ix_shipping_receipt_report_files_unit', 'unit_run_id'),
|
|
Index('ix_shipping_receipt_report_files_expires', 'archive_expires_at', 'archive_deleted_at'),
|
|
)
|
|
|
|
id: Mapped[int] = mapped_column(primary_key=True)
|
|
unit_run_id: Mapped[int] = mapped_column(ForeignKey('shipping_receipt_rpa_unit_runs.id'), nullable=False, index=True)
|
|
storage_provider: Mapped[str] = mapped_column(String(32), nullable=False)
|
|
storage_bucket: Mapped[str] = mapped_column(String(128), nullable=False)
|
|
storage_key: Mapped[str] = mapped_column(String(1024), nullable=False)
|
|
original_filename: Mapped[str] = mapped_column(String(255), nullable=False)
|
|
content_type: Mapped[Optional[str]] = mapped_column(String(128), nullable=True)
|
|
file_size: Mapped[int] = mapped_column(Integer, nullable=False)
|
|
sha256: Mapped[str] = mapped_column(String(64), nullable=False)
|
|
# 解析失败分层标记:归档文件解析失败时写入;三者全空=未失败/已清除。
|
|
parse_failed_at: Mapped[Optional[datetime]] = mapped_column(DateTime, nullable=True)
|
|
parse_error_code: Mapped[Optional[str]] = mapped_column(String(64), nullable=True)
|
|
parse_error_message: Mapped[Optional[str]] = mapped_column(Text, nullable=True)
|
|
archive_expires_at: Mapped[Optional[datetime]] = mapped_column(DateTime, nullable=True)
|
|
archive_deleted_at: Mapped[Optional[datetime]] = mapped_column(DateTime, nullable=True)
|
|
archive_delete_result_json: Mapped[dict[str, Any]] = mapped_column(JSON, nullable=False, default=dict)
|
|
created_at: Mapped[datetime] = mapped_column(DateTime, default=datetime.utcnow, nullable=False)
|
|
|
|
unit_run: Mapped[ShippingReceiptRpaUnitRun] = relationship(back_populates='files')
|
|
deliveries: Mapped[list['ShippingReceiptNotificationDelivery']] = relationship(
|
|
back_populates='report_file', cascade='all, delete-orphan',
|
|
)
|
|
|
|
|
|
class ShippingReceiptNotificationDelivery(Base):
|
|
"""收料文件或汇总企微消息的幂等投递事实,响应仅保存脱敏摘要。"""
|
|
|
|
__tablename__ = 'shipping_receipt_notification_deliveries'
|
|
__table_args__ = (
|
|
UniqueConstraint('idempotency_key', name='uq_shipping_receipt_notification_deliveries_idempotency'),
|
|
CheckConstraint("message_type IN ('file', 'summary')", name='ck_shipping_receipt_notification_deliveries_type'),
|
|
CheckConstraint(
|
|
"status IN ('pending', 'sending', 'success', 'failed')",
|
|
name='ck_shipping_receipt_notification_deliveries_status',
|
|
),
|
|
Index('ix_shipping_receipt_delivery_file_recipient', 'report_file_id', 'recipient_user_id'),
|
|
Index('ix_shipping_receipt_delivery_unit_created', 'unit_run_id', 'created_at'),
|
|
)
|
|
|
|
id: Mapped[int] = mapped_column(primary_key=True)
|
|
unit_run_id: Mapped[Optional[int]] = mapped_column(
|
|
ForeignKey('shipping_receipt_rpa_unit_runs.id'), nullable=True, index=True,
|
|
)
|
|
report_file_id: Mapped[Optional[int]] = mapped_column(
|
|
ForeignKey('shipping_receipt_report_files.id'), nullable=True, index=True,
|
|
)
|
|
recipient_user_id: Mapped[int] = mapped_column(ForeignKey('users.id'), nullable=False, index=True)
|
|
message_type: Mapped[str] = mapped_column(String(16), nullable=False)
|
|
idempotency_key: Mapped[str] = mapped_column(String(255), nullable=False)
|
|
status: Mapped[str] = mapped_column(String(16), nullable=False, default='pending', server_default='pending')
|
|
response_summary_json: Mapped[dict[str, Any]] = mapped_column(JSON, nullable=False, default=dict)
|
|
error_code: Mapped[Optional[str]] = mapped_column(String(64), nullable=True)
|
|
error_message: Mapped[Optional[str]] = mapped_column(Text, nullable=True)
|
|
sent_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)
|
|
|
|
unit_run: Mapped[Optional[ShippingReceiptRpaUnitRun]] = relationship(foreign_keys=[unit_run_id])
|
|
report_file: Mapped[Optional[ShippingReceiptReportFile]] = relationship(back_populates='deliveries')
|