From f3672d3cc299dc5256e02da3c134fe639f2500b9 Mon Sep 17 00:00:00 2001 From: zlwu <704541015@qq.com> Date: Fri, 20 Nov 2026 17:03:45 +0800 Subject: [PATCH] =?UTF-8?q?feat(production):=20=E4=B8=B4=E5=BA=8A=E7=A7=91?= =?UTF-8?q?=E7=A0=94=E5=87=BA=E5=BA=93=E7=BB=86=E8=83=9E=E6=94=B9=E7=94=B1?= =?UTF-8?q?=E7=94=9F=E4=BA=A7=E7=A1=AE=E8=AE=A4?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 市场出库交接对 basic_experiment 保留细胞只读并不改库存;生产工单新增确认出库细胞 API/弹窗,从细胞产品库勾选并联动已售状态。 Co-authored-by: Cursor --- backend/app/api/production.py | 52 +++++ backend/app/crud/outbound_handover.py | 131 ++++++++++++- .../app/crud/outbound_handover_inventory.py | 39 ++-- backend/app/schemas/production_work_order.py | 10 +- .../test_production_delivery_cell_outbound.py | 172 ++++++++++++++++ web-admin/src/api/production-work-order.ts | 42 ++++ .../outbound-material-form-sections/index.vue | 89 ++++++--- .../InfusionScheduleHandoverDialog.vue | 9 +- .../ProductionDeliveryCellOutboundDialog.vue | 185 ++++++++++++++++++ .../ProductionWorkOrderCardList.vue | 19 +- .../tabs/ProductionWorkOrderTabPanel.vue | 20 ++ 11 files changed, 713 insertions(+), 55 deletions(-) create mode 100644 backend/tests/test_production_delivery_cell_outbound.py create mode 100644 web-admin/src/views/production/production-work-order/ProductionDeliveryCellOutboundDialog.vue diff --git a/backend/app/api/production.py b/backend/app/api/production.py index d03e3498..f28f902b 100644 --- a/backend/app/api/production.py +++ b/backend/app/api/production.py @@ -49,7 +49,9 @@ from app.schemas.production_work_order import ( ProductionWorkOrderCreate, ProductionWorkOrderConfigureBody, ProductionWorkOrderUpdate, + ProductionDeliveryCellOutboundBody, ) +from app.crud import outbound_handover as outbound_handover_svc from app.schemas.production_task_set import ( ProductionTaskSetCreateBody, ProductionTaskSetFromWorkOrderBody, @@ -2025,6 +2027,56 @@ async def list_production_work_order_cell_product_options( return error_response(400, str(e)) +@router.get("/production-work-orders/{wo_id}/delivery-cell-outbound") +async def get_production_delivery_cell_outbound_api( + wo_id: str, + user: dict = Depends(require_admin_or_super_admin), + db: Session = Depends(get_db), +): + """临床科研出库工单 — 获取已确认的交付出库细胞。""" + try: + tenant_no = _get_tenant_no(db, user) + data = outbound_handover_svc.get_production_delivery_cell_outbound( + db, wo_id, tenant_no + ) + return success_response(data) + except HTTPException as e: + return error_response(e.status_code, e.detail) + except ValueError as e: + return error_response(400, str(e)) + + +@router.put("/production-work-orders/{wo_id}/delivery-cell-outbound") +async def save_production_delivery_cell_outbound_api( + wo_id: str, + body: ProductionDeliveryCellOutboundBody, + user: dict = Depends(require_admin_or_super_admin), + db: Session = Depends(get_db), +): + """临床科研出库工单 — 确认出库细胞(写台账并联动库存)。""" + try: + tenant_no = _get_tenant_no(db, user) + data = outbound_handover_svc.save_production_delivery_cell_outbound( + db, + wo_id, + cells=body.cells or [], + tenant_no=tenant_no, + user_id=user.get("user_id") or "", + create_dept_id=user.get("dept_id") or "", + ) + db.commit() + return success_response(data, message="出库细胞已确认") + except HTTPException as e: + db.rollback() + return error_response(e.status_code, e.detail) + except ValueError as e: + db.rollback() + return error_response(400, str(e)) + except Exception as e: + db.rollback() + return error_response(400, str(e)) + + @router.get("/production-work-orders/{wo_id}/qc-info") async def get_production_work_order_qc_info_api( wo_id: str, diff --git a/backend/app/crud/outbound_handover.py b/backend/app/crud/outbound_handover.py index 57e17618..dbce866c 100644 --- a/backend/app/crud/outbound_handover.py +++ b/backend/app/crud/outbound_handover.py @@ -179,23 +179,38 @@ def save_outbound_handover( handover_person = handover_user_id om = outbound_material or {} - cells = om.get("cells") or [] qc_materials = om.get("qcMaterials") or om.get("qc_materials") or [] paper_materials = om.get("paperMaterials") or om.get("paper_materials") or [] material_remark = om.get("remark") + biz_type = (detail.get("bizType") or "").strip() + if not biz_type: + biz_type = ((detail.get("application") or {}).get("bizType") or "").strip() + # 临床科研:细胞出库由生产工单确认,市场交接只维护物料/交接信息 + cells_owned_by_production = biz_type == "basic_experiment" + # 读取保存前台账,用于回滚上次出库交接的库存/状态变更 previous_material = orm_material_svc.get_outbound_material(db, schedule_id, tenant_no) + previous_cells = previous_material.get("cells") or [] previous_cell_ids = [ (c.get("cellProductId") or c.get("cell_product_id") or "").strip() - for c in (previous_material.get("cells") or []) + for c in previous_cells ] had_previous_handover = get_handover_by_schedule(db, schedule_id, tenant_no) is not None + if cells_owned_by_production: + cells = previous_cells + revert_cell_ids: List[str] = [] + apply_cells: List[Any] = [] + else: + cells = om.get("cells") or [] + revert_cell_ids = previous_cell_ids if had_previous_handover else [] + apply_cells = cells + handover_inv_svc.revert_handover_inventory( db, schedule_id, - cell_product_ids=previous_cell_ids if had_previous_handover else [], + cell_product_ids=revert_cell_ids, tenant_no=tenant_no, user_id=user_id, ) @@ -216,7 +231,7 @@ def save_outbound_handover( handover_inv_svc.apply_handover_inventory( db, schedule_id, - cells=cells, + cells=apply_cells, qc_materials=qc_materials, tenant_no=tenant_no, user_id=user_id, @@ -255,3 +270,111 @@ def save_outbound_handover( db.flush() return get_outbound_handover(db, schedule_id, tenant_no) + + +def _assert_basic_experiment_outbound_wo(wo: Any) -> str: + """校验临床科研出库工单,返回关联交付计划 ID。""" + if not wo: + raise HTTPException(status_code=404, detail="工单不存在") + if (getattr(wo, "work_order_type", None) or "") != "outbound": + raise HTTPException(status_code=400, detail="仅出库工单可确认出库细胞") + if (getattr(wo, "biz_type", None) or "") != "basic_experiment": + raise HTTPException(status_code=400, detail="仅临床科研出库工单可确认出库细胞") + if (getattr(wo, "work_order_status", None) or "") == "yzf": + raise HTTPException(status_code=400, detail="已作废工单不可确认出库细胞") + schedule_id = (getattr(wo, "infusion_schedule_id", None) or "").strip() + if not schedule_id: + raise HTTPException(status_code=400, detail="工单未关联交付计划,无法确认出库细胞") + return schedule_id + + +def get_production_delivery_cell_outbound( + db: Session, + work_order_id: str, + tenant_no: str, +) -> Dict[str, Any]: + """生产工单 — 临床科研交付出库细胞编辑数据。""" + from app.crud import production_work_order as production_work_order_svc + + wo = production_work_order_svc.get_production_work_order(db, work_order_id, tenant_no) + schedule_id = _assert_basic_experiment_outbound_wo(wo) + detail = schedule_mgmt_svc.get_infusion_schedule_detail(db, schedule_id, tenant_no) + if not detail: + raise HTTPException(status_code=404, detail="交付计划不存在") + outbound_material = orm_material_svc.get_outbound_material(db, schedule_id, tenant_no) + return { + "workOrderId": wo.id, + "workOrderNo": wo.work_order_no or "", + "infusionScheduleId": schedule_id, + "scheduleSummary": { + "applicationNo": detail.get("applicationNo") or "", + "patientName": detail.get("patientName") or "", + "infusionTime": detail.get("infusionTime") or "", + "projectTitle": detail.get("projectTitle") + or ((detail.get("application") or {}).get("projectTitle") or ""), + "customerName": detail.get("customerName") + or ((detail.get("application") or {}).get("customerName") or ""), + }, + "cells": outbound_material.get("cells") or [], + } + + +def save_production_delivery_cell_outbound( + db: Session, + work_order_id: str, + *, + cells: List[Dict[str, Any]], + tenant_no: str, + user_id: str, + create_dept_id: str = "", +) -> Dict[str, Any]: + """生产确认临床科研交付出库细胞:写台账并联动细胞产品库存(不 commit)。""" + from app.crud import production_work_order as production_work_order_svc + + wo = production_work_order_svc.get_production_work_order(db, work_order_id, tenant_no) + schedule_id = _assert_basic_experiment_outbound_wo(wo) + detail = schedule_mgmt_svc.get_infusion_schedule_detail(db, schedule_id, tenant_no) + if not detail: + raise HTTPException(status_code=404, detail="交付计划不存在") + + previous_material = orm_material_svc.get_outbound_material(db, schedule_id, tenant_no) + previous_cell_ids = [ + (c.get("cellProductId") or c.get("cell_product_id") or "").strip() + for c in (previous_material.get("cells") or []) + ] + + # 仅回滚细胞;市场物料扣减由出库交接维护,不可一并撤销 + handover_inv_svc.revert_handover_inventory( + db, + schedule_id, + cell_product_ids=previous_cell_ids, + tenant_no=tenant_no, + user_id=user_id, + skip_materials=True, + ) + + orm_material_svc.save_outbound_material( + db, + schedule_id, + paper_attachments=None, + paper_materials=previous_material.get("paperMaterials") or [], + remark=previous_material.get("remark"), + cells=cells or [], + qc_materials=previous_material.get("qcMaterials") or [], + tenant_no=tenant_no, + user_id=user_id, + create_dept_id=create_dept_id, + ) + + handover_inv_svc.apply_handover_inventory( + db, + schedule_id, + cells=cells or [], + qc_materials=[], + tenant_no=tenant_no, + user_id=user_id, + create_dept_id=create_dept_id, + receiver_person="", + ) + + return get_production_delivery_cell_outbound(db, work_order_id, tenant_no) diff --git a/backend/app/crud/outbound_handover_inventory.py b/backend/app/crud/outbound_handover_inventory.py index ea433e57..8e0d8af2 100644 --- a/backend/app/crud/outbound_handover_inventory.py +++ b/backend/app/crud/outbound_handover_inventory.py @@ -264,30 +264,35 @@ def revert_handover_inventory( cell_product_ids: Iterable[str], tenant_no: str, user_id: Optional[str] = None, + skip_materials: bool = False, ) -> None: - """回滚本输注计划上次出库交接产生的库存/状态变更(不 commit)。""" + """回滚本输注计划上次出库交接产生的库存/状态变更(不 commit)。 + + skip_materials=True 时仅回滚细胞产品状态(临床科研由生产单独出库细胞时使用)。 + """ sid = (schedule_id or "").strip() if not sid: return remark_prefix = _handover_usage_remark_prefix(sid) now = datetime.now() - _revert_legacy_qc_handover_usages( - db, - schedule_id=sid, - tenant_no=tenant_no, - user_id=user_id, - remark_prefix=remark_prefix, - now=now, - ) - _revert_market_handover_usages( - db, - schedule_id=sid, - tenant_no=tenant_no, - user_id=user_id, - remark_prefix=remark_prefix, - now=now, - ) + if not skip_materials: + _revert_legacy_qc_handover_usages( + db, + schedule_id=sid, + tenant_no=tenant_no, + user_id=user_id, + remark_prefix=remark_prefix, + now=now, + ) + _revert_market_handover_usages( + db, + schedule_id=sid, + tenant_no=tenant_no, + user_id=user_id, + remark_prefix=remark_prefix, + now=now, + ) seen: Set[str] = set() for raw_id in cell_product_ids: diff --git a/backend/app/schemas/production_work_order.py b/backend/app/schemas/production_work_order.py index 2b6ac5bd..fca843e8 100644 --- a/backend/app/schemas/production_work_order.py +++ b/backend/app/schemas/production_work_order.py @@ -1,5 +1,5 @@ from pydantic import BaseModel, ConfigDict, Field -from typing import Optional +from typing import Any, Dict, List, Optional from datetime import datetime @@ -57,3 +57,11 @@ class ProductionWorkOrderConfigureBody(BaseModel): route_id: Optional[str] = Field(None, alias="routeId") description: Optional[str] = Field("", alias="description") remark: Optional[str] = Field("", alias="remark") + + +class ProductionDeliveryCellOutboundBody(BaseModel): + """临床科研出库工单 — 确认出库细胞。""" + + model_config = ConfigDict(populate_by_name=True) + + cells: List[Dict[str, Any]] = Field(default_factory=list, description="出库细胞产品列表") diff --git a/backend/tests/test_production_delivery_cell_outbound.py b/backend/tests/test_production_delivery_cell_outbound.py new file mode 100644 index 00000000..3640696a --- /dev/null +++ b/backend/tests/test_production_delivery_cell_outbound.py @@ -0,0 +1,172 @@ +"""临床科研:生产确认出库细胞 / 市场交接不改细胞库存。""" +import unittest +from unittest.mock import MagicMock, patch + +from fastapi import HTTPException + + +class TestAssertBasicExperimentOutboundWo(unittest.TestCase): + def test_rejects_non_outbound(self): + from app.crud.outbound_handover import _assert_basic_experiment_outbound_wo + + wo = MagicMock( + work_order_type="inbound", + biz_type="basic_experiment", + work_order_status="jxz", + infusion_schedule_id="s1", + ) + with self.assertRaises(HTTPException) as ctx: + _assert_basic_experiment_outbound_wo(wo) + self.assertEqual(ctx.exception.status_code, 400) + + def test_rejects_other_biz_type(self): + from app.crud.outbound_handover import _assert_basic_experiment_outbound_wo + + wo = MagicMock( + work_order_type="outbound", + biz_type="pilot_service", + work_order_status="jxz", + infusion_schedule_id="s1", + ) + with self.assertRaises(HTTPException) as ctx: + _assert_basic_experiment_outbound_wo(wo) + self.assertEqual(ctx.exception.status_code, 400) + + def test_ok_returns_schedule_id(self): + from app.crud.outbound_handover import _assert_basic_experiment_outbound_wo + + wo = MagicMock( + work_order_type="outbound", + biz_type="basic_experiment", + work_order_status="jxz", + infusion_schedule_id="sched-1", + ) + self.assertEqual(_assert_basic_experiment_outbound_wo(wo), "sched-1") + + +class TestSaveOutboundHandoverBasicExperiment(unittest.TestCase): + @patch("app.crud.outbound_handover.handover_inv_svc") + @patch("app.crud.outbound_handover.orm_material_svc") + @patch("app.crud.outbound_handover.get_outbound_handover") + @patch("app.crud.outbound_handover.get_handover_by_schedule") + @patch("app.crud.outbound_handover.schedule_mgmt_svc") + def test_market_keeps_previous_cells_and_skips_cell_inventory( + self, + schedule_mgmt_svc, + get_handover_by_schedule, + get_outbound_handover, + orm_material_svc, + handover_inv_svc, + ): + from app.crud.outbound_handover import save_outbound_handover + + schedule_mgmt_svc.get_infusion_schedule_detail.return_value = { + "bizType": "basic_experiment", + "applicationNo": "A1", + } + previous_cells = [{"cellProductId": "cp-prod", "productCode": "P1"}] + orm_material_svc.get_outbound_material.return_value = { + "cells": previous_cells, + "qcMaterials": [], + "paperMaterials": [], + "remark": "", + } + get_handover_by_schedule.return_value = None + get_outbound_handover.return_value = {"ok": True} + + db = MagicMock() + sys_user = MagicMock(nick_name="张三", user_no="u1", del_flag=0) + db.query.return_value.filter.return_value.first.return_value = sys_user + + save_outbound_handover( + db, + "sched-1", + outbound_material={ + "cells": [{"cellProductId": "cp-market-tamper"}], + "qcMaterials": [{"materialId": "m1", "qty": 1}], + "paperMaterials": [], + }, + handover={ + "handoverUserId": "u1", + "receiverPerson": "李四", + "handoverTime": "2026-11-20 10:00:00", + }, + tenant_no="t1", + user_id="u1", + ) + + handover_inv_svc.revert_handover_inventory.assert_called_once() + revert_kwargs = handover_inv_svc.revert_handover_inventory.call_args.kwargs + self.assertEqual(revert_kwargs["cell_product_ids"], []) + + save_kwargs = orm_material_svc.save_outbound_material.call_args.kwargs + self.assertEqual(save_kwargs["cells"], previous_cells) + + apply_kwargs = handover_inv_svc.apply_handover_inventory.call_args.kwargs + self.assertEqual(apply_kwargs["cells"], []) + self.assertEqual(apply_kwargs["qc_materials"], [{"materialId": "m1", "qty": 1}]) + + +class TestSaveProductionDeliveryCellOutbound(unittest.TestCase): + @patch("app.crud.outbound_handover.get_production_delivery_cell_outbound") + @patch("app.crud.outbound_handover.handover_inv_svc") + @patch("app.crud.outbound_handover.orm_material_svc") + @patch("app.crud.outbound_handover.schedule_mgmt_svc") + @patch("app.crud.production_work_order.get_production_work_order") + def test_reverts_cells_only_then_applies( + self, + get_wo, + schedule_mgmt_svc, + orm_material_svc, + handover_inv_svc, + get_delivery, + ): + from app.crud.outbound_handover import save_production_delivery_cell_outbound + + get_wo.return_value = MagicMock( + id="wo1", + work_order_no="WO1", + work_order_type="outbound", + biz_type="basic_experiment", + work_order_status="jxz", + infusion_schedule_id="sched-1", + ) + schedule_mgmt_svc.get_infusion_schedule_detail.return_value = { + "bizType": "basic_experiment" + } + orm_material_svc.get_outbound_material.return_value = { + "cells": [{"cellProductId": "old-cp"}], + "qcMaterials": [{"materialId": "m1", "qty": 2}], + "paperMaterials": [{"paperName": "出库单", "copies": 1}], + "remark": "keep", + } + get_delivery.return_value = {"cells": []} + + cells = [{"cellProductId": "new-cp"}] + save_production_delivery_cell_outbound( + MagicMock(), + "wo1", + cells=cells, + tenant_no="t1", + user_id="u1", + create_dept_id="d1", + ) + + revert_kwargs = handover_inv_svc.revert_handover_inventory.call_args.kwargs + self.assertEqual(revert_kwargs["cell_product_ids"], ["old-cp"]) + self.assertTrue(revert_kwargs["skip_materials"]) + + save_kwargs = orm_material_svc.save_outbound_material.call_args.kwargs + self.assertEqual(save_kwargs["cells"], cells) + self.assertEqual(save_kwargs["qc_materials"], [{"materialId": "m1", "qty": 2}]) + self.assertEqual( + save_kwargs["paper_materials"], [{"paperName": "出库单", "copies": 1}] + ) + + apply_kwargs = handover_inv_svc.apply_handover_inventory.call_args.kwargs + self.assertEqual(apply_kwargs["cells"], cells) + self.assertEqual(apply_kwargs["qc_materials"], []) + + +if __name__ == "__main__": + unittest.main() diff --git a/web-admin/src/api/production-work-order.ts b/web-admin/src/api/production-work-order.ts index fe806f48..afed2964 100644 --- a/web-admin/src/api/production-work-order.ts +++ b/web-admin/src/api/production-work-order.ts @@ -316,3 +316,45 @@ export function fetchMineProductionWorkOrderTabStepCountsDeduped() { } return mineTabStepCountsInflight } + +/** 临床科研出库工单 — 交付出库细胞 */ +export type ProductionDeliveryCellOutbound = { + workOrderId?: string + workOrderNo?: string + infusionScheduleId?: string + scheduleSummary?: { + applicationNo?: string + patientName?: string + infusionTime?: string + projectTitle?: string + customerName?: string + } + cells?: Array<{ + cellProductId?: string + productCode?: string + productName?: string + productTypeName?: string + specName?: string + passage?: string + batchNo?: string + storageSlotName?: string + productStatus?: string + remark?: string + }> +} + +export function fetchProductionDeliveryCellOutbound(workOrderId: string) { + return request.get({ + url: `/web/admin/production-work-orders/${workOrderId}/delivery-cell-outbound` + }) +} + +export function saveProductionDeliveryCellOutbound( + workOrderId: string, + data: { cells: Array<{ cellProductId: string; remark?: string }> } +) { + return request.put({ + url: `/web/admin/production-work-orders/${workOrderId}/delivery-cell-outbound`, + data + }) +} diff --git a/web-admin/src/components/business/outbound-material-form-sections/index.vue b/web-admin/src/components/business/outbound-material-form-sections/index.vue index 51b913a4..351bbd60 100644 --- a/web-admin/src/components/business/outbound-material-form-sections/index.vue +++ b/web-admin/src/components/business/outbound-material-form-sections/index.vue @@ -4,10 +4,16 @@
-
+

出库细胞

- + 添加细胞产品 @@ -18,7 +24,7 @@ class="list-table inner-table" stripe max-height="280" - empty-text="请从细胞产品库勾选" + :empty-text="cellsReadonly ? '暂无生产确认的出库细胞' : '请从细胞产品库勾选'" > — - + @@ -242,6 +249,7 @@ import { PRODUCTION_WORK_ORDER_STATUS_FILTER_OPTIONS } from '@/utils/status-tag-registry' import ProductionWorkOrderCardList from '../ProductionWorkOrderCardList.vue' import ProductionWorkOrderQcViewDialog from '../ProductionWorkOrderQcViewDialog.vue' + import ProductionDeliveryCellOutboundDialog from '../ProductionDeliveryCellOutboundDialog.vue' import SampleReceptionDialog from '../SampleReceptionDialog.vue' import SaveProductionTaskSetDialog from '../SaveProductionTaskSetDialog.vue' import { PRODUCTION_WORK_ORDER_VIEW_DIALOG_COMPONENTS } from '../view-dialogs/view-dialog-registry' @@ -283,6 +291,8 @@ const saveSetLabStorageCode = computed(() => resolveWorkOrderLabStorageCode(saveSetRow.value) ) + const deliveryCellOutboundVisible = ref(false) + const deliveryCellOutboundWorkOrderId = ref('') const { cardMetaConfig, @@ -366,6 +376,16 @@ qcViewVisible.value = true } + function handleDeliveryCellOutbound(row: ProductionWorkOrderItem) { + deliveryCellOutboundWorkOrderId.value = row.id || '' + deliveryCellOutboundVisible.value = true + } + + function handleDeliveryCellOutboundSaved() { + loadData() + emit('data-changed') + } + function handleSampleReceptionSuccess() { bumpTaskRefresh() loadData()