diff --git a/backend/app/crud/qc_task.py b/backend/app/crud/qc_task.py index 345e13a2..84b8a804 100644 --- a/backend/app/crud/qc_task.py +++ b/backend/app/crud/qc_task.py @@ -20,6 +20,7 @@ from app.crud.qc_work_order import ( _work_order_qc_biz_type, _OUTBOUND_BIZ_TYPE_LABELS, apply_research_donation_protocol_status_filter, + build_qc_work_order_keyword_conditions, build_work_order_list_summaries, ) from app.core.db_collation import eq_unicode, unicode_ci @@ -603,14 +604,21 @@ def list_mine_tasks( kw = (keyword or "").strip() if kw: like = f"%{kw}%" - q0 = q0.filter( - or_( - BizQcTask.task_no.like(like), - BizQcTask.item_name.like(like), - BizQcWorkOrder.work_order_no.like(like), - BizQcWorkOrder.sample_code.like(like), - ) - ) + wt = (qc_biz_type or "").strip() + # temporary Tab 的 qcBizType=temporary,与工单 work_order_type 对齐 + wo_type = "temporary" if wt == "temporary" else (wt or None) + keyword_conditions = [ + BizQcTask.task_no.like(like), + BizQcTask.item_name.like(like), + *build_qc_work_order_keyword_conditions( + db, + tenant_no=tenant_no, + keyword=kw, + work_order_type=wo_type, + reception_source=(reception_source or "").strip() or None, + ), + ] + q0 = q0.filter(or_(*keyword_conditions)) tt = (task_type or "").strip() if tt: q0 = q0.filter(BizQcTask.task_type == tt) diff --git a/backend/app/crud/qc_work_order.py b/backend/app/crud/qc_work_order.py index f18561e3..83d4596d 100644 --- a/backend/app/crud/qc_work_order.py +++ b/backend/app/crud/qc_work_order.py @@ -560,6 +560,56 @@ def _inbound_qc_work_order_ids_by_mother_name( return list(ids) +def build_qc_work_order_keyword_conditions( + db: Optional[Session], + *, + tenant_no: Optional[str], + keyword: str, + work_order_type: Optional[str] = None, + reception_source: Optional[str] = None, +) -> List[Any]: + """质检工单列表关键词 OR 条件(工单号/样本/协议号/外部编号 + 入库编码/母亲/出库申请字段)。 + + 供质检工单列表与「我的质检任务」共用,避免两端口径漂移。 + """ + kw = (keyword or "").strip() + if not kw: + return [] + like = f"%{kw}%" + wt = (work_order_type or "").strip() + rs = (reception_source or "").strip() + conditions: List[Any] = [ + BizQcWorkOrder.work_order_no.like(like), + BizQcWorkOrder.sample_code.like(like), + BizQcWorkOrder.protocol_no.like(like), + BizQcWorkOrder.external_target_code.like(like), + ] + if db is not None and tenant_no: + from app.crud import sample_reception as reception_svc + + rec_ids = reception_svc.reception_ids_matching_lab_storage_keyword( + db, tenant_no, kw + ) + if rec_ids: + conditions.append(BizQcWorkOrder.reception_id.in_(rec_ids)) + lab_codes = reception_svc.sample_codes_matching_lab_storage_keyword( + db, tenant_no, kw + ) + if lab_codes: + conditions.append(BizQcWorkOrder.sample_code.in_(lab_codes)) + if wt == "outbound" or not wt: + outbound_ids = _outbound_qc_work_order_ids_by_keyword(db, tenant_no, kw) + if outbound_ids: + conditions.append(BizQcWorkOrder.id.in_(outbound_ids)) + if not wt or wt == "inbound": + inbound_mother_ids = _inbound_qc_work_order_ids_by_mother_name( + db, tenant_no, kw, reception_source=rs or None + ) + if inbound_mother_ids: + conditions.append(BizQcWorkOrder.id.in_(inbound_mother_ids)) + return conditions + + def _accept_conclusion_label(v: Optional[str]) -> str: key = (v or "").strip() return _ACCEPT_CONCLUSION_LABELS.get(key, key) @@ -1791,37 +1841,15 @@ def list_work_orders( q0 = q0.filter(BizQcWorkOrder.trigger_source == ts) kw = (keyword or "").strip() if kw: - like = f"%{kw}%" - keyword_conditions = [ - BizQcWorkOrder.work_order_no.like(like), - BizQcWorkOrder.sample_code.like(like), - BizQcWorkOrder.protocol_no.like(like), - BizQcWorkOrder.external_target_code.like(like), - ] - if tenant_no: - from app.crud import sample_reception as reception_svc - - rec_ids = reception_svc.reception_ids_matching_lab_storage_keyword( - db, tenant_no, kw - ) - if rec_ids: - keyword_conditions.append(BizQcWorkOrder.reception_id.in_(rec_ids)) - lab_codes = reception_svc.sample_codes_matching_lab_storage_keyword( - db, tenant_no, kw - ) - if lab_codes: - keyword_conditions.append(BizQcWorkOrder.sample_code.in_(lab_codes)) - if tenant_no and (wt == "outbound" or not wt): - outbound_ids = _outbound_qc_work_order_ids_by_keyword(db, tenant_no, kw) - if outbound_ids: - keyword_conditions.append(BizQcWorkOrder.id.in_(outbound_ids)) - if tenant_no and (not wt or wt == "inbound"): - inbound_mother_ids = _inbound_qc_work_order_ids_by_mother_name( - db, tenant_no, kw, reception_source=rs or None - ) - if inbound_mother_ids: - keyword_conditions.append(BizQcWorkOrder.id.in_(inbound_mother_ids)) - q0 = q0.filter(or_(*keyword_conditions)) + keyword_conditions = build_qc_work_order_keyword_conditions( + db, + tenant_no=tenant_no, + keyword=kw, + work_order_type=wt or None, + reception_source=rs or None, + ) + if keyword_conditions: + q0 = q0.filter(or_(*keyword_conditions)) st_raw = (work_order_status or "").strip() if st_raw: statuses = [s.strip() for s in st_raw.split(",") if s.strip()] diff --git a/backend/tests/test_qc_work_order_keyword_conditions.py b/backend/tests/test_qc_work_order_keyword_conditions.py new file mode 100644 index 00000000..f75f46a0 --- /dev/null +++ b/backend/tests/test_qc_work_order_keyword_conditions.py @@ -0,0 +1,34 @@ +"""质检工单 / 我的质检任务共用关键词条件(无库路径)。""" + +# qc_work_order ↔ qc_task 存在循环依赖:先经 qc_task 再取 helper +import app.crud.qc_task # noqa: F401 +from app.crud.qc_work_order import build_qc_work_order_keyword_conditions +from app.models.models import BizQcWorkOrder + + +def test_empty_keyword_returns_no_conditions(): + assert build_qc_work_order_keyword_conditions( + None, tenant_no=None, keyword="" + ) == [] + assert build_qc_work_order_keyword_conditions( + None, tenant_no="t1", keyword=" " + ) == [] + + +def test_base_conditions_without_tenant_cover_list_fields(): + conds = build_qc_work_order_keyword_conditions( + None, + tenant_no=None, + keyword="ABC", + work_order_type="inbound", + reception_source="cell_storage", + ) + assert len(conds) == 4 + # 与质检工单列表一致的四列(无租户时不做入库/母亲/出库扩展) + cols = {str(c.left) for c in conds} + assert str(BizQcWorkOrder.work_order_no) in cols or any( + "work_order_no" in str(c) for c in conds + ) + assert any("sample_code" in str(c) for c in conds) + assert any("protocol_no" in str(c) for c in conds) + assert any("external_target_code" in str(c) for c in conds)