市场出库交接对 basic_experiment 保留细胞只读并不改库存;生产工单新增确认出库细胞 API/弹窗,从细胞产品库勾选并联动已售状态。 Co-authored-by: Cursor <cursoragent@cursor.com>
381 lines
13 KiB
Python
381 lines
13 KiB
Python
"""输注计划管理 · 出库交接 CRUD"""
|
|
from __future__ import annotations
|
|
|
|
from datetime import datetime
|
|
from typing import Any, Dict, List, Optional
|
|
|
|
from fastapi import HTTPException
|
|
from sqlalchemy.orm import Session
|
|
|
|
from app.core.utils import generate_uuid
|
|
from app.crud import outbound_release_material as orm_material_svc
|
|
from app.crud import outbound_handover_inventory as handover_inv_svc
|
|
from app.crud import pilot_infusion_schedule_mgmt as schedule_mgmt_svc
|
|
from app.models.models import BizOutboundHandover, SysUser
|
|
|
|
|
|
def _fmt_dt(t: Optional[datetime]) -> str:
|
|
return t.strftime("%Y-%m-%d %H:%M:%S") if t else ""
|
|
|
|
|
|
def _parse_handover_time(value: str) -> datetime:
|
|
raw = (value or "").strip()
|
|
if not raw:
|
|
raise HTTPException(status_code=400, detail="交接时间不能为空")
|
|
for fmt in ("%Y-%m-%d %H:%M:%S", "%Y-%m-%d %H:%M"):
|
|
try:
|
|
return datetime.strptime(raw, fmt)
|
|
except ValueError:
|
|
continue
|
|
raise HTTPException(status_code=400, detail="交接时间格式无效")
|
|
|
|
|
|
def _handover_row_to_dict(row: Optional[BizOutboundHandover]) -> Dict[str, Any]:
|
|
if not row:
|
|
return {
|
|
"handoverUserId": "",
|
|
"handoverPerson": "",
|
|
"handoverTime": "",
|
|
"handoverLocation": "",
|
|
"receiverPerson": "",
|
|
"cellPurpose": "",
|
|
"attachments": "",
|
|
"remark": "",
|
|
}
|
|
return {
|
|
"handoverUserId": row.handover_user_id or "",
|
|
"handoverPerson": row.handover_person or "",
|
|
"handoverTime": _fmt_dt(row.handover_time),
|
|
"handoverLocation": row.handover_location or "",
|
|
"receiverPerson": row.receiver_person or "",
|
|
"cellPurpose": row.cell_purpose or "",
|
|
"attachments": row.attachments or "",
|
|
"remark": row.remark or "",
|
|
}
|
|
|
|
|
|
def _handover_view_dict(row: Optional[BizOutboundHandover]) -> Dict[str, Any]:
|
|
if not row:
|
|
return {
|
|
"handoverPerson": "",
|
|
"handoverTime": "",
|
|
"handoverLocation": "",
|
|
"receiverPerson": "",
|
|
"cellPurpose": "",
|
|
"attachments": "",
|
|
"remark": "",
|
|
}
|
|
return {
|
|
"handoverPerson": row.handover_person or "",
|
|
"handoverTime": _fmt_dt(row.handover_time),
|
|
"handoverLocation": row.handover_location or "",
|
|
"receiverPerson": row.receiver_person or "",
|
|
"cellPurpose": row.cell_purpose or "",
|
|
"attachments": row.attachments or "",
|
|
"remark": row.remark or "",
|
|
}
|
|
|
|
|
|
def get_handover_by_schedule(
|
|
db: Session,
|
|
schedule_id: str,
|
|
tenant_no: str,
|
|
) -> Optional[BizOutboundHandover]:
|
|
sid = (schedule_id or "").strip()
|
|
if not sid:
|
|
return None
|
|
return (
|
|
db.query(BizOutboundHandover)
|
|
.filter(
|
|
BizOutboundHandover.infusion_schedule_id == sid,
|
|
BizOutboundHandover.tenant_no == tenant_no,
|
|
BizOutboundHandover.del_flag == 0,
|
|
)
|
|
.first()
|
|
)
|
|
|
|
|
|
def get_outbound_handover_view(
|
|
db: Session,
|
|
schedule_id: str,
|
|
tenant_no: str,
|
|
) -> Dict[str, Any]:
|
|
"""供输注计划 detail 聚合的交接只读信息。"""
|
|
row = get_handover_by_schedule(db, schedule_id, tenant_no)
|
|
return _handover_view_dict(row)
|
|
|
|
|
|
def get_outbound_handover(
|
|
db: Session,
|
|
schedule_id: str,
|
|
tenant_no: str,
|
|
) -> Dict[str, Any]:
|
|
"""获取出库交接编辑数据(物料 + 交接信息 + 摘要)。"""
|
|
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)
|
|
handover_row = get_handover_by_schedule(db, schedule_id, tenant_no)
|
|
|
|
return {
|
|
"scheduleSummary": {
|
|
"applicationNo": detail.get("applicationNo") or "",
|
|
"patientName": detail.get("patientName") or "",
|
|
"infusionTime": detail.get("infusionTime") or "",
|
|
},
|
|
"outboundMaterial": {
|
|
"remark": outbound_material.get("remark") or "",
|
|
"cells": outbound_material.get("cells") or [],
|
|
"qcMaterials": outbound_material.get("qcMaterials") or [],
|
|
"paperMaterials": outbound_material.get("paperMaterials") or [],
|
|
},
|
|
"handover": _handover_row_to_dict(handover_row),
|
|
}
|
|
|
|
|
|
def save_outbound_handover(
|
|
db: Session,
|
|
schedule_id: str,
|
|
*,
|
|
outbound_material: Dict[str, Any],
|
|
handover: Dict[str, Any],
|
|
tenant_no: str,
|
|
user_id: str,
|
|
create_dept_id: str = "",
|
|
) -> Dict[str, Any]:
|
|
"""保存出库物料台账 + 出库交接(单事务,调用方 commit)。"""
|
|
detail = schedule_mgmt_svc.get_infusion_schedule_detail(db, schedule_id, tenant_no)
|
|
if not detail:
|
|
raise HTTPException(status_code=404, detail="输注计划不存在")
|
|
|
|
handover_user_id = (handover.get("handoverUserId") or handover.get("handover_user_id") or "").strip()
|
|
receiver_person = (handover.get("receiverPerson") or handover.get("receiver_person") or "").strip()
|
|
handover_location = (handover.get("handoverLocation") or handover.get("handover_location") or "").strip()
|
|
handover_remark = (handover.get("remark") or "").strip()
|
|
cell_purpose = (handover.get("cellPurpose") or handover.get("cell_purpose") or "").strip()
|
|
attachments = (handover.get("attachments") or "").strip()
|
|
handover_time_raw = handover.get("handoverTime") or handover.get("handover_time") or ""
|
|
|
|
if not handover_user_id:
|
|
raise HTTPException(status_code=400, detail="交接人不能为空")
|
|
if not receiver_person:
|
|
raise HTTPException(status_code=400, detail="接收人不能为空")
|
|
|
|
sys_user = (
|
|
db.query(SysUser)
|
|
.filter(
|
|
SysUser.id == handover_user_id,
|
|
SysUser.del_flag == 0,
|
|
)
|
|
.first()
|
|
)
|
|
if not sys_user:
|
|
raise HTTPException(status_code=400, detail="交接人用户不存在或已删除")
|
|
|
|
handover_time = _parse_handover_time(str(handover_time_raw))
|
|
handover_person = (sys_user.nick_name or sys_user.user_no or "").strip()
|
|
if not handover_person:
|
|
handover_person = handover_user_id
|
|
|
|
om = outbound_material 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_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=revert_cell_ids,
|
|
tenant_no=tenant_no,
|
|
user_id=user_id,
|
|
)
|
|
|
|
orm_material_svc.save_outbound_material(
|
|
db,
|
|
schedule_id,
|
|
paper_attachments=None,
|
|
paper_materials=paper_materials,
|
|
remark=material_remark,
|
|
cells=cells,
|
|
qc_materials=qc_materials,
|
|
tenant_no=tenant_no,
|
|
user_id=user_id,
|
|
create_dept_id=create_dept_id,
|
|
)
|
|
|
|
handover_inv_svc.apply_handover_inventory(
|
|
db,
|
|
schedule_id,
|
|
cells=apply_cells,
|
|
qc_materials=qc_materials,
|
|
tenant_no=tenant_no,
|
|
user_id=user_id,
|
|
create_dept_id=create_dept_id,
|
|
receiver_person=receiver_person,
|
|
)
|
|
|
|
now = datetime.now()
|
|
row = get_handover_by_schedule(db, schedule_id, tenant_no)
|
|
if not row:
|
|
row = BizOutboundHandover(
|
|
id=generate_uuid(),
|
|
infusion_schedule_id=schedule_id,
|
|
tenant_no=tenant_no,
|
|
order_num=0,
|
|
create_by=user_id,
|
|
create_dept_id=create_dept_id or "",
|
|
create_time=now,
|
|
del_flag=0,
|
|
)
|
|
db.add(row)
|
|
db.flush()
|
|
else:
|
|
row.update_by = user_id
|
|
row.update_time = now
|
|
|
|
row.handover_user_id = handover_user_id
|
|
row.handover_person = handover_person
|
|
row.handover_time = handover_time
|
|
row.handover_location = handover_location or None
|
|
row.receiver_person = receiver_person
|
|
row.cell_purpose = cell_purpose or None
|
|
row.attachments = attachments or None
|
|
row.remark = handover_remark or None
|
|
db.add(row)
|
|
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)
|