Files
along_django/utils/shangpin_guishu.py

68 lines
2.4 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.
"""
隐藏式商品/类型归属商家端类型:点单写入订单抢单字段时使用。
"""
from shangpin.models import ShangpinLeixing
def parse_guishu_leixing_id(raw, shenhezhuangtai=None):
"""
解析归属商家端类型 ID。
非隐藏式(4)一律视为无归属;有值时必须指向 shenhezhuangtai=3 的类型。
返回 (guishu_leixing_id|None, error_message|None)
"""
try:
status = int(shenhezhuangtai) if shenhezhuangtai is not None else None
except (TypeError, ValueError):
status = None
if status is not None and status != 4:
return None, None
if raw is None or raw == '' or str(raw).lower() in ('null', 'none', 'undefined'):
return None, None
try:
gid = int(raw)
except (TypeError, ValueError):
return None, '归属商家端类型ID无效'
if gid <= 0:
return None, None
if not ShangpinLeixing.objects.filter(id=gid, shenhezhuangtai=3).exists():
return None, '归属类型必须是商家端商品类型(审核状态=3'
return gid, None
def resolve_order_qiangdan_fields(shangpin):
"""
点单写入 Dingdan 的抢单相关字段:
- 抢单要求yaoqiuleixing / huiyuan_id / yongjin一律用商品自身后台配置
- 若商品或所属类型配置了 guishu_leixing_id → 仅把 leixing_id 映射到商家端类型
(抢单池按商家端类型展示;押金金额仍按商品配置,不覆盖)
"""
# 商品自身抢单要求(后台商品/类型配置的会员或押金门槛)
yaoqiu = getattr(shangpin, 'yaoqiuleixing', None)
huiyuan_id = getattr(shangpin, 'huiyuan_id', '') or ''
yongjin = getattr(shangpin, 'yongjin', None)
if yongjin is None:
yongjin = 0
leixing_id = getattr(shangpin, 'leixing_id', None)
guishu_id = getattr(shangpin, 'guishu_leixing_id', None)
if not guishu_id and leixing_id:
own_type = ShangpinLeixing.objects.filter(id=leixing_id).only(
'guishu_leixing_id',
).first()
if own_type:
guishu_id = own_type.guishu_leixing_id
if guishu_id:
merchant = ShangpinLeixing.objects.filter(id=guishu_id, shenhezhuangtai=3).only('id').first()
if merchant:
leixing_id = merchant.id
return {
'leixing_id': leixing_id,
'yaoqiuleixing': yaoqiu,
'huiyuan_id': huiyuan_id,
'yongjin': yongjin,
}