@@ -0,0 +1,40 @@
|
||||
from __future__ import annotations
|
||||
|
||||
from app.modules.zootech.lab.models import LabRationLine
|
||||
|
||||
|
||||
def find_rations_by_component(component_id: str) -> list[str]:
|
||||
"""Recipe IDs с не удалёнными строками рациона, использующими component."""
|
||||
if not component_id:
|
||||
return []
|
||||
rows = (
|
||||
LabRationLine.query.filter_by(component_id=component_id, is_deleted=False)
|
||||
.with_entities(LabRationLine.recipe_id)
|
||||
.distinct()
|
||||
.all()
|
||||
)
|
||||
return sorted({str(r[0]) for r in rows if r[0]})
|
||||
|
||||
|
||||
def recalculate_rations(recipe_ids: list[str], user_id: str = "system") -> dict:
|
||||
from app.modules.zootech.lab.commands.recalculate import recalculate_ration
|
||||
|
||||
ok: list[str] = []
|
||||
failed: dict[str, str] = {}
|
||||
results: dict[str, dict] = {}
|
||||
for recipe_id in recipe_ids:
|
||||
try:
|
||||
results[recipe_id] = recalculate_ration(recipe_id, user_id)
|
||||
ok.append(recipe_id)
|
||||
except Exception as exc:
|
||||
failed[recipe_id] = str(exc)
|
||||
return {"ok": ok, "failed": failed, "results": results}
|
||||
|
||||
|
||||
def on_component_nutrients_changed(component_id: str, user_id: str = "system") -> dict:
|
||||
"""Найти и пересчитать все рационы с данным компонентом (для будущего UI)."""
|
||||
recipe_ids = find_rations_by_component(component_id)
|
||||
if not recipe_ids:
|
||||
return {"recipeIds": [], "ok": [], "failed": {}}
|
||||
report = recalculate_rations(recipe_ids, user_id)
|
||||
return {"recipeIds": recipe_ids, **report}
|
||||
Reference in New Issue
Block a user