diff --git a/backend/app/crud/sample_storage_cert_qc.py b/backend/app/crud/sample_storage_cert_qc.py index 915a8ce8..3e63bdca 100644 --- a/backend/app/crud/sample_storage_cert_qc.py +++ b/backend/app/crud/sample_storage_cert_qc.py @@ -21,6 +21,7 @@ from app.services.sample_storage_cert_qc import ( loads_payload, normalize_payload, resolve_sample_type_key, + sanitize_payload_for_type, ) from app.services.sample_storage_cert_qc_prefill import collect_prefill @@ -44,7 +45,7 @@ def _row_to_dict( "collectionProtocolId": row.collection_protocol_id or "", "qcWorkOrderId": row.qc_work_order_id or "", "filledFlag": str(row.filled_flag or "0") == "1", - "payload": loads_payload(row.payload), + "payload": sanitize_payload_for_type(loads_payload(row.payload), row.sample_type_key), "createTime": row.create_time.isoformat(sep=" ", timespec="seconds") if row.create_time else "", "updateTime": row.update_time.isoformat(sep=" ", timespec="seconds") if row.update_time else "", } @@ -173,7 +174,7 @@ def upsert( key = resolve_sample_type_key(type_name) if not key: raise ValueError("该样本类型不支持填写储存证质检信息") - clean = normalize_payload(payload) + clean = sanitize_payload_for_type(normalize_payload(payload), key) now = datetime.now() row = ( db.query(BizSampleStorageCertQc) diff --git a/backend/app/services/sample_storage_cert_qc.py b/backend/app/services/sample_storage_cert_qc.py index 11414710..ecb106ab 100644 --- a/backend/app/services/sample_storage_cert_qc.py +++ b/backend/app/services/sample_storage_cert_qc.py @@ -24,6 +24,16 @@ 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 必须为对象") @@ -38,6 +48,16 @@ def normalize_payload(payload: Any) -> Dict[str, Any]: 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=(",", ":")) diff --git a/backend/app/services/sample_storage_cert_qc_prefill.py b/backend/app/services/sample_storage_cert_qc_prefill.py index dc222d39..8cd71c45 100644 --- a/backend/app/services/sample_storage_cert_qc_prefill.py +++ b/backend/app/services/sample_storage_cert_qc_prefill.py @@ -168,6 +168,11 @@ _HSC_SEROLOGY_RULES = ( ) _MOTHER_SEROLOGY_RULES = tuple(rule for rule in _HSC_SEROLOGY_RULES if str(rule[0]).startswith("mother_")) +# 脐带间充质不预填母亲血液三项核酸。 +_UC_MSC_EXCLUDED_MOTHER_KEYS = frozenset({"mother_hbv_dna", "mother_hcv_rna", "mother_hiv1_rna"}) +_MOTHER_SEROLOGY_RULES_UC_MSC = tuple( + rule for rule in _MOTHER_SEROLOGY_RULES if str(rule[0]) not in _UC_MSC_EXCLUDED_MOTHER_KEYS +) def _serology_from_tasks( @@ -278,9 +283,19 @@ def _process_qual_from_tasks(tasks: List[Dict[str, Any]]) -> Dict[str, str]: return out -def _fill_shared_mother(suggested: Dict[str, str], tasks: Optional[List[Dict[str, Any]]]) -> None: +def _fill_shared_mother( + suggested: Dict[str, str], + tasks: Optional[List[Dict[str, Any]]], + *, + sample_type_key: str, +) -> None: """只补母亲血液空项。共享任务里的脐血不参与。""" - shared = _serology_from_tasks(tasks or [], _MOTHER_SEROLOGY_RULES) + rules = ( + _MOTHER_SEROLOGY_RULES_UC_MSC + if sample_type_key == "uc_msc" + else _MOTHER_SEROLOGY_RULES + ) + shared = _serology_from_tasks(tasks or [], rules) for key, value in shared.items(): suggested.setdefault(key, value) @@ -330,7 +345,7 @@ def build_suggested( if count: suggested["available_cell_count_1e7"] = count suggested.update(_process_qual_from_tasks(process_tasks or [])) - suggested.update(_serology_from_tasks(gate_tasks or [], _MOTHER_SEROLOGY_RULES)) + suggested.update(_serology_from_tasks(gate_tasks or [], _MOTHER_SEROLOGY_RULES_UC_MSC)) suggested.setdefault("bacteria_test", "阴性") suggested.setdefault("result_judgment", "合格") if freeze: @@ -338,7 +353,7 @@ def build_suggested( suggested.update(_FIXED_COMMON) suggested.update(_FIXED_UC_MSC) if sample_type_key in ("perinatal_hsc", "uc_msc"): - _fill_shared_mother(suggested, shared_mother_tasks) + _fill_shared_mother(suggested, shared_mother_tasks, sample_type_key=sample_type_key) return {key: value for key, value in suggested.items() if str(value).strip()} diff --git a/backend/tests/test_sample_storage_cert_qc.py b/backend/tests/test_sample_storage_cert_qc.py index 501c26b5..9bde6dfd 100644 --- a/backend/tests/test_sample_storage_cert_qc.py +++ b/backend/tests/test_sample_storage_cert_qc.py @@ -10,6 +10,7 @@ from app.services.sample_storage_cert_qc import ( empty_detail, normalize_payload, resolve_sample_type_key, + sanitize_payload_for_type, supports_storage_cert_qc, ) from app.services.sample_storage_cert_qc_prefill import ( @@ -51,6 +52,34 @@ class TestNormalizePayload(unittest.TestCase): self.assertNotIn(1, out) +class TestSanitizePayloadForType(unittest.TestCase): + def test_uc_msc_drops_mother_nucleic_acids(self): + out = sanitize_payload_for_type( + { + "mother_hbsag": "阴性", + "mother_hbv_dna": "阴性", + "mother_hcv_rna": "阳性", + "mother_hiv1_rna": "阴性", + "mother_cmv_igm": "阴性", + }, + SAMPLE_TYPE_KEY_UC_MSC, + ) + self.assertEqual(out["mother_hbsag"], "阴性") + self.assertEqual(out["mother_cmv_igm"], "阴性") + self.assertNotIn("mother_hbv_dna", out) + self.assertNotIn("mother_hcv_rna", out) + self.assertNotIn("mother_hiv1_rna", out) + + def test_perinatal_keeps_mother_nucleic_acids(self): + payload = { + "mother_hbv_dna": "阴性", + "mother_hcv_rna": "阴性", + "mother_hiv1_rna": "阴性", + } + out = sanitize_payload_for_type(payload, SAMPLE_TYPE_KEY_PERINATAL_HSC) + self.assertEqual(out, payload) + + class TestEmptyDetail(unittest.TestCase): def test_shape(self): d = empty_detail( @@ -302,13 +331,13 @@ class TestStorageCertPrefill(unittest.TestCase): ] suggested = build_suggested(sample_type_key="uc_msc", shared_mother_tasks=shared) self.assertEqual(suggested["mother_hbsag"], "阴性") - self.assertEqual(suggested["mother_hbv_dna"], "阴性") + self.assertNotIn("mother_hbv_dna", suggested) self.assertEqual(suggested["mother_anti_hcv"], "阴性") - self.assertEqual(suggested["mother_hcv_rna"], "阴性") + self.assertNotIn("mother_hcv_rna", suggested) self.assertEqual(suggested["mother_anti_tp"], "阴性") self.assertEqual(suggested["mother_cmv_igm"], "阴性") self.assertEqual(suggested["mother_anti_hiv"], "阴性") - self.assertEqual(suggested["mother_hiv1_rna"], "阴性") + self.assertNotIn("mother_hiv1_rna", suggested) self.assertNotIn("hbsag", suggested) def test_own_mother_blood_wins_and_shared_only_fills_gaps(self): diff --git a/web-admin/src/utils/storage-cert-qc-fields.ts b/web-admin/src/utils/storage-cert-qc-fields.ts index fd56bc53..08109f77 100644 --- a/web-admin/src/utils/storage-cert-qc-fields.ts +++ b/web-admin/src/utils/storage-cert-qc-fields.ts @@ -33,7 +33,8 @@ export function supportsStorageCertQc(sampleTypeName?: string | null): boolean { return resolveStorageCertQcTypeKey(sampleTypeName) != null } -const MOTHER_BLOOD: StorageCertQcField[] = [ +/** 围产期造血:母亲血液含三项核酸检测。 */ +const MOTHER_BLOOD_PERINATAL: StorageCertQcField[] = [ { key: 'mother_hbsag', label: '乙型肝炎病毒表面抗原(HBsAg)', control: 'select_yny' }, { key: 'mother_hbv_dna', label: '乙型肝炎病毒核酸(HBV DNA)', control: 'select_yny' }, { key: 'mother_anti_hcv', label: '丙型肝炎病毒抗体(抗HCV)', control: 'select_yny' }, @@ -48,6 +49,15 @@ const MOTHER_BLOOD: StorageCertQcField[] = [ } ] +/** 脐带间充质:不展示 HBV DNA / HCV RNA / HIV-1RNA。 */ +const MOTHER_BLOOD_UC_MSC: StorageCertQcField[] = [ + { key: 'mother_hbsag', label: '乙型肝炎病毒表面抗原(HBsAg)', control: 'select_yny' }, + { key: 'mother_anti_hcv', label: '丙型肝炎病毒抗体(抗HCV)', control: 'select_yny' }, + { key: 'mother_anti_tp', label: '梅毒螺旋体特异性抗体(抗TP)', control: 'select_yny' }, + { key: 'mother_cmv_igm', label: '巨细胞病毒 IgM 抗体 (CMV IgM)', control: 'select_yny' }, + { key: 'mother_anti_hiv', label: '人类免疫缺陷病毒抗体(抗HIV-1/2)', control: 'select_yny' } +] + export const STORAGE_CERT_QC_GROUPS: Record = { perinatal_hsc: [ { @@ -77,7 +87,7 @@ export const STORAGE_CERT_QC_GROUPS: Record