From df55b4ab411d06f7fc83d176354046863ad46cb0 Mon Sep 17 00:00:00 2001 From: XingQue Date: Mon, 27 Jul 2026 08:48:45 +0800 Subject: [PATCH] =?UTF-8?q?fix:=20=E6=88=90=E4=BA=A4=E6=8C=87=E6=A0=87=20e?= =?UTF-8?q?nsure=20=E9=81=87=E5=94=AF=E4=B8=80=E9=94=AE=E5=86=B2=E7=AA=81?= =?UTF-8?q?=E6=97=B6=E5=A4=8D=E7=94=A8=E5=B7=B2=E6=9C=89=E8=A1=8C?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 回填并发插入 merchant_deal_stat/dashou_deal_stat 时不再因 Duplicate entry 中断。 Co-authored-by: Cursor --- jituan/services/deal_stats.py | 19 +++++++++++++++---- 1 file changed, 15 insertions(+), 4 deletions(-) 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):