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 .crud import BreedingGermplasmCRUD from .model import BreedingGermplasmModel from .schema import ( BreedingGermplasmCreateSchema, BreedingGermplasmOutSchema, BreedingGermplasmQueryParam, BreedingGermplasmUpdateSchema, ) from app.core.base_crud import assert_dict_values, assert_no_children, assert_parents_exist from app.api.v1.module_bre.cross_combination.model import CrossCombinationModel from app.api.v1.module_bre.statistics.model import PredictionValueModel from app.api.v1.module_bre.trial_study_entry.model import TrialStudyEntryModel 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 def _to_bool(v: Any) -> bool: if _is_blank(v): return False return str(v).strip().lower() in ("是", "true", "1", "yes", "y") class BreedingGermplasmService: """亲本资源管理模块服务层""" def __init__(self, auth: AuthSchema, db: AsyncSession) -> None: self.auth = auth self.db = db async def _attach_fk_labels(self, items: list[BreedingGermplasmOutSchema]) -> None: if not items: return rootstock_id_ids = {getattr(it, "rootstock_id") for it in items if getattr(it, "rootstock_id")} if rootstock_id_ids: refs = await BreedingGermplasmCRUD(self.auth, self.db).get_list(search={"id": ("in", list(rootstock_id_ids))}) ref_map = {r.id: getattr(r, "cultivar_name") for r in refs} for it in items: it.rootstock_name = ref_map.get(getattr(it, "rootstock_id")) async def detail(self, id: int) -> BreedingGermplasmOutSchema: obj = await BreedingGermplasmCRUD(self.auth, self.db).get(id=id) if not obj: raise CustomException(msg="该亲本资源不存在") out = BreedingGermplasmOutSchema.model_validate(obj) await self._attach_fk_labels([out]) return out async def get_list( self, search: BreedingGermplasmQueryParam | None = None, order_by: list[dict[str, str]] | None = None, ) -> list[BreedingGermplasmOutSchema]: obj_list = await BreedingGermplasmCRUD(self.auth, self.db).get_list( search=search_to_dict(search), order_by=order_by ) outs = [BreedingGermplasmOutSchema.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: BreedingGermplasmQueryParam | None = None, order_by: list[dict[str, str]] | None = None, ) -> PageResultSchema[BreedingGermplasmOutSchema]: offset = (page_no - 1) * page_size result = await BreedingGermplasmCRUD(self.auth, self.db).page( offset=offset, limit=page_size, order_by=order_by or [{"id": "asc"}], search=search_to_dict(search, {}), out_schema=BreedingGermplasmOutSchema, ) await self._attach_fk_labels(result.items) return result async def create(self, data: BreedingGermplasmCreateSchema) -> BreedingGermplasmOutSchema: exist_obj = await BreedingGermplasmCRUD(self.auth, self.db).get(cultivar_name=data.cultivar_name) if exist_obj: raise CustomException(msg="创建失败,品种/种质名称已存在") if data.accession_no: exist_acc = await BreedingGermplasmCRUD(self.auth, self.db).get(accession_no=data.accession_no) if exist_acc: raise CustomException(msg="创建失败,登记号已存在") await assert_dict_values( self.db, [ ('peach_variety_type', data.variety_type, '变种类型'), ('maturity_period', data.maturity_period, '成熟期'), ('firmness', data.firmness, '硬度'), ('flowering_period', data.flowering_period, '花期'), ('breeding_stage', data.stage, '育种阶段'), ('breeding_generation', data.generation, '世代'), ('storage_type', data.storage_type, '保存方式'), ('biological_status', data.biological_status, '生物学状态'), ], ) await assert_parents_exist(self.db, [(BreedingGermplasmModel, data.rootstock_id, '砧木材料')]) obj = await BreedingGermplasmCRUD(self.auth, self.db).create(data=data) out = BreedingGermplasmOutSchema.model_validate(obj) await self._attach_fk_labels([out]) return out async def update(self, id: int, data: BreedingGermplasmUpdateSchema) -> BreedingGermplasmOutSchema: obj = await BreedingGermplasmCRUD(self.auth, self.db).get(id=id) if not obj: raise CustomException(msg="更新失败,该亲本资源不存在") if data.cultivar_name is not None: exist_obj = await BreedingGermplasmCRUD(self.auth, self.db).get(cultivar_name=data.cultivar_name) if exist_obj and exist_obj.id != id: raise CustomException(msg="更新失败,品种/种质名称重复") if data.accession_no: exist_acc = await BreedingGermplasmCRUD(self.auth, self.db).get(accession_no=data.accession_no) if exist_acc and exist_acc.id != id: raise CustomException(msg="更新失败,登记号重复") if data.rootstock_id == id: raise CustomException(msg="更新失败,砧木材料不能指向自身") await assert_dict_values( self.db, [ ('peach_variety_type', data.variety_type, '变种类型'), ('maturity_period', data.maturity_period, '成熟期'), ('firmness', data.firmness, '硬度'), ('flowering_period', data.flowering_period, '花期'), ('breeding_stage', data.stage, '育种阶段'), ('breeding_generation', data.generation, '世代'), ('storage_type', data.storage_type, '保存方式'), ('biological_status', data.biological_status, '生物学状态'), ], ) await assert_parents_exist(self.db, [(BreedingGermplasmModel, data.rootstock_id, '砧木材料')]) obj = await BreedingGermplasmCRUD(self.auth, self.db).update(id=id, data=data) out = BreedingGermplasmOutSchema.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 BreedingGermplasmCRUD(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, [ (CrossCombinationModel, 'female_parent_id', '作为母本的杂交组合'), (CrossCombinationModel, 'male_parent_id', '作为父本的杂交组合'), (TrialStudyEntryModel, 'germplasm_id', '试验参试'), (PredictionValueModel, 'germplasm_id', '单株预测值'), ], ) await BreedingGermplasmCRUD(self.auth, self.db).delete(ids=ids) async def list_options(self, can_be_female: bool | None = None, can_be_male: bool | None = None) -> list[dict[str, Any]]: """供前端下拉选择使用:返回 [{value, label}];可按可作母本/父本过滤。""" obj_list = await BreedingGermplasmCRUD(self.auth, self.db).get_list(order_by=[{"id": "asc"}]) options = [] for o in obj_list: if can_be_female is not None and getattr(o, "can_be_female") != can_be_female: continue if can_be_male is not None and getattr(o, "can_be_male") != can_be_male: continue options.append({"value": o.id, "label": o.cultivar_name}) return options @staticmethod def batch_export(obj_list: list[dict[str, Any]]) -> bytes: mapping_dict = { "id": "编号", "cultivar_name": "品种/种质名称", "variety_type": "变种类型", "origin": "来源", "preservation_site": "保存地", "avg_fruit_weight": "平均果重(g)", "ssc": "可溶性固形物(%)", "firmness": "硬度", "maturity_period": "成熟期", "flowering_period": "花期", "bloom_start_date": "花期起始", "bloom_end_date": "花期结束", "chilling_requirement": "需冷量(h)", "disease_resistance": "抗病性描述", "can_be_female": "可作母本", "can_be_male": "可作父本", "pedigree_note": "系谱备注", "photo_path": "照片地址", "accession_no": "登记号", "stage": "育种阶段", "generation": "世代", "storage_type": "保存方式", "is_rootstock": "是否砧木材料", "rootstock_name": "砧木材料", "institute_code": "保存机构代码", "country_origin": "原产国/原产地", "collection_site": "采集地点", "acquisition_date": "引种/收集日期", "biological_status": "生物学状态", "breeding_program": "所属育种项目", "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["can_be_female"] = "是" if item.get("can_be_female") else "否" item["can_be_male"] = "是" if item.get("can_be_male") else "否" item["is_rootstock"] = "是" if item.get("is_rootstock") else "否" item["variety_type"] = dict_value_to_label("peach_variety_type", item.get("variety_type")) item["firmness"] = dict_value_to_label("firmness", item.get("firmness")) item["maturity_period"] = dict_value_to_label("maturity_period", item.get("maturity_period")) item["flowering_period"] = dict_value_to_label("flowering_period", item.get("flowering_period")) item["stage"] = dict_value_to_label("breeding_stage", item.get("stage")) item["generation"] = dict_value_to_label("breeding_generation", item.get("generation")) item["storage_type"] = dict_value_to_label("storage_type", item.get("storage_type")) item["biological_status"] = dict_value_to_label("biological_status", item.get("biological_status")) 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 = { "品种/种质名称": "cultivar_name", "变种类型": "variety_type", "来源": "origin", "保存地": "preservation_site", "平均果重(g)": "avg_fruit_weight", "可溶性固形物(%)": "ssc", "硬度": "firmness", "成熟期": "maturity_period", "花期": "flowering_period", "花期起始": "bloom_start_date", "花期结束": "bloom_end_date", "需冷量(h)": "chilling_requirement", "抗病性描述": "disease_resistance", "可作母本": "can_be_female", "可作父本": "can_be_male", "系谱备注": "pedigree_note", "照片地址": "photo_path", "登记号": "accession_no", "育种阶段": "stage", "世代": "generation", "保存方式": "storage_type", "是否砧木材料": "is_rootstock", "砧木材料": "rootstock_id", "保存机构代码": "institute_code", "原产国/原产地": "country_origin", "采集地点": "collection_site", "引种/收集日期": "acquisition_date", "生物学状态": "biological_status", "所属育种项目": "breeding_program", } 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)}") mapped_rows = [] for row in rows: mapped_rows.append({en: row.get(ch) for ch, en in header_dict.items()}) required_fields = ["cultivar_name"] 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 = BreedingGermplasmCRUD(self.auth, self.db) resolver = DictLabelResolver( self.auth, self.db, ["peach_variety_type", "maturity_period", "firmness", "flowering_period", "breeding_stage", "breeding_generation", "storage_type", "biological_status"], ) germplasm_refs = await crud.get_list(order_by=[{"id": "asc"}]) rootstock_id_map = {getattr(r, "cultivar_name"): r.id for r in germplasm_refs} for i, row in enumerate(mapped_rows, start=1): try: fields = { "cultivar_name": str(row["cultivar_name"]).strip(), "variety_type": await resolver.resolve("peach_variety_type", row.get("variety_type")), "origin": _none_if_blank(row.get("origin")), "preservation_site": _none_if_blank(row.get("preservation_site")), "avg_fruit_weight": _to_float(row.get("avg_fruit_weight")), "ssc": _to_float(row.get("ssc")), "firmness": await resolver.resolve("firmness", row.get("firmness")), "maturity_period": await resolver.resolve("maturity_period", row.get("maturity_period")), "flowering_period": await resolver.resolve("flowering_period", row.get("flowering_period")), "bloom_start_date": _none_if_blank(row.get("bloom_start_date")), "bloom_end_date": _none_if_blank(row.get("bloom_end_date")), "chilling_requirement": _to_int(row.get("chilling_requirement")), "disease_resistance": _none_if_blank(row.get("disease_resistance")), "can_be_female": _to_bool(row.get("can_be_female")), "can_be_male": _to_bool(row.get("can_be_male")), "pedigree_note": _none_if_blank(row.get("pedigree_note")), "photo_path": _none_if_blank(row.get("photo_path")), "accession_no": _none_if_blank(row.get("accession_no")), "stage": await resolver.resolve("breeding_stage", row.get("stage")), "generation": await resolver.resolve("breeding_generation", row.get("generation")), "storage_type": await resolver.resolve("storage_type", row.get("storage_type")), "is_rootstock": _to_bool(row.get("is_rootstock")), "rootstock_id": ( rootstock_id_map.get(str(row.get("rootstock_id")).strip()) if not _is_blank(row.get("rootstock_id")) else None ), "institute_code": _none_if_blank(row.get("institute_code")), "country_origin": _none_if_blank(row.get("country_origin")), "collection_site": _none_if_blank(row.get("collection_site")), "acquisition_date": _none_if_blank(row.get("acquisition_date")), "biological_status": await resolver.resolve("biological_status", row.get("biological_status")), "breeding_program": _none_if_blank(row.get("breeding_program")), } create_data = BreedingGermplasmCreateSchema(**fields) exist_obj = await crud.get(cultivar_name=create_data.cultivar_name) if exist_obj: if update_support: await crud.update(id=exist_obj.id, data=BreedingGermplasmUpdateSchema(**fields)) success_count += 1 else: error_msgs.append(f"第{i}行: 品种/种质名称 {create_data.cultivar_name} 已存在") 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 = [ "品种/种质名称", "变种类型", "来源", "保存地", "平均果重(g)", "可溶性固形物(%)", "硬度", "成熟期", "花期", "花期起始", "花期结束", "需冷量(h)", "抗病性描述", "可作母本", "可作父本", "系谱备注", "照片地址", "登记号", "育种阶段", "世代", "保存方式", "是否砧木材料", "砧木材料", "保存机构代码", "原产国/原产地", "采集地点", "引种/收集日期", "生物学状态", "所属育种项目", ] selector_header_list = ["可作母本", "可作父本", "是否砧木材料"] option_list = [ {"可作母本": ["是", "否"]}, {"可作父本": ["是", "否"]}, {"是否砧木材料": ["是", "否"]}, ] return ExcelUtil.get_excel_template( header_list=header_list, selector_header_list=selector_header_list, option_list=option_list, )