""" models/shipping_excel_template.py 用途:定义版本化发货 Excel 模板及不可变 Notice 导出产物。 依赖:models.base.Base、FreightRpaSource、订单 Schema、ShippingNotice、freight_rpa_parsing_profiles 与 freight_rpa_onhand_report_files。 逻辑:库存源只绑定已发布模板版本;每次生成均新增 Artifact 并锁定输入版本与校验和; co8e9f0a1b2c 已删除 export artifact 的 batch_id 列;Artifact 直接属于一张 Notice。 """ from __future__ import annotations from datetime import datetime from typing import Any, Optional from sqlalchemy import DateTime, ForeignKey, Index, Integer, JSON, String, UniqueConstraint, text from sqlalchemy.orm import Mapped, mapped_column, relationship from app.models.base import Base class ShippingExcelTemplate(Base): """可跨库存源复用的发货 Excel 模板版本。""" __tablename__ = 'shipping_excel_templates' __table_args__ = ( UniqueConstraint('template_code', 'version', name='uq_shipping_excel_templates_code_version'), Index('ix_shipping_excel_templates_status', 'status'), ) id: Mapped[int] = mapped_column(primary_key=True) template_code: Mapped[str] = mapped_column(String(64), nullable=False) display_name: Mapped[str] = mapped_column(String(128), nullable=False) version: Mapped[int] = mapped_column(Integer, nullable=False, default=1, server_default=text('1')) status: Mapped[str] = mapped_column(String(16), nullable=False, default='draft', server_default='draft') storage_provider: Mapped[str] = mapped_column(String(32), nullable=False, default='minio', server_default='minio') storage_bucket: Mapped[str] = mapped_column(String(128), nullable=False) storage_key: Mapped[str] = mapped_column(String(512), nullable=False) original_filename: Mapped[str] = mapped_column(String(255), nullable=False) file_checksum: Mapped[str] = mapped_column(String(64), nullable=False) definition_json: Mapped[dict[str, Any]] = mapped_column(JSON, nullable=False, default=dict) definition_checksum: Mapped[Optional[str]] = mapped_column(String(64), nullable=True) created_by: Mapped[Optional[int]] = mapped_column(ForeignKey('users.id'), nullable=True) published_by: Mapped[Optional[int]] = mapped_column(ForeignKey('users.id'), nullable=True) created_at: Mapped[datetime] = mapped_column(DateTime, default=datetime.utcnow, nullable=False) published_at: Mapped[Optional[datetime]] = mapped_column(DateTime, nullable=True) sources: Mapped[list['FreightRpaSource']] = relationship(back_populates='excel_template') class ShippingExportArtifact(Base): """一次 Notice 导出的不可变产物及生成输入快照。""" __tablename__ = 'shipping_notice_export_artifacts' __table_args__ = ( UniqueConstraint('artifact_no', name='uq_shipping_notice_export_artifacts_no'), Index('ix_shipping_notice_export_artifacts_task_created', 'task_id', 'created_at'), ) id: Mapped[int] = mapped_column(primary_key=True) artifact_no: Mapped[str] = mapped_column(String(64), nullable=False) task_id: Mapped[int] = mapped_column(ForeignKey('shipping_notices.id'), nullable=False, index=True) template_id: Mapped[int] = mapped_column(ForeignKey('shipping_excel_templates.id'), nullable=False) order_schema_version_id: Mapped[int] = mapped_column(ForeignKey('shipping_order_schema_versions.id'), nullable=False) order_extraction_id: Mapped[int] = mapped_column(ForeignKey('shipping_order_extractions.id'), nullable=False) inventory_parsing_profile_id: Mapped[int] = mapped_column(ForeignKey('freight_rpa_parsing_profiles.id'), nullable=False) inventory_snapshot_id: Mapped[Optional[int]] = mapped_column(ForeignKey('freight_rpa_onhand_report_files.id'), nullable=True) input_checksum: Mapped[str] = mapped_column(String(64), nullable=False) file_path: Mapped[str] = mapped_column(String(500), nullable=False) file_name: Mapped[str] = mapped_column(String(255), nullable=False) file_checksum: Mapped[str] = mapped_column(String(64), nullable=False) file_size: Mapped[int] = mapped_column(Integer, nullable=False) status: Mapped[str] = mapped_column(String(16), nullable=False, default='ready', server_default='ready') created_at: Mapped[datetime] = mapped_column(DateTime, default=datetime.utcnow, nullable=False) template: Mapped[ShippingExcelTemplate] = relationship() task: Mapped['ShippingNotice'] = relationship()