from __future__ import annotations from app.modules.zootech.lab.models import LabAnimalProfile from app.modules.zootech.lab.services.profile_norms import load_norms_api def list_animal_profiles(ration_type: str | None = None) -> list[dict]: q = LabAnimalProfile.query.filter_by(is_deleted=False) if ration_type: q = q.filter_by(ration_type=ration_type.upper()) rows = q.order_by(LabAnimalProfile.profile_key).all() return [_profile_dict(p, include_resolved=False) for p in rows] def load_animal_profile(profile_id: str) -> dict: profile = LabAnimalProfile.query.filter_by(id=profile_id, is_deleted=False).first() if profile is None: raise LookupError("Профиль не найден") return _profile_dict(profile, resolve_norms=True, include_resolved=True) def _profile_dict( profile: LabAnimalProfile, *, resolve_norms: bool = False, include_resolved: bool = False, ) -> dict: norms_api = load_norms_api(profile, resolve_dynamic=include_resolved) if resolve_norms else {} if not resolve_norms: from app.modules.zootech.lab.services.profile_norms import load_norms_dict indicators = load_norms_dict(profile.id) norms_api = {"indicators": indicators} from app.modules.zootech.lab.services.norms_params import norms_params_api out: dict = { "id": profile.id, "profileKey": profile.profile_key, "label": profile.label, "rationType": profile.ration_type, "massKg": profile.mass_kg, "milkYieldKg": profile.milk_yield_kg, "externalNo": profile.external_no, "normsMethod": profile.norms_method or "wesp", "normsParams": norms_params_api(profile), "normsData": norms_api, "norms": norms_api.get("indicators") or {}, "normsProfileKey": profile.profile_key, "legacyNormsRemapped": False, } if include_resolved: out["resolvedNorms"] = norms_api.get("resolvedIndicators") or {} out["dynamicNorms"] = norms_api.get("dynamicNorms") or {} if norms_api.get("coverage"): out["coverage"] = norms_api["coverage"] if norms_api.get("normsMeta"): out["normsMeta"] = norms_api["normsMeta"] return out