Files
Django/utils/fadan_utils.py

146 lines
5.3 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.
# utils/fadan_utils.py
from orders.models import Penalty
from utils.penalty_status import (
PENALTY_GRAB_BLOCK_STATUSES,
PENALTY_WITHDRAW_BLOCK_STATUSES,
)
import logging
logger = logging.getLogger('xiaochengxu')
def check_fadan_qiangdan_eligible(yonghuid: str, action_type: int) -> tuple:
"""
通用罚单资格检查。
参数:
yonghuid : 被检查的用户 yonghuid
action_type : 1 = 抢单, 2 = 提现
返回:
(is_eligible: bool, message: str)
True 允许操作False 返回拦截原因。
"""
try:
if action_type == 1:
# 抢单:仅「影响抢单」且「平台审核通过后仍未结案」的罚单拦截
blocked = Penalty.objects.filter(
PenalizedUserID=yonghuid,
AffectsGrabbing=1,
Status__in=PENALTY_GRAB_BLOCK_STATUSES,
).exists()
if blocked:
logger.info(f"用户 {yonghuid} 抢单被拦截:存在未解决的影响抢单罚单")
return (False, "您有未缴纳或申诉中的罚单,请先处理后再抢单")
return (True, "无罚单限制")
elif action_type == 2:
# 提现:平台审核中 / 待缴纳 / 申诉中 均禁止提现
blocked = Penalty.objects.filter(
PenalizedUserID=yonghuid,
Status__in=PENALTY_WITHDRAW_BLOCK_STATUSES,
).exists()
if blocked:
logger.info(f"用户 {yonghuid} 提现被拦截:存在未解决的罚单")
return (False, "您有未处理完的罚单,需全部完成或驳回后方可提现")
return (True, "罚单状态正常,可提现")
else:
logger.warning(f"未知的检查类型: {action_type}")
return (False, "罚单查询系统异常(无效的检查类型)")
except Exception as e:
logger.error(f"检查用户 {yonghuid} 罚单状态异常: {e}", exc_info=True)
# 安全起见,异常时禁止操作
return (False, "罚单查询系统繁忙,请稍后重试")
import logging
from django.db import transaction
from decimal import Decimal
from orders.models import Penalty, PenaltyBonus, PenaltyBonusRate
logger = logging.getLogger('xiaochengxu')
def fadan_fenhong(fadan_id):
"""
罚款缴纳后,给申请处罚人累加分红。
自动映射身份,异常不抛出,不影响主流程。
"""
try:
# 1. 查询罚单
try:
fadan = Penalty.objects.get(id=fadan_id)
except Penalty.DoesNotExist:
logger.warning(f"罚单 {fadan_id} 不存在,分红跳过")
return
if fadan.Status != 2:
logger.info(f"罚单 {fadan_id} 状态非已缴纳,不分红")
return
fenhongzhe_id = fadan.ApplicantID or ''
shenfen_in_fadan = fadan.ApplicantIdentity # 罚单中的申请人身份
jine = fadan.FineAmount
if not fenhongzhe_id or jine <= 0:
logger.info(f"罚单 {fadan_id} 缺少申请处罚人或金额为0不分红")
return
# 2. 直接用罚单身份的原始值查询费率(费率表 shenfen 与之含义一致)
rate_record = PenaltyBonusRate.objects.filter(Identity=shenfen_in_fadan).first()
if not rate_record or rate_record.Rate <= 0:
logger.info(f"身份 {shenfen_in_fadan} 无有效分红费率,不分红")
return
rate = rate_record.Rate
# 3. 计算分红金额
amount = (jine * rate).quantize(Decimal('0.01'))
if amount <= 0:
logger.info(f"罚单 {fadan_id} 分红金额为0不分红")
return
# 4. 硬编码映射:罚单身份 → 分红记录表身份
SHENFEN_MAP = {
1: 2, # 客服 → 客服
2: 5, # 售后 → 售后
3: 3, # 管理员 → 平台管理者
4: 4, # 店铺 → 店铺
5: 1, # 商家 → 商家
}
shenfen_in_fenhong = SHENFEN_MAP.get(shenfen_in_fadan)
if shenfen_in_fenhong is None:
logger.info(f"罚单 {fadan_id} 身份 {shenfen_in_fadan} 无对应的分红记录映射,不分红")
return
# 5. 事务累加分红记录(同一用户+身份只保留一条记录)
with transaction.atomic():
record = PenaltyBonus.objects.select_for_update().filter(
BeneficiaryID=fenhongzhe_id,
Identity=shenfen_in_fenhong # 使用映射后的身份值
).first()
if record:
record.TotalAmount += amount
record.WithdrawableAmount += amount
record.BonusCount += 1
record.IndividualRate = rate
record.save()
else:
PenaltyBonus.objects.create(
BeneficiaryID=fenhongzhe_id,
Identity=shenfen_in_fenhong,
TotalAmount=amount,
WithdrawableAmount=amount,
BonusCount=1,
IndividualRate=rate,
)
logger.info(f"罚单 {fadan_id} 分红完成:用户 {fenhongzhe_id} 身份 {shenfen_in_fenhong} 金额 {amount}")
except Exception as e:
logger.error(f"罚单 {fadan_id} 分红异常:{e}", exc_info=True)