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 BreedingTraitCRUD from .schema import ( TraitCreateSchema, TraitOutSchema, TraitQueryParam, TraitUpdateSchema, ) from app.core.base_crud import assert_no_children, assert_parents_exist from app.api.v1.module_bre.statistics.model import CombiningAbilityModel from app.api.v1.module_bre.statistics.model import PredictionModel from app.api.v1.module_bre.trait_observation.model import TraitObservationModel 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 TraitService: """性状字典 模块服务层""" def __init__(self, auth: AuthSchema, db: AsyncSession) -> None: self.auth = auth self.db = db async def _attach_fk_labels(self, items: list[TraitOutSchema]) -> None: if not items: return crud = BreedingTraitCRUD(self.auth, self.db) async def detail(self, id: int) -> TraitOutSchema: obj = await BreedingTraitCRUD(self.auth, self.db).get(id=id) if not obj: raise CustomException(msg="该性状字典不存在") out = TraitOutSchema.model_validate(obj) await self._attach_fk_labels([out]) return out async def get_list( self, search: TraitQueryParam | None = None, order_by: list[dict[str, str]] | None = None, ) -> list[TraitOutSchema]: obj_list = await BreedingTraitCRUD(self.auth, self.db).get_list( search=search_to_dict(search), order_by=order_by ) outs = [TraitOutSchema.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: TraitQueryParam | None = None, order_by: list[dict[str, str]] | None = None, ) -> PageResultSchema[TraitOutSchema]: offset = (page_no - 1) * page_size result = await BreedingTraitCRUD(self.auth, self.db).page( offset=offset, limit=page_size, order_by=order_by or [{"id": "asc"}], search=search_to_dict(search, {}), out_schema=TraitOutSchema, ) await self._attach_fk_labels(result.items) return result async def create(self, data: TraitCreateSchema) -> TraitOutSchema: exist_obj = await BreedingTraitCRUD(self.auth, self.db).get(trait_code=data.trait_code) if exist_obj: raise CustomException(msg="创建失败,性状编码已存在") obj = await BreedingTraitCRUD(self.auth, self.db).create(data=data) out = TraitOutSchema.model_validate(obj) return out async def update(self, id: int, data: TraitUpdateSchema) -> TraitOutSchema: obj = await BreedingTraitCRUD(self.auth, self.db).get(id=id) if not obj: raise CustomException(msg="更新失败,该性状字典不存在") if data.trait_code is not None: exist_obj = await BreedingTraitCRUD(self.auth, self.db).get(trait_code=data.trait_code) if exist_obj and exist_obj.id != id: raise CustomException(msg="更新失败,性状编码重复") obj = await BreedingTraitCRUD(self.auth, self.db).update(id=id, data=data) out = TraitOutSchema.model_validate(obj) return out async def delete(self, ids: list[int]) -> None: if not ids: raise CustomException(msg="删除失败,删除对象不能为空") objs = await BreedingTraitCRUD(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, [ (TraitObservationModel, 'trait_id', '性状观测'), (PredictionModel, 'trait_id', '育种值预测'), (CombiningAbilityModel, 'trait_id', '配合力分析'), ], ) await BreedingTraitCRUD(self.auth, self.db).delete(ids=ids) async def list_options(self) -> list[dict[str, Any]]: """供前端下拉选择使用:返回 [{value, label}]。""" obj_list = await BreedingTraitCRUD(self.auth, self.db).get_list(order_by=[{"id": "asc"}]) return [{"value": o.id, "label": o.trait_code} for o in obj_list] @staticmethod def batch_export(obj_list: list[dict[str, Any]]) -> bytes: mapping_dict = { "trait_code": "性状编码", "trait_name": "性状名称", "category": "性状类别", "data_type": "数据类型", "unit": "单位", "is_core": "是否核心性状", "scale_json": "量规/分级JSON", "valid_min": "有效最小值", "valid_max": "有效最大值", "ontology_uri": "本体IRI", "stage": "测定阶段", "method": "测定方法", "method_uri": "测定方法URI", "direction": "性状方向(desc越大/asc越小)", "into_ebv": "是否选种目标", "default_h2": "先验遗传力h²", "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 = { "性状编码": "trait_code", "性状名称": "trait_name", "性状类别": "category", "数据类型": "data_type", "单位": "unit", "是否核心性状": "is_core", "量规/分级JSON": "scale_json", "有效最小值": "valid_min", "有效最大值": "valid_max", "本体IRI": "ontology_uri", "测定阶段": "stage", "测定方法": "method", "测定方法URI": "method_uri", "性状方向": "direction", "是否选种目标": "into_ebv", "先验遗传力h²": "default_h2", "备注": "remark", } # 新列向后兼容:旧模板缺失新列时用默认值,仅校验历史必填列 required_headers = [ "性状编码", "性状名称", "性状类别", "数据类型", "单位", "是否核心性状", "量规/分级JSON", "有效最小值", "有效最大值", "本体IRI", "测定阶段", "测定方法", "测定方法URI", "备注", ] 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 required_headers 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 = ["trait_code", "trait_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 = BreedingTraitCRUD(self.auth, self.db) for i, row in enumerate(mapped_rows, start=1): try: fields = { "trait_code": _none_if_blank(row.get("trait_code")), "trait_name": _none_if_blank(row.get("trait_name")), "category": _none_if_blank(row.get("category")), "data_type": _none_if_blank(row.get("data_type")), "unit": _none_if_blank(row.get("unit")), "is_core": _none_if_blank(row.get("is_core")), "scale_json": _none_if_blank(row.get("scale_json")), "valid_min": _to_float(row.get("valid_min")), "valid_max": _to_float(row.get("valid_max")), "ontology_uri": _none_if_blank(row.get("ontology_uri")), "stage": _none_if_blank(row.get("stage")) or "evaluation", "method": _none_if_blank(row.get("method")), "method_uri": _none_if_blank(row.get("method_uri")), "direction": _none_if_blank(row.get("direction")) or "desc", "into_ebv": _none_if_blank(row.get("into_ebv")) or "1", "default_h2": _to_float(row.get("default_h2")), "remark": _none_if_blank(row.get("remark")), } unique_kwargs = {"trait_code": fields["trait_code"]} create_data = TraitCreateSchema(**fields) exist_obj = await crud.get(**unique_kwargs) if exist_obj: if update_support: await crud.update(id=exist_obj.id, data=TraitUpdateSchema(**fields)) success_count += 1 else: error_msgs.append(f"第{i}行: 性状编码 {fields['trait_code']} 已存在") 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 = [ "性状编码", "性状名称", "性状类别", "数据类型", "单位", "是否核心性状", "量规/分级JSON", "有效最小值", "有效最大值", "本体IRI", "测定阶段", "测定方法", "测定方法URI", "性状方向", "是否选种目标", "先验遗传力h²", "备注", ] selector_header_list = ["数据类型", "是否核心性状", "测定阶段", "性状方向", "是否选种目标"] option_list = [ {"数据类型": ["numeric", "categorical"]}, {"是否核心性状": ["1", "0"]}, {"测定阶段": ["juvenile", "evaluation"]}, {"性状方向": ["desc", "asc"]}, {"是否选种目标": ["1", "0"]}, ] return ExcelUtil.get_excel_template( header_list=header_list, selector_header_list=selector_header_list, option_list=option_list, )