脐带间充质干细胞填写/查看储存证质检时不展示、不预填、不落库 HBV DNA / HCV RNA / HIV-1RNA;围产期造血干细胞保持原字段。 Co-authored-by: Cursor <cursoragent@cursor.com>
100 lines
2.8 KiB
Python
100 lines
2.8 KiB
Python
"""样本储存证质检信息 — 类型键与 payload 工具。"""
|
|
from __future__ import annotations
|
|
|
|
import json
|
|
from typing import Any, Dict, Optional
|
|
|
|
SAMPLE_TYPE_NAME_PERINATAL_HSC = "围产期造血干细胞"
|
|
SAMPLE_TYPE_NAME_UC_MSC = "脐带间充质干细胞"
|
|
SAMPLE_TYPE_KEY_PERINATAL_HSC = "perinatal_hsc"
|
|
SAMPLE_TYPE_KEY_UC_MSC = "uc_msc"
|
|
|
|
_NAME_TO_KEY = {
|
|
SAMPLE_TYPE_NAME_PERINATAL_HSC: SAMPLE_TYPE_KEY_PERINATAL_HSC,
|
|
SAMPLE_TYPE_NAME_UC_MSC: SAMPLE_TYPE_KEY_UC_MSC,
|
|
}
|
|
|
|
|
|
def resolve_sample_type_key(sample_type_name: Optional[str]) -> Optional[str]:
|
|
name = (sample_type_name or "").strip()
|
|
return _NAME_TO_KEY.get(name)
|
|
|
|
|
|
def supports_storage_cert_qc(sample_type_name: Optional[str]) -> bool:
|
|
return resolve_sample_type_key(sample_type_name) is not None
|
|
|
|
|
|
# 脐带间充质储存证不收录母亲血液三项核酸检测。
|
|
UC_MSC_EXCLUDED_PAYLOAD_KEYS = frozenset(
|
|
{
|
|
"mother_hbv_dna",
|
|
"mother_hcv_rna",
|
|
"mother_hiv1_rna",
|
|
}
|
|
)
|
|
|
|
|
|
def normalize_payload(payload: Any) -> Dict[str, Any]:
|
|
if not isinstance(payload, dict):
|
|
raise ValueError("payload 必须为对象")
|
|
out: Dict[str, Any] = {}
|
|
for k, v in payload.items():
|
|
if not isinstance(k, str):
|
|
continue
|
|
key = k.strip()
|
|
if not key:
|
|
continue
|
|
out[key] = v
|
|
return out
|
|
|
|
|
|
def sanitize_payload_for_type(
|
|
payload: Dict[str, Any],
|
|
sample_type_key: Optional[str],
|
|
) -> Dict[str, Any]:
|
|
"""按样本类型去掉不应展示/落库的键。"""
|
|
if sample_type_key != SAMPLE_TYPE_KEY_UC_MSC:
|
|
return payload
|
|
return {key: value for key, value in payload.items() if key not in UC_MSC_EXCLUDED_PAYLOAD_KEYS}
|
|
|
|
|
|
def dumps_payload(payload: Dict[str, Any]) -> str:
|
|
return json.dumps(payload, ensure_ascii=False, separators=(",", ":"))
|
|
|
|
|
|
def loads_payload(raw: Optional[str]) -> Dict[str, Any]:
|
|
if not raw:
|
|
return {}
|
|
try:
|
|
data = json.loads(raw)
|
|
except (TypeError, ValueError):
|
|
return {}
|
|
if not isinstance(data, dict):
|
|
return {}
|
|
return data
|
|
|
|
|
|
def empty_detail(
|
|
*,
|
|
sample_code: str,
|
|
lab_flow_code: str = "",
|
|
protocol_code: str = "",
|
|
mother_name: str = "",
|
|
sample_type_name: str = "",
|
|
sample_type_key: Optional[str] = None,
|
|
collection_protocol_id: str = "",
|
|
qc_work_order_id: str = "",
|
|
) -> Dict[str, Any]:
|
|
return {
|
|
"sampleCode": sample_code,
|
|
"labFlowCode": lab_flow_code or "",
|
|
"protocolCode": protocol_code or "",
|
|
"motherName": mother_name or "",
|
|
"sampleTypeName": sample_type_name or "",
|
|
"sampleTypeKey": sample_type_key or resolve_sample_type_key(sample_type_name),
|
|
"collectionProtocolId": collection_protocol_id or "",
|
|
"qcWorkOrderId": qc_work_order_id or "",
|
|
"filledFlag": False,
|
|
"payload": {},
|
|
}
|