fix: 处罚/罚款列表俱乐部过滤与全部Tab;回填fadan club_id
This commit is contained in:
@@ -1,18 +1,23 @@
|
||||
"""回填 chufajilu / duoci_fenhong club_id。"""
|
||||
"""回填 chufajilu / fadan / duoci_fenhong club_id。"""
|
||||
from django.core.management.base import BaseCommand
|
||||
from django.db import transaction
|
||||
|
||||
from jituan.constants import CLUB_ID_DEFAULT
|
||||
from jituan.services.club_penalty import resolve_gsfenhong_club_id, resolve_penalty_record_club_id
|
||||
from orders.models import PenaltyRecord
|
||||
from jituan.services.club_penalty import (
|
||||
resolve_gsfenhong_club_id,
|
||||
resolve_penalty_club_id,
|
||||
resolve_penalty_record_club_id,
|
||||
)
|
||||
from orders.models import Penalty, PenaltyRecord
|
||||
from products.models import DuociFenhong
|
||||
|
||||
|
||||
class Command(BaseCommand):
|
||||
help = '回填 PenaltyRecord / DuociFenhong 的 club_id'
|
||||
help = '回填 PenaltyRecord / Penalty / DuociFenhong 的 club_id'
|
||||
|
||||
def handle(self, *args, **options):
|
||||
pr_updated = 0
|
||||
fd_updated = 0
|
||||
df_updated = 0
|
||||
with transaction.atomic():
|
||||
for row in PenaltyRecord.query.all().only('id', 'OrderID', 'PlayerID', 'ClubID'):
|
||||
@@ -20,6 +25,19 @@ class Command(BaseCommand):
|
||||
if row.ClubID != cid:
|
||||
PenaltyRecord.query.filter(id=row.id).update(ClubID=cid)
|
||||
pr_updated += 1
|
||||
for row in Penalty.query.all().only('id', 'RelatedOrderID', 'PenalizedUserID', 'ClubID'):
|
||||
cid = resolve_penalty_club_id(
|
||||
order=None,
|
||||
penalized_user_id=row.PenalizedUserID,
|
||||
)
|
||||
if row.RelatedOrderID:
|
||||
from orders.models import Order
|
||||
o = Order.query.filter(OrderID=row.RelatedOrderID).first()
|
||||
if o:
|
||||
cid = resolve_penalty_club_id(order=o, penalized_user_id=row.PenalizedUserID)
|
||||
if row.ClubID != cid:
|
||||
Penalty.query.filter(id=row.id).update(ClubID=cid)
|
||||
fd_updated += 1
|
||||
for row in DuociFenhong.query.all().only('huiyuan', 'yonghuid', 'cishu', 'club_id'):
|
||||
cid = resolve_gsfenhong_club_id(dashouid=row.yonghuid) or CLUB_ID_DEFAULT
|
||||
if row.club_id != cid:
|
||||
@@ -31,5 +49,5 @@ class Command(BaseCommand):
|
||||
).update(club_id=cid)
|
||||
df_updated += 1
|
||||
self.stdout.write(self.style.SUCCESS(
|
||||
f'PenaltyRecord={pr_updated}, DuociFenhong={df_updated}'
|
||||
f'PenaltyRecord={pr_updated}, Penalty={fd_updated}, DuociFenhong={df_updated}'
|
||||
))
|
||||
|
||||
@@ -4,7 +4,6 @@ from rest_framework.response import Response
|
||||
from jituan.constants import CLUB_ID_DEFAULT
|
||||
from jituan.services.club_context import (
|
||||
DATA_SCOPE_ALL,
|
||||
filter_queryset_by_club,
|
||||
resolve_club_id_from_request,
|
||||
resolve_club_scope,
|
||||
)
|
||||
@@ -52,8 +51,55 @@ def resolve_gsfenhong_club_id(dingdan_id=None, dashouid=None, order=None, czjilu
|
||||
return CLUB_ID_DEFAULT
|
||||
|
||||
|
||||
def _collect_club_user_ids(club_id):
|
||||
"""俱乐部下属用户 UID(User.ClubID + 微信绑定 club)。"""
|
||||
from users.business_models import User
|
||||
from jituan.models import UserWxOpenid
|
||||
ids = set(User.query.filter(ClubID=club_id).values_list('UserUID', flat=True))
|
||||
ids.update(UserWxOpenid.query.filter(club_id=club_id).values_list('yonghuid', flat=True))
|
||||
return [x for x in ids if x]
|
||||
|
||||
|
||||
def _filter_club_penalty_like_qs(qs, request, user_id_field, order_id_field):
|
||||
"""罚单/积分处罚共用:ClubID + 被罚人 + 关联订单 归属当前俱乐部。"""
|
||||
if resolve_club_scope(request) == DATA_SCOPE_ALL:
|
||||
return qs
|
||||
club_id = resolve_club_id_from_request(request)
|
||||
from django.db.models import Q
|
||||
from orders.models import Order
|
||||
|
||||
parts = []
|
||||
if hasattr(qs.model, 'ClubID'):
|
||||
if club_id == CLUB_ID_DEFAULT:
|
||||
parts.append(
|
||||
Q(ClubID=club_id) | Q(ClubID__isnull=True) | Q(ClubID='')
|
||||
)
|
||||
else:
|
||||
parts.append(Q(ClubID=club_id))
|
||||
|
||||
user_ids = _collect_club_user_ids(club_id)
|
||||
if user_ids:
|
||||
parts.append(Q(**{f'{user_id_field}__in': user_ids}))
|
||||
|
||||
order_q = Q(ClubID=club_id)
|
||||
if club_id == CLUB_ID_DEFAULT:
|
||||
order_q = Q(ClubID=club_id) | Q(ClubID__isnull=True) | Q(ClubID='')
|
||||
order_ids = list(Order.query.filter(order_q).values_list('OrderID', flat=True))
|
||||
if order_ids:
|
||||
parts.append(Q(**{f'{order_id_field}__in': order_ids}))
|
||||
|
||||
if not parts:
|
||||
return qs.none()
|
||||
combined = parts[0]
|
||||
for p in parts[1:]:
|
||||
combined |= p
|
||||
return qs.filter(combined)
|
||||
|
||||
|
||||
def filter_penalty_qs(qs, request):
|
||||
return filter_queryset_by_club(qs, request, club_field='ClubID')
|
||||
return _filter_club_penalty_like_qs(
|
||||
qs, request, user_id_field='PenalizedUserID', order_id_field='RelatedOrderID'
|
||||
)
|
||||
|
||||
|
||||
def filter_gsfenhong_qs(qs, request):
|
||||
@@ -89,17 +135,6 @@ def resolve_penalty_record_club_id(order_id=None, player_id=None):
|
||||
|
||||
def filter_penalty_record_qs(qs, request):
|
||||
"""积分处罚记录(chufajilu)按俱乐部过滤。"""
|
||||
if resolve_club_scope(request) == DATA_SCOPE_ALL:
|
||||
return qs
|
||||
club_id = resolve_club_id_from_request(request)
|
||||
from django.db.models import Q
|
||||
from orders.models import Order
|
||||
from users.business_models import User
|
||||
user_ids = User.query.filter(ClubID=club_id).values_list('UserUID', flat=True)
|
||||
order_ids = Order.query.filter(ClubID=club_id).values_list('OrderID', flat=True)
|
||||
# ClubID 命中,或历史数据 club_id 未回填时按打手/订单归属俱乐部
|
||||
if hasattr(qs.model, 'ClubID'):
|
||||
return qs.filter(
|
||||
Q(ClubID=club_id) | Q(PlayerID__in=user_ids) | Q(OrderID__in=order_ids)
|
||||
)
|
||||
return qs.filter(Q(PlayerID__in=user_ids) | Q(OrderID__in=order_ids))
|
||||
return _filter_club_penalty_like_qs(
|
||||
qs, request, user_id_field='PlayerID', order_id_field='OrderID'
|
||||
)
|
||||
|
||||
Reference in New Issue
Block a user