Files
zkxw/backend/app/crud/production_task_set.py
T
2026-08-18 15:38:52 +08:00

59 lines
2.0 KiB
Python

"""生产任务集合 CRUD"""
from __future__ import annotations
from typing import Any, Dict, List, Optional, Set, Tuple
def _strip_or_none(raw: Any) -> Optional[str]:
text = str(raw or "").strip()
return text or None
def normalize_item_payload(raw: Optional[Dict[str, Any]]) -> Optional[Dict[str, Any]]:
data = raw or {}
task_name = str(data.get("taskName") or data.get("task_name") or "").strip()
if not task_name:
return None
route_raw = data.get("routeId") if "routeId" in data else data.get("route_id")
return {
"task_name": task_name,
"route_id": _strip_or_none(route_raw),
"description": _strip_or_none(data.get("description")),
"remark": _strip_or_none(data.get("remark")),
}
def skip_empty_named_tasks(tasks: List[Dict[str, Any]]) -> Tuple[List[Dict[str, Any]], int]:
kept: List[Dict[str, Any]] = []
skipped = 0
for row in tasks or []:
name = str(row.get("task_name") or row.get("taskName") or "").strip()
if not name:
skipped += 1
continue
route_raw = row.get("route_id") if "route_id" in row else row.get("routeId")
name_raw = row.get("route_name") if "route_name" in row else row.get("routeName")
kept.append(
{
"task_name": name,
"route_id": _strip_or_none(route_raw),
"route_name": _strip_or_none(name_raw),
"description": _strip_or_none(row.get("description")),
"remark": _strip_or_none(row.get("remark")),
}
)
return kept, skipped
def collect_missing_route_ids(items: List[Dict[str, Any]], existing_ids: Set[str]) -> List[str]:
missing: List[str] = []
seen: Set[str] = set()
alive = existing_ids or set()
for item in items or []:
rid = _strip_or_none(item.get("route_id"))
if not rid or rid in alive or rid in seen:
continue
seen.add(rid)
missing.append(rid)
return missing