26 lines
712 B
Python
26 lines
712 B
Python
from django.db.models import F
|
|||
|
|
from menu.models import Dish
|
||
|
|
|
||
|
|
|
||
|
|
class DishService:
|
||
|
|
"""菜品业务逻辑"""
|
||
|
|
|
||
|
|
@staticmethod
|
||
|
|
def get_queryset(category_id=None, search=None):
|
||
|
|
"""获取菜品查询集"""
|
||
|
|
queryset = Dish.objects.select_related('category')
|
||
|
|
|
||
|
|
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)
|
||
|
|
dish.refresh_from_db(fields=['view_count'])
|
||
|
|
return dish
|