162 lines
5.5 KiB
Python
162 lines
5.5 KiB
Python
"""后台会员管理:全局 Huiyuan 定义 + 各俱乐部 ClubHuiyuanPrice 售价/分成。"""
|
||
import random
|
||
import string
|
||
from decimal import Decimal
|
||
|
||
from django.db import transaction
|
||
|
||
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
|
||
|
||
|
||
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': (
|
||
'当前展示俱乐部专属售价(club_huiyuan_price);与星阙相同说明尚未单独改价,'
|
||
'在本俱乐部视图下编辑即可覆盖。'
|
||
if scope != DATA_SCOPE_ALL else
|
||
'集团汇总视图展示全局默认价;切换具体俱乐部可查看/编辑各俱乐部售价。'
|
||
),
|
||
**list_response_meta(request),
|
||
}
|
||
|
||
|
||
def update_member_for_club(request, huiyuan_id, jieshao, jiage, guanshifc, zuzhangfc, jtjieshao):
|
||
if resolve_club_scope(request) == DATA_SCOPE_ALL:
|
||
return None, '集团汇总视图下不可修改,请切换到具体俱乐部'
|
||
|
||
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 resolve_club_scope(request) == DATA_SCOPE_ALL:
|
||
return None, '集团汇总视图下不可添加,请切换到具体俱乐部'
|
||
|
||
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
|
||
|
||
with transaction.atomic():
|
||
new_id = generate_huiyuan_id()
|
||
member = Huiyuan.query.create(
|
||
huiyuan_id=new_id,
|
||
jieshao=jieshao,
|
||
jiage=jiage,
|
||
guanshifc=guanshifc,
|
||
zuzhangfc=zuzhangfc,
|
||
jtjieshao=jtjieshao,
|
||
goumai_cishu=0,
|
||
bankuai_id=bankuai_id,
|
||
)
|
||
ClubHuiyuanPrice.query.create(
|
||
club_id=club_id,
|
||
huiyuan_id=new_id,
|
||
jiage=jiage,
|
||
guanshifc=guanshifc,
|
||
zuzhangfc=zuzhangfc,
|
||
is_enabled=True,
|
||
bankuai_id=bankuai_id,
|
||
)
|
||
|
||
return {'huiyuan_id': member.huiyuan_id}, None
|