fix --> sync livestocks with table ExcelLivestocks

This commit is contained in:
2026-02-10 12:30:41 +03:30
parent 67fa1e23e7
commit 1e773ef53d
4 changed files with 191 additions and 22 deletions

View File

@@ -4,12 +4,12 @@ from django.core.management.base import BaseCommand
from django.db import transaction
from apps.herd.models import Herd
from apps.livestock.models import LiveStock, LiveStockType
from apps.tag.models import Tag, TemporaryTags
from apps.livestock.models import LiveStock, LiveStockType, ExcelLiveStocks
from apps.tag.models import Tag
from common.generics import parse_birthdate
BATCH_SIZE = 5000
CHUNK_SIZE = 10000
BATCH_SIZE = 1000
CHUNK_SIZE = 1000
class Command(BaseCommand):
@@ -22,16 +22,16 @@ class Command(BaseCommand):
)
qs = (
TemporaryTags.objects
ExcelLiveStocks.objects
.filter(sync_status__isnull=True)
.only('herd_code', 'birthdate', 'gender', 'tag')
.only('herd_code', 'birthdate', 'gender', 'national_id')
)
total = qs.count()
processed = 0
start_time = time.time()
LOG_EVERY = 10000
LOG_EVERY = 1000
buffer = []
for temp in qs.iterator(chunk_size=CHUNK_SIZE):
@@ -64,7 +64,7 @@ class Command(BaseCommand):
self.stdout.write(self.style.SUCCESS("DONE ✅"))
def process_batch(self, temps):
herd_codes = {t.herd_code for t in temps if t.herd_code}
herd_codes = {self.normalize_herd_code(t.herd_code) for t in temps if t.herd_code}
herds = {
h.code: h
@@ -90,7 +90,7 @@ class Command(BaseCommand):
existing_tags = {
t.tag_code: t
for t in Tag.objects.filter(
tag_code__in=[t.tag for t in temps if t.tag]
tag_code__in=[t.national_id for t in temps if t.national_id]
)
}
@@ -99,28 +99,28 @@ class Command(BaseCommand):
new_tags = []
for temp in temps:
herd = herds.get(temp.herd_code)
herd = herds.get(self.normalize_herd_code(temp.herd_code))
if not herd:
continue
birthdate = parse_birthdate(temp.birthdate)
gender = 1 if temp.gender == 'M' else 2
livestock_type = livestock_types.get(temp.type)
livestock_type = livestock_types.get(temp.species)
weight_type = livestock_type.weight_type
key = (temp.herd_code, birthdate, gender)
key = (self.normalize_herd_code(temp.herd_code), birthdate, gender)
livestock = livestock_map.get(key)
if not livestock:
if not temp.tag:
if not temp.national_id:
continue
tag = existing_tags.get(temp.tag)
tag = existing_tags.get(temp.national_id)
if not tag:
tag = Tag(tag_code=temp.tag, status='A')
tag = Tag(tag_code=temp.national_id, status='A')
new_tags.append(tag)
existing_tags[temp.tag] = tag
existing_tags[temp.national_id] = tag
livestock = LiveStock(
herd=herd,
@@ -136,13 +136,13 @@ class Command(BaseCommand):
temp.sync_status = 'S'
continue
if livestock.tag is None and temp.tag:
tag = existing_tags.get(temp.tag)
if livestock.tag is None and temp.national_id:
tag = existing_tags.get(temp.national_id)
if not tag:
tag = Tag(tag_code=temp.tag, status='A')
tag = Tag(tag_code=temp.national_id, status='A')
new_tags.append(tag)
existing_tags[temp.tag] = tag
existing_tags[temp.national_id] = tag
livestock.tag = tag
updated_livestock.append(livestock)
@@ -151,18 +151,24 @@ class Command(BaseCommand):
with transaction.atomic():
Tag.objects.bulk_create(new_tags, batch_size=BATCH_SIZE)
LiveStock.objects.bulk_create(
ss = LiveStock.objects.bulk_create(
new_livestock,
batch_size=BATCH_SIZE,
ignore_conflicts=True
)
print(ss)
LiveStock.objects.bulk_update(
updated_livestock,
['tag'],
batch_size=BATCH_SIZE
)
TemporaryTags.objects.bulk_update(
ExcelLiveStocks.objects.bulk_update(
temps,
['sync_status'],
batch_size=BATCH_SIZE
)
def normalize_herd_code(self, value, length=10):
if value is None:
return None
return str(value).strip().zfill(length)