отображение уличных тегов в таблице

This commit is contained in:
zakhy 2025-07-09 17:30:07 +07:00
parent 22873235a4
commit 7f3adf5592
3 changed files with 25 additions and 35 deletions

View File

@ -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

View File

@ -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,

View File

@ -37,7 +37,7 @@
<th>SN</th>
<th>ФИО</th>
<th>Заряд</th>
<th>Отметка</th>
<th>Местонахождение</th>
</tr>
</thead>
<tbody>