feat(material): 新增 H5 大类/种类聚合查询接口

Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>
This commit is contained in:
caoqianming 2026-04-24 15:51:17 +08:00
parent 807e7de443
commit 815e8df10b
1 changed files with 32 additions and 0 deletions

View File

@ -7,6 +7,7 @@ from openpyxl import Workbook
from openpyxl.worksheet.datavalidation import DataValidation
from openpyxl.styles import Alignment, Border, Font, PatternFill, Side
from openpyxl.utils import get_column_letter
from django.db.models import Count
from rest_framework import generics, status
from rest_framework.decorators import api_view, action
from rest_framework.permissions import IsAuthenticated
@ -483,6 +484,37 @@ class MaterialViewSet(ModelViewSet):
return Response(result)
@action(detail=False, methods=['get'], url_path='categories-by-major')
def categories_by_major(self, request):
major = request.query_params.get('major_category')
if not major:
return Response({"detail": "major_category 参数必填"}, status=status.HTTP_400_BAD_REQUEST)
qs = (Material.objects
.filter(major_category=major, status='approved')
.exclude(material_category__isnull=True)
.exclude(material_category__exact='')
.values('material_category')
.annotate(count=Count('id'))
.order_by('material_category'))
data = [{"value": row['material_category'], "count": row['count']} for row in qs]
return Response(data)
@action(detail=False, methods=['get'], url_path='subcategories-by-category')
def subcategories_by_category(self, request):
major = request.query_params.get('major_category')
category = request.query_params.get('material_category')
if not major or not category:
return Response({"detail": "major_category 和 material_category 均必填"}, status=status.HTTP_400_BAD_REQUEST)
qs = (Material.objects
.filter(major_category=major, material_category=category, status='approved')
.exclude(material_subcategory__isnull=True)
.exclude(material_subcategory__exact='')
.values('material_subcategory')
.annotate(count=Count('id'))
.order_by('material_subcategory'))
data = [{"value": row['material_subcategory'], "count": row['count']} for row in qs]
return Response(data)
class MaterialCategoryViewSet(ModelViewSet):
"""