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 ФИО Заряд - Отметка + Местонахождение