diff --git a/jituan/services/identity_tag.py b/jituan/services/identity_tag.py index 231d741..3e0ccd3 100644 --- a/jituan/services/identity_tag.py +++ b/jituan/services/identity_tag.py @@ -1,6 +1,9 @@ """身份装饰标签查询与组装(与考核称号、订单需求标签分离)。""" import json +from django.db.models import Q + +from jituan.constants import CLUB_ID_DEFAULT from jituan.models import IdentityTag, IdentityTagBind SHENFEN_DASHOU = 1 @@ -29,12 +32,24 @@ def format_tag_item(tag): } +def _club_id_filter(club_id): + """按俱乐部查绑定;非默认俱乐部时回退 xq / 空,兼容集团历史数据。""" + q = Q(club_id=club_id) + if club_id and club_id != CLUB_ID_DEFAULT: + q |= Q(club_id=CLUB_ID_DEFAULT) | Q(club_id__isnull=True) | Q(club_id='') + elif club_id == CLUB_ID_DEFAULT: + q |= Q(club_id__isnull=True) | Q(club_id='') + return q + + def get_identity_tags_for_user(club_id, yonghuid, shenfen): """单用户单身份装饰标签列表。""" binds = ( IdentityTagBind.query.filter( - club_id=club_id, yonghuid=yonghuid, shenfen=shenfen, + yonghuid=yonghuid, + shenfen=shenfen, ) + .filter(_club_id_filter(club_id)) .select_related('tag') .order_by('tag__sort_order', 'tag__id') ) @@ -57,19 +72,26 @@ def batch_identity_tags(club_id, yonghuid_shenfen_pairs): shenfens = {p[1] for p in yonghuid_shenfen_pairs} binds = ( IdentityTagBind.query.filter( - club_id=club_id, yonghuid__in=yonghuids, shenfen__in=shenfens, + yonghuid__in=yonghuids, + shenfen__in=shenfens, ) + .filter(_club_id_filter(club_id)) .select_related('tag') .order_by('tag__sort_order', 'tag__id') ) out = {} pair_set = set(yonghuid_shenfen_pairs) + seen = {} for b in binds: key = (b.yonghuid, b.shenfen) if key not in pair_set: continue if b.tag.status != 1: continue + tag_id = b.tag.id + if seen.get(key, set()).intersection({tag_id}): + continue + seen.setdefault(key, set()).add(tag_id) out.setdefault(key, []).append(format_tag_item(b.tag)) return out diff --git a/orders/views.py b/orders/views.py index 0e0f2ff..f4e769e 100644 --- a/orders/views.py +++ b/orders/views.py @@ -3154,6 +3154,7 @@ class DashouDingdanHuoquView(APIView): SHENFEN_SHANGJIA, batch_identity_tags, ) + from jituan.services.club_context import resolve_effective_club_id from users.models import UserShangjia identity_pairs = [] @@ -3175,7 +3176,7 @@ class DashouDingdanHuoquView(APIView): if sj_uid: identity_pairs.append((sj_uid, SHENFEN_SHANGJIA)) - club_id = resolve_club_id_from_request(request) + club_id = resolve_effective_club_id(request, getattr(request, 'user', None)) identity_tag_map = batch_identity_tags(club_id, identity_pairs) if identity_pairs else {} # 12. 格式化返回数据