fix: 订单列表 FluentQuery 状态统计污染导致列表为空
This commit is contained in:
@@ -93,6 +93,40 @@ def orders_for_request(request):
|
|||||||
return filter_queryset_by_club(Order.query.all(), request, club_field='ClubID')
|
return filter_queryset_by_club(Order.query.all(), request, club_field='ClubID')
|
||||||
|
|
||||||
|
|
||||||
|
def order_scope_qs(request=None):
|
||||||
|
"""
|
||||||
|
订单列表作用域 QuerySet。
|
||||||
|
request 为 None 时不按俱乐部过滤(旧 /yonghu 订单列表,与改造前一致)。
|
||||||
|
"""
|
||||||
|
from orders.models import Order
|
||||||
|
qs = Order.query.all()
|
||||||
|
if request is not None:
|
||||||
|
qs = filter_queryset_by_club(qs, request, club_field='ClubID')
|
||||||
|
return qs
|
||||||
|
|
||||||
|
|
||||||
|
def count_order_status_buckets(q_conditions, request=None):
|
||||||
|
"""
|
||||||
|
统计各状态订单数。
|
||||||
|
每次从全新 FluentQuery 起链,避免 FluentQuery.filter 原地累积导致列表被滤空。
|
||||||
|
"""
|
||||||
|
from django.db.models import Q
|
||||||
|
|
||||||
|
buckets = (
|
||||||
|
('1,7', Q(Status__in=[1, 7])),
|
||||||
|
('2', Q(Status=2)),
|
||||||
|
('8', Q(Status=8)),
|
||||||
|
('3', Q(Status=3)),
|
||||||
|
('4', Q(Status=4)),
|
||||||
|
('5', Q(Status=5)),
|
||||||
|
('6', Q(Status=6)),
|
||||||
|
)
|
||||||
|
counts = {'all': order_scope_qs(request).filter(q_conditions).count()}
|
||||||
|
for key, status_q in buckets:
|
||||||
|
counts[key] = order_scope_qs(request).filter(q_conditions & status_q).count()
|
||||||
|
return counts
|
||||||
|
|
||||||
|
|
||||||
def paginate_fluent_query(fluent_qs, page, page_size):
|
def paginate_fluent_query(fluent_qs, page, page_size):
|
||||||
"""
|
"""
|
||||||
FluentQuery 分页。直接切片底层 Django QuerySet,绕过 FluentQuery 缓存。
|
FluentQuery 分页。直接切片底层 Django QuerySet,绕过 FluentQuery 缓存。
|
||||||
|
|||||||
@@ -7077,31 +7077,24 @@ class KefuGetOrderListView(APIView):
|
|||||||
if clkf:
|
if clkf:
|
||||||
q_conditions &= Q(AssignedCS__icontains=clkf)
|
q_conditions &= Q(AssignedCS__icontains=clkf)
|
||||||
|
|
||||||
from jituan.services.club_context import orders_for_request, paginate_fluent_query
|
from jituan.services.club_context import (
|
||||||
|
count_order_status_buckets,
|
||||||
|
order_scope_qs,
|
||||||
|
paginate_fluent_query,
|
||||||
|
)
|
||||||
from orders.models import PlatformOrderExt
|
from orders.models import PlatformOrderExt
|
||||||
|
|
||||||
# ---------- 统计与列表共用同一筛选条件 ----------
|
# 旧 /yonghu/kfhqddlb 不按俱乐部过滤,与改造前行为一致
|
||||||
club_orders = orders_for_request(request)
|
stats_qs = order_scope_qs(None).filter(q_conditions)
|
||||||
filtered_orders = club_orders.filter(q_conditions)
|
stats = stats_qs.aggregate(
|
||||||
stats = filtered_orders.aggregate(
|
|
||||||
total_orders=Count('id'),
|
total_orders=Count('id'),
|
||||||
completed_orders=Count('id', filter=Q(Status=3)),
|
completed_orders=Count('id', filter=Q(Status=3)),
|
||||||
refund_orders=Count('id', filter=Q(Status=5)),
|
refund_orders=Count('id', filter=Q(Status=5)),
|
||||||
pending_orders=Count('id', filter=Q(Status__in=[4, 8]))
|
pending_orders=Count('id', filter=Q(Status__in=[4, 8]))
|
||||||
)
|
)
|
||||||
|
status_counts = count_order_status_buckets(q_conditions, request=None)
|
||||||
|
|
||||||
status_counts = {
|
list_qs = order_scope_qs(None).filter(q_conditions).order_by('-CreateTime')
|
||||||
'all': filtered_orders.count(),
|
|
||||||
'1,7': filtered_orders.filter(Status__in=[1, 7]).count(),
|
|
||||||
'2': filtered_orders.filter(Status=2).count(),
|
|
||||||
'8': filtered_orders.filter(Status=8).count(),
|
|
||||||
'3': filtered_orders.filter(Status=3).count(),
|
|
||||||
'4': filtered_orders.filter(Status=4).count(),
|
|
||||||
'5': filtered_orders.filter(Status=5).count(),
|
|
||||||
'6': filtered_orders.filter(Status=6).count(),
|
|
||||||
}
|
|
||||||
|
|
||||||
list_qs = filtered_orders.order_by('-CreateTime')
|
|
||||||
total_count, orders_page = paginate_fluent_query(list_qs, page, page_size)
|
total_count, orders_page = paginate_fluent_query(list_qs, page, page_size)
|
||||||
if total_count > 0 and not orders_page:
|
if total_count > 0 and not orders_page:
|
||||||
start = (max(1, page) - 1) * page_size
|
start = (max(1, page) - 1) * page_size
|
||||||
@@ -7256,30 +7249,23 @@ class KefuGetShangjiaOrderListView(APIView):
|
|||||||
ShopProfile__nicheng__icontains=shangjia_nicheng
|
ShopProfile__nicheng__icontains=shangjia_nicheng
|
||||||
).values_list('UserUID', flat=True))
|
).values_list('UserUID', flat=True))
|
||||||
|
|
||||||
from jituan.services.club_context import orders_for_request, paginate_fluent_query
|
from jituan.services.club_context import (
|
||||||
|
count_order_status_buckets,
|
||||||
|
order_scope_qs,
|
||||||
|
paginate_fluent_query,
|
||||||
|
)
|
||||||
from orders.models import MerchantOrderExt
|
from orders.models import MerchantOrderExt
|
||||||
|
|
||||||
club_orders = orders_for_request(request)
|
stats_qs = order_scope_qs(None).filter(q_conditions)
|
||||||
filtered_orders = club_orders.filter(q_conditions)
|
stats = stats_qs.aggregate(
|
||||||
stats = filtered_orders.aggregate(
|
|
||||||
total_orders=Count('id'),
|
total_orders=Count('id'),
|
||||||
completed_orders=Count('id', filter=Q(Status=3)),
|
completed_orders=Count('id', filter=Q(Status=3)),
|
||||||
refund_orders=Count('id', filter=Q(Status=5)),
|
refund_orders=Count('id', filter=Q(Status=5)),
|
||||||
pending_orders=Count('id', filter=Q(Status__in=[4, 8]))
|
pending_orders=Count('id', filter=Q(Status__in=[4, 8]))
|
||||||
)
|
)
|
||||||
|
status_counts = count_order_status_buckets(q_conditions, request=None)
|
||||||
|
|
||||||
status_counts = {
|
list_qs = order_scope_qs(None).filter(q_conditions).order_by('-CreateTime')
|
||||||
'all': filtered_orders.count(),
|
|
||||||
'1,7': filtered_orders.filter(Status__in=[1, 7]).count(),
|
|
||||||
'2': filtered_orders.filter(Status=2).count(),
|
|
||||||
'8': filtered_orders.filter(Status=8).count(),
|
|
||||||
'3': filtered_orders.filter(Status=3).count(),
|
|
||||||
'4': filtered_orders.filter(Status=4).count(),
|
|
||||||
'5': filtered_orders.filter(Status=5).count(),
|
|
||||||
'6': filtered_orders.filter(Status=6).count(),
|
|
||||||
}
|
|
||||||
|
|
||||||
list_qs = filtered_orders.order_by('-CreateTime')
|
|
||||||
total_count, orders_page = paginate_fluent_query(list_qs, page, page_size)
|
total_count, orders_page = paginate_fluent_query(list_qs, page, page_size)
|
||||||
if total_count > 0 and not orders_page:
|
if total_count > 0 and not orders_page:
|
||||||
start = (max(1, page) - 1) * page_size
|
start = (max(1, page) - 1) * page_size
|
||||||
|
|||||||
Reference in New Issue
Block a user