sample_storage_cert_qc.py 2.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. """样本储存证质检信息 — 类型键与 payload 工具。"""
  2. from __future__ import annotations
  3. import json
  4. from typing import Any, Dict, Optional
  5. SAMPLE_TYPE_NAME_PERINATAL_HSC = "围产期造血干细胞"
  6. SAMPLE_TYPE_NAME_UC_MSC = "脐带间充质干细胞"
  7. SAMPLE_TYPE_KEY_PERINATAL_HSC = "perinatal_hsc"
  8. SAMPLE_TYPE_KEY_UC_MSC = "uc_msc"
  9. _NAME_TO_KEY = {
  10. SAMPLE_TYPE_NAME_PERINATAL_HSC: SAMPLE_TYPE_KEY_PERINATAL_HSC,
  11. SAMPLE_TYPE_NAME_UC_MSC: SAMPLE_TYPE_KEY_UC_MSC,
  12. }
  13. def resolve_sample_type_key(sample_type_name: Optional[str]) -> Optional[str]:
  14. name = (sample_type_name or "").strip()
  15. return _NAME_TO_KEY.get(name)
  16. def supports_storage_cert_qc(sample_type_name: Optional[str]) -> bool:
  17. return resolve_sample_type_key(sample_type_name) is not None
  18. def normalize_payload(payload: Any) -> Dict[str, Any]:
  19. if not isinstance(payload, dict):
  20. raise ValueError("payload 必须为对象")
  21. out: Dict[str, Any] = {}
  22. for k, v in payload.items():
  23. if not isinstance(k, str):
  24. continue
  25. key = k.strip()
  26. if not key:
  27. continue
  28. out[key] = v
  29. return out
  30. def dumps_payload(payload: Dict[str, Any]) -> str:
  31. return json.dumps(payload, ensure_ascii=False, separators=(",", ":"))
  32. def loads_payload(raw: Optional[str]) -> Dict[str, Any]:
  33. if not raw:
  34. return {}
  35. try:
  36. data = json.loads(raw)
  37. except (TypeError, ValueError):
  38. return {}
  39. if not isinstance(data, dict):
  40. return {}
  41. return data
  42. def empty_detail(
  43. *,
  44. sample_code: str,
  45. lab_flow_code: str = "",
  46. protocol_code: str = "",
  47. mother_name: str = "",
  48. sample_type_name: str = "",
  49. sample_type_key: Optional[str] = None,
  50. collection_protocol_id: str = "",
  51. qc_work_order_id: str = "",
  52. ) -> Dict[str, Any]:
  53. return {
  54. "sampleCode": sample_code,
  55. "labFlowCode": lab_flow_code or "",
  56. "protocolCode": protocol_code or "",
  57. "motherName": mother_name or "",
  58. "sampleTypeName": sample_type_name or "",
  59. "sampleTypeKey": sample_type_key or resolve_sample_type_key(sample_type_name),
  60. "collectionProtocolId": collection_protocol_id or "",
  61. "qcWorkOrderId": qc_work_order_id or "",
  62. "filledFlag": False,
  63. "payload": {},
  64. }