import - sync_livestock/herd_rancher_sync/purchase_policy & service_area in organization

This commit is contained in:
2025-12-30 11:54:09 +03:30
parent fe68b2db78
commit b94484eaaf
11 changed files with 982 additions and 29 deletions

View File

@@ -12,17 +12,11 @@ class HerdRancherSyncService:
optimized bulk sync for large datasets
"""
# -------------------------
# Cache Cities
# -------------------------
city_map = {
name.strip(): id
for id, name in City.objects.all().values_list('id', 'name')
}
# -------------------------
# Cache existing ranchers
# -------------------------
rancher_map = {
r.national_code: r
for r in Rancher.objects.filter(
@@ -49,9 +43,6 @@ class HerdRancherSyncService:
for temp in queryset.iterator(chunk_size=batch_size):
# -------------------------
# Rancher
# -------------------------
rancher = rancher_map.get(temp.rancher_national_code)
if not rancher:
@@ -66,9 +57,6 @@ class HerdRancherSyncService:
new_ranchers.append(rancher)
rancher_map[temp.rancher_national_code] = rancher
# -------------------------
# Herd
# -------------------------
herd_key = (temp.rancher_national_code, temp.herd_code)
if herd_key in existing_herds:
@@ -95,9 +83,6 @@ class HerdRancherSyncService:
}
)
# -------------------------
# Bulk DB Operations
# -------------------------
with transaction.atomic():
Rancher.objects.bulk_create(
new_ranchers,
@@ -119,7 +104,7 @@ class HerdRancherSyncService:
rancher = rancher_map.get(item["rancher_code"])
if not rancher:
continue # یا raise error
continue
herd = item["herd"]
herd.rancher = rancher