"""考核模块按俱乐部过滤。"""
from jituan.constants import CLUB_ID_DEFAULT, DATA_SCOPE_ALL
from jituan.services.club_context import resolve_club_id_from_request, resolve_club_scope
from jituan.services.club_user import get_user_club_id
from users.business_models import User

def filter_shenhe_jilu_by_request(qs, request):
    """考核记录：按申请人（打手）所属俱乐部过滤。"""
    if resolve_club_scope(request) == DATA_SCOPE_ALL:
        return qs
    club_id = resolve_club_id_from_request(request)
    club_uids = list(
        User.query.filter(ClubID=club_id).values_list('UserUID', flat=True)
    )
    if not club_uids:
        return qs.none()
    return qs.filter(shenqingren_id__in=club_uids)


def yonghu_chenghao_qs_for_request(base_qs, request):
    """板块标签用户数统计：按俱乐部过滤打手。"""
    if resolve_club_scope(request) == DATA_SCOPE_ALL:
        return base_qs
    return base_qs.filter(yonghu__ClubID=resolve_club_id_from_request(request))


def user_belongs_to_request_club(user, request):
    """考核官添加等：用户须归属当前请求俱乐部（含集团视图下 header 所选 club）。"""
    expected = (resolve_club_id_from_request(request) or CLUB_ID_DEFAULT).strip()
    actual = (get_user_club_id(user) or CLUB_ID_DEFAULT).strip()
    return actual == expected
