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,6 +12,11 @@ class DuplicateRancherException(APIException):
default_detail = "اطلاعات دامدار استعلام شده دارای مشکل میباشد با پشتیبانی تماس بگیرید" # noqa
class RancherOrganizationLinkException(APIException):
status_code = status.HTTP_403_FORBIDDEN
default_detail = "این دامدار دسترسی خرید از سازمان (تعاونی) مرتبط با این دستگاه را ندارد" # noqa
class HerdCapacityException(APIException):
status_code = status.HTTP_403_FORBIDDEN
default_detail = "مقدار حجم سبک و سنگین وارد شده از ظرفیت گله بیشتر میباشد" # noqa

View File

@@ -0,0 +1,39 @@
from multiprocessing import cpu_count, Pool
from django.core.management.base import BaseCommand
from django.db import connection
from apps.herd.models import Rancher
from .link_ranchers_parallel_worker import process_city
class Command(BaseCommand):
help = "Parallel link ranchers to cooperative by city"
def add_arguments(self, parser):
parser.add_argument(
'--worker',
type=int,
default=cpu_count() // 2
)
def handle(self, *args, **options):
workers = options['workers']
city_ids = (
Rancher.objects.filter(city__isnull=False)
.values_list('city_id', flat=True)
.distinct()
)
self.stdout.write(
f"Starting parallel sync for {len(city_ids)} cities "
f"with {workers} workers"
)
connection.close()
with Pool(processes=workers) as pool:
pool.map(process_city, city_ids)
self.stdout.write(self.style.SUCCESS("DONE ✅"))

View File

@@ -0,0 +1,55 @@
from django.db import connection, transaction
from apps.herd.models import Organization, Rancher, RancherOrganizationLink
BATCH_SIZE = 2000
def process_city(city_id):
connection.close()
orgs = Organization.objects.filter(
city_id=city_id,
type__key='CO'
)
if not orgs.exists() or orgs.count() > 1:
return
coop = orgs.first()
ranchers = (
Rancher.objects.filter(city_id=city_id)
.only('id')
)
buffer = []
for rancher in ranchers.iterator(chunk_size=BATCH_SIZE):
if RancherOrganizationLink.objects.filter(
rancher_id=rancher.id
).exists():
continue
buffer.append(
RancherOrganizationLink(
rancher_id=rancher.id,
organization_id=coop.id
)
)
if len(buffer) >= BATCH_SIZE:
bulk_insert(buffer)
buffer.clear()
if buffer:
bulk_insert(buffer)
@transaction.atomic
def bulk_insert(objs):
RancherOrganizationLink.objects.bulk_create(
objs,
ignore_conflicts=True
)

View File

@@ -11,6 +11,7 @@ from apps.herd.exception import DuplicateRancherException
from apps.herd.models import Herd, Rancher
from apps.herd.pos.api.v1.serializers import HerdSerializer, RancherSerializer
from apps.livestock.web.api.v1.serializers import LiveStockSerializer
from apps.pos_device.mixins.pos_device_mixin import POSDeviceMixin
from common.tools import CustomOperations
@@ -111,7 +112,7 @@ class HerdViewSet(viewsets.ModelViewSet):
return self.get_paginated_response(serializer.data)
class RancherViewSet(viewsets.ModelViewSet, DynamicSearchMixin):
class RancherViewSet(viewsets.ModelViewSet, DynamicSearchMixin, POSDeviceMixin):
queryset = Rancher.objects.all() # noqa
serializer_class = RancherSerializer
permission_classes = [AllowAny]
@@ -140,11 +141,17 @@ class RancherViewSet(viewsets.ModelViewSet, DynamicSearchMixin):
""" check national code & existence of rancher """
rancher = self.queryset.filter(national_code=request.data['national_code'])
org = self.get_device_organization()
if len(rancher) > 1:
raise DuplicateRancherException()
if rancher.exists():
# if not RancherOrganizationLink.objects.filter(organization=org, rancher=rancher).exists():
# if org.purchase_policy == 'INTERNAL_ONLY':
# raise RancherOrganizationLinkException()
serializer = self.serializer_class(rancher.first())
return Response(serializer.data, status=status.HTTP_200_OK)
else:

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