80 lines
2.2 KiB
Python
80 lines
2.2 KiB
Python
from __future__ import annotations
|
|
|
|
from datetime import datetime
|
|
from typing import Any, Dict, List, Optional
|
|
|
|
from sqlalchemy.orm import Session
|
|
|
|
from app.core.utils import generate_uuid
|
|
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_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 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]]:
|
|
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)
|