diff --git a/users/views/kefu_orders.py b/users/views/kefu_orders.py index f1f9749..c64bc8f 100644 --- a/users/views/kefu_orders.py +++ b/users/views/kefu_orders.py @@ -1716,35 +1716,103 @@ class KefuGetOrderDetailView(APIView): ) response_data['zhiding_dhj_sm'] = getattr(dingdan_obj, 'AssignedLaterNote', '') or '' - # 9. 关联罚款(打手) + # 9. 关联罚款(本单全部,含组队均摊) from utils.penalty_status import PENALTY_STATUS_LABELS as PENALTY_STATUS try: from orders.models import Penalty - dashou_id = dingdan_obj.PlayerID or dingdan_obj.AssignedID or '' - fakuan_qs = Penalty.query.filter(RelatedOrderID=dingdan_id) - if dashou_id: - fakuan_qs = fakuan_qs.filter(PenalizedUserID=dashou_id) + from users.business_models import User as _U + fakuan_qs = list( + Penalty.query.filter(RelatedOrderID=dingdan_id).order_by('-CreateTime')[:30] + ) + uid_set = set() + for p in fakuan_qs: + if p.PenalizedUserID: + uid_set.add(str(p.PenalizedUserID)) + if p.ApplicantID: + uid_set.add(str(p.ApplicantID)) + nick_map = {} + if uid_set: + for u in _U.query.filter(UserUID__in=list(uid_set)).select_related('DashouProfile'): + try: + nick = '' + if getattr(u, 'DashouProfile', None): + nick = u.DashouProfile.nicheng or '' + nick_map[u.UserUID] = nick or (getattr(u, 'Phone', None) or u.UserUID) + except Exception: + nick_map[u.UserUID] = u.UserUID fakuan_list = [] - for p in fakuan_qs.order_by('-CreateTime')[:5]: + for p in fakuan_qs: + pid = str(p.PenalizedUserID or '') + aid = str(p.ApplicantID or '') fakuan_list.append({ + 'id': p.id, 'amount': str(p.FineAmount or '0.00'), 'status': p.Status, 'status_text': PENALTY_STATUS.get(p.Status, ''), 'reason': p.Reason or '', + 'spec_id': getattr(p, 'SpecID', None), + 'spec_name': getattr(p, 'SpecName', None) or '', + 'penalized_uid': pid, + 'penalized_nicheng': nick_map.get(pid, ''), + 'applicant_id': aid, + 'applicant_nicheng': nick_map.get(aid, aid), + 'affects_grabbing': bool(getattr(p, 'AffectsGrabbing', True)), 'create_time': p.CreateTime.strftime('%Y-%m-%d %H:%M:%S') if p.CreateTime else '', }) response_data['fakuan_list'] = fakuan_list latest = fakuan_list[0] if fakuan_list else None response_data['fakuan_info'] = latest + # 本单是否已有未驳回罚单(客服再次申请前前端提示用) response_data['is_fadaned'] = Penalty.query.filter( - RelatedOrderID=dingdan_id, ApplicantID=getattr(current_user, 'UserUID', '') - ).exists() + RelatedOrderID=dingdan_id, + ).exclude(Status=4).exists() except Exception as e: logger.exception(f"[kefuhqddxq] 罚款查询异常 dingdan_id={dingdan_id}") response_data['fakuan_list'] = [] response_data['fakuan_info'] = None response_data['is_fadaned'] = False + # 10. 组队成员(平台单/商家单均返回;有则展示) + try: + from orders.services.team_recruit import ( + get_active_recruit, serialize_recruit_public, + ) + from orders.models import TeamRecruit + from orders.services.partial_settle import preview_partial_amounts, parse_settle_ratio + + rec = get_active_recruit(dingdan_id) + if not rec: + rec = TeamRecruit.query.filter(order_id=dingdan_id).order_by('-id').first() + if rec: + team = serialize_recruit_public(rec, dingdan_obj, include_game_ids=True) + # 附带比例结算预览份额(若已锁定比例或有默认) + ratio_raw = getattr(dingdan_obj, 'PartialSettleRatio', None) + if ratio_raw is None and int(dingdan_obj.Status or 0) == 4: + ratio_raw = response_data.get('default_partial_settle_ratio') or '0.5' + if ratio_raw is not None: + try: + prev = preview_partial_amounts(dingdan_obj, parse_settle_ratio(ratio_raw)) + share_map = { + str(x.get('user_uid')): x for x in (prev.get('team_shares') or []) + } + for m in (team.get('members') or []): + s = share_map.get(str(m.get('user_uid')), {}) + m['full_share'] = s.get('full_share', m.get('share_amount')) + m['partial_share'] = s.get('partial_share') + team['partial_preview'] = { + 'settle_percent': prev.get('settle_percent'), + 'partial_player_amount': prev.get('partial_player_amount'), + 'partial_manager_amount': prev.get('partial_manager_amount'), + } + except Exception: + pass + response_data['team'] = team + else: + response_data['team'] = None + except Exception as e: + logger.warning('[kefuhqddxq] 组队信息附加失败 dingdan_id=%s: %s', dingdan_id, e) + response_data['team'] = None + try: from jituan.services.order_deal import order_auto_settle_payload response_data.update(order_auto_settle_payload(dingdan_obj))