import - rancher page main dashboard
This commit is contained in:
@@ -1,47 +1,45 @@
|
||||
from django.db import models
|
||||
from django.db.models import Count, Sum
|
||||
from django.db.models import Sum, Count, Q
|
||||
from django.db.models.functions import Coalesce
|
||||
|
||||
from apps.herd.models import Rancher, Herd
|
||||
from apps.livestock.models import LiveStock
|
||||
|
||||
|
||||
class FarmerDashboardService:
|
||||
""" Dashboard aggregation service for Rancher """
|
||||
class RancherDashboardService:
|
||||
"""
|
||||
Rancher Dashboard Service
|
||||
"""
|
||||
|
||||
@staticmethod
|
||||
def get_basic_info(rancher: Rancher):
|
||||
return {
|
||||
"id": rancher.id,
|
||||
"name": f"{rancher.first_name} {rancher.last_name}",
|
||||
"mobile": rancher.mobile,
|
||||
"national_code": rancher.national_code,
|
||||
"province": rancher.province.name if rancher.province else None,
|
||||
"city": rancher.city.name if rancher.city else None,
|
||||
}
|
||||
def get_main_dashboard(
|
||||
self,
|
||||
rancher_search_fields: list[str] = None,
|
||||
herd_search_fields: list[str] = None,
|
||||
query_string: str = None
|
||||
):
|
||||
"""
|
||||
get ranchers main page dashboard
|
||||
"""
|
||||
ranchers = Rancher.objects.all()
|
||||
herds = Herd.objects.all()
|
||||
|
||||
@staticmethod
|
||||
def get_herd_counts(rancher_id):
|
||||
return Herd.objects.filter(rancher_id=rancher_id).aggregate(
|
||||
total_herds=Count('id'),
|
||||
total_heavy=Sum('heavy_livestock_number'),
|
||||
total_light=Sum('light_livestock_number'),
|
||||
ranchers_data = ranchers.aggregate(
|
||||
total_ranchers_count=Coalesce(Count("id"), 0),
|
||||
total_with_herd=Coalesce(Count("id", filter=Q(without_herd=False)), 0),
|
||||
total_without_herd=Coalesce(Count("id", filter=Q(without_herd=True)), 0),
|
||||
total_natural_ranchers=Coalesce(Count("id", filter=Q(rancher_type='N')), 0),
|
||||
total_legal_ranchers=Coalesce(Count("id", filter=Q(rancher_type='L')), 0),
|
||||
total_industrial_ranchers=Coalesce(Count("id", filter=Q(rancher_type='I')), 0),
|
||||
total_village_ranchers=Coalesce(Count("id", filter=Q(rancher_type='V')), 0),
|
||||
total_nomadic_ranchers=Coalesce(Count("id", filter=Q(rancher_type='N')), 0),
|
||||
)
|
||||
|
||||
@staticmethod
|
||||
def get_livestock_counts(rancher_id):
|
||||
return LiveStock.objects.filter(herd__rancher_id=rancher_id, archive=False).aggregate(
|
||||
total=Count('id'),
|
||||
heavy=Count('id', filter=models.Q(weight_type='H')),
|
||||
light=Count('id', filter=models.Q(weight_type='L')),
|
||||
herds_data = herds.aggregate(
|
||||
total_herds_count=Coalesce(Count("id"), 0),
|
||||
total_industrial_herds_count=Coalesce(Count("id", filter=Q(activity='I')), 0),
|
||||
total_Village_herds_count=Coalesce(Count("id", filter=Q(activity='V')), 0),
|
||||
total_nomadic_herds_count=Coalesce(Count("id", filter=Q(activity='N')), 0),
|
||||
total_heavy_livestock_count=Coalesce(Sum("heavy_livestock_number"), 0),
|
||||
total_light_livestock_count=Coalesce(Sum("light_livestock_number"), 0),
|
||||
)
|
||||
|
||||
@staticmethod
|
||||
def get_livestock_by_type(rancher_id):
|
||||
return (
|
||||
LiveStock.objects.filter(herd__rancher_id=rancher_id, archive=False)
|
||||
.values("type__name")
|
||||
.annotate(count=Count("id"))
|
||||
)
|
||||
|
||||
# @staticmethod
|
||||
# def get_dashboard(rancher_id):
|
||||
return {"rancher_dashboard": ranchers_data, "herd_dashboard": herds_data}
|
||||
|
||||
Reference in New Issue
Block a user