246 lines
9.6 KiB
Python
246 lines
9.6 KiB
Python
import json
|
|
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 BreedingAnalysisDatasetCRUD
|
|
from .schema import (
|
|
AnalysisDatasetCreateSchema,
|
|
AnalysisDatasetOutSchema,
|
|
AnalysisDatasetQueryParam,
|
|
AnalysisDatasetUpdateSchema,
|
|
)
|
|
|
|
|
|
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
|
|
|
|
|
|
def _parse_str_list(v: Any) -> list[str] | None:
|
|
"""导入用:逗号分隔/数组 → 字符串列表;空返回 None。"""
|
|
if _is_blank(v):
|
|
return None
|
|
if isinstance(v, (list, tuple)):
|
|
out = [str(x).strip() for x in v if not _is_blank(x)]
|
|
return out or None
|
|
out = [s.strip() for s in str(v).split(",") if s.strip()]
|
|
return out or None
|
|
|
|
|
|
def _parse_int_list(v: Any) -> list[int] | None:
|
|
"""导入用:逗号分隔/数组 → 整数列表;空返回 None。"""
|
|
if _is_blank(v):
|
|
return None
|
|
if isinstance(v, (list, tuple)):
|
|
out = [x for x in (_to_int(i) for i in v) if x is not None]
|
|
return out or None
|
|
out = [x for x in (_to_int(s) for s in str(v).split(",") if s.strip()) if x is not None]
|
|
return out or None
|
|
|
|
|
|
def _parse_config(v: Any) -> dict | None:
|
|
"""导入用:JSON 字符串/字典 → 对象;无法解析返回 None。"""
|
|
if _is_blank(v):
|
|
return None
|
|
if isinstance(v, dict):
|
|
return v
|
|
try:
|
|
parsed = json.loads(str(v))
|
|
return parsed if isinstance(parsed, dict) else None
|
|
except (TypeError, ValueError):
|
|
return None
|
|
|
|
|
|
class AnalysisDatasetService:
|
|
"""分析数据集 模块服务层(纯元数据,无外键联表)。"""
|
|
|
|
def __init__(self, auth: AuthSchema, db: AsyncSession) -> None:
|
|
self.auth = auth
|
|
self.db = db
|
|
|
|
async def _attach_fk_labels(self, items: list[AnalysisDatasetOutSchema]) -> None:
|
|
"""本模块无外键联表字段 —— 保持签名与其它模块一致,避免导出/详情路径报错。"""
|
|
return
|
|
|
|
async def detail(self, id: int) -> AnalysisDatasetOutSchema:
|
|
obj = await BreedingAnalysisDatasetCRUD(self.auth, self.db).get(id=id)
|
|
if not obj:
|
|
raise CustomException(msg="该分析数据集不存在")
|
|
out = AnalysisDatasetOutSchema.model_validate(obj)
|
|
await self._attach_fk_labels([out])
|
|
return out
|
|
|
|
async def get_list(
|
|
self,
|
|
search: AnalysisDatasetQueryParam | None = None,
|
|
order_by: list[dict[str, str]] | None = None,
|
|
) -> list[AnalysisDatasetOutSchema]:
|
|
obj_list = await BreedingAnalysisDatasetCRUD(self.auth, self.db).get_list(
|
|
search=search_to_dict(search), order_by=order_by
|
|
)
|
|
outs = [AnalysisDatasetOutSchema.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: AnalysisDatasetQueryParam | None = None,
|
|
order_by: list[dict[str, str]] | None = None,
|
|
) -> PageResultSchema[AnalysisDatasetOutSchema]:
|
|
offset = (page_no - 1) * page_size
|
|
result = await BreedingAnalysisDatasetCRUD(self.auth, self.db).page(
|
|
offset=offset,
|
|
limit=page_size,
|
|
order_by=order_by or [{"id": "asc"}],
|
|
search=search_to_dict(search, {}),
|
|
out_schema=AnalysisDatasetOutSchema,
|
|
)
|
|
await self._attach_fk_labels(result.items)
|
|
return result
|
|
|
|
async def create(self, data: AnalysisDatasetCreateSchema) -> AnalysisDatasetOutSchema:
|
|
if _is_blank(data.name):
|
|
raise CustomException(msg="数据集名称不能为空")
|
|
obj = await BreedingAnalysisDatasetCRUD(self.auth, self.db).create(data=data)
|
|
out = AnalysisDatasetOutSchema.model_validate(obj)
|
|
await self._attach_fk_labels([out])
|
|
return out
|
|
|
|
async def update(self, id: int, data: AnalysisDatasetUpdateSchema) -> AnalysisDatasetOutSchema:
|
|
obj = await BreedingAnalysisDatasetCRUD(self.auth, self.db).get(id=id)
|
|
if not obj:
|
|
raise CustomException(msg="更新失败,该分析数据集不存在")
|
|
if data.name is not None and _is_blank(data.name):
|
|
raise CustomException(msg="数据集名称不能为空")
|
|
obj = await BreedingAnalysisDatasetCRUD(self.auth, self.db).update(id=id, data=data)
|
|
out = AnalysisDatasetOutSchema.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 BreedingAnalysisDatasetCRUD(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 BreedingAnalysisDatasetCRUD(self.auth, self.db).delete(ids=ids)
|
|
|
|
async def list_options(self) -> list[dict[str, Any]]:
|
|
"""供前端下拉选择使用:返回 [{value, label}]。"""
|
|
obj_list = await BreedingAnalysisDatasetCRUD(self.auth, self.db).get_list(order_by=[{"id": "asc"}])
|
|
return [{"value": o.id, "label": o.name} for o in obj_list]
|
|
|
|
@staticmethod
|
|
def batch_export(obj_list: list[dict[str, Any]]) -> bytes:
|
|
mapping_dict = {
|
|
"name": "数据集名称",
|
|
"description": "数据集描述",
|
|
"trait_codes": "性状编码",
|
|
"sample_ids": "样本ID",
|
|
"config": "分析配置",
|
|
"status": "状态",
|
|
"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 "未知"
|
|
if isinstance(item.get("trait_codes"), (list, tuple)):
|
|
item["trait_codes"] = ",".join(str(x) for x in item["trait_codes"])
|
|
if isinstance(item.get("sample_ids"), (list, tuple)):
|
|
item["sample_ids"] = ",".join(str(x) for x in item["sample_ids"])
|
|
if isinstance(item.get("config"), (dict, list)):
|
|
item["config"] = json.dumps(item["config"], ensure_ascii=False)
|
|
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 = {
|
|
"数据集名称": "name",
|
|
"数据集描述": "description",
|
|
"性状编码": "trait_codes",
|
|
"样本ID": "sample_ids",
|
|
"分析配置": "config",
|
|
"状态": "status",
|
|
}
|
|
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)}")
|
|
error_msgs: list[str] = []
|
|
success_count = 0
|
|
crud = BreedingAnalysisDatasetCRUD(self.auth, self.db)
|
|
for i, row in enumerate(rows, start=1):
|
|
try:
|
|
fields = {
|
|
"name": _none_if_blank(row.get("name")),
|
|
"description": _none_if_blank(row.get("description")),
|
|
"trait_codes": _parse_str_list(row.get("trait_codes")),
|
|
"sample_ids": _parse_int_list(row.get("sample_ids")),
|
|
"config": _parse_config(row.get("config")),
|
|
"status": _none_if_blank(row.get("status")) or "draft",
|
|
}
|
|
create_data = AnalysisDatasetCreateSchema(**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,
|
|
)
|