fix: 修复微信入账重复统计,入账幂等防双记
This commit is contained in:
@@ -33,7 +33,6 @@ from jituan.services.club_config import get_huiyuan_fenchong
|
||||
from jituan.services.club_penalty import resolve_gsfenhong_club_id
|
||||
from jituan.services.club_user import get_user_club_id
|
||||
from jituan.services.wechat_pay import club_id_from_czjilu, get_wechat_v2_config
|
||||
from orders.utils import update_daily_income
|
||||
from products.models import Czjilu, DuociFenhong, Gsfenhong, Huiyuan, Huiyuangoumai
|
||||
from users.business_models import User
|
||||
from users.models import UserDashou, UserGuanshi, UserZuzhang
|
||||
@@ -331,9 +330,8 @@ def fulfill_member_recharge(dingdan_id, paid_amount_yuan=None, source='callback'
|
||||
|
||||
jine_decimal = Decimal(str(order.jine))
|
||||
try:
|
||||
update_daily_income(jine_decimal, club_id)
|
||||
from jituan.services.szjilu_accounting import apply_szjilu_income
|
||||
apply_szjilu_income(jine_decimal, club_id)
|
||||
from jituan.services.szjilu_accounting import record_wechat_income_once
|
||||
record_wechat_income_once(jine_decimal, club_id, biz_ref=f'cz:{dingdan_id}')
|
||||
except Exception as exc:
|
||||
logger.error('会员收支统计更新失败: %s', exc, exc_info=True)
|
||||
|
||||
|
||||
@@ -1,11 +1,14 @@
|
||||
"""各俱乐部全局收支流水(szjilu)统一记账。"""
|
||||
import logging
|
||||
from decimal import Decimal
|
||||
|
||||
from django.db import transaction
|
||||
from django.db import IntegrityError, transaction
|
||||
|
||||
from config.models import Szjilu
|
||||
from config.models import PlatformIncomeLog, Szjilu
|
||||
from jituan.constants import CLUB_ID_DEFAULT
|
||||
|
||||
logger = logging.getLogger(__name__)
|
||||
|
||||
_ZERO = Decimal('0.00')
|
||||
_SZJILU_DEFAULTS = {
|
||||
'TotalIncome': _ZERO,
|
||||
@@ -40,6 +43,39 @@ def apply_szjilu_income(amount, club_id=None):
|
||||
szjilu.save()
|
||||
|
||||
|
||||
def record_wechat_income_once(amount, club_id=None, biz_ref=None):
|
||||
"""
|
||||
微信入账:同一 biz_ref 只记一次 daily_income_stat + szjilu。
|
||||
返回 True=本次新入账;False=已记过(重复回调/重复代码路径)。
|
||||
"""
|
||||
ref = (biz_ref or '').strip()
|
||||
if not ref:
|
||||
raise ValueError('biz_ref 不能为空')
|
||||
cid = normalize_szjilu_club_id(club_id)
|
||||
jine = Decimal(str(amount))
|
||||
from orders.utils import update_daily_income
|
||||
|
||||
try:
|
||||
with transaction.atomic():
|
||||
PlatformIncomeLog.objects.create(
|
||||
biz_ref=ref,
|
||||
club_id=cid,
|
||||
amount=jine,
|
||||
)
|
||||
except IntegrityError:
|
||||
logger.info('平台入账已存在,跳过重复记账 biz_ref=%s', ref)
|
||||
return False
|
||||
|
||||
try:
|
||||
update_daily_income(jine, cid)
|
||||
apply_szjilu_income(jine, cid)
|
||||
except Exception:
|
||||
PlatformIncomeLog.objects.filter(biz_ref=ref).delete()
|
||||
raise
|
||||
logger.info('平台入账记账成功 biz_ref=%s club=%s amount=%s', ref, cid, jine)
|
||||
return True
|
||||
|
||||
|
||||
def apply_szjilu_expense(amount, club_id=None):
|
||||
"""平台出款/支出:总收益减少,总支出、今日支出增加。"""
|
||||
jine = Decimal(str(amount))
|
||||
|
||||
Reference in New Issue
Block a user