修复管事双分红与押金分红:移除错误冲突校验、优化分成计算与建聊补偿

Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
XingQue
2026-06-22 23:36:18 +08:00
parent bd821faf9d
commit 7daa05c8c6
4 changed files with 143 additions and 80 deletions

View File

@@ -356,10 +356,10 @@ def _normalize_lilu_rate(raw):
def calc_shangjia_order_fencheng(jine):
"""
商家发单时计算打手/管事分成。
- 打手分成Lilubiao=3最高不超过订单金额
- 打手分成Lilubiao=3
- 管事打手接单分红Lilubiao=13
- 管事商家派单分红Lilubiao=15
打手优先;三项合计超过订单金额时,优先清零管事商家分红,再清零管事打手分红。
三项合计超过订单金额时,优先削减打手分成,再削减商家派单管事分红,最后削减打手接单管事分红。
"""
jine = Decimal(str(jine))
try:
@@ -387,14 +387,19 @@ def calc_shangjia_order_fencheng(jine):
guanshi_fencheng = (jine * rate_guanshi_dashou).quantize(Decimal('0.01'))
guanshi_shangjia_fencheng = (jine * rate_guanshi_shangjia).quantize(Decimal('0.01'))
if dashou_fencheng + guanshi_fencheng > jine:
guanshi_fencheng = Decimal('0.00')
if dashou_fencheng + guanshi_fencheng + guanshi_shangjia_fencheng > jine:
guanshi_shangjia_fencheng = Decimal('0.00')
if dashou_fencheng + guanshi_fencheng + guanshi_shangjia_fencheng > jine:
guanshi_fencheng = Decimal('0.00')
total = dashou_fencheng + guanshi_fencheng + guanshi_shangjia_fencheng
if total > jine:
overflow = total - jine
cut = min(overflow, dashou_fencheng)
dashou_fencheng -= cut
overflow -= cut
if overflow > 0:
cut = min(overflow, guanshi_shangjia_fencheng)
guanshi_shangjia_fencheng -= cut
overflow -= cut
if overflow > 0:
cut = min(overflow, guanshi_fencheng)
guanshi_fencheng -= cut
return dashou_fencheng, guanshi_fencheng, guanshi_shangjia_fencheng
@@ -413,17 +418,6 @@ def _pay_guanshi_order_fenhong(order, guanshi_id, amount, related_user_id, niche
logger.info(f"管事分红已处理: 订单{order.dingdan_id}, 类型{fenhong_leixing}")
return False
# 旧表结构 dingdan_id 全局唯一时,第二笔分红会插入失败
legacy_conflict = Gsfenhong.objects.filter(dingdan_id=order.dingdan_id).exclude(
fenhong_leixing=fenhong_leixing
).exists()
if legacy_conflict:
logger.error(
f"管事分红写入失败: 订单{order.dingdan_id} 已有其他类型分红记录,"
f"请执行 python manage.py migrate shangpin 0010 更新 gsfenhong 表唯一约束"
)
return False
try:
with transaction.atomic():
guanshi = UserGuanshi.objects.select_for_update().get(user__yonghuid=guanshi_id)
@@ -569,4 +563,76 @@ def settle_shangjia_order_guanshi_fenhong(order, dashou_id):
else:
logger.info(
f"管事商家分红跳过: 商家{shangjia_id}打手身份无邀请管事(yaoqingren为空)"
)
)
def pay_deposit_guanshi_fenhong(dashou, order, recharge_amount):
"""
打手充值押金后向邀请管事发放分红幂等dingdan_id + fenhong_leixing=2
网络重试/微信重复回调时不会重复入账。
"""
from shangpin.models import Gsfenhong
from yonghu.models import UserGuanshi
guanshi_id = (dashou.yaoqingren or '').strip()
if not guanshi_id:
logger.info(f"押金分红跳过: 打手{dashou.user.yonghuid}无邀请管事")
return False
if Gsfenhong.objects.filter(dingdan_id=order.dingdan_id, fenhong_leixing=2).exists():
logger.info(f"押金分红已处理: 订单{order.dingdan_id}")
return True
lilu_obj = Lilubiao.objects.filter(fadanpingtai='12').first()
rate = lilu_obj.lilu if lilu_obj and lilu_obj.lilu is not None else Decimal('0')
rate = _normalize_lilu_rate(rate) if rate else Decimal('0')
if rate <= 0:
logger.info("押金分红跳过: 费率未配置或为0")
return False
recharge_amount = Decimal(str(recharge_amount))
fenhong_jine = (recharge_amount * rate).quantize(Decimal('0.01'))
if fenhong_jine <= 0:
return False
try:
with transaction.atomic():
guanshi = UserGuanshi.objects.select_for_update().get(user__yonghuid=guanshi_id)
UserGuanshi.objects.filter(pk=guanshi.pk).update(
yue=F('yue') + fenhong_jine,
chongzhifenrun=F('chongzhifenrun') + fenhong_jine,
)
user_main = dashou.user
Gsfenhong.objects.create(
dingdan_id=order.dingdan_id,
guanshi=guanshi_id,
dashouid=dashou.user.yonghuid,
shuoming='押金分红',
fenhong=fenhong_jine,
avatar=user_main.avatar if user_main else None,
nicheng=dashou.nicheng or '未知打手',
fenhong_leixing=2,
)
logger.info(
f"押金分红成功: 订单{order.dingdan_id}, 管事{guanshi_id}, "
f"打手{dashou.user.yonghuid}, 金额{fenhong_jine}"
)
try:
from houtai.utils import update_guanshi_daily_by_action
update_guanshi_daily_by_action(
yonghuid=guanshi_id,
action=4,
amount=fenhong_jine,
)
except Exception as e:
logger.error(f"押金分红管事日统计更新失败: {e}")
return True
except UserGuanshi.DoesNotExist:
logger.warning(f"押金分红跳过: 管事{guanshi_id}不存在")
return False
except IntegrityError:
logger.info(f"押金分红已处理(并发): 订单{order.dingdan_id}")
return True
except Exception as e:
logger.error(f"押金分红处理失败: {e}", exc_info=True)
return False