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 BreedingPropagationCRUD from .schema import ( PropagationCreateSchema, PropagationOutSchema, PropagationQueryParam, PropagationUpdateSchema, ) from app.core.base_crud import assert_parents_exist from app.api.v1.module_bre.clone.crud import BreedingCloneCRUD from app.api.v1.module_bre.clone.model import CloneModel from app.api.v1.module_bre.rootstock.crud import BreedingRootstockCRUD from app.api.v1.module_bre.rootstock.model import RootstockModel from app.api.v1.module_bre.site.crud import BreedingSiteCRUD from app.api.v1.module_bre.site.model import BreedingSiteModel 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.model import BreedingGermplasmModel from app.api.v1.module_bre.tree.model import TreeModel _SCION_SOURCE_TYPES = {"germplasm", "tree"} 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 PropagationService: """克隆扩繁批次 模块服务层""" def __init__(self, auth: AuthSchema, db: AsyncSession) -> None: self.auth = auth self.db = db async def _attach_fk_labels(self, items: list[PropagationOutSchema]) -> None: if not items: return clone_ids = {getattr(it, "produced_clone_id") for it in items if getattr(it, "produced_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.produced_clone_name = ref_map.get(getattr(it, "produced_clone_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")) site_ids = {getattr(it, "nursery_site_id") for it in items if getattr(it, "nursery_site_id")} if site_ids: refs = await BreedingSiteCRUD(self.auth, self.db).get_list(search={"id": ("in", list(site_ids))}) ref_map = {r.id: getattr(r, "site_name") for r in refs} for it in items: it.site_name = ref_map.get(getattr(it, "nursery_site_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 _assert_scion_source(self, source_type: str | None, source_id: int | None) -> None: """接穗来源多态校验:类型合法且对应种质/单株存在。""" if _is_blank(source_type) or _is_blank(source_id): return if source_type not in _SCION_SOURCE_TYPES: raise CustomException(msg=f"接穗来源类型不合法: {source_type}(仅支持 germplasm/tree)", status_code=409) model = BreedingGermplasmModel if source_type == "germplasm" else TreeModel await assert_parents_exist(self.db, [(model, source_id, "接穗来源")]) async def detail(self, id: int) -> PropagationOutSchema: obj = await BreedingPropagationCRUD(self.auth, self.db).get(id=id) if not obj: raise CustomException(msg="该扩繁批次不存在") out = PropagationOutSchema.model_validate(obj) await self._attach_fk_labels([out]) return out async def get_list( self, search: PropagationQueryParam | None = None, order_by: list[dict[str, str]] | None = None, ) -> list[PropagationOutSchema]: obj_list = await BreedingPropagationCRUD(self.auth, self.db).get_list( search=search_to_dict(search), order_by=order_by ) outs = [PropagationOutSchema.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: PropagationQueryParam | None = None, order_by: list[dict[str, str]] | None = None, ) -> PageResultSchema[PropagationOutSchema]: offset = (page_no - 1) * page_size result = await BreedingPropagationCRUD(self.auth, self.db).page( offset=offset, limit=page_size, order_by=order_by or [{"id": "asc"}], search=search_to_dict(search, {}), out_schema=PropagationOutSchema, ) await self._attach_fk_labels(result.items) return result async def create(self, data: PropagationCreateSchema) -> PropagationOutSchema: if _is_blank(data.batch_code): raise CustomException(msg="扩繁批次编号不能为空") await assert_parents_exist( self.db, [ (CloneModel, data.produced_clone_id, '无性系'), (RootstockModel, data.rootstock_id, '砧木'), (BreedingSiteModel, data.nursery_site_id, '试验基地'), (PersonnelModel, data.operator_id, '育种人员'), ], ) await self._assert_scion_source(data.scion_source_type, data.scion_source_id) obj = await BreedingPropagationCRUD(self.auth, self.db).create(data=data) out = PropagationOutSchema.model_validate(obj) await self._attach_fk_labels([out]) return out async def update(self, id: int, data: PropagationUpdateSchema) -> PropagationOutSchema: obj = await BreedingPropagationCRUD(self.auth, self.db).get(id=id) if not obj: raise CustomException(msg="更新失败,该扩繁批次不存在") await assert_parents_exist( self.db, [ (CloneModel, data.produced_clone_id, '无性系'), (RootstockModel, data.rootstock_id, '砧木'), (BreedingSiteModel, data.nursery_site_id, '试验基地'), (PersonnelModel, data.operator_id, '育种人员'), ], ) await self._assert_scion_source(data.scion_source_type, data.scion_source_id) obj = await BreedingPropagationCRUD(self.auth, self.db).update(id=id, data=data) out = PropagationOutSchema.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 BreedingPropagationCRUD(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 BreedingPropagationCRUD(self.auth, self.db).delete(ids=ids) async def list_options(self) -> list[dict[str, Any]]: """供前端下拉选择使用:返回 [{value, label}]。""" obj_list = await BreedingPropagationCRUD(self.auth, self.db).get_list(order_by=[{"id": "asc"}]) return [{"value": o.id, "label": o.batch_code} for o in obj_list] @staticmethod def batch_export(obj_list: list[dict[str, Any]]) -> bytes: mapping_dict = { "batch_code": "扩繁批次编号", "scion_source_type": "接穗来源类型", "scion_source_id": "接穗来源ID", "produced_clone_name": "产出无性系", "rootstock_name": "砧木", "method": "繁殖方法", "graft_date": "嫁接日期", "site_name": "育苗基地", "operator_name": "操作人", "scion_count": "接穗数量", "grafted_count": "嫁接数量", "survival_count": "成活数量", "destination": "去向", "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 = { "扩繁批次编号": "batch_code", "接穗来源类型": "scion_source_type", "接穗来源ID": "scion_source_id", "产出无性系": "produced_clone_id", "砧木": "rootstock_id", "繁殖方法": "method", "嫁接日期": "graft_date", "育苗基地": "nursery_site_id", "操作人": "operator_id", "接穗数量": "scion_count", "嫁接数量": "grafted_count", "成活数量": "survival_count", "去向": "destination", "备注": "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)}") clone_refs = await BreedingCloneCRUD(self.auth, self.db).get_list(order_by=[{"id": "asc"}]) clone_map = {getattr(r, "clone_code"): r.id for r in clone_refs} rootstock_refs = await BreedingRootstockCRUD(self.auth, self.db).get_list(order_by=[{"id": "asc"}]) rootstock_map = {getattr(r, "rootstock_name"): r.id for r in rootstock_refs} site_refs = await BreedingSiteCRUD(self.auth, self.db).get_list(order_by=[{"id": "asc"}]) site_map = {getattr(r, "site_name"): r.id for r in site_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 = BreedingPropagationCRUD(self.auth, self.db) for i, row in enumerate(rows, start=1): try: fields = { "batch_code": _none_if_blank(row.get("batch_code")), "scion_source_type": _none_if_blank(row.get("scion_source_type")), "scion_source_id": _to_int(row.get("scion_source_id")), "produced_clone_id": clone_map.get(str(row.get("produced_clone_id")).strip()) if not _is_blank(row.get("produced_clone_id")) else None, "rootstock_id": rootstock_map.get(str(row.get("rootstock_id")).strip()) if not _is_blank(row.get("rootstock_id")) else None, "method": _none_if_blank(row.get("method")), "graft_date": _none_if_blank(row.get("graft_date")), "nursery_site_id": site_map.get(str(row.get("nursery_site_id")).strip()) if not _is_blank(row.get("nursery_site_id")) else None, "operator_id": operator_map.get(str(row.get("operator_id")).strip()) if not _is_blank(row.get("operator_id")) else None, "scion_count": _to_int(row.get("scion_count")), "grafted_count": _to_int(row.get("grafted_count")), "survival_count": _to_int(row.get("survival_count")), "destination": _none_if_blank(row.get("destination")), "remark": _none_if_blank(row.get("remark")), } create_data = PropagationCreateSchema(**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 = [ "扩繁批次编号", "接穗来源类型", "接穗来源ID", "产出无性系", "砧木", "繁殖方法", "嫁接日期", "育苗基地", "操作人", "接穗数量", "嫁接数量", "成活数量", "去向", "备注", ] selector_header_list = [] option_list = [] return ExcelUtil.get_excel_template( header_list=header_list, selector_header_list=selector_header_list, option_list=option_list, )