54 lines
1.6 KiB
Python
54 lines
1.6 KiB
Python
from __future__ import annotations
|
|
|
|
from typing import Any
|
|
|
|
from app.modules.zootech.lab.constants import DIFF_TOLERANCE_KG
|
|
|
|
|
|
def compare_master_execution(
|
|
master_lines: list[dict[str, Any]],
|
|
execution_lines: list[dict[str, Any]],
|
|
) -> dict[str, Any]:
|
|
master_map: dict[str, float] = {}
|
|
for line in master_lines:
|
|
if not line.get("in_ration"):
|
|
continue
|
|
cid = line.get("component_id")
|
|
if not cid:
|
|
continue
|
|
master_map[str(cid)] = float(line.get("daily_kg") or 0)
|
|
|
|
exec_map: dict[str, float] = {}
|
|
for line in execution_lines:
|
|
cid = line.get("component_id")
|
|
if not cid:
|
|
continue
|
|
exec_map[str(cid)] = float(line.get("daily_kg_total") or 0)
|
|
|
|
all_ids = set(master_map) | set(exec_map)
|
|
diff_lines = []
|
|
has_changes = False
|
|
for cid in sorted(all_ids):
|
|
m = master_map.get(cid)
|
|
e = exec_map.get(cid)
|
|
reasons = []
|
|
if m is None:
|
|
reasons.append("missing_in_master")
|
|
has_changes = True
|
|
if e is None:
|
|
reasons.append("missing_in_execution")
|
|
has_changes = True
|
|
if m is not None and e is not None and abs(m - e) > DIFF_TOLERANCE_KG:
|
|
reasons.append("kg_mismatch")
|
|
has_changes = True
|
|
if reasons:
|
|
diff_lines.append(
|
|
{
|
|
"component_id": cid,
|
|
"master_kg": m,
|
|
"execution_kg": e,
|
|
"reasons": reasons,
|
|
}
|
|
)
|
|
return {"has_changes": has_changes, "lines": diff_lines}
|