避免只改接收表编码后,生产/质检/库位等关联副本仍保留旧码。 Co-authored-by: Cursor <cursoragent@cursor.com>
400 lines
12 KiB
Python
400 lines
12 KiB
Python
"""编辑样本接收时,把实验室入库编码同步到已落库的关联副本。"""
|
|
from __future__ import annotations
|
|
|
|
from datetime import datetime
|
|
from typing import Iterable, List, Optional, Sequence
|
|
|
|
from sqlalchemy.orm import Session
|
|
|
|
from app.models.models import (
|
|
BizCellProduct,
|
|
BizLabSampleProcessingExtra,
|
|
BizLabSampleProcessSupplement,
|
|
BizOutboundReleaseCell,
|
|
BizProductionTask,
|
|
BizProductionTaskInput,
|
|
BizProductionTaskOutput,
|
|
BizProductionTaskStep,
|
|
BizProductionTaskStepOutput,
|
|
BizProductionWorkOrder,
|
|
BizQcWorkOrder,
|
|
BizQcWorkOrderInspectionBatch,
|
|
BizSampleReception,
|
|
BizStorageSlotOccupancy,
|
|
)
|
|
|
|
DUPLICATE_MESSAGE = "实验室入库编码已存在"
|
|
|
|
|
|
def _ascii_alnum(ch: str) -> bool:
|
|
return bool(ch) and ch.isascii() and ch.isalnum()
|
|
|
|
|
|
def replace_lab_code_token(text: Optional[str], old: str, new: str) -> Optional[str]:
|
|
"""替换完整编码。前后紧挨 ASCII 字母或数字时不替换,避免改掉更长的编码。"""
|
|
if text is None or not old or old == new or old not in text:
|
|
return text
|
|
out: List[str] = []
|
|
i = 0
|
|
size = len(old)
|
|
while i < len(text):
|
|
if text.startswith(old, i):
|
|
prev_ok = i == 0 or not _ascii_alnum(text[i - 1])
|
|
end = i + size
|
|
next_ok = end == len(text) or not _ascii_alnum(text[end])
|
|
if prev_ok and next_ok:
|
|
out.append(new)
|
|
i = end
|
|
continue
|
|
out.append(text[i])
|
|
i += 1
|
|
return "".join(out)
|
|
|
|
|
|
def _occupies_new_code(value: Optional[str], new: str) -> bool:
|
|
"""批次名等于新编码,或是「新编码 + 非字母数字后缀」。"""
|
|
text = (value or "").strip()
|
|
if not text or not new:
|
|
return False
|
|
if text == new:
|
|
return True
|
|
if not text.startswith(new) or len(text) == len(new):
|
|
return False
|
|
return not _ascii_alnum(text[len(new)])
|
|
|
|
|
|
def _like_prefix(code: str) -> str:
|
|
escaped = code.replace("\\", "\\\\").replace("%", "\\%").replace("_", "\\_")
|
|
return escaped + "%"
|
|
|
|
|
|
def _ids(values: Iterable[str]) -> List[str]:
|
|
return [v for v in values if v]
|
|
|
|
|
|
def _touch(row, user_id: Optional[str], now: datetime) -> None:
|
|
row.update_by = user_id
|
|
row.update_time = now
|
|
|
|
|
|
def _rewrite_fields(row, fields: Sequence[str], old: str, new: str, user_id: Optional[str], now: datetime) -> bool:
|
|
changed = False
|
|
for field in fields:
|
|
current = getattr(row, field)
|
|
updated = replace_lab_code_token(current, old, new)
|
|
if updated != current:
|
|
setattr(row, field, updated)
|
|
changed = True
|
|
if changed:
|
|
_touch(row, user_id, now)
|
|
return changed
|
|
|
|
|
|
def _raise_if_duplicate() -> None:
|
|
raise ValueError(DUPLICATE_MESSAGE)
|
|
|
|
|
|
def rename_lab_storage_code_copies(
|
|
db: Session,
|
|
*,
|
|
tenant_no: str,
|
|
reception_id: str,
|
|
old_code: str,
|
|
new_code: str,
|
|
user_id: Optional[str] = None,
|
|
) -> int:
|
|
"""把本条接收记录关联出去的旧编码副本改成新编码。冲突时不写任何副本。"""
|
|
old = (old_code or "").strip()
|
|
new = (new_code or "").strip()
|
|
if not old or not new or old == new:
|
|
return 0
|
|
|
|
other_reception = (
|
|
db.query(BizSampleReception.id)
|
|
.filter(
|
|
BizSampleReception.tenant_no == tenant_no,
|
|
BizSampleReception.del_flag == 0,
|
|
BizSampleReception.lab_storage_code == new,
|
|
BizSampleReception.id != reception_id,
|
|
)
|
|
.first()
|
|
)
|
|
if other_reception:
|
|
_raise_if_duplicate()
|
|
|
|
prod_orders = (
|
|
db.query(BizProductionWorkOrder)
|
|
.filter(
|
|
BizProductionWorkOrder.tenant_no == tenant_no,
|
|
BizProductionWorkOrder.del_flag == 0,
|
|
BizProductionWorkOrder.reception_id == reception_id,
|
|
)
|
|
.all()
|
|
)
|
|
qc_orders = (
|
|
db.query(BizQcWorkOrder)
|
|
.filter(
|
|
BizQcWorkOrder.tenant_no == tenant_no,
|
|
BizQcWorkOrder.del_flag == 0,
|
|
BizQcWorkOrder.reception_id == reception_id,
|
|
)
|
|
.all()
|
|
)
|
|
prod_ids = [row.id for row in prod_orders]
|
|
qc_ids = [row.id for row in qc_orders]
|
|
tasks = (
|
|
db.query(BizProductionTask)
|
|
.filter(
|
|
BizProductionTask.tenant_no == tenant_no,
|
|
BizProductionTask.del_flag == 0,
|
|
BizProductionTask.work_order_id.in_(prod_ids),
|
|
)
|
|
.all()
|
|
if prod_ids
|
|
else []
|
|
)
|
|
task_ids = [row.id for row in tasks]
|
|
steps = (
|
|
db.query(BizProductionTaskStep)
|
|
.filter(
|
|
BizProductionTaskStep.tenant_no == tenant_no,
|
|
BizProductionTaskStep.del_flag == 0,
|
|
BizProductionTaskStep.task_id.in_(task_ids),
|
|
)
|
|
.all()
|
|
if task_ids
|
|
else []
|
|
)
|
|
step_ids = [row.id for row in steps]
|
|
inputs = (
|
|
db.query(BizProductionTaskInput)
|
|
.filter(
|
|
BizProductionTaskInput.tenant_no == tenant_no,
|
|
BizProductionTaskInput.del_flag == 0,
|
|
BizProductionTaskInput.task_id.in_(task_ids),
|
|
)
|
|
.all()
|
|
if task_ids
|
|
else []
|
|
)
|
|
task_outputs = (
|
|
db.query(BizProductionTaskOutput)
|
|
.filter(
|
|
BizProductionTaskOutput.tenant_no == tenant_no,
|
|
BizProductionTaskOutput.del_flag == 0,
|
|
BizProductionTaskOutput.task_id.in_(task_ids),
|
|
)
|
|
.all()
|
|
if task_ids
|
|
else []
|
|
)
|
|
step_outputs = (
|
|
db.query(BizProductionTaskStepOutput)
|
|
.filter(
|
|
BizProductionTaskStepOutput.tenant_no == tenant_no,
|
|
BizProductionTaskStepOutput.del_flag == 0,
|
|
BizProductionTaskStepOutput.step_id.in_(step_ids),
|
|
)
|
|
.all()
|
|
if step_ids
|
|
else []
|
|
)
|
|
products = (
|
|
db.query(BizCellProduct)
|
|
.filter(
|
|
BizCellProduct.tenant_no == tenant_no,
|
|
BizCellProduct.del_flag == 0,
|
|
BizCellProduct.production_task_id.in_(task_ids),
|
|
)
|
|
.all()
|
|
if task_ids
|
|
else []
|
|
)
|
|
product_ids = [row.id for row in products]
|
|
occupancies = (
|
|
db.query(BizStorageSlotOccupancy)
|
|
.filter(
|
|
BizStorageSlotOccupancy.tenant_no == tenant_no,
|
|
BizStorageSlotOccupancy.del_flag == 0,
|
|
BizStorageSlotOccupancy.item_id.in_(product_ids),
|
|
)
|
|
.all()
|
|
if product_ids
|
|
else []
|
|
)
|
|
outbound_cells = (
|
|
db.query(BizOutboundReleaseCell)
|
|
.filter(
|
|
BizOutboundReleaseCell.tenant_no == tenant_no,
|
|
BizOutboundReleaseCell.del_flag == 0,
|
|
BizOutboundReleaseCell.cell_product_id.in_(product_ids),
|
|
)
|
|
.all()
|
|
if product_ids
|
|
else []
|
|
)
|
|
batches = (
|
|
db.query(BizQcWorkOrderInspectionBatch)
|
|
.filter(
|
|
BizQcWorkOrderInspectionBatch.tenant_no == tenant_no,
|
|
BizQcWorkOrderInspectionBatch.del_flag == 0,
|
|
BizQcWorkOrderInspectionBatch.work_order_id.in_(qc_ids),
|
|
)
|
|
.all()
|
|
if qc_ids
|
|
else []
|
|
)
|
|
supplements = (
|
|
db.query(BizLabSampleProcessSupplement)
|
|
.filter(
|
|
BizLabSampleProcessSupplement.tenant_no == tenant_no,
|
|
BizLabSampleProcessSupplement.del_flag == 0,
|
|
BizLabSampleProcessSupplement.lab_storage_code == old,
|
|
)
|
|
.all()
|
|
)
|
|
extras = (
|
|
db.query(BizLabSampleProcessingExtra)
|
|
.filter(
|
|
BizLabSampleProcessingExtra.tenant_no == tenant_no,
|
|
BizLabSampleProcessingExtra.del_flag == 0,
|
|
BizLabSampleProcessingExtra.sample_code == old,
|
|
)
|
|
.all()
|
|
)
|
|
|
|
_assert_copies_available(
|
|
db,
|
|
tenant_no=tenant_no,
|
|
new=new,
|
|
product_ids=product_ids,
|
|
task_output_ids=[row.id for row in task_outputs],
|
|
step_output_ids=[row.id for row in step_outputs],
|
|
occupancy_ids=[row.id for row in occupancies],
|
|
qc_ids=qc_ids,
|
|
)
|
|
if (
|
|
db.query(BizLabSampleProcessSupplement.id)
|
|
.filter(
|
|
BizLabSampleProcessSupplement.tenant_no == tenant_no,
|
|
BizLabSampleProcessSupplement.del_flag == 0,
|
|
BizLabSampleProcessSupplement.lab_storage_code == new,
|
|
)
|
|
.first()
|
|
):
|
|
_raise_if_duplicate()
|
|
if (
|
|
db.query(BizLabSampleProcessingExtra.id)
|
|
.filter(
|
|
BizLabSampleProcessingExtra.tenant_no == tenant_no,
|
|
BizLabSampleProcessingExtra.del_flag == 0,
|
|
BizLabSampleProcessingExtra.sample_code == new,
|
|
)
|
|
.first()
|
|
):
|
|
_raise_if_duplicate()
|
|
|
|
now = datetime.now()
|
|
changed = 0
|
|
for row in tasks:
|
|
if _rewrite_fields(row, ("task_name", "description", "remark"), old, new, user_id, now):
|
|
changed += 1
|
|
for row in steps:
|
|
if _rewrite_fields(row, ("description", "remark", "sop_content"), old, new, user_id, now):
|
|
changed += 1
|
|
for row in inputs:
|
|
if _rewrite_fields(row, ("input_name", "input_code"), old, new, user_id, now):
|
|
changed += 1
|
|
for row in task_outputs:
|
|
if _rewrite_fields(row, ("output_name", "output_barcode"), old, new, user_id, now):
|
|
changed += 1
|
|
for row in step_outputs:
|
|
if _rewrite_fields(row, ("output_name", "output_barcode"), old, new, user_id, now):
|
|
changed += 1
|
|
for row in products:
|
|
if _rewrite_fields(row, ("product_code", "product_name", "batch_no"), old, new, user_id, now):
|
|
changed += 1
|
|
for row in occupancies:
|
|
if _rewrite_fields(row, ("item_code", "item_name"), old, new, user_id, now):
|
|
changed += 1
|
|
for row in outbound_cells:
|
|
if _rewrite_fields(row, ("product_code", "product_name", "batch_no"), old, new, user_id, now):
|
|
changed += 1
|
|
for row in batches:
|
|
if _rewrite_fields(row, ("batch_name",), old, new, user_id, now):
|
|
changed += 1
|
|
for row in supplements:
|
|
row.lab_storage_code = new
|
|
_touch(row, user_id, now)
|
|
changed += 1
|
|
for row in extras:
|
|
row.sample_code = new
|
|
_touch(row, user_id, now)
|
|
changed += 1
|
|
if changed:
|
|
db.flush()
|
|
return changed
|
|
|
|
|
|
def _assert_copies_available(
|
|
db: Session,
|
|
*,
|
|
tenant_no: str,
|
|
new: str,
|
|
product_ids: Sequence[str],
|
|
task_output_ids: Sequence[str],
|
|
step_output_ids: Sequence[str],
|
|
occupancy_ids: Sequence[str],
|
|
qc_ids: Sequence[str],
|
|
) -> None:
|
|
product_q = db.query(BizCellProduct.id).filter(
|
|
BizCellProduct.tenant_no == tenant_no,
|
|
BizCellProduct.del_flag == 0,
|
|
BizCellProduct.product_code == new,
|
|
)
|
|
if product_ids:
|
|
product_q = product_q.filter(~BizCellProduct.id.in_(_ids(product_ids)))
|
|
if product_q.first():
|
|
_raise_if_duplicate()
|
|
|
|
task_barcode_q = db.query(BizProductionTaskOutput.id).filter(
|
|
BizProductionTaskOutput.tenant_no == tenant_no,
|
|
BizProductionTaskOutput.del_flag == 0,
|
|
BizProductionTaskOutput.output_barcode == new,
|
|
)
|
|
if task_output_ids:
|
|
task_barcode_q = task_barcode_q.filter(~BizProductionTaskOutput.id.in_(_ids(task_output_ids)))
|
|
if task_barcode_q.first():
|
|
_raise_if_duplicate()
|
|
|
|
step_barcode_q = db.query(BizProductionTaskStepOutput.id).filter(
|
|
BizProductionTaskStepOutput.tenant_no == tenant_no,
|
|
BizProductionTaskStepOutput.del_flag == 0,
|
|
BizProductionTaskStepOutput.output_barcode == new,
|
|
)
|
|
if step_output_ids:
|
|
step_barcode_q = step_barcode_q.filter(~BizProductionTaskStepOutput.id.in_(_ids(step_output_ids)))
|
|
if step_barcode_q.first():
|
|
_raise_if_duplicate()
|
|
|
|
occupancy_q = db.query(BizStorageSlotOccupancy.id).filter(
|
|
BizStorageSlotOccupancy.tenant_no == tenant_no,
|
|
BizStorageSlotOccupancy.del_flag == 0,
|
|
BizStorageSlotOccupancy.item_code == new,
|
|
)
|
|
if occupancy_ids:
|
|
occupancy_q = occupancy_q.filter(~BizStorageSlotOccupancy.id.in_(_ids(occupancy_ids)))
|
|
if occupancy_q.first():
|
|
_raise_if_duplicate()
|
|
|
|
batch_q = db.query(BizQcWorkOrderInspectionBatch.batch_name).filter(
|
|
BizQcWorkOrderInspectionBatch.tenant_no == tenant_no,
|
|
BizQcWorkOrderInspectionBatch.del_flag == 0,
|
|
BizQcWorkOrderInspectionBatch.batch_name.like(_like_prefix(new), escape="\\"),
|
|
)
|
|
if qc_ids:
|
|
batch_q = batch_q.filter(~BizQcWorkOrderInspectionBatch.work_order_id.in_(_ids(qc_ids)))
|
|
for (name,) in batch_q.all():
|
|
if _occupies_new_code(name, new):
|
|
_raise_if_duplicate()
|