init: 初始化 dpb 桃育种系统代码库

前后端 + 后端 FastAPI 全量源码、部署脚本与文档。
This commit is contained in:
34047007@qq.com
2026-08-06 00:17:49 +08:00
commit b95053c52c
1469 changed files with 322298 additions and 0 deletions
@@ -0,0 +1,135 @@
import urllib.parse
from typing import Annotated
from fastapi import APIRouter, Body, Depends, File, Path, Query, UploadFile
from fastapi.responses import JSONResponse, StreamingResponse
from sqlalchemy.ext.asyncio import AsyncSession
from app.common.response import ResponseSchema, StreamResponse, SuccessResponse
from app.core.base_schema import AuthSchema, PageResultSchema, PaginationQueryParam, ImportResultSchema
from app.core.dependencies import AuthPermission, db_getter
from app.core.router_class import OperationLogRoute
from app.utils.common_util import bytes2file_response
from .schema import (
TreeCreateSchema,
TreeOutSchema,
TreeQueryParam,
TreeUpdateSchema,
)
from .service import TreeService
TreeRouter = APIRouter(route_class=OperationLogRoute, prefix="/tree", tags=["育种单株"])
@TreeRouter.get("/detail/{id}", summary="获取育种单株详情", response_model=ResponseSchema[TreeOutSchema])
async def get_tree__detail_controller(
auth: Annotated[AuthSchema, Depends(AuthPermission(["module_bre:tree:detail"]))],
id: Annotated[int, Path(description="育种单株ID")],
db: Annotated[AsyncSession, Depends(db_getter)],
) -> JSONResponse:
service = TreeService(auth, db)
result_dict = await service.detail(id=id)
return SuccessResponse(data=result_dict, msg="获取育种单株详情成功")
@TreeRouter.get("/list", summary="分页查询育种单株", response_model=ResponseSchema[PageResultSchema[TreeOutSchema]])
async def get_tree__list_controller(
auth: Annotated[AuthSchema, Depends(AuthPermission(["module_bre:tree:query"]))],
page: Annotated[PaginationQueryParam, Depends()],
search: Annotated[TreeQueryParam, Query()],
db: Annotated[AsyncSession, Depends(db_getter)],
) -> JSONResponse:
service = TreeService(auth, db)
result_dict = await service.page(
page_no=page.page_no,
page_size=page.page_size,
search=search,
order_by=page.order_by,
)
return SuccessResponse(data=result_dict, msg="查询育种单株列表成功")
@TreeRouter.get("/options", summary="育种单株下拉选项")
async def get_tree__options_controller(
auth: Annotated[AuthSchema, Depends(AuthPermission(["module_bre:tree:query"]))],
db: Annotated[AsyncSession, Depends(db_getter)],
) -> JSONResponse:
service = TreeService(auth, db)
options = await service.list_options()
return SuccessResponse(data=options, msg="获取育种单株选项成功")
@TreeRouter.post("/create", summary="创建育种单株", response_model=ResponseSchema[TreeOutSchema])
async def create_tree__controller(
auth: Annotated[AuthSchema, Depends(AuthPermission(["module_bre:tree:create"]))],
data: Annotated[TreeCreateSchema, Body(description="创建参数")],
db: Annotated[AsyncSession, Depends(db_getter)],
) -> JSONResponse:
service = TreeService(auth, db)
result_dict = await service.create(data=data)
return SuccessResponse(data=result_dict, msg="创建育种单株成功")
@TreeRouter.put("/update/{id}", summary="修改育种单株", response_model=ResponseSchema[TreeOutSchema])
async def update_tree__controller(
auth: Annotated[AuthSchema, Depends(AuthPermission(["module_bre:tree:update"]))],
id: Annotated[int, Path(description="育种单株ID")],
data: Annotated[TreeUpdateSchema, Body(description="修改参数")],
db: Annotated[AsyncSession, Depends(db_getter)],
) -> JSONResponse:
service = TreeService(auth, db)
result_dict = await service.update(id=id, data=data)
return SuccessResponse(data=result_dict, msg="修改育种单株成功")
@TreeRouter.delete("/delete", summary="删除育种单株", response_model=ResponseSchema[None])
async def delete_tree__controller(
auth: Annotated[AuthSchema, Depends(AuthPermission(["module_bre:tree:delete"]))],
ids: Annotated[list[int], Body(description="ID列表")],
db: Annotated[AsyncSession, Depends(db_getter)],
) -> JSONResponse:
service = TreeService(auth, db)
await service.delete(ids=ids)
return SuccessResponse(msg="删除育种单株成功")
@TreeRouter.post("/export", summary="导出育种单株")
async def export_tree__controller(
auth: Annotated[AuthSchema, Depends(AuthPermission(["module_bre:tree:export"]))],
search: Annotated[TreeQueryParam, Query()],
db: Annotated[AsyncSession, Depends(db_getter)],
) -> StreamingResponse:
service = TreeService(auth, db)
result_dict_list = await service.get_list(search=search)
export_result = TreeService.batch_export(obj_list=[item.model_dump() for item in result_dict_list])
return StreamResponse(
data=bytes2file_response(export_result),
media_type="application/vnd.openxmlformats-officedocument.spreadsheetml.sheet",
headers={"Content-Disposition": f"attachment; filename={urllib.parse.quote('育种单株管理.xlsx')}"},
)
@TreeRouter.post("/import", summary="导入育种单株", response_model=ResponseSchema[ImportResultSchema])
async def import_tree__controller(
file: Annotated[UploadFile, File(description="导入文件")],
auth: Annotated[AuthSchema, Depends(AuthPermission(["module_bre:tree:import"]))],
db: Annotated[AsyncSession, Depends(db_getter)],
) -> JSONResponse:
service = TreeService(auth, db)
batch_import_result = await service.batch_import(file=file, update_support=True)
return SuccessResponse(data=batch_import_result, msg="导入育种单株成功")
@TreeRouter.post("/download/template", summary="获取育种单株导入模板", dependencies=[Depends(AuthPermission(["module_bre:tree:download"]))])
async def download_tree__template_controller() -> StreamingResponse:
import_template_result = TreeService.import_template_download()
return StreamResponse(
data=bytes2file_response(import_template_result),
media_type="application/vnd.openxmlformats-officedocument.spreadsheetml.sheet",
headers={
"Content-Disposition": f"attachment; filename={urllib.parse.quote('育种单株管理导入模板.xlsx')}",
"Access-Control-Expose-Headers": "Content-Disposition",
},
)
@@ -0,0 +1,18 @@
from typing import Any
from sqlalchemy.ext.asyncio import AsyncSession
from app.core.base_crud import CRUDBase
from app.core.base_schema import AuthSchema
from .model import TreeModel
class BreedingTreeCRUD(CRUDBase[TreeModel, Any, Any]):
"""育种单株 CRUD —— 直接复用 CRUDBase(已自动注入数据权限过滤)。"""
def __init__(self, auth: AuthSchema, db: AsyncSession) -> None:
super().__init__(TreeModel, auth, db)
tree_crud = BreedingTreeCRUD
+120
View File
@@ -0,0 +1,120 @@
"""育种单株 数据模型"""
from datetime import datetime
from sqlalchemy import Float, ForeignKey, Index, Integer, String, Text, text
from sqlalchemy.orm import Mapped, mapped_column
from app.core.base_model import MappedBase, ModelMixin, UserMixin
class TreeModel(ModelMixin, UserMixin, MappedBase):
"""育种单株 主数据表。"""
__tablename__ = "bre_tree"
combination_id: Mapped[int] = mapped_column(
Integer, ForeignKey("bre_cross_combination.id", ondelete="CASCADE"),
index=True, nullable=False, comment="杂交组合"
)
dam_id: Mapped[int | None] = mapped_column(
Integer, ForeignKey("bre_germplasm.id", ondelete="SET NULL"),
index=True, nullable=True, comment="母本(种质)"
)
sire_id: Mapped[int | None] = mapped_column(
Integer, ForeignKey("bre_germplasm.id", ondelete="SET NULL"),
index=True, nullable=True, comment="父本(种质)"
)
trial_study_id: Mapped[int | None] = mapped_column(
Integer, ForeignKey("bre_trial_study.id", ondelete="SET NULL"),
index=True, nullable=True, comment="所属试验/站点(MET)"
)
block_no: Mapped[int | None] = mapped_column(Integer, nullable=True, comment="区组号")
rootstock_id: Mapped[int | None] = mapped_column(
Integer, ForeignKey("bre_rootstock.id", ondelete="SET NULL"),
index=True, nullable=True, comment="砧木"
)
clone_id: Mapped[int | None] = mapped_column(
Integer, ForeignKey("bre_clone.id", ondelete="SET NULL"),
index=True, nullable=True, comment="入选克隆"
)
plot_id: Mapped[int] = mapped_column(
Integer, ForeignKey("bre_plot.id", ondelete="CASCADE"),
index=True, nullable=True, comment="试验地块/位置"
)
planting_id: Mapped[int | None] = mapped_column(
Integer, ForeignKey("bre_planting.id", ondelete="SET NULL"),
index=True, nullable=True, comment="来源定植批次(一次定植包含多棵单株)"
)
tree_no: Mapped[str] = mapped_column(String(100), nullable=False, comment="单株编号")
row_orientation: Mapped[str | None] = mapped_column(String(50), nullable=True, comment="行向", default=None)
row_no: Mapped[int | None] = mapped_column(Integer, nullable=True, comment="行数", default=None)
col_no: Mapped[int | None] = mapped_column(Integer, nullable=True, comment="列数", default=None)
planted_date: Mapped[str | None] = mapped_column(String(20), nullable=True, comment="定植时间", default=None)
bre_personnel_id: Mapped[int] = mapped_column(
Integer, ForeignKey("bre_personnel.id", ondelete="CASCADE"),
index=True, nullable=True, comment="责任人"
)
status: Mapped[str | None] = mapped_column(String(50), nullable=True, comment="状态", default=None)
remark: Mapped[str | None] = mapped_column(Text, nullable=True, comment="备注", default=None)
stage: Mapped[str | None] = mapped_column(String(32), nullable=True, comment="当前育种阶段(breeding_stage)")
generation: Mapped[str | None] = mapped_column(String(16), nullable=True, comment="世代(breeding_generation)")
germplasm_id: Mapped[int | None] = mapped_column(
Integer, ForeignKey("bre_germplasm.id", ondelete="SET NULL"),
index=True, nullable=True, comment="关联种质(晋升回填,不手填)"
)
entry_id: Mapped[int | None] = mapped_column(
Integer, ForeignKey("bre_trial_study_entry.id", ondelete="SET NULL"),
index=True, nullable=True, comment="参试条目(派生自定植)"
)
# 无 status 列:覆盖基类默认 ix_<表>_status_deleted 索引,
# 仅保留 (created_time, is_deleted) 复合索引用于数据权限过滤。
__table_args__ = (
Index("ix_bre_tree_created_deleted", "created_time", "is_deleted"),
# 单株编号全局唯一(仅约束未软删记录,与 service 层查重语义一致)
Index(
"uniq_bre_tree_no", "tree_no",
unique=True, postgresql_where=text("is_deleted = false"),
),
)
class TreePedigreeExclusionModel(ModelMixin, UserMixin, MappedBase):
"""系谱隔离注册表(v2.26 N1):孟德尔检验 flag 的树断开声明亲本链(退化为 founder),
自身表型保留;后续检验不再 flag 时行被清除(可逆)。系谱消费方据此置空 dam/sire。"""
__tablename__ = "bre_pedigree_exclusion"
tree_id: Mapped[int] = mapped_column(
Integer, ForeignKey("bre_tree.id", ondelete="CASCADE"),
index=True, nullable=False, comment="单株"
)
reason: Mapped[str | None] = mapped_column(String(512), nullable=True, comment="隔离原因")
source_type: Mapped[str | None] = mapped_column(String(32), nullable=True, comment="来源类型")
source_check_id: Mapped[int | None] = mapped_column(Integer, nullable=True, comment="来源检验批次ID")
__table_args__ = (
# 唯一树(同树仅一条隔离记录;同树多来源时按最新覆盖)
Index(
"uniq_bre_pedigree_exclusion_tree", "tree_id",
unique=True, postgresql_where=text("is_deleted = false"),
),
Index("ix_bre_pedigree_exclusion_created_deleted", "created_time", "is_deleted"),
)
@@ -0,0 +1,102 @@
from app.core.base_schema import CommonSchema
"""育种单株 —— Pydantic 校验/序列化模型。"""
from datetime import datetime
from pydantic import BaseModel, ConfigDict, Field
class TreeBaseSchema(BaseModel):
model_config = ConfigDict(from_attributes=True)
combination_id: int = Field(..., description="杂交组合")
dam_id: int | None = Field(default=None, description="母本(种质);留空自动取组合母本")
sire_id: int | None = Field(default=None, description="父本(种质);留空自动取组合父本")
trial_study_id: int | None = Field(default=None, description="试验执行(MET,规格 §3.13)")
block_no: int | None = Field(default=None, description="区组号")
rootstock_id: int | None = Field(default=None, description="砧木(规格 §3.17)")
clone_id: int | None = Field(default=None, description="无性系(规格 §3.0)")
plot_id: int | None = Field(default=None, description="试验地块/位置")
planting_id: int | None = Field(default=None, description="来源定植批次")
tree_no: str | None = Field(default=None, description="单株编号(留空自动生成 组合编号-序号)")
row_orientation: str | None = Field(default=None, description="行向")
row_no: int | None = Field(default=None, description="行数")
col_no: int | None = Field(default=None, description="列数")
planted_date: str | None = Field(default=None, description="定植时间")
bre_personnel_id: int | None = Field(default=None, description="责任人")
status: str | None = Field(default=None, description="状态")
remark: str | None = Field(default=None, description="备注")
stage: str | None = Field(default=None, description="当前育种阶段(breeding_stage)")
generation: str | None = Field(default=None, description="世代(breeding_generation)")
entry_id: int | None = Field(default=None, description="参试条目(派生自定植,表单不暴露)")
class TreeCreateSchema(TreeBaseSchema):
pass
class TreeUpdateSchema(TreeBaseSchema):
combination_id: int | None = Field(default=None, description="杂交组合")
plot_id: int | None = Field(default=None, description="试验地块/位置")
planting_id: int | None = Field(default=None, description="来源定植批次")
tree_no: str | None = Field(default=None, description="单株编号")
row_orientation: str | None = Field(default=None, description="行向")
row_no: int | None = Field(default=None, description="行数")
col_no: int | None = Field(default=None, description="列数")
planted_date: str | None = Field(default=None, description="定植时间")
bre_personnel_id: int | None = Field(default=None, description="责任人")
status: str | None = Field(default=None, description="状态")
remark: str | None = Field(default=None, description="备注")
stage: str | None = Field(default=None, description="当前育种阶段(breeding_stage)")
generation: str | None = Field(default=None, description="世代(breeding_generation)")
class TreeOutSchema(TreeBaseSchema):
id: int
uuid: str
combination_name: str | None = None # 由 Service 层联表填充
dam_name: str | None = None # 由 Service 层联表填充(母本品种名)
sire_name: str | None = None # 由 Service 层联表填充(父本品种名)
study_name: str | None = None # 由 Service 层联表填充(试验执行名称)
rootstock_name: str | None = None # 由 Service 层联表填充(砧木名称)
clone_name: str | None = None # 由 Service 层联表填充(无性系编号)
plot_name: str | None = None # 由 Service 层联表填充
planting_id: int | None = None
planting_name: str | None = None # 由 Service 层联表填充(定植批次展示)
tree_no: str | None = None
row_orientation: str | None = None
row_no: int | None = None
col_no: int | None = None
planted_date: str | None = None
bre_personnel_name: str | None = None # 由 Service 层联表填充
status: str | None = None
remark: str | None = None
stage: str | None = None
generation: str | None = None
warning: str | None = None # 创建/更新时显式亲本与组合父母本不一致的警示(仅返回体,不落库)
germplasm_id: int | None = None # 晋升回填,非表单字段
entry_id: int | None = None # 派生自定植,非表单字段
germplasm_name: str | None = None # 由 Service 层联表填充(关联种质名)
entry_number: int | None = None # 由 Service 层联表填充(参试编号)
created_time: datetime | None = None
updated_time: datetime | None = None
created_by: CommonSchema | None = None
updated_by: CommonSchema | None = None
class TreeQueryParam(BaseModel):
combination_id: int | None = Field(default=None, description="杂交组合", json_schema_extra={"q": "eq"})
trial_study_id: int | None = Field(default=None, description="试验执行", json_schema_extra={"q": "eq"})
rootstock_id: int | None = Field(default=None, description="砧木", json_schema_extra={"q": "eq"})
clone_id: int | None = Field(default=None, description="无性系", json_schema_extra={"q": "eq"})
plot_id: int | None = Field(default=None, description="试验地块/位置", json_schema_extra={"q": "eq"})
planting_id: int | None = Field(default=None, description="来源定植批次", json_schema_extra={"q": "eq"})
tree_no: str | None = Field(default=None, description="单株编号", json_schema_extra={"q": "like"})
planted_date: str | None = Field(default=None, description="定植时间", json_schema_extra={"q": "like"})
row_orientation: str | None = Field(default=None, description="行向", json_schema_extra={"q": "eq"})
bre_personnel_id: int | None = Field(default=None, description="责任人", json_schema_extra={"q": "eq"})
status: str | None = Field(default=None, description="状态", json_schema_extra={"q": "eq"})
stage: str | None = Field(default=None, description="育种阶段", json_schema_extra={"q": "eq"})
generation: str | None = Field(default=None, description="世代", json_schema_extra={"q": "eq"})
germplasm_id: int | None = Field(default=None, description="关联种质", json_schema_extra={"q": "eq"})
entry_id: int | None = Field(default=None, description="参试条目", json_schema_extra={"q": "eq"})
@@ -0,0 +1,540 @@
from typing import Any
from fastapi import UploadFile
from sqlalchemy.ext.asyncio import AsyncSession
from app.core.base_schema import AuthSchema, PageResultSchema, ImportResultSchema
from app.core.exceptions import CustomException
from app.core.logger import logger
from app.utils.common_util import search_to_dict
from app.utils.excel_util import ExcelUtil
from app.utils.dict_util import DictLabelResolver, dict_value_to_label
from app.utils.number_gen import NumberGenService
from .crud import BreedingTreeCRUD
from .schema import (
TreeCreateSchema,
TreeOutSchema,
TreeQueryParam,
TreeUpdateSchema,
)
from app.api.v1.module_bre.cross_combination.crud import BreedingCrossCombinationCRUD
from app.api.v1.module_bre.germplasm.crud import BreedingGermplasmCRUD
from app.api.v1.module_bre.germplasm.model import BreedingGermplasmModel
from app.api.v1.module_bre.clone.crud import BreedingCloneCRUD
from app.api.v1.module_bre.rootstock.crud import BreedingRootstockCRUD
from app.api.v1.module_bre.trial_study.crud import BreedingTrialStudyCRUD
from app.api.v1.module_bre.site.crud import BreedingPlotCRUD
from app.api.v1.module_bre.personnel.crud import BreedingPersonnelCRUD
from app.api.v1.module_bre.planting.crud import BreedingPlantingCRUD
from app.api.v1.module_bre.trial_study_entry.crud import BreedingTrialStudyEntryCRUD
from app.core.base_crud import assert_dict_values, assert_no_children, assert_parents_exist, assert_status_forward
from app.api.v1.module_bre.site.model import BreedingPlotModel
from app.api.v1.module_bre.cross_combination.model import CrossCombinationModel
from app.api.v1.module_bre.clone.model import CloneModel
from app.api.v1.module_bre.rootstock.model import RootstockModel
from app.api.v1.module_bre.trial_study.model import TrialStudyModel
from app.api.v1.module_bre.personnel.model import PersonnelModel
from app.api.v1.module_bre.planting.model import PlantingModel
from app.api.v1.module_bre.selection_result.model import SelectionResultModel
from app.api.v1.module_bre.trait_observation.model import TraitObservationModel
from app.api.v1.module_bre.tree_evaluation.model import TreeEvaluationModel
from app.api.v1.module_bre.tree_photo.model import TreePhotoModel
def _is_blank(v: Any) -> bool:
return v is None or (isinstance(v, str) and v.strip() == "")
def _none_if_blank(v: Any) -> Any:
if _is_blank(v):
return None
return str(v).strip() if isinstance(v, str) else v
def _to_float(v: Any) -> float | None:
if _is_blank(v):
return None
try:
return float(v)
except (TypeError, ValueError):
return None
def _to_int(v: Any) -> int | None:
if _is_blank(v):
return None
try:
return int(float(v))
except (TypeError, ValueError):
return None
class TreeService:
"""育种单株 模块服务层"""
def __init__(self, auth: AuthSchema, db: AsyncSession) -> None:
self.auth = auth
self.db = db
async def _resolve_parents(
self, combination_id: int | None, dam_id: int | None, sire_id: int | None
) -> tuple[int | None, int | None, str | None]:
"""母本/父本:未显式给出时从组合取 female/male 亲本;显式给出时校验种质存在且方向可用。
显式亲本与组合父母本不一致时返回警示文案(以显式为准,不阻断)。
"""
combo_dam = combo_sire = None
if combination_id is not None:
cc = await BreedingCrossCombinationCRUD(self.auth, self.db).get(id=combination_id)
if cc:
combo_dam = cc.female_parent_id
combo_sire = cc.male_parent_id
dam = dam_id if dam_id is not None else combo_dam
sire = sire_id if sire_id is not None else combo_sire
mismatches = []
if dam_id is not None and combo_dam is not None and dam_id != combo_dam:
mismatches.append("显式母本与组合母本不一致")
if sire_id is not None and combo_sire is not None and sire_id != combo_sire:
mismatches.append("显式父本与组合父本不一致")
warning = ("".join(mismatches) + "(以显式为准)") if mismatches else None
explicit = [
(dam_id, "can_be_female", "母本") if dam_id is not None else None,
(sire_id, "can_be_male", "父本") if sire_id is not None else None,
]
explicit = [e for e in explicit if e]
if explicit:
ids = {e[0] for e in explicit}
germs = await BreedingGermplasmCRUD(self.auth, self.db).get_list(search={"id": ("in", list(ids))})
germ_map = {g.id: g for g in germs}
for gid, flag, role in explicit:
g = germ_map.get(gid)
if not g:
raise CustomException(msg=f"种质不存在(id={gid}),无法作为{role}")
if not getattr(g, flag):
raise CustomException(msg=f"种质「{g.cultivar_name}」不可作{role}")
return dam, sire, warning
async def _attach_fk_labels(self, items: list[TreeOutSchema]) -> None:
if not items:
return
crud = BreedingTreeCRUD(self.auth, self.db)
combination_id_ids = {getattr(it, "combination_id") for it in items if getattr(it, "combination_id")}
if combination_id_ids:
refs = await BreedingCrossCombinationCRUD(self.auth, self.db).get_list(search={"id": ("in", list(combination_id_ids))})
ref_map = {r.id: getattr(r, "combination_code") for r in refs}
for it in items:
it.combination_name = ref_map.get(getattr(it, "combination_id"))
germ_ids = {getattr(it, "dam_id") for it in items if getattr(it, "dam_id")}
germ_ids |= {getattr(it, "sire_id") for it in items if getattr(it, "sire_id")}
if germ_ids:
germs = await BreedingGermplasmCRUD(self.auth, self.db).get_list(search={"id": ("in", list(germ_ids))})
germ_map = {r.id: getattr(r, "cultivar_name") for r in germs}
for it in items:
it.dam_name = germ_map.get(getattr(it, "dam_id"))
it.sire_name = germ_map.get(getattr(it, "sire_id"))
plot_id_ids = {getattr(it, "plot_id") for it in items if getattr(it, "plot_id")}
if plot_id_ids:
refs = await BreedingPlotCRUD(self.auth, self.db).get_list(search={"id": ("in", list(plot_id_ids))})
ref_map = {r.id: getattr(r, "plot_code") for r in refs}
for it in items:
it.plot_name = ref_map.get(getattr(it, "plot_id"))
bre_personnel_id_ids = {getattr(it, "bre_personnel_id") for it in items if getattr(it, "bre_personnel_id")}
if bre_personnel_id_ids:
refs = await BreedingPersonnelCRUD(self.auth, self.db).get_list(search={"id": ("in", list(bre_personnel_id_ids))})
ref_map = {r.id: getattr(r, "name") for r in refs}
for it in items:
it.bre_personnel_name = ref_map.get(getattr(it, "bre_personnel_id"))
planting_id_ids = {getattr(it, "planting_id") for it in items if getattr(it, "planting_id")}
if planting_id_ids:
refs = await BreedingPlantingCRUD(self.auth, self.db).get_list(search={"id": ("in", list(planting_id_ids))})
ref_map = {r.id: (getattr(r, "planting_date") or f"定植#{r.id}") for r in refs}
for it in items:
it.planting_name = ref_map.get(getattr(it, "planting_id"))
study_ids = {getattr(it, "trial_study_id") for it in items if getattr(it, "trial_study_id")}
if study_ids:
refs = await BreedingTrialStudyCRUD(self.auth, self.db).get_list(search={"id": ("in", list(study_ids))})
ref_map = {r.id: getattr(r, "study_name") for r in refs}
for it in items:
it.study_name = ref_map.get(getattr(it, "trial_study_id"))
rootstock_ids = {getattr(it, "rootstock_id") for it in items if getattr(it, "rootstock_id")}
if rootstock_ids:
refs = await BreedingRootstockCRUD(self.auth, self.db).get_list(search={"id": ("in", list(rootstock_ids))})
ref_map = {r.id: getattr(r, "rootstock_name") for r in refs}
for it in items:
it.rootstock_name = ref_map.get(getattr(it, "rootstock_id"))
clone_ids = {getattr(it, "clone_id") for it in items if getattr(it, "clone_id")}
if clone_ids:
refs = await BreedingCloneCRUD(self.auth, self.db).get_list(search={"id": ("in", list(clone_ids))})
ref_map = {r.id: getattr(r, "clone_code") for r in refs}
for it in items:
it.clone_name = ref_map.get(getattr(it, "clone_id"))
germ_ids = {getattr(it, "germplasm_id") for it in items if getattr(it, "germplasm_id")}
if germ_ids:
germs = await BreedingGermplasmCRUD(self.auth, self.db).get_list(search={"id": ("in", list(germ_ids))})
germ_map = {r.id: getattr(r, "cultivar_name") for r in germs}
for it in items:
it.germplasm_name = germ_map.get(getattr(it, "germplasm_id"))
entry_ids = {getattr(it, "entry_id") for it in items if getattr(it, "entry_id")}
if entry_ids:
entries = await BreedingTrialStudyEntryCRUD(self.auth, self.db).get_list(search={"id": ("in", list(entry_ids))})
entry_map = {r.id: getattr(r, "entry_number") for r in entries}
for it in items:
it.entry_number = entry_map.get(getattr(it, "entry_id"))
async def detail(self, id: int) -> TreeOutSchema:
obj = await BreedingTreeCRUD(self.auth, self.db).get(id=id)
if not obj:
raise CustomException(msg="该育种单株不存在")
out = TreeOutSchema.model_validate(obj)
await self._attach_fk_labels([out])
return out
async def get_list(
self,
search: TreeQueryParam | None = None,
order_by: list[dict[str, str]] | None = None,
) -> list[TreeOutSchema]:
obj_list = await BreedingTreeCRUD(self.auth, self.db).get_list(
search=search_to_dict(search), order_by=order_by
)
outs = [TreeOutSchema.model_validate(obj) for obj in obj_list]
await self._attach_fk_labels(outs)
return outs
async def page(
self,
page_no: int,
page_size: int,
search: TreeQueryParam | None = None,
order_by: list[dict[str, str]] | None = None,
) -> PageResultSchema[TreeOutSchema]:
offset = (page_no - 1) * page_size
result = await BreedingTreeCRUD(self.auth, self.db).page(
offset=offset,
limit=page_size,
order_by=order_by or [{"id": "asc"}],
search=search_to_dict(search, {}),
out_schema=TreeOutSchema,
)
await self._attach_fk_labels(result.items)
return result
async def _gen_tree_no(self, combination_id: int) -> str:
"""自动生成单株编号:{组合编号}-{两位序号}(如 26001-01),原子取号。"""
cc = await BreedingCrossCombinationCRUD(self.auth, self.db).get(id=combination_id)
base = (cc.combination_code or f"C{combination_id}") if cc else f"C{combination_id}"
async def _seed() -> int | None:
crud = BreedingTreeCRUD(self.auth, self.db)
objs = await crud.get_list(search={"combination_id": ("eq", combination_id)})
max_seq = 0
for o in objs:
tail = (o.tree_no or "").rsplit("-", 1)[-1]
if tail.isdigit():
max_seq = max(max_seq, int(tail))
return max_seq + 1
seq = await NumberGenService(self.db).next_seq(f"tree:{combination_id}", seed_fn=_seed)
return f"{base}-{seq:02d}"
async def _derive_from_planting(self, planting_id: int | None, fields: dict[str, Any]) -> None:
"""单写点派生:试验/区组/参试条目以定植记录为准,覆盖写入单株。"""
if not planting_id:
return
planting = await BreedingPlantingCRUD(self.auth, self.db).get(id=planting_id)
if not planting:
return
for col in ("trial_study_id", "block_no", "entry_id"):
val = getattr(planting, col, None)
if val is not None:
fields[col] = val
async def create(self, data: TreeCreateSchema) -> TreeOutSchema:
fields = data.model_dump(exclude_none=True)
if _is_blank(fields.get("tree_no")):
fields["tree_no"] = await self._gen_tree_no(fields["combination_id"])
# 育种阶段自动设置:F 代/回交杂种实生苗 → seedling(童期),亲本/成株由调用方显式给
if _is_blank(fields.get("stage")):
gen = str(fields.get("generation") or "").strip().upper()
if gen in ("F1", "F2", "BC1", "BC2", "BC3"):
fields["stage"] = "seedling"
fields["dam_id"], fields["sire_id"], parent_warning = await self._resolve_parents(
fields.get("combination_id"), fields.get("dam_id"), fields.get("sire_id")
)
await self._derive_from_planting(fields.get("planting_id"), fields)
data = TreeCreateSchema(**fields)
exist_obj = await BreedingTreeCRUD(self.auth, self.db).get(tree_no=data.tree_no)
if exist_obj:
raise CustomException(msg="创建失败,单株编号已存在")
await assert_parents_exist(
self.db,
[
(CrossCombinationModel, data.combination_id, '杂交组合'),
(TrialStudyModel, data.trial_study_id, '试验执行'),
(CloneModel, data.clone_id, '无性系'),
(RootstockModel, data.rootstock_id, '砧木'),
(BreedingPlotModel, data.plot_id, '试验地块'),
(PlantingModel, data.planting_id, '定植'),
(PersonnelModel, data.bre_personnel_id, '育种人员'),
],
)
await assert_dict_values(
self.db,
[
('row_orientation', data.row_orientation, '行向'),
('tree_status', data.status, '单株状态'),
('breeding_stage', data.stage, '育种阶段'),
('breeding_generation', data.generation, '世代'),
],
)
obj = await BreedingTreeCRUD(self.auth, self.db).create(data=data)
out = TreeOutSchema.model_validate(obj)
if parent_warning:
out.warning = parent_warning
await self._attach_fk_labels([out])
return out
async def update(self, id: int, data: TreeUpdateSchema) -> TreeOutSchema:
obj = await BreedingTreeCRUD(self.auth, self.db).get(id=id)
if not obj:
raise CustomException(msg="更新失败,该育种单株不存在")
if data.tree_no is not None:
exist_obj = await BreedingTreeCRUD(self.auth, self.db).get(tree_no=data.tree_no)
if exist_obj and exist_obj.id != id:
raise CustomException(msg="更新失败,单株编号重复")
await assert_parents_exist(
self.db,
[
(CrossCombinationModel, data.combination_id, '杂交组合'),
(TrialStudyModel, data.trial_study_id, '试验执行'),
(CloneModel, data.clone_id, '无性系'),
(RootstockModel, data.rootstock_id, '砧木'),
(BreedingPlotModel, data.plot_id, '试验地块'),
(PlantingModel, data.planting_id, '定植'),
(PersonnelModel, data.bre_personnel_id, '育种人员'),
],
)
await assert_dict_values(
self.db,
[
('row_orientation', data.row_orientation, '行向'),
('tree_status', data.status, '单株状态'),
('breeding_stage', data.stage, '育种阶段'),
('breeding_generation', data.generation, '世代'),
],
)
await assert_status_forward(self.db, "tree_status", [obj.status], data.status, "单株状态")
# 母本/父本:显式给出则校验;否则随当前(新)组合自动派生
eff_combo = data.combination_id if data.combination_id is not None else obj.combination_id
dam, sire, parent_warning = await self._resolve_parents(eff_combo, data.dam_id, data.sire_id)
data = data.model_copy(update={"dam_id": dam, "sire_id": sire})
# 单写点派生:定植记录持有试验/区组/参试条目
eff_planting = data.planting_id if data.planting_id is not None else obj.planting_id
if data.planting_id is not None or obj.planting_id is not None:
fields = dict(data.model_dump(exclude_none=True))
await self._derive_from_planting(eff_planting, fields)
if any(fields.get(c) != getattr(obj, c) for c in ("trial_study_id", "block_no", "entry_id")):
data = TreeUpdateSchema(**fields)
obj = await BreedingTreeCRUD(self.auth, self.db).update(id=id, data=data)
out = TreeOutSchema.model_validate(obj)
if parent_warning:
out.warning = parent_warning
await self._attach_fk_labels([out])
return out
async def delete(self, ids: list[int]) -> None:
if not ids:
raise CustomException(msg="删除失败,删除对象不能为空")
objs = await BreedingTreeCRUD(self.auth, self.db).get_list(search={"id": ("in", ids)})
obj_map = {o.id: o for o in objs}
for id_ in ids:
if id_ not in obj_map:
raise CustomException(msg="删除失败,该育种单株不存在")
await assert_no_children(
self.db,
ids,
[
(TreeEvaluationModel, 'tree_id', '单株评价'),
(TreePhotoModel, 'tree_id', '单株照片'),
(SelectionResultModel, 'tree_id', '选择结果'),
(TraitObservationModel, 'tree_id', '性状观测'),
],
)
await BreedingTreeCRUD(self.auth, self.db).delete(ids=ids)
async def list_options(self) -> list[dict[str, Any]]:
"""供前端下拉选择使用:返回 [{value, label}]。"""
obj_list = await BreedingTreeCRUD(self.auth, self.db).get_list(order_by=[{"id": "asc"}])
return [{"value": o.id, "label": o.tree_no} for o in obj_list]
@staticmethod
def batch_export(obj_list: list[dict[str, Any]]) -> bytes:
mapping_dict = {
"combination_name": "杂交组合",
"plot_name": "试验地块/位置",
"planting_name": "来源定植批次",
"tree_no": "单株编号",
"row_orientation": "行向",
"row_no": "行数",
"col_no": "列数",
"planted_date": "定植时间",
"bre_personnel_name": "责任人",
"status": "状态",
"stage": "育种阶段",
"generation": "世代",
"germplasm_name": "关联种质",
"entry_number": "参试编号",
"remark": "备注",
"created_time": "创建时间",
"created_by": "创建者",
}
data = [dict(item) for item in obj_list]
for item in data:
creator = item.get("created_by")
item["created_by"] = creator.get("name", "未知") if isinstance(creator, dict) else "未知"
item["row_orientation"] = dict_value_to_label("row_orientation", item.get("row_orientation"))
item["status"] = dict_value_to_label("tree_status", item.get("status"))
item["stage"] = dict_value_to_label("breeding_stage", item.get("stage"))
item["generation"] = dict_value_to_label("breeding_generation", item.get("generation"))
return ExcelUtil.export_list2excel(list_data=data, mapping_dict=mapping_dict)
async def batch_import(self, file: UploadFile, update_support: bool = False) -> ImportResultSchema:
header_dict = {
"杂交组合": "combination_id",
"试验地块/位置": "plot_id",
"来源定植批次": "planting_id",
"单株编号": "tree_no",
"行向": "row_orientation",
"行数": "row_no",
"列数": "col_no",
"定植时间": "planted_date",
"责任人": "bre_personnel_id",
"状态": "status",
"育种阶段": "stage",
"世代": "generation",
"备注": "remark",
}
try:
contents = await file.read()
rows = ExcelUtil.read_excel_to_dicts(contents)
await file.close()
if not rows:
raise CustomException(msg="导入文件为空")
missing_headers = [h for h in header_dict if h not in rows[0]]
if missing_headers:
raise CustomException(msg=f"导入文件缺少必要的列: {', '.join(missing_headers)}")
combination_id_refs = await BreedingCrossCombinationCRUD(self.auth, self.db).get_list(order_by=[{"id": "asc"}])
combination_id_map = {getattr(r, "combination_code"): r.id for r in combination_id_refs}
combination_parents_map = {
r.id: (getattr(r, "female_parent_id"), getattr(r, "male_parent_id")) for r in combination_id_refs
}
plot_id_refs = await BreedingPlotCRUD(self.auth, self.db).get_list(order_by=[{"id": "asc"}])
plot_id_map = {getattr(r, "plot_code"): r.id for r in plot_id_refs}
bre_personnel_id_refs = await BreedingPersonnelCRUD(self.auth, self.db).get_list(order_by=[{"id": "asc"}])
bre_personnel_id_map = {getattr(r, "name"): r.id for r in bre_personnel_id_refs}
planting_id_refs = await BreedingPlantingCRUD(self.auth, self.db).get_list(order_by=[{"id": "asc"}])
planting_id_map = {str(getattr(r, "planting_date") or r.id): r.id for r in planting_id_refs}
mapped_rows = []
for row in rows:
mapped_rows.append({en: row.get(ch) for ch, en in header_dict.items()})
required_fields = ["combination_id", "tree_no"]
errors = []
for field in required_fields:
missing_indices = [i + 1 for i, r in enumerate(mapped_rows) if _is_blank(r.get(field))]
if missing_indices:
field_name = next(k for k, v in header_dict.items() if v == field)
rows_str = "".join(str(i) for i in missing_indices)
errors.append(f"{field_name}不能为空,第{rows_str}")
if errors:
raise CustomException(msg=f"导入失败,以下行缺少必要字段:\n{'; '.join(errors)}")
error_msgs: list[str] = []
success_count = 0
crud = BreedingTreeCRUD(self.auth, self.db)
resolver = DictLabelResolver(
self.auth, self.db,
["row_orientation", "tree_status", "breeding_stage", "breeding_generation"],
)
for i, row in enumerate(mapped_rows, start=1):
try:
combination_id_val = combination_id_map.get(str(row.get("combination_id")).strip()) if not _is_blank(row.get("combination_id")) else None
plot_id_val = plot_id_map.get(str(row.get("plot_id")).strip()) if not _is_blank(row.get("plot_id")) else None
planting_id_val = planting_id_map.get(str(row.get("planting_id")).strip()) if not _is_blank(row.get("planting_id")) else None
bre_personnel_id_val = bre_personnel_id_map.get(str(row.get("bre_personnel_id")).strip()) if not _is_blank(row.get("bre_personnel_id")) else None
combo_parents = combination_parents_map.get(combination_id_val, (None, None))
fields = {
"combination_id": combination_id_val,
"dam_id": combo_parents[0],
"sire_id": combo_parents[1],
"plot_id": plot_id_val,
"planting_id": planting_id_val,
"tree_no": _none_if_blank(row.get("tree_no")),
"row_orientation": await resolver.resolve("row_orientation", row.get("row_orientation")),
"row_no": _to_int(row.get("row_no")),
"col_no": _to_int(row.get("col_no")),
"planted_date": _none_if_blank(row.get("planted_date")),
"bre_personnel_id": bre_personnel_id_val,
"status": await resolver.resolve("tree_status", row.get("status")),
"stage": await resolver.resolve("breeding_stage", row.get("stage")),
"generation": await resolver.resolve("breeding_generation", row.get("generation")),
"remark": _none_if_blank(row.get("remark")),
}
unique_kwargs = {"tree_no": fields["tree_no"]}
create_data = TreeCreateSchema(**fields)
exist_obj = await crud.get(**unique_kwargs)
if exist_obj:
if update_support:
await crud.update(id=exist_obj.id, data=TreeUpdateSchema(**fields))
success_count += 1
else:
error_msgs.append(f"{i}行: 单株编号 {fields['tree_no']} 已存在")
else:
await crud.create(data=create_data)
success_count += 1
except Exception as e:
error_msgs.append(f"{i}行: {e!s}")
continue
return ImportResultSchema(
valid_count=success_count,
invalid_count=len(error_msgs),
message_list=error_msgs,
)
except Exception as e:
logger.error(f"批量导入育种单株失败: {e!s}")
raise CustomException(msg=f"导入失败: {e!s}")
@staticmethod
def import_template_download() -> bytes:
header_list = [
"杂交组合",
"试验地块/位置",
"来源定植批次",
"单株编号",
"行向",
"行数",
"列数",
"定植时间",
"责任人",
"状态",
"育种阶段",
"世代",
"备注",
]
selector_header_list = ["行向", "状态", "育种阶段", "世代"]
option_list = [
{"行向": ["南北行", "东西行"]},
{"状态": ["存活", "入选", "初选", "重点", "保存", "淘汰"]},
{"育种阶段": ["种质", "亲本", "实生苗", "初选株", "复选株", "品系", "区试品系", "新品种"]},
{"世代": ["F1", "F2", "BC1", "BC2", "BC3"]},
]
return ExcelUtil.get_excel_template(
header_list=header_list,
selector_header_list=selector_header_list,
option_list=option_list,
)