fix(api): сравнение — одна строка на клинику с показательной ценой
Проверено end-to-end на реальной БД: 8871 позиций, 73.9% автонормализации, живое сравнение цен по клиникам во фронте. Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
This commit is contained in:
+20
-3
@@ -66,13 +66,30 @@ def list_services(specialty: str | None = None, q: str | None = None, limit: int
|
||||
@app.get("/services/{service_id}/partners", response_model=list[PriceOut],
|
||||
summary="Кто оказывает услугу и по какой цене")
|
||||
def service_partners(service_id: str):
|
||||
"""Одна строка на клинику с показательной ценой (резидент или минимальный тариф)."""
|
||||
sql = """SELECT pi.partner_id, p.name AS partner_name, pi.price_resident, pi.price_nonresident,
|
||||
pi.prices, pi.effective_date
|
||||
FROM price_item pi JOIN partner p ON pi.partner_id = p.partner_id
|
||||
WHERE pi.service_id = ? AND pi.is_active = 1
|
||||
ORDER BY COALESCE(pi.price_resident, pi.price_nonresident)"""
|
||||
WHERE pi.service_id = ? AND pi.is_active = 1"""
|
||||
best: dict[str, PriceOut] = {}
|
||||
with db() as conn:
|
||||
return [_price_out(r) for r in conn.execute(sql, [service_id])]
|
||||
for r in conn.execute(sql, [service_id]):
|
||||
prices = json.loads(r["prices"] or "{}")
|
||||
display = r["price_resident"] or (min(prices.values()) if prices else None)
|
||||
current = best.get(r["partner_id"])
|
||||
if current is None or (
|
||||
display is not None
|
||||
and (current.price_resident_kzt is None or display < current.price_resident_kzt)
|
||||
):
|
||||
best[r["partner_id"]] = PriceOut(
|
||||
partner_id=r["partner_id"],
|
||||
partner_name=r["partner_name"],
|
||||
price_resident_kzt=display,
|
||||
price_nonresident_kzt=r["price_nonresident"],
|
||||
prices=prices,
|
||||
effective_date=r["effective_date"],
|
||||
)
|
||||
return sorted(best.values(), key=lambda x: x.price_resident_kzt or float("inf"))
|
||||
|
||||
|
||||
@app.get("/partners", response_model=list[PartnerOut], summary="Партнёры")
|
||||
|
||||
Reference in New Issue
Block a user