fix: 今日订单假单批次并发创建防重

This commit is contained in:
XingQue committed 2026-08-11 10:09:55 +08:00
1 parent c6ac1ef405
commit 08732feecf
1 file changed
+16 -2
+16 -2
View File
@@ -7,6 +7,7 @@ from datetime import datetime, time, timedelta
from decimal import Decimal
from django.conf import settings
from django.db import IntegrityError
from django.db.models import Count, Q
from django.utils import timezone
@@ -51,7 +52,12 @@ def get_or_create_config(club_id: str) -> TodayOrderShowConfig:
if row:
return row
row = TodayOrderShowConfig(club_id=cid, mix_enabled=False, batch_size=5, interval_seconds=120)
row.save()
try:
row.save()
except IntegrityError:
row = TodayOrderShowConfig.query.filter(club_id=cid).first()
if not row:
raise
return row
@@ -143,7 +149,15 @@ def _pick_fake_batch(club_id: str, cfg: TodayOrderShowConfig) -> list[TodayOrder
state = TodayOrderFakeBatchState(club_id=club_id)
state.last_shuffle_at = now
state.fake_ids = ids
state.save()
try:
state.save()
except IntegrityError:
# 并发首次创建同一 club 批次行:改更新已有行
state = TodayOrderFakeBatchState.query.filter(club_id=club_id).first()
if state:
state.last_shuffle_at = now
state.fake_ids = ids
state.save()
return chosen
id_set = set(ids)
return [x for x in pool if int(x.id) in id_set]