154 lines
4.9 KiB
Python
154 lines
4.9 KiB
Python
from __future__ import annotations
|
|
|
|
from datetime import datetime
|
|
from typing import Any, Dict, List, Optional, Tuple
|
|
|
|
from sqlalchemy.orm import Session
|
|
|
|
from app.core.utils import generate_uuid
|
|
from app.crud import qc_item_param as param_svc
|
|
from app.crud.qc_task import get_task
|
|
from app.models.models import BizQcWorkOrderLabItem, BizQcWorkOrderThirdItem
|
|
from app.models.qc_param import BizQcTaskResult
|
|
|
|
|
|
def _row_to_dict(r: BizQcTaskResult) -> Dict[str, Any]:
|
|
return {
|
|
"id": r.id,
|
|
"paramId": r.param_id or "",
|
|
"paramName": r.param_name,
|
|
"paramCode": r.param_code or "",
|
|
"paramUnit": r.param_unit or "",
|
|
"paramType": r.param_type or "text",
|
|
"paramValue": r.param_value or "",
|
|
}
|
|
|
|
|
|
def _list_saved_results_by_task(
|
|
db: Session, task_id: str, tenant_no: str
|
|
) -> List[Dict[str, Any]]:
|
|
rows = (
|
|
db.query(BizQcTaskResult)
|
|
.filter(
|
|
BizQcTaskResult.task_id == task_id,
|
|
BizQcTaskResult.tenant_no == tenant_no,
|
|
BizQcTaskResult.del_flag == 0,
|
|
)
|
|
.order_by(BizQcTaskResult.create_time.asc())
|
|
.all()
|
|
)
|
|
return [_row_to_dict(r) for r in rows]
|
|
|
|
|
|
def _resolve_task_item_ref(db: Session, task) -> Tuple[Optional[str], Optional[str]]:
|
|
"""返回 (item_source, item_id) 供 biz_qc_item_param 查询。"""
|
|
if task.task_type == "sysz":
|
|
line = (
|
|
db.query(BizQcWorkOrderLabItem)
|
|
.filter(
|
|
BizQcWorkOrderLabItem.id == task.source_line_id,
|
|
BizQcWorkOrderLabItem.del_flag == 0,
|
|
)
|
|
.first()
|
|
)
|
|
if line:
|
|
return "lab", line.lab_test_item_id
|
|
elif task.task_type == "dsfjc":
|
|
line = (
|
|
db.query(BizQcWorkOrderThirdItem)
|
|
.filter(
|
|
BizQcWorkOrderThirdItem.id == task.source_line_id,
|
|
BizQcWorkOrderThirdItem.del_flag == 0,
|
|
)
|
|
.first()
|
|
)
|
|
if line:
|
|
return "third_party", line.third_party_item_id
|
|
return None, None
|
|
|
|
|
|
def _merge_saved_into_template(
|
|
templates: List[Dict[str, Any]], saved: List[Dict[str, Any]]
|
|
) -> List[Dict[str, Any]]:
|
|
by_id = {s["paramId"]: s for s in saved if s.get("paramId")}
|
|
by_code = {s["paramCode"]: s for s in saved if s.get("paramCode")}
|
|
out: List[Dict[str, Any]] = []
|
|
for p in templates:
|
|
pid = p.get("id") or ""
|
|
code = p.get("paramCode") or ""
|
|
hit = by_id.get(pid) or (by_code.get(code) if code else None)
|
|
out.append(
|
|
{
|
|
"paramId": pid,
|
|
"paramName": p.get("paramName", ""),
|
|
"paramCode": code,
|
|
"paramUnit": p.get("paramUnit", ""),
|
|
"paramType": p.get("paramType", "text"),
|
|
"paramValue": (hit.get("paramValue") if hit else "") or "",
|
|
"requiredFlag": p.get("requiredFlag", "0"),
|
|
}
|
|
)
|
|
return out
|
|
|
|
|
|
def list_results_by_task(
|
|
db: Session, task_id: str, tenant_no: str
|
|
) -> List[Dict[str, Any]]:
|
|
"""返回任务结果填写表单:项目参数模板 + 已保存值(无模板时仅返回已保存记录)。"""
|
|
saved = _list_saved_results_by_task(db, task_id, tenant_no)
|
|
task = get_task(db, task_id, tenant_no=tenant_no)
|
|
if not task:
|
|
return saved
|
|
item_source, item_id = _resolve_task_item_ref(db, task)
|
|
if not item_source or not item_id:
|
|
return saved
|
|
templates = param_svc.list_params_by_item(db, item_source, item_id, tenant_no)
|
|
if not templates:
|
|
return saved
|
|
return _merge_saved_into_template(templates, saved)
|
|
|
|
|
|
def save_results_for_task(
|
|
db: Session,
|
|
task_id: str,
|
|
results: List[Dict[str, Any]],
|
|
tenant_no: str,
|
|
user_id: Optional[str] = None,
|
|
) -> List[Dict[str, Any]]:
|
|
task = get_task(db, task_id, tenant_no=tenant_no)
|
|
if not task:
|
|
raise ValueError("任务不存在")
|
|
now = datetime.now()
|
|
db.query(BizQcTaskResult).filter(
|
|
BizQcTaskResult.task_id == task_id,
|
|
BizQcTaskResult.tenant_no == tenant_no,
|
|
BizQcTaskResult.del_flag == 0,
|
|
).update(
|
|
{"del_flag": 1, "update_by": user_id, "update_time": now},
|
|
synchronize_session="fetch",
|
|
)
|
|
db.flush()
|
|
|
|
for res in results:
|
|
obj = BizQcTaskResult(
|
|
id=generate_uuid(),
|
|
task_id=task_id,
|
|
param_id=res.get("paramId") or None,
|
|
param_name=res.get("paramName", ""),
|
|
param_code=res.get("paramCode") or None,
|
|
param_unit=res.get("paramUnit") or None,
|
|
param_type=res.get("paramType", "text"),
|
|
param_value=res.get("paramValue") or None,
|
|
tenant_no=tenant_no,
|
|
create_by=user_id,
|
|
create_dept_id="",
|
|
create_time=now,
|
|
update_by=user_id,
|
|
update_time=now,
|
|
del_flag=0,
|
|
)
|
|
db.add(obj)
|
|
|
|
db.commit()
|
|
return list_results_by_task(db, task_id, tenant_no)
|