"""种子批(库存) 数据模型""" from datetime import date from sqlalchemy import Date, ForeignKey, Index, Integer, Numeric, String, Text from sqlalchemy.orm import Mapped, mapped_column from app.core.base_model import MappedBase, ModelMixin, UserMixin class SeedLotModel(ModelMixin, UserMixin, MappedBase): """种子批·库存(规格 §3.13;used_count 派生 remaining,选择强度链源头)。""" __tablename__ = "bre_seed_lot" combination_id: Mapped[int] = mapped_column( Integer, ForeignKey("bre_cross_combination.id", ondelete="CASCADE"), index=True, nullable=False, comment="杂交组合" ) pollination_id: Mapped[int | None] = mapped_column( Integer, ForeignKey("bre_pollination.id", ondelete="SET NULL"), index=True, nullable=True, comment="授粉记录(关联补链)", default=None ) lot_code: Mapped[str] = mapped_column(String(64), nullable=False, index=True, comment="种子批号") harvest_year: Mapped[int | None] = mapped_column(Integer, nullable=True, comment="收获年份", default=None) seed_count: Mapped[int | None] = mapped_column(Integer, nullable=True, comment="收获粒数(选择强度源头)", default=None) used_count: Mapped[int] = mapped_column(Integer, nullable=False, default=0, comment="已取用粒数(seedling 按出苗数累加)") germination_rate: Mapped[float | None] = mapped_column(Numeric(5, 2), nullable=True, comment="发芽率%(活力)", default=None) storage_type: Mapped[str | None] = mapped_column(String(16), nullable=True, comment="存储类型(种子库/离体/DNA)", default=None) storage_location: Mapped[str | None] = mapped_column(String(128), nullable=True, comment="存放位置", default=None) test_date: Mapped[date | None] = mapped_column(Date, nullable=True, comment="活力检测日期", default=None) remark: Mapped[str | None] = mapped_column(Text, nullable=True, comment="备注", default=None) # 无 status 列:覆盖基类默认 ix_<表>_status_deleted 索引, # 仅保留 (created_time, is_deleted) 复合索引用于数据权限过滤。 __table_args__ = ( Index("ix_bre_seed_lot_created_deleted", "created_time", "is_deleted"), Index("uq_bre_seed_lot_code", "lot_code", unique=True), )