init: 初始化 dpb 桃育种系统代码库
前后端 + 后端 FastAPI 全量源码、部署脚本与文档。
This commit is contained in:
@@ -0,0 +1,134 @@
|
||||
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 (
|
||||
ObservationCreateSchema,
|
||||
ObservationOutSchema,
|
||||
ObservationQueryParam,
|
||||
ObservationUpdateSchema,
|
||||
)
|
||||
from .service import ObservationService
|
||||
|
||||
ObservationRouter = APIRouter(route_class=OperationLogRoute, prefix="/observation", tags=["通用观测"])
|
||||
|
||||
|
||||
@ObservationRouter.get("/detail/{id}", summary="获取通用观测详情", response_model=ResponseSchema[ObservationOutSchema])
|
||||
async def get_observation__detail_controller(
|
||||
auth: Annotated[AuthSchema, Depends(AuthPermission(["module_bre:observation:detail"]))],
|
||||
id: Annotated[int, Path(description="观测ID")],
|
||||
db: Annotated[AsyncSession, Depends(db_getter)],
|
||||
) -> JSONResponse:
|
||||
service = ObservationService(auth, db)
|
||||
result_dict = await service.detail(id=id)
|
||||
return SuccessResponse(data=result_dict, msg="获取通用观测详情成功")
|
||||
|
||||
|
||||
@ObservationRouter.get("/list", summary="分页查询通用观测", response_model=ResponseSchema[PageResultSchema[ObservationOutSchema]])
|
||||
async def get_observation__list_controller(
|
||||
auth: Annotated[AuthSchema, Depends(AuthPermission(["module_bre:observation:query"]))],
|
||||
page: Annotated[PaginationQueryParam, Depends()],
|
||||
search: Annotated[ObservationQueryParam, Query()],
|
||||
db: Annotated[AsyncSession, Depends(db_getter)],
|
||||
) -> JSONResponse:
|
||||
service = ObservationService(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="查询通用观测列表成功")
|
||||
|
||||
|
||||
@ObservationRouter.get("/options", summary="通用观测下拉选项")
|
||||
async def get_observation__options_controller(
|
||||
auth: Annotated[AuthSchema, Depends(AuthPermission(["module_bre:observation:query"]))],
|
||||
db: Annotated[AsyncSession, Depends(db_getter)],
|
||||
) -> JSONResponse:
|
||||
service = ObservationService(auth, db)
|
||||
options = await service.list_options()
|
||||
return SuccessResponse(data=options, msg="获取通用观测选项成功")
|
||||
|
||||
|
||||
@ObservationRouter.post("/create", summary="创建通用观测", response_model=ResponseSchema[ObservationOutSchema])
|
||||
async def create_observation__controller(
|
||||
auth: Annotated[AuthSchema, Depends(AuthPermission(["module_bre:observation:create"]))],
|
||||
data: Annotated[ObservationCreateSchema, Body(description="创建参数")],
|
||||
db: Annotated[AsyncSession, Depends(db_getter)],
|
||||
) -> JSONResponse:
|
||||
service = ObservationService(auth, db)
|
||||
result_dict = await service.create(data=data)
|
||||
return SuccessResponse(data=result_dict, msg="创建通用观测成功")
|
||||
|
||||
|
||||
@ObservationRouter.put("/update/{id}", summary="修改通用观测", response_model=ResponseSchema[ObservationOutSchema])
|
||||
async def update_observation__controller(
|
||||
auth: Annotated[AuthSchema, Depends(AuthPermission(["module_bre:observation:update"]))],
|
||||
id: Annotated[int, Path(description="观测ID")],
|
||||
data: Annotated[ObservationUpdateSchema, Body(description="修改参数")],
|
||||
db: Annotated[AsyncSession, Depends(db_getter)],
|
||||
) -> JSONResponse:
|
||||
service = ObservationService(auth, db)
|
||||
result_dict = await service.update(id=id, data=data)
|
||||
return SuccessResponse(data=result_dict, msg="修改通用观测成功")
|
||||
|
||||
|
||||
@ObservationRouter.delete("/delete", summary="删除通用观测", response_model=ResponseSchema[None])
|
||||
async def delete_observation__controller(
|
||||
auth: Annotated[AuthSchema, Depends(AuthPermission(["module_bre:observation:delete"]))],
|
||||
ids: Annotated[list[int], Body(description="ID列表")],
|
||||
db: Annotated[AsyncSession, Depends(db_getter)],
|
||||
) -> JSONResponse:
|
||||
service = ObservationService(auth, db)
|
||||
await service.delete(ids=ids)
|
||||
return SuccessResponse(msg="删除通用观测成功")
|
||||
|
||||
|
||||
@ObservationRouter.post("/export", summary="导出通用观测")
|
||||
async def export_observation__controller(
|
||||
auth: Annotated[AuthSchema, Depends(AuthPermission(["module_bre:observation:export"]))],
|
||||
search: Annotated[ObservationQueryParam, Query()],
|
||||
db: Annotated[AsyncSession, Depends(db_getter)],
|
||||
) -> StreamingResponse:
|
||||
service = ObservationService(auth, db)
|
||||
result_dict_list = await service.get_list(search=search)
|
||||
export_result = ObservationService.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')}"},
|
||||
)
|
||||
|
||||
|
||||
@ObservationRouter.post("/import", summary="导入通用观测", response_model=ResponseSchema[ImportResultSchema])
|
||||
async def import_observation__controller(
|
||||
file: Annotated[UploadFile, File(description="导入文件")],
|
||||
auth: Annotated[AuthSchema, Depends(AuthPermission(["module_bre:observation:import"]))],
|
||||
db: Annotated[AsyncSession, Depends(db_getter)],
|
||||
) -> JSONResponse:
|
||||
service = ObservationService(auth, db)
|
||||
batch_import_result = await service.batch_import(file=file, update_support=True)
|
||||
return SuccessResponse(data=batch_import_result, msg="导入通用观测成功")
|
||||
|
||||
|
||||
@ObservationRouter.post("/download/template", summary="获取通用观测导入模板", dependencies=[Depends(AuthPermission(["module_bre:observation:download"]))])
|
||||
async def download_observation__template_controller() -> StreamingResponse:
|
||||
import_template_result = ObservationService.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 ObservationModel
|
||||
|
||||
|
||||
class BreedingObservationCRUD(CRUDBase[ObservationModel, Any, Any]):
|
||||
"""通用观测 CRUD —— 直接复用 CRUDBase(已自动注入数据权限过滤)。"""
|
||||
|
||||
def __init__(self, auth: AuthSchema, db: AsyncSession) -> None:
|
||||
super().__init__(ObservationModel, auth, db)
|
||||
|
||||
|
||||
observation_crud = BreedingObservationCRUD
|
||||
@@ -0,0 +1,57 @@
|
||||
"""通用观测(EAV长表) 数据模型"""
|
||||
from datetime import date
|
||||
|
||||
from sqlalchemy import Date, ForeignKey, Index, Integer, String, Text
|
||||
from sqlalchemy.orm import Mapped, mapped_column
|
||||
|
||||
from app.core.base_model import MappedBase, ModelMixin, UserMixin
|
||||
|
||||
|
||||
class ObservationModel(ModelMixin, UserMixin, MappedBase):
|
||||
"""通用观测 EAV 长表(§3.2;与 tree_evaluation 经统一性状值视图归一)。
|
||||
|
||||
EAV 通用观测:trait_id 可空(不强制挂性状),obs_value 按 obs_type
|
||||
(numeric/text/date) 解释,均存 text。
|
||||
"""
|
||||
|
||||
__tablename__ = "bre_observation"
|
||||
|
||||
tree_id: Mapped[int | None] = mapped_column(
|
||||
Integer, ForeignKey("bre_tree.id", ondelete="CASCADE"), index=True, nullable=True, comment="单株(可空)", default=None
|
||||
)
|
||||
|
||||
plot_id: Mapped[int | None] = mapped_column(
|
||||
Integer, ForeignKey("bre_plot.id", ondelete="CASCADE"), index=True, nullable=True, comment="小区(可空)", default=None
|
||||
)
|
||||
|
||||
germplasm_id: Mapped[int | None] = mapped_column(
|
||||
Integer, ForeignKey("bre_germplasm.id", ondelete="CASCADE"), index=True, nullable=True, comment="种质资源(可空,描述性观测)", default=None
|
||||
)
|
||||
|
||||
trait_id: Mapped[int | None] = mapped_column(
|
||||
Integer, ForeignKey("bre_trait.id", ondelete="CASCADE"), index=True, nullable=True, comment="性状(可空,EAV)", default=None
|
||||
)
|
||||
|
||||
obs_date: Mapped[date | None] = mapped_column(Date, nullable=True, comment="观测日期", default=None)
|
||||
|
||||
obs_year: Mapped[int | None] = mapped_column(Integer, nullable=True, comment="观测年份", default=None)
|
||||
|
||||
obs_value: Mapped[str | None] = mapped_column(Text, nullable=True, comment="观测值(text多态,按obs_type解释)", default=None)
|
||||
|
||||
obs_type: Mapped[str | None] = mapped_column(String(32), nullable=True, comment="观测类型(numeric/text/date)", default=None)
|
||||
|
||||
trial_study_id: Mapped[int | None] = mapped_column(
|
||||
Integer, ForeignKey("bre_trial_study.id", ondelete="CASCADE"), index=True, nullable=True, comment="试验(可空)", default=None
|
||||
)
|
||||
|
||||
operator_id: Mapped[int | None] = mapped_column(
|
||||
Integer, ForeignKey("bre_personnel.id", ondelete="CASCADE"), index=True, nullable=True, comment="观测人", default=None
|
||||
)
|
||||
|
||||
remark: Mapped[str | None] = mapped_column(String(512), nullable=True, comment="备注", default=None)
|
||||
|
||||
status: Mapped[int] = mapped_column(Integer, nullable=False, default=1, comment="状态(1正常/0停用)")
|
||||
|
||||
__table_args__ = (
|
||||
Index("ix_bre_observation_created_deleted", "created_time", "is_deleted"),
|
||||
)
|
||||
@@ -0,0 +1,77 @@
|
||||
"""通用观测 —— Pydantic 校验/序列化模型。"""
|
||||
from datetime import date, datetime
|
||||
|
||||
from pydantic import BaseModel, ConfigDict, Field
|
||||
|
||||
from app.core.base_schema import CommonSchema
|
||||
|
||||
|
||||
class ObservationBaseSchema(BaseModel):
|
||||
model_config = ConfigDict(from_attributes=True)
|
||||
|
||||
tree_id: int | None = Field(default=None, description="单株(可空)")
|
||||
plot_id: int | None = Field(default=None, description="小区(可空)")
|
||||
germplasm_id: int | None = Field(default=None, description="种质资源(可空,描述性观测)")
|
||||
trait_id: int | None = Field(default=None, description="性状(可空,EAV)")
|
||||
obs_date: date | None = Field(default=None, description="观测日期")
|
||||
obs_year: int | None = Field(default=None, description="观测年份")
|
||||
obs_value: str | None = Field(default=None, description="观测值(text多态,按obs_type解释)")
|
||||
obs_type: str = Field(..., description="观测类型(numeric/text/date)")
|
||||
trial_study_id: int | None = Field(default=None, description="试验(可空)")
|
||||
operator_id: int | None = Field(default=None, description="观测人")
|
||||
remark: str | None = Field(default=None, description="备注")
|
||||
|
||||
|
||||
class ObservationCreateSchema(ObservationBaseSchema):
|
||||
pass
|
||||
|
||||
|
||||
class ObservationUpdateSchema(ObservationBaseSchema):
|
||||
tree_id: int | None = Field(default=None, description="单株(可空)")
|
||||
plot_id: int | None = Field(default=None, description="小区(可空)")
|
||||
germplasm_id: int | None = Field(default=None, description="种质资源(可空,描述性观测)")
|
||||
trait_id: int | None = Field(default=None, description="性状(可空,EAV)")
|
||||
obs_date: date | None = Field(default=None, description="观测日期")
|
||||
obs_year: int | None = Field(default=None, description="观测年份")
|
||||
obs_value: str | None = Field(default=None, description="观测值(text多态,按obs_type解释)")
|
||||
obs_type: str | None = Field(default=None, description="观测类型(numeric/text/date)")
|
||||
trial_study_id: int | None = Field(default=None, description="试验(可空)")
|
||||
operator_id: int | None = Field(default=None, description="观测人")
|
||||
remark: str | None = Field(default=None, description="备注")
|
||||
|
||||
|
||||
class ObservationOutSchema(ObservationBaseSchema):
|
||||
id: int
|
||||
uuid: str
|
||||
tree_id: int | None = None
|
||||
plot_id: int | None = None
|
||||
germplasm_id: int | None = None
|
||||
trait_id: int | None = None
|
||||
obs_date: date | None = None
|
||||
obs_year: int | None = None
|
||||
obs_value: str | None = None
|
||||
obs_type: str | None = None
|
||||
trial_study_id: int | None = None
|
||||
operator_id: int | None = None
|
||||
remark: str | None = None
|
||||
status: int | None = None
|
||||
tree_no: str | None = None # 由 Service 层联表填充
|
||||
plot_label: str | None = None # 由 Service 层联表填充 ({site_name}-{plot_code})
|
||||
germplasm_name: str | None = None # 由 Service 层联表填充
|
||||
trait_name: str | None = None # 由 Service 层联表填充
|
||||
trial_study_name: str | None = None # 由 Service 层联表填充
|
||||
operator_name: str | None = None # 由 Service 层联表填充
|
||||
created_time: datetime | None = None
|
||||
updated_time: datetime | None = None
|
||||
created_by: CommonSchema | None = None
|
||||
updated_by: CommonSchema | None = None
|
||||
|
||||
|
||||
class ObservationQueryParam(BaseModel):
|
||||
obs_type: str | None = Field(default=None, description="观测类型", json_schema_extra={"q": "eq"})
|
||||
obs_year: int | None = Field(default=None, description="观测年份", json_schema_extra={"q": "eq"})
|
||||
trait_id: int | None = Field(default=None, description="性状", json_schema_extra={"q": "eq"})
|
||||
tree_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"})
|
||||
germplasm_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"})
|
||||
@@ -0,0 +1,357 @@
|
||||
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 .crud import BreedingObservationCRUD
|
||||
from .schema import (
|
||||
ObservationCreateSchema,
|
||||
ObservationOutSchema,
|
||||
ObservationQueryParam,
|
||||
ObservationUpdateSchema,
|
||||
)
|
||||
from app.core.base_crud import assert_parents_exist
|
||||
from app.api.v1.module_bre.tree.crud import BreedingTreeCRUD
|
||||
from app.api.v1.module_bre.tree.model import TreeModel
|
||||
from app.api.v1.module_bre.trait.crud import BreedingTraitCRUD
|
||||
from app.api.v1.module_bre.trait.model import TraitModel
|
||||
from app.api.v1.module_bre.trial_study.crud import BreedingTrialStudyCRUD
|
||||
from app.api.v1.module_bre.trial_study.model import TrialStudyModel
|
||||
from app.api.v1.module_bre.personnel.crud import BreedingPersonnelCRUD
|
||||
from app.api.v1.module_bre.personnel.model import PersonnelModel
|
||||
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.site.crud import BreedingPlotCRUD, BreedingSiteCRUD
|
||||
from app.api.v1.module_bre.site.model import BreedingPlotModel
|
||||
|
||||
|
||||
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_int(v: Any) -> int | None:
|
||||
if _is_blank(v):
|
||||
return None
|
||||
try:
|
||||
return int(float(v))
|
||||
except (TypeError, ValueError):
|
||||
return None
|
||||
|
||||
|
||||
class ObservationService:
|
||||
"""通用观测 模块服务层"""
|
||||
|
||||
def __init__(self, auth: AuthSchema, db: AsyncSession) -> None:
|
||||
self.auth = auth
|
||||
self.db = db
|
||||
|
||||
async def _validate_obs(self, data: Any, exclude_id: int | None = None) -> None:
|
||||
"""EAV 观测校验(A5):obs_type 解析校验 + 数值范围 + 同日同目标同性状判重。"""
|
||||
obs_type = getattr(data, "obs_type", None)
|
||||
obs_value = getattr(data, "obs_value", None)
|
||||
if obs_type and obs_value is not None:
|
||||
val = str(obs_value).strip()
|
||||
if obs_type == "numeric":
|
||||
try:
|
||||
float(val)
|
||||
except (TypeError, ValueError):
|
||||
raise CustomException(msg=f"观测值「{obs_value}」不是有效数值(obs_type=numeric)")
|
||||
elif obs_type == "date":
|
||||
import datetime
|
||||
|
||||
try:
|
||||
datetime.date.fromisoformat(val)
|
||||
except ValueError:
|
||||
raise CustomException(msg=f"观测值「{obs_value}」不是有效日期(obs_type=date,需 YYYY-MM-DD)")
|
||||
trait_id = getattr(data, "trait_id", None)
|
||||
if trait_id and obs_type == "numeric" and obs_value is not None:
|
||||
trait = await BreedingTraitCRUD(self.auth, self.db).get(id=trait_id)
|
||||
if trait:
|
||||
num = float(obs_value)
|
||||
if trait.valid_min is not None and num < float(trait.valid_min):
|
||||
raise CustomException(msg=f"数值 {num} 低于性状「{trait.trait_name}」有效下限 {trait.valid_min}")
|
||||
if trait.valid_max is not None and num > float(trait.valid_max):
|
||||
raise CustomException(msg=f"数值 {num} 超出性状「{trait.trait_name}」有效上限 {trait.valid_max}")
|
||||
subj = [
|
||||
("tree_id", getattr(data, "tree_id", None)),
|
||||
("plot_id", getattr(data, "plot_id", None)),
|
||||
("germplasm_id", getattr(data, "germplasm_id", None)),
|
||||
]
|
||||
present = [(k, v) for k, v in subj if v is not None]
|
||||
if len(present) > 1:
|
||||
raise CustomException(msg="观测目标只能指定一个:单株/小区/种质资源 不可同时指定")
|
||||
if trait_id and present:
|
||||
key, subj_id = present[0]
|
||||
dup = await BreedingObservationCRUD(self.auth, self.db).get(
|
||||
**{key: subj_id},
|
||||
trait_id=trait_id,
|
||||
obs_date=getattr(data, "obs_date", None),
|
||||
obs_year=getattr(data, "obs_year", None),
|
||||
)
|
||||
if dup and dup.id != exclude_id:
|
||||
raise CustomException(msg="该目标在此日期/年份已存在同性状观测(同日同性状判重)")
|
||||
|
||||
async def _attach_fk_labels(self, items: list[ObservationOutSchema]) -> None:
|
||||
if not items:
|
||||
return
|
||||
tree_ids = {getattr(it, "tree_id") for it in items if getattr(it, "tree_id")}
|
||||
if tree_ids:
|
||||
refs = await BreedingTreeCRUD(self.auth, self.db).get_list(search={"id": ("in", list(tree_ids))})
|
||||
ref_map = {r.id: getattr(r, "tree_no") for r in refs}
|
||||
for it in items:
|
||||
it.tree_no = ref_map.get(getattr(it, "tree_id"))
|
||||
plot_ids = {getattr(it, "plot_id") for it in items if getattr(it, "plot_id")}
|
||||
if plot_ids:
|
||||
plots = await BreedingPlotCRUD(self.auth, self.db).get_list(search={"id": ("in", list(plot_ids))})
|
||||
site_ids = {getattr(p, "site_id") for p in plots if getattr(p, "site_id")}
|
||||
site_map: dict[int, str] = {}
|
||||
if site_ids:
|
||||
sites = await BreedingSiteCRUD(self.auth, self.db).get_list(search={"id": ("in", list(site_ids))})
|
||||
site_map = {r.id: getattr(r, "site_name") for r in sites}
|
||||
plot_map = {p.id: f"{site_map.get(p.site_id, '')}-{p.plot_code}" for p in plots}
|
||||
for it in items:
|
||||
it.plot_label = plot_map.get(getattr(it, "plot_id"))
|
||||
trait_ids = {getattr(it, "trait_id") for it in items if getattr(it, "trait_id")}
|
||||
if trait_ids:
|
||||
refs = await BreedingTraitCRUD(self.auth, self.db).get_list(search={"id": ("in", list(trait_ids))})
|
||||
ref_map = {r.id: getattr(r, "trait_name") for r in refs}
|
||||
for it in items:
|
||||
it.trait_name = ref_map.get(getattr(it, "trait_id"))
|
||||
germplasm_ids = {getattr(it, "germplasm_id") for it in items if getattr(it, "germplasm_id")}
|
||||
if germplasm_ids:
|
||||
refs = await BreedingGermplasmCRUD(self.auth, self.db).get_list(search={"id": ("in", list(germplasm_ids))})
|
||||
ref_map = {r.id: getattr(r, "cultivar_name") for r in refs}
|
||||
for it in items:
|
||||
it.germplasm_name = ref_map.get(getattr(it, "germplasm_id"))
|
||||
trial_study_ids = {getattr(it, "trial_study_id") for it in items if getattr(it, "trial_study_id")}
|
||||
if trial_study_ids:
|
||||
refs = await BreedingTrialStudyCRUD(self.auth, self.db).get_list(search={"id": ("in", list(trial_study_ids))})
|
||||
ref_map = {r.id: getattr(r, "study_name") for r in refs}
|
||||
for it in items:
|
||||
it.trial_study_name = ref_map.get(getattr(it, "trial_study_id"))
|
||||
operator_ids = {getattr(it, "operator_id") for it in items if getattr(it, "operator_id")}
|
||||
if operator_ids:
|
||||
refs = await BreedingPersonnelCRUD(self.auth, self.db).get_list(search={"id": ("in", list(operator_ids))})
|
||||
ref_map = {r.id: getattr(r, "name") for r in refs}
|
||||
for it in items:
|
||||
it.operator_name = ref_map.get(getattr(it, "operator_id"))
|
||||
|
||||
async def detail(self, id: int) -> ObservationOutSchema:
|
||||
obj = await BreedingObservationCRUD(self.auth, self.db).get(id=id)
|
||||
if not obj:
|
||||
raise CustomException(msg="该观测不存在")
|
||||
out = ObservationOutSchema.model_validate(obj)
|
||||
await self._attach_fk_labels([out])
|
||||
return out
|
||||
|
||||
async def get_list(
|
||||
self,
|
||||
search: ObservationQueryParam | None = None,
|
||||
order_by: list[dict[str, str]] | None = None,
|
||||
) -> list[ObservationOutSchema]:
|
||||
obj_list = await BreedingObservationCRUD(self.auth, self.db).get_list(
|
||||
search=search_to_dict(search), order_by=order_by
|
||||
)
|
||||
outs = [ObservationOutSchema.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: ObservationQueryParam | None = None,
|
||||
order_by: list[dict[str, str]] | None = None,
|
||||
) -> PageResultSchema[ObservationOutSchema]:
|
||||
offset = (page_no - 1) * page_size
|
||||
result = await BreedingObservationCRUD(self.auth, self.db).page(
|
||||
offset=offset,
|
||||
limit=page_size,
|
||||
order_by=order_by or [{"id": "asc"}],
|
||||
search=search_to_dict(search, {}),
|
||||
out_schema=ObservationOutSchema,
|
||||
)
|
||||
await self._attach_fk_labels(result.items)
|
||||
return result
|
||||
|
||||
async def create(self, data: ObservationCreateSchema) -> ObservationOutSchema:
|
||||
await assert_parents_exist(
|
||||
self.db,
|
||||
[
|
||||
(TreeModel, data.tree_id, '单株'),
|
||||
(BreedingPlotModel, data.plot_id, '小区'),
|
||||
(BreedingGermplasmModel, data.germplasm_id, '种质资源'),
|
||||
(TraitModel, data.trait_id, '性状'),
|
||||
(TrialStudyModel, data.trial_study_id, '试验'),
|
||||
(PersonnelModel, data.operator_id, '观测人'),
|
||||
],
|
||||
)
|
||||
await self._validate_obs(data)
|
||||
obj = await BreedingObservationCRUD(self.auth, self.db).create(data=data)
|
||||
out = ObservationOutSchema.model_validate(obj)
|
||||
await self._attach_fk_labels([out])
|
||||
return out
|
||||
|
||||
async def update(self, id: int, data: ObservationUpdateSchema) -> ObservationOutSchema:
|
||||
obj = await BreedingObservationCRUD(self.auth, self.db).get(id=id)
|
||||
if not obj:
|
||||
raise CustomException(msg="更新失败,该观测不存在")
|
||||
await assert_parents_exist(
|
||||
self.db,
|
||||
[
|
||||
(TreeModel, data.tree_id, '单株'),
|
||||
(BreedingPlotModel, data.plot_id, '小区'),
|
||||
(BreedingGermplasmModel, data.germplasm_id, '种质资源'),
|
||||
(TraitModel, data.trait_id, '性状'),
|
||||
(TrialStudyModel, data.trial_study_id, '试验'),
|
||||
(PersonnelModel, data.operator_id, '观测人'),
|
||||
],
|
||||
)
|
||||
await self._validate_obs(data, exclude_id=id)
|
||||
obj = await BreedingObservationCRUD(self.auth, self.db).update(id=id, data=data)
|
||||
out = ObservationOutSchema.model_validate(obj)
|
||||
await self._attach_fk_labels([out])
|
||||
return out
|
||||
|
||||
async def delete(self, ids: list[int]) -> None:
|
||||
if not ids:
|
||||
raise CustomException(msg="删除失败,删除对象不能为空")
|
||||
objs = await BreedingObservationCRUD(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 BreedingObservationCRUD(self.auth, self.db).delete(ids=ids)
|
||||
|
||||
async def list_options(self) -> list[dict[str, Any]]:
|
||||
"""供前端下拉选择使用:返回 [{value, label}]。"""
|
||||
obj_list = await BreedingObservationCRUD(self.auth, self.db).get_list(order_by=[{"id": "asc"}])
|
||||
return [{"value": o.id, "label": f"{o.obs_type or 'obs'}: {o.obs_value or ''}"} for o in obj_list]
|
||||
|
||||
@staticmethod
|
||||
def batch_export(obj_list: list[dict[str, Any]]) -> bytes:
|
||||
mapping_dict = {
|
||||
"tree_no": "单株",
|
||||
"plot_label": "小区",
|
||||
"trait_name": "性状",
|
||||
"obs_type": "观测类型",
|
||||
"obs_value": "观测值",
|
||||
"obs_date": "观测日期",
|
||||
"obs_year": "观测年份",
|
||||
"trial_study_name": "试验",
|
||||
"operator_name": "观测人",
|
||||
"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 "未知"
|
||||
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 = {
|
||||
"单株": "tree_id",
|
||||
"小区": "plot_id",
|
||||
"性状": "trait_id",
|
||||
"观测类型": "obs_type",
|
||||
"观测值": "obs_value",
|
||||
"观测日期": "obs_date",
|
||||
"观测年份": "obs_year",
|
||||
"试验": "trial_study_id",
|
||||
"观测人": "operator_id",
|
||||
"备注": "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)}")
|
||||
tree_refs = await BreedingTreeCRUD(self.auth, self.db).get_list(order_by=[{"id": "asc"}])
|
||||
tree_map = {getattr(r, "tree_no"): r.id for r in tree_refs}
|
||||
plot_refs = await BreedingPlotCRUD(self.auth, self.db).get_list(order_by=[{"id": "asc"}])
|
||||
site_ids = {getattr(r, "site_id") for r in plot_refs if getattr(r, "site_id")}
|
||||
site_refs = await BreedingSiteCRUD(self.auth, self.db).get_list(search={"id": ("in", list(site_ids))}) if site_ids else []
|
||||
site_map = {r.id: getattr(r, "site_name") for r in site_refs}
|
||||
plot_map = {f"{site_map.get(r.site_id) or ''}-{r.plot_code}": r.id for r in plot_refs}
|
||||
trait_refs = await BreedingTraitCRUD(self.auth, self.db).get_list(order_by=[{"id": "asc"}])
|
||||
trait_map = {getattr(r, "trait_name"): r.id for r in trait_refs}
|
||||
trial_study_refs = await BreedingTrialStudyCRUD(self.auth, self.db).get_list(order_by=[{"id": "asc"}])
|
||||
trial_study_map = {getattr(r, "study_name"): r.id for r in trial_study_refs}
|
||||
operator_refs = await BreedingPersonnelCRUD(self.auth, self.db).get_list(order_by=[{"id": "asc"}])
|
||||
operator_map = {getattr(r, "name"): r.id for r in operator_refs}
|
||||
error_msgs: list[str] = []
|
||||
success_count = 0
|
||||
crud = BreedingObservationCRUD(self.auth, self.db)
|
||||
for i, row in enumerate(rows, start=1):
|
||||
try:
|
||||
fields = {
|
||||
"tree_id": tree_map.get(str(row.get("tree_id")).strip())
|
||||
if not _is_blank(row.get("tree_id")) else None,
|
||||
"plot_id": plot_map.get(str(row.get("plot_id")).strip())
|
||||
if not _is_blank(row.get("plot_id")) else None,
|
||||
"trait_id": trait_map.get(str(row.get("trait_id")).strip())
|
||||
if not _is_blank(row.get("trait_id")) else None,
|
||||
"obs_type": _none_if_blank(row.get("obs_type")),
|
||||
"obs_value": _none_if_blank(row.get("obs_value")),
|
||||
"obs_date": _none_if_blank(row.get("obs_date")),
|
||||
"obs_year": _to_int(row.get("obs_year")),
|
||||
"trial_study_id": trial_study_map.get(str(row.get("trial_study_id")).strip())
|
||||
if not _is_blank(row.get("trial_study_id")) else None,
|
||||
"operator_id": operator_map.get(str(row.get("operator_id")).strip())
|
||||
if not _is_blank(row.get("operator_id")) else None,
|
||||
"remark": _none_if_blank(row.get("remark")),
|
||||
}
|
||||
create_data = ObservationCreateSchema(**fields)
|
||||
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 = []
|
||||
return ExcelUtil.get_excel_template(
|
||||
header_list=header_list,
|
||||
selector_header_list=selector_header_list,
|
||||
option_list=option_list,
|
||||
)
|
||||
Reference in New Issue
Block a user