fix: 成交指标 ensure 遇唯一键冲突时复用已有行

回填并发插入 merchant_deal_stat/dashou_deal_stat 时不再因 Duplicate entry 中断。

Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
XingQue
2026-07-27 08:48:45 +08:00
parent e3f9654002
commit 3c02b9477d

View File

@@ -91,20 +91,31 @@ def _claim_event(club_id: str, event_key: str, event_type: str, order_id='', sub
def _ensure_merchant(club_id, uid):
from jituan.models import MerchantDealStat
from django.db import IntegrityError
row = MerchantDealStat.query.filter(club_id=club_id, merchant_uid=uid).first()
if not row:
if row:
return row
try:
row = MerchantDealStat(club_id=club_id, merchant_uid=uid)
row.save()
return row
return row
except IntegrityError:
# 并发回填/记账时可能已有同 club+merchant 行,读回即可
return MerchantDealStat.query.filter(club_id=club_id, merchant_uid=uid).first()
def _ensure_dashou(club_id, uid):
from jituan.models import DashouDealStat
from django.db import IntegrityError
row = DashouDealStat.query.filter(club_id=club_id, dashou_uid=uid).first()
if not row:
if row:
return row
try:
row = DashouDealStat(club_id=club_id, dashou_uid=uid)
row.save()
return row
return row
except IntegrityError:
return DashouDealStat.query.filter(club_id=club_id, dashou_uid=uid).first()
def _merchant_uid(order):