Files
Django/jituan/services/club_member_admin.py

180 lines
6.1 KiB
Python
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
"""后台会员管理:全局 Huiyuan 定义 + 各俱乐部 ClubHuiyuanPrice 售价/分成。"""
import logging
import random
import string
from decimal import Decimal
from django.db import transaction, IntegrityError
from jituan.constants import CLUB_ID_DEFAULT, DATA_SCOPE_ALL
from jituan.models import ClubHuiyuanPrice
from jituan.services.club_context import club_id_for_write, resolve_club_id_from_request, resolve_club_scope
from jituan.services.club_user_access import list_response_meta
from products.models import Huiyuan
logger = logging.getLogger(__name__)
CLUB_WRITE_SCOPE_MSG = '当前为集团汇总视图,请先在顶栏切换到具体俱乐部(如星阙、星之界)后再操作'
def club_write_blocked(request):
return resolve_club_scope(request) == DATA_SCOPE_ALL
def _format_member(m, price_row=None):
global_jiage = str(m.jiage)
global_guanshifc = str(m.guanshifc)
global_zuzhangfc = str(m.zuzhangfc)
item = {
'huiyuan_id': m.huiyuan_id,
'jieshao': m.jieshao,
'jtjieshao': m.jtjieshao,
'jiage': global_jiage,
'guanshifc': global_guanshifc,
'zuzhangfc': global_zuzhangfc,
'global_jiage': global_jiage,
'global_guanshifc': global_guanshifc,
'global_zuzhangfc': global_zuzhangfc,
'goumai_cishu': m.goumai_cishu,
'CreateTime': m.CreateTime.isoformat() if m.CreateTime else None,
'UpdateTime': m.UpdateTime.isoformat() if m.UpdateTime else None,
'bankuai_id': m.bankuai_id,
'club_enabled': True,
'price_source': 'global',
}
if price_row is not None:
item['jiage'] = str(price_row.jiage)
item['guanshifc'] = str(price_row.guanshifc)
item['zuzhangfc'] = str(price_row.zuzhangfc)
item['club_enabled'] = bool(price_row.is_enabled)
item['price_source'] = 'club'
return item
def build_member_list_payload(request):
scope = resolve_club_scope(request)
club_id = resolve_club_id_from_request(request)
members = Huiyuan.query.all().order_by('-CreateTime')
member_list = []
price_map = {}
if scope != DATA_SCOPE_ALL:
price_map = {
r.huiyuan_id: r
for r in ClubHuiyuanPrice.query.filter(club_id=club_id)
}
if scope == DATA_SCOPE_ALL:
for m in members:
member_list.append(_format_member(m))
else:
for m in members:
row = price_map.get(m.huiyuan_id)
if row is None and club_id != CLUB_ID_DEFAULT:
continue
if row is not None and not row.is_enabled:
continue
member_list.append(_format_member(m, row))
return {
'list': member_list,
'club_id': club_id,
'scope': scope,
'price_note': (
f'当前俱乐部 {club_id} 专属售价(表 club_huiyuan_price'
'若与星阙相同,是创建时从 xq 复制的默认值,请在本俱乐部视图下修改价格。'
if scope != DATA_SCOPE_ALL else
'集团汇总:展示全局默认价。请切换到具体俱乐部查看/编辑专属价。'
),
**list_response_meta(request),
}
def update_member_for_club(request, huiyuan_id, jieshao, jiage, guanshifc, zuzhangfc, jtjieshao):
if club_write_blocked(request):
return None, CLUB_WRITE_SCOPE_MSG
club_id = club_id_for_write(request)
with transaction.atomic():
try:
member = Huiyuan.objects.select_for_update().get(huiyuan_id=huiyuan_id)
except Huiyuan.DoesNotExist:
return None, '会员不存在'
member.jieshao = jieshao
member.jtjieshao = jtjieshao
member.save(update_fields=['jieshao', 'jtjieshao', 'UpdateTime'])
row, created = ClubHuiyuanPrice.query.get_or_create(
club_id=club_id,
huiyuan_id=huiyuan_id,
defaults={
'jiage': jiage,
'guanshifc': guanshifc,
'zuzhangfc': zuzhangfc,
'is_enabled': True,
'bankuai_id': member.bankuai_id,
},
)
if not created:
row.jiage = jiage
row.guanshifc = guanshifc
row.zuzhangfc = zuzhangfc
row.is_enabled = True
row.save()
if club_id == CLUB_ID_DEFAULT:
member.jiage = jiage
member.guanshifc = guanshifc
member.zuzhangfc = zuzhangfc
member.save(update_fields=['jiage', 'guanshifc', 'zuzhangfc', 'UpdateTime'])
return {'huiyuan_id': huiyuan_id}, None
def add_member_for_club(request, jieshao, jiage, guanshifc, zuzhangfc, jtjieshao, bankuai_id=None):
if club_write_blocked(request):
return None, CLUB_WRITE_SCOPE_MSG
club_id = club_id_for_write(request)
def generate_huiyuan_id():
while True:
new_id = ''.join(random.choices(string.ascii_uppercase + string.digits, k=6))
if not Huiyuan.query.filter(huiyuan_id=new_id).exists():
return new_id
try:
with transaction.atomic():
new_id = generate_huiyuan_id()
create_kwargs = dict(
huiyuan_id=new_id,
jieshao=jieshao,
jiage=jiage,
guanshifc=guanshifc,
zuzhangfc=zuzhangfc,
jtjieshao=jtjieshao or '',
goumai_cishu=0,
)
if bankuai_id is not None:
create_kwargs['bankuai_id'] = bankuai_id
member = Huiyuan.query.create(**create_kwargs)
ClubHuiyuanPrice.query.create(
club_id=club_id,
huiyuan_id=new_id,
jiage=jiage,
guanshifc=guanshifc,
zuzhangfc=zuzhangfc,
is_enabled=True,
bankuai_id=bankuai_id,
)
except IntegrityError as e:
logger.exception('添加会员失败 club_id=%s', club_id)
return None, f'添加失败:数据冲突({e}'
except Exception as e:
logger.exception('添加会员失败 club_id=%s', club_id)
return None, f'添加失败:{e}'
return {'huiyuan_id': member.huiyuan_id}, None