diff --git a/jituan/services/deal_stats.py b/jituan/services/deal_stats.py index d3f2427..70b903e 100644 --- a/jituan/services/deal_stats.py +++ b/jituan/services/deal_stats.py @@ -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):