461 lines
14 KiB
Python
461 lines
14 KiB
Python
"""质检任务 CRUD"""
|
|
from __future__ import annotations
|
|
|
|
from datetime import datetime
|
|
from decimal import Decimal
|
|
from typing import Any, Dict, List, Optional, Tuple
|
|
|
|
from sqlalchemy import and_, func, or_
|
|
from sqlalchemy.orm import Session
|
|
|
|
from app.core.utils import generate_uuid
|
|
from app.crud.collection_protocol import urls_to_attachment_list
|
|
from app.crud import sample_reception as sample_reception_svc
|
|
from app.crud.qc_work_order import (
|
|
_load_sample_type_name_map,
|
|
_qc_biz_type_label,
|
|
_reception_source_label,
|
|
_sample_type_name_for_reception,
|
|
_work_order_qc_biz_type,
|
|
)
|
|
from app.models.models import (
|
|
BizProtocolSample,
|
|
BizQcTask,
|
|
BizQcWorkOrder,
|
|
BizSampleReception,
|
|
)
|
|
from app.models.research_donation import BizResearchDonationSample
|
|
|
|
|
|
def _dec(v: Any) -> Optional[float]:
|
|
if v is None:
|
|
return None
|
|
return float(v)
|
|
|
|
|
|
def _fmt_dt(t: Optional[datetime]) -> str:
|
|
return t.strftime("%Y-%m-%d %H:%M:%S") if t else ""
|
|
|
|
|
|
def _gen_task_no(db: Session, work_order: BizQcWorkOrder, seq: int) -> str:
|
|
return f"{work_order.work_order_no}-{seq:02d}"
|
|
|
|
|
|
_CONCLUSION_LABELS = {"tg": "通过", "btg": "不通过"}
|
|
|
|
|
|
def _conclusion_label(v: Optional[str]) -> str:
|
|
key = (v or "").strip()
|
|
return _CONCLUSION_LABELS.get(key, key)
|
|
|
|
|
|
def _task_to_dict(
|
|
t: BizQcTask,
|
|
work_order_no: str = "",
|
|
db: Optional[Session] = None,
|
|
tenant_no: Optional[str] = None,
|
|
) -> Dict[str, Any]:
|
|
tn = tenant_no or t.tenant_no
|
|
return {
|
|
"id": t.id,
|
|
"workOrderId": t.work_order_id,
|
|
"workOrderNo": work_order_no,
|
|
"taskNo": t.task_no,
|
|
"taskType": t.task_type,
|
|
"sourceLineId": t.source_line_id,
|
|
"itemCode": t.item_code,
|
|
"itemName": t.item_name,
|
|
"companyId": t.company_id or "",
|
|
"companyName": t.company_name or "",
|
|
"feeAmount": _dec(t.fee_amount),
|
|
"taskStatus": t.task_status,
|
|
"conclusion": t.conclusion or "",
|
|
"conclusionLabel": _conclusion_label(t.conclusion),
|
|
"thirdPartyAttachments": t.third_party_attachments or "",
|
|
"attachmentFiles": urls_to_attachment_list(t.third_party_attachments, db, tn) if db else [],
|
|
"completedTime": _fmt_dt(t.completed_time),
|
|
"completedBy": t.completed_by or "",
|
|
"remark": t.remark or "",
|
|
"createTime": _fmt_dt(t.create_time),
|
|
"updateTime": _fmt_dt(t.update_time),
|
|
}
|
|
|
|
|
|
def _task_work_order_context(wo: BizQcWorkOrder, t: BizQcTask) -> Dict[str, Any]:
|
|
biz = _work_order_qc_biz_type(wo)
|
|
source = getattr(wo, "reception_source", "cell_storage") or "cell_storage"
|
|
return {
|
|
"receptionId": wo.reception_id,
|
|
"protocolNo": wo.protocol_no or "",
|
|
"sampleCode": wo.sample_code or t.sample_code or "",
|
|
"qcBizType": biz,
|
|
"qcBizTypeLabel": _qc_biz_type_label(biz),
|
|
"receptionSource": source,
|
|
"receptionSourceLabel": _reception_source_label(source),
|
|
"receptionTime": "",
|
|
"handlerName": "",
|
|
"sampleTypeName": "",
|
|
"workOrderStatus": wo.work_order_status or "",
|
|
}
|
|
|
|
|
|
def _enrich_tasks_reception_fields(
|
|
db: Session, items: List[Dict[str, Any]]
|
|
) -> List[Dict[str, Any]]:
|
|
if not items:
|
|
return items
|
|
rec_ids = [x.get("receptionId") for x in items if x.get("receptionId")]
|
|
if not rec_ids:
|
|
return items
|
|
rec_map = {
|
|
r.id: r
|
|
for r in db.query(BizSampleReception)
|
|
.filter(BizSampleReception.id.in_(rec_ids), BizSampleReception.del_flag == 0)
|
|
.all()
|
|
}
|
|
cell_sample_ids = [
|
|
r.protocol_sample_id
|
|
for r in rec_map.values()
|
|
if (r.reception_source or "cell_storage") != "research_donation" and r.protocol_sample_id
|
|
]
|
|
donation_sample_ids = [
|
|
r.donation_sample_id
|
|
for r in rec_map.values()
|
|
if (r.reception_source or "") == "research_donation" and r.donation_sample_id
|
|
]
|
|
protocol_samples: Dict[str, BizProtocolSample] = {}
|
|
if cell_sample_ids:
|
|
protocol_samples = {
|
|
s.id: s
|
|
for s in db.query(BizProtocolSample)
|
|
.filter(BizProtocolSample.id.in_(cell_sample_ids), BizProtocolSample.del_flag == 0)
|
|
.all()
|
|
}
|
|
donation_samples: Dict[str, BizResearchDonationSample] = {}
|
|
if donation_sample_ids:
|
|
donation_samples = {
|
|
s.id: s
|
|
for s in db.query(BizResearchDonationSample)
|
|
.filter(
|
|
BizResearchDonationSample.id.in_(donation_sample_ids),
|
|
BizResearchDonationSample.del_flag == 0,
|
|
)
|
|
.all()
|
|
}
|
|
type_ids = list(
|
|
{
|
|
s.sample_type_id
|
|
for s in list(protocol_samples.values()) + list(donation_samples.values())
|
|
if s.sample_type_id
|
|
}
|
|
)
|
|
type_map = _load_sample_type_name_map(db, type_ids)
|
|
for item in items:
|
|
rec = rec_map.get(item.get("receptionId") or "")
|
|
if not rec:
|
|
continue
|
|
item["receptionTime"] = _fmt_dt(rec.reception_time)
|
|
item["handlerName"] = rec.handler_name or ""
|
|
item["sampleTypeName"] = _sample_type_name_for_reception(
|
|
db,
|
|
rec,
|
|
type_map,
|
|
protocol_samples=protocol_samples,
|
|
donation_samples=donation_samples,
|
|
)
|
|
return items
|
|
|
|
|
|
def _task_row_to_dict(
|
|
t: BizQcTask,
|
|
wo: BizQcWorkOrder,
|
|
db: Session,
|
|
tenant_no: Optional[str],
|
|
) -> Dict[str, Any]:
|
|
out = _task_to_dict(t, wo.work_order_no or "", db, tenant_no)
|
|
out.update(_task_work_order_context(wo, t))
|
|
return out
|
|
|
|
|
|
def create_task_for_line(
|
|
db: Session,
|
|
*,
|
|
work_order: BizQcWorkOrder,
|
|
task_type: str,
|
|
source_line_id: str,
|
|
item_code: str,
|
|
item_name: str,
|
|
tenant_no: str,
|
|
user_id: Optional[str] = None,
|
|
create_dept_id: str = "",
|
|
seq: int = 1,
|
|
company_id: Optional[str] = None,
|
|
company_name: Optional[str] = None,
|
|
fee_amount: Optional[Decimal] = None,
|
|
) -> BizQcTask:
|
|
obj = BizQcTask(
|
|
id=generate_uuid(),
|
|
work_order_id=work_order.id,
|
|
task_no=_gen_task_no(db, work_order, seq),
|
|
task_type=task_type,
|
|
source_line_id=source_line_id,
|
|
sample_code=work_order.sample_code or None,
|
|
item_code=item_code,
|
|
item_name=item_name,
|
|
company_id=company_id,
|
|
company_name=company_name,
|
|
fee_amount=fee_amount,
|
|
task_status="dzx",
|
|
tenant_no=tenant_no,
|
|
order_num=seq,
|
|
create_by=user_id,
|
|
create_dept_id=create_dept_id or "",
|
|
create_time=datetime.now(),
|
|
update_by=user_id,
|
|
update_time=datetime.now(),
|
|
del_flag=0,
|
|
)
|
|
db.add(obj)
|
|
db.flush()
|
|
return obj
|
|
|
|
|
|
def list_tasks(
|
|
db: Session,
|
|
*,
|
|
page: int = 1,
|
|
page_size: int = 10,
|
|
keyword: Optional[str] = None,
|
|
task_type: Optional[str] = None,
|
|
task_status: Optional[str] = None,
|
|
work_order_id: Optional[str] = None,
|
|
qc_biz_type: Optional[str] = None,
|
|
tenant_no: Optional[str] = None,
|
|
) -> Tuple[List[Dict[str, Any]], int]:
|
|
def _filtered():
|
|
q0 = (
|
|
db.query(BizQcTask, BizQcWorkOrder)
|
|
.join(
|
|
BizQcWorkOrder,
|
|
and_(
|
|
BizQcWorkOrder.id == BizQcTask.work_order_id,
|
|
BizQcWorkOrder.del_flag == 0,
|
|
),
|
|
)
|
|
.filter(BizQcTask.del_flag == 0)
|
|
)
|
|
if tenant_no:
|
|
q0 = q0.filter(BizQcTask.tenant_no == tenant_no)
|
|
if work_order_id:
|
|
q0 = q0.filter(BizQcTask.work_order_id == work_order_id)
|
|
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),
|
|
BizQcWorkOrder.protocol_no.like(like),
|
|
)
|
|
)
|
|
tt = (task_type or "").strip()
|
|
if tt:
|
|
q0 = q0.filter(BizQcTask.task_type == tt)
|
|
st = (task_status or "").strip()
|
|
if st:
|
|
q0 = q0.filter(BizQcTask.task_status == st)
|
|
bt = (qc_biz_type or "").strip()
|
|
if bt:
|
|
q0 = q0.filter(BizQcWorkOrder.qc_biz_type == bt)
|
|
return q0
|
|
|
|
total = _filtered().order_by(None).with_entities(func.count(BizQcTask.id)).scalar() or 0
|
|
page = max(1, page)
|
|
page_size = min(max(1, page_size), 100)
|
|
rows = (
|
|
_filtered()
|
|
.order_by(
|
|
BizQcTask.create_time.desc(),
|
|
BizQcWorkOrder.work_order_no.desc(),
|
|
BizQcTask.task_no.asc(),
|
|
)
|
|
.offset((page - 1) * page_size)
|
|
.limit(page_size)
|
|
.all()
|
|
)
|
|
items = [_task_row_to_dict(t, wo, db, tenant_no) for t, wo in rows]
|
|
return _enrich_tasks_reception_fields(db, items), int(total)
|
|
|
|
|
|
def list_tasks_by_work_order(db: Session, work_order_id: str, tenant_no: str) -> List[Dict[str, Any]]:
|
|
rows = (
|
|
db.query(BizQcTask, BizQcWorkOrder)
|
|
.join(BizQcWorkOrder, BizQcWorkOrder.id == BizQcTask.work_order_id)
|
|
.filter(
|
|
BizQcTask.work_order_id == work_order_id,
|
|
BizQcTask.tenant_no == tenant_no,
|
|
BizQcTask.del_flag == 0,
|
|
BizQcWorkOrder.del_flag == 0,
|
|
)
|
|
.order_by(BizQcTask.order_num.asc())
|
|
.all()
|
|
)
|
|
items = [_task_row_to_dict(t, wo, db, tenant_no) for t, wo in rows]
|
|
return _enrich_tasks_reception_fields(db, items)
|
|
|
|
|
|
def get_task(
|
|
db: Session, row_id: str, tenant_no: Optional[str] = None
|
|
) -> Optional[BizQcTask]:
|
|
q = db.query(BizQcTask).filter(BizQcTask.id == row_id, BizQcTask.del_flag == 0)
|
|
if tenant_no:
|
|
q = q.filter(BizQcTask.tenant_no == tenant_no)
|
|
return q.first()
|
|
|
|
|
|
def get_task_detail(db: Session, row_id: str, tenant_no: str) -> Dict[str, Any]:
|
|
row = (
|
|
db.query(BizQcTask, BizQcWorkOrder)
|
|
.join(BizQcWorkOrder, BizQcWorkOrder.id == BizQcTask.work_order_id)
|
|
.filter(
|
|
BizQcTask.id == row_id,
|
|
BizQcTask.tenant_no == tenant_no,
|
|
BizQcTask.del_flag == 0,
|
|
BizQcWorkOrder.del_flag == 0,
|
|
)
|
|
.first()
|
|
)
|
|
if not row:
|
|
raise ValueError("任务不存在")
|
|
t, wo = row
|
|
out = _task_row_to_dict(t, wo, db, tenant_no)
|
|
out = _enrich_tasks_reception_fields(db, [out])[0]
|
|
reception_detail = sample_reception_svc.get_reception_by_id(db, wo.reception_id, tenant_no)
|
|
if reception_detail:
|
|
out["reception"] = reception_detail
|
|
return out
|
|
|
|
|
|
def start_task(db: Session, row_id: str, *, tenant_no: str, user_id: Optional[str]) -> BizQcTask:
|
|
t = get_task(db, row_id, tenant_no=tenant_no)
|
|
if not t:
|
|
raise ValueError("任务不存在")
|
|
if t.task_type != "sysz":
|
|
raise ValueError("仅实验室自检任务可开始")
|
|
if t.task_status != "dzx":
|
|
raise ValueError("当前状态不可开始")
|
|
t.task_status = "jxz"
|
|
t.update_by = user_id
|
|
t.update_time = datetime.now()
|
|
db.add(t)
|
|
db.commit()
|
|
db.refresh(t)
|
|
return t
|
|
|
|
|
|
def _set_task_remark(t: BizQcTask, remark: Optional[str]) -> None:
|
|
t.remark = (remark or "").strip() or None
|
|
|
|
|
|
def complete_task(
|
|
db: Session,
|
|
row_id: str,
|
|
*,
|
|
tenant_no: str,
|
|
user_id: Optional[str],
|
|
conclusion: Optional[str] = None,
|
|
remark: Optional[str] = None,
|
|
) -> BizQcTask:
|
|
t = get_task(db, row_id, tenant_no=tenant_no)
|
|
if not t:
|
|
raise ValueError("任务不存在")
|
|
concl = (conclusion or "").strip() if conclusion is not None else ""
|
|
if concl and concl not in ("tg", "btg"):
|
|
raise ValueError("检测结论无效")
|
|
if t.task_type == "sysz":
|
|
if t.task_status not in ("dzx", "jxz"):
|
|
raise ValueError("当前状态不可完成")
|
|
elif t.task_type == "dsfjc":
|
|
if not (t.third_party_attachments or "").strip():
|
|
raise ValueError("请先上传第三方检测材料")
|
|
else:
|
|
raise ValueError("任务类型无效")
|
|
t.task_status = "ywc"
|
|
if concl:
|
|
t.conclusion = concl
|
|
_set_task_remark(t, remark)
|
|
t.completed_time = datetime.now()
|
|
t.completed_by = user_id
|
|
t.update_by = user_id
|
|
t.update_time = datetime.now()
|
|
db.add(t)
|
|
db.commit()
|
|
db.refresh(t)
|
|
from app.crud import qc_work_order as wo_svc
|
|
|
|
wo_svc.try_complete_work_order(db, t.work_order_id, tenant_no)
|
|
return t
|
|
|
|
|
|
def apply_task_result(
|
|
db: Session,
|
|
row_id: str,
|
|
*,
|
|
tenant_no: str,
|
|
user_id: Optional[str],
|
|
conclusion: str,
|
|
remark: Optional[str] = None,
|
|
) -> BizQcTask:
|
|
"""保存检测结果与结论;任务未完成时顺带标记为已完成。"""
|
|
t = get_task(db, row_id, tenant_no=tenant_no)
|
|
if not t:
|
|
raise ValueError("任务不存在")
|
|
concl = (conclusion or "").strip()
|
|
if concl not in ("tg", "btg"):
|
|
raise ValueError("请选择检测结论(通过/不通过)")
|
|
if (t.task_status or "").strip() == "ywc":
|
|
t.conclusion = concl
|
|
_set_task_remark(t, remark if remark is not None else "")
|
|
t.update_by = user_id
|
|
t.update_time = datetime.now()
|
|
db.add(t)
|
|
db.commit()
|
|
db.refresh(t)
|
|
return t
|
|
return complete_task(
|
|
db,
|
|
row_id,
|
|
tenant_no=tenant_no,
|
|
user_id=user_id,
|
|
conclusion=concl,
|
|
remark=remark,
|
|
)
|
|
|
|
|
|
def update_task_attachments(
|
|
db: Session,
|
|
row_id: str,
|
|
*,
|
|
attachments: str,
|
|
tenant_no: str,
|
|
user_id: Optional[str],
|
|
) -> BizQcTask:
|
|
t = get_task(db, row_id, tenant_no=tenant_no)
|
|
if not t:
|
|
raise ValueError("任务不存在")
|
|
t.third_party_attachments = (attachments or "").strip() or None
|
|
if (
|
|
t.task_type == "dsfjc"
|
|
and t.task_status == "dzx"
|
|
and t.third_party_attachments
|
|
):
|
|
t.task_status = "jxz"
|
|
t.update_by = user_id
|
|
t.update_time = datetime.now()
|
|
db.add(t)
|
|
db.commit()
|
|
db.refresh(t)
|
|
return t
|