feat: 商家订单准确统计、客服备注与按客服筛选
This commit is contained in:
@@ -78,4 +78,21 @@ def apply_merchant_order_list_filters(qs, data):
|
||||
| Q(Order__PlayerID__exact=keyword)
|
||||
| Q(Order__ExternalOrderID__exact=keyword)
|
||||
)
|
||||
|
||||
staff_member_id = data.get('staff_member_id')
|
||||
if staff_member_id:
|
||||
from merchant_ops.models import MerchantOrderDispatch
|
||||
try:
|
||||
smid = int(staff_member_id)
|
||||
except (TypeError, ValueError):
|
||||
return qs.none()
|
||||
merchant_id = data.get('_merchant_id_filter')
|
||||
disp_q = MerchantOrderDispatch.objects.filter(staff_member_id=smid)
|
||||
if merchant_id:
|
||||
disp_q = disp_q.filter(merchant_id=merchant_id)
|
||||
order_ids = list(disp_q.values_list('order_id', flat=True))
|
||||
if not order_ids:
|
||||
return qs.none()
|
||||
qs = qs.filter(Order__OrderID__in=order_ids)
|
||||
|
||||
return qs
|
||||
|
||||
71
orders/services/merchant_order_stats.py
Normal file
71
orders/services/merchant_order_stats.py
Normal file
@@ -0,0 +1,71 @@
|
||||
"""商家订单维度统计(从订单表聚合,保证金额准确)。"""
|
||||
from decimal import Decimal
|
||||
|
||||
from django.db.models import Count, Sum
|
||||
|
||||
from merchant_ops.models import MerchantOrderDispatch
|
||||
from orders.models import MerchantOrderExt
|
||||
|
||||
STATUS_PENDING = 8
|
||||
STATUS_COMPLETED = 3
|
||||
STATUS_REFUNDED = 5
|
||||
|
||||
|
||||
def _money_str(val):
|
||||
if val is None:
|
||||
return '0.00'
|
||||
return str(val)
|
||||
|
||||
|
||||
def _agg_amount(qs, status=None):
|
||||
if status is not None:
|
||||
qs = qs.filter(Order__Status=status)
|
||||
row = qs.aggregate(cnt=Count('id'), amt=Sum('Order__Amount'))
|
||||
return int(row['cnt'] or 0), _money_str(row['amt'])
|
||||
|
||||
|
||||
def build_merchant_order_stats(base_qs):
|
||||
"""
|
||||
base_qs: MerchantOrderExt queryset(已含商家/客服范围与时间等筛选)
|
||||
返回订单统计字典。
|
||||
"""
|
||||
pending_cnt, pending_amt = _agg_amount(base_qs, STATUS_PENDING)
|
||||
completed_cnt, completed_amt = _agg_amount(base_qs, STATUS_COMPLETED)
|
||||
refund_cnt, refund_amt = _agg_amount(base_qs, STATUS_REFUNDED)
|
||||
dispatch_cnt, dispatch_amt = _agg_amount(base_qs, None)
|
||||
|
||||
return {
|
||||
'pending_count': pending_cnt,
|
||||
'pending_amount': pending_amt,
|
||||
'completed_count': completed_cnt,
|
||||
'completed_amount': completed_amt,
|
||||
'refund_count': refund_cnt,
|
||||
'refund_amount': refund_amt,
|
||||
'dispatch_count': dispatch_cnt,
|
||||
'dispatch_amount': dispatch_amt,
|
||||
}
|
||||
|
||||
|
||||
def merchant_ext_qs(merchant_id, zhuangtai_list=None):
|
||||
qs = MerchantOrderExt.query.filter(MerchantID=merchant_id).select_related('Order')
|
||||
if zhuangtai_list is not None:
|
||||
qs = qs.filter(Order__Status__in=zhuangtai_list)
|
||||
return qs
|
||||
|
||||
|
||||
def filter_by_staff_member(qs, merchant_id, staff_member_id):
|
||||
if not staff_member_id:
|
||||
return qs
|
||||
try:
|
||||
mid = int(staff_member_id)
|
||||
except (TypeError, ValueError):
|
||||
return qs.none()
|
||||
order_ids = list(
|
||||
MerchantOrderDispatch.query.filter(
|
||||
merchant_id=merchant_id,
|
||||
staff_member_id=mid,
|
||||
).values_list('order_id', flat=True)
|
||||
)
|
||||
if not order_ids:
|
||||
return qs.none()
|
||||
return qs.filter(Order__OrderID__in=order_ids)
|
||||
Reference in New Issue
Block a user