From 9d48575be47cf1e394d168da1ff9c2f1a951bf0f Mon Sep 17 00:00:00 2001 From: caoqianming Date: Mon, 26 Feb 2024 15:22:36 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20enp=20count=5Fcate=E6=8E=A5=E5=8F=A3?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- apps/enp/views.py | 26 ++++++++++++++++++++++++++ 1 file changed, 26 insertions(+) diff --git a/apps/enp/views.py b/apps/enp/views.py index 73b34153..fa80064a 100644 --- a/apps/enp/views.py +++ b/apps/enp/views.py @@ -8,6 +8,7 @@ from apps.utils.sql import query_all_dict from drf_yasg.utils import swagger_auto_schema from drf_yasg import openapi from rest_framework.response import Response +from django.db.models import Count, Case, When, IntegerField # Create your views here. @@ -34,6 +35,31 @@ class DrainViewSet(CustomModelViewSet): def list(self, request, *args, **kwargs): return super().list(request, *args, **kwargs) + @action(methods=['get'], detail=False, perms_map={'get': '*'}) + def count_cate(self, request, *args, **kwargs): + """排口分类数量 + + 排口分类数量 + """ + queryset = self.filter_queryset(self.get_queryset()) + result = queryset.aggregate( + count=Count('id'), + count_product=Count( + Case(When(cate='product', then=1), output_field=IntegerField())), + count_mtrans=Count( + Case(When(cate='mtrans', then=1), output_field=IntegerField())), + count_mstore=Count( + Case(When(cate='mstore', then=1), output_field=IntegerField())), + ) + json_result = { + 'count': result['count'], + 'count_product': result['count_product'], + 'count_mtrans': result['count_mtrans'], + 'count_mstore': result['count_mstore'] + } + + return Response(json_result) + class DrainEquipViewSet(ListModelMixin, BulkCreateModelMixin, BulkDestroyModelMixin, CustomGenericViewSet): """