From 7f3adf5592bcc2cfef215e3e60d56dcf789978e5 Mon Sep 17 00:00:00 2001 From: zakhy <=> Date: Wed, 9 Jul 2025 17:30:07 +0700 Subject: [PATCH] =?UTF-8?q?=D0=BE=D1=82=D0=BE=D0=B1=D1=80=D0=B0=D0=B6?= =?UTF-8?q?=D0=B5=D0=BD=D0=B8=D0=B5=20=D1=83=D0=BB=D0=B8=D1=87=D0=BD=D1=8B?= =?UTF-8?q?=D1=85=20=D1=82=D0=B5=D0=B3=D0=BE=D0=B2=20=D0=B2=20=D1=82=D0=B0?= =?UTF-8?q?=D0=B1=D0=BB=D0=B8=D1=86=D0=B5?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../apps/eps/services/statistic/repository.py | 44 +++++-------------- .../apps/eps/services/statistic/service.py | 14 +++++- .../templates/eps/statistic/statistic.html | 2 +- 3 files changed, 25 insertions(+), 35 deletions(-) diff --git a/positioning/apps/eps/services/statistic/repository.py b/positioning/apps/eps/services/statistic/repository.py index 2687576..7169525 100644 --- a/positioning/apps/eps/services/statistic/repository.py +++ b/positioning/apps/eps/services/statistic/repository.py @@ -1,38 +1,18 @@ import datetime +from apps.eps.models.tags.model_tags import Tag from apps.eps.repositories.worker_repository import WorkerRepository -from django.db import connection +from apps.workers.models.model_workers import Worker +from django.db.models import Q, QuerySet class WorkerStatisticRepository(WorkerRepository): - def get_workers_with_threshold(self, threshold: datetime.datetime) -> list[dict]: - data = [] - # TODO техдолг - query = """ - SELECT concat(w."firstName", ' ', w."lastName"), - t.sn, - t.charge, - z.name - FROM eps_tag t - LEFT JOIN workers_worker w ON t.id = w.tag_id - INNER JOIN eps_zone z ON t.zone_id = z.id - WHERE t.date_time >= %s - AND z.name != 'АБК' \ - """ - params = [threshold] - - with connection.cursor() as cursor: - cursor.execute(query, params) - recent_tags = cursor.fetchall() - - for tag in recent_tags: - fio, sn, charge, zone = tag - data.append( - { - "fio": fio if fio else sn, - "sn": sn, - "charge": round(charge * 100), - "location": zone, - }, - ) - return data + def get_workers_with_threshold( + self, threshold: datetime.datetime + ) -> QuerySet[Worker]: + condition = Q(date_time__gte=threshold) | Q(date_time_street__gte=threshold) + active_tags = Tag.objects.filter(condition) + workers_with_active_tags = Worker.objects.filter( + tag__in=active_tags + ).select_related("tag", "tag__zone") + return workers_with_active_tags diff --git a/positioning/apps/eps/services/statistic/service.py b/positioning/apps/eps/services/statistic/service.py index 2b486f9..796f732 100644 --- a/positioning/apps/eps/services/statistic/service.py +++ b/positioning/apps/eps/services/statistic/service.py @@ -16,8 +16,18 @@ class EpsStatisticService(BaseService): tag_timeout = int(Settings.get_settings("tag_timeout", 300)) time_threshold = self.get_current_time() - timedelta(seconds=tag_timeout) - workers_mas = self.repository.get_workers_with_threshold(time_threshold) - + workers_qs = self.repository.get_workers_with_threshold(time_threshold) + workers_mas = [] + for worker in workers_qs: + location = "На улице" if worker.tag.zone is None else worker.tag.zone.name + workers_mas.append( + { + "fio": worker.name, + "sn": worker.tag.sn, + "charge": worker.tag.charge, + "location": location, + } + ) res = { "error": "", "status": 0, diff --git a/positioning/apps/eps/templates/eps/statistic/statistic.html b/positioning/apps/eps/templates/eps/statistic/statistic.html index 3f0de70..3c88e27 100644 --- a/positioning/apps/eps/templates/eps/statistic/statistic.html +++ b/positioning/apps/eps/templates/eps/statistic/statistic.html @@ -37,7 +37,7 @@ SN ФИО Заряд - Отметка + Местонахождение