41 lines
1.5 KiB
Python
41 lines
1.5 KiB
Python
from __future__ import annotations
|
|
|
|
from app import db
|
|
from app.lab.db_guard import retry_locked
|
|
from app.lab.loaders.execution_loader import load_execution
|
|
from app.lab.models import LabRationLine, LabRecipeRation
|
|
from app.models import Component
|
|
from app.models.base import default_uuid
|
|
|
|
|
|
@retry_locked
|
|
def seed_from_execution(recipe_id: str, user_id: str = "system") -> dict:
|
|
existing = LabRecipeRation.query.filter_by(recipe_id=recipe_id, is_deleted=False).first()
|
|
if existing is not None:
|
|
return {"recipeId": recipe_id, "seeded": False, "reason": "already_exists"}
|
|
|
|
execution = load_execution(recipe_id)
|
|
header = LabRecipeRation(recipe_id=recipe_id, created_by=user_id, updated_by=user_id)
|
|
db.session.add(header)
|
|
|
|
for idx, line in enumerate(execution.lines):
|
|
comp = Component.query.get(line.component_id) if line.component_id else None
|
|
daily_kg = line.daily_kg_total
|
|
db.session.add(
|
|
LabRationLine(
|
|
id=default_uuid(),
|
|
recipe_id=recipe_id,
|
|
component_id=line.component_id,
|
|
ingredient_name=line.name,
|
|
row_index=idx,
|
|
daily_kg=daily_kg,
|
|
in_ration=True,
|
|
in_compound=False,
|
|
created_by=user_id,
|
|
updated_by=user_id,
|
|
)
|
|
)
|
|
header.seed_source = "execution"
|
|
db.session.commit()
|
|
return {"recipeId": recipe_id, "seeded": True, "lines": len(execution.lines)}
|