50 lines
2.5 KiB
Python
50 lines
2.5 KiB
Python
"""
|
|
backend/app/models/file_asset.py
|
|
|
|
用途:平台级通用文件资产模型,承载私有 MinIO 存储元数据与生命周期。
|
|
依赖:models.base.Base。
|
|
逻辑:由 file_assets.service 创建与更新;ShippingNoticeIntake 等业务通过 asset_id 引用。
|
|
"""
|
|
|
|
from __future__ import annotations
|
|
|
|
from datetime import datetime
|
|
from typing import Optional
|
|
|
|
from sqlalchemy import DateTime, ForeignKey, Index, Integer, String, UniqueConstraint
|
|
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'),
|
|
)
|
|
|
|
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)
|
|
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)
|