2026-03-12 11:27:23 +08:00
|
|
|
from django.db.models import F
|
|
|
|
|
from menu.models import Dish
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class DishService:
|
|
|
|
|
"""菜品业务逻辑"""
|
|
|
|
|
|
|
|
|
|
@staticmethod
|
|
|
|
|
def get_queryset(category_id=None, search=None):
|
|
|
|
|
"""获取菜品查询集"""
|
2026-03-23 17:18:59 +08:00
|
|
|
queryset = Dish.objects.select_related('category').only(
|
|
|
|
|
'id', 'name', 'category', 'category__name', 'cover_image',
|
|
|
|
|
'description', 'ingredients', 'view_count', 'created_at', 'updated_at'
|
|
|
|
|
)
|
2026-03-12 11:27:23 +08:00
|
|
|
|
|
|
|
|
if category_id:
|
|
|
|
|
queryset = queryset.filter(category_id=category_id)
|
|
|
|
|
if search:
|
|
|
|
|
queryset = queryset.filter(name__icontains=search)
|
|
|
|
|
|
|
|
|
|
return queryset
|
|
|
|
|
|
|
|
|
|
@staticmethod
|
|
|
|
|
def increment_view_count(dish):
|
|
|
|
|
"""增加浏览次数"""
|
|
|
|
|
Dish.objects.filter(pk=dish.pk).update(view_count=F('view_count') + 1)
|