""" backend/app/models/file_asset.py 用途:平台级通用文件资产模型,承载私有 MinIO 存储元数据与生命周期。 依赖:models.base.Base。 逻辑:由 file_assets.service 创建与更新;ShippingOrderIntake 经 asset_id 引用上传根资产; EML 解析后的订单附件以 parent_asset_id 挂为 derived 子资产(阶段二); 派生资产对 (parent_asset_id, content_hash) 使用未删除行的部分唯一索引。 """ from __future__ import annotations from datetime import datetime from typing import Optional from sqlalchemy import DateTime, ForeignKey, Index, Integer, String, UniqueConstraint, text from sqlalchemy.orm import Mapped, mapped_column from app.models.base import Base class FileAsset(Base): """通用文件资产(私有对象存储 + 审计元数据)。""" __tablename__ = 'file_assets' __table_args__ = ( UniqueConstraint('asset_no', name='uq_file_assets_asset_no'), Index('ix_file_assets_owner_content_hash', 'owner_user_id', 'content_hash'), Index('ix_file_assets_lifecycle_expires', 'lifecycle_status', 'expires_at'), Index('ix_file_assets_owner_created', 'owner_user_id', 'created_at'), # 仅约束未删除派生资产,避免 soft-delete 永久阻塞同哈希重资产化 Index( 'uq_file_assets_parent_content_hash_alive', 'parent_asset_id', 'content_hash', unique=True, postgresql_where=text('deleted_at IS NULL AND parent_asset_id IS NOT NULL'), sqlite_where=text('deleted_at IS NULL AND parent_asset_id IS NOT NULL'), ), Index('ix_file_assets_parent_attachment_key', 'parent_asset_id', 'source_attachment_key'), ) id: Mapped[int] = mapped_column(primary_key=True) asset_no: Mapped[str] = mapped_column(String(64), nullable=False) owner_user_id: Mapped[int] = mapped_column(ForeignKey('users.id'), index=True, nullable=False) tenant_id: Mapped[Optional[str]] = mapped_column(String(64), nullable=True) # 派生资产:EML 根资产 → 附件 PDF 等子资产 parent_asset_id: Mapped[Optional[int]] = mapped_column( ForeignKey('file_assets.id'), index=True, nullable=True, ) asset_role: Mapped[str] = mapped_column(String(32), default='upload', nullable=False) # 派生资产首次落库时的 attachment_key;后续复用同哈希时不覆盖(当前来源见 Document.attachment_key) source_attachment_key: Mapped[Optional[str]] = mapped_column(String(64), nullable=True) original_filename: Mapped[str] = mapped_column(String(255), nullable=False) storage_provider: Mapped[str] = mapped_column(String(32), default='minio', nullable=False) storage_bucket: Mapped[str] = mapped_column(String(128), nullable=False) storage_key: Mapped[str] = mapped_column(String(512), nullable=False) content_hash: Mapped[str] = mapped_column(String(64), nullable=False) file_size: Mapped[int] = mapped_column(Integer, nullable=False) mime_type: Mapped[Optional[str]] = mapped_column(String(128), nullable=True) extension: Mapped[Optional[str]] = mapped_column(String(32), nullable=True) upload_status: Mapped[str] = mapped_column(String(32), default='uploading', nullable=False) lifecycle_status: Mapped[str] = mapped_column(String(32), default='temporary', nullable=False) reference_count: Mapped[int] = mapped_column(Integer, default=0, nullable=False) expires_at: Mapped[Optional[datetime]] = mapped_column(DateTime, nullable=True) deleted_at: Mapped[Optional[datetime]] = mapped_column(DateTime, nullable=True) created_at: Mapped[datetime] = mapped_column(default=datetime.utcnow, index=True) updated_at: Mapped[datetime] = mapped_column(default=datetime.utcnow, onupdate=datetime.utcnow)