From 4f700689e515fc1ec667cdb4397ec9f08571b54c Mon Sep 17 00:00:00 2001 From: Lemon <15040771@qq.com> Date: Mon, 19 Oct 2020 17:47:16 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BC=98=E6=83=A0=E5=88=B8=E6=A0=A1=E9=AA=8C?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../v3/Implementations/CouponRecService.php | 17 +++++++++++------ 1 file changed, 11 insertions(+), 6 deletions(-) diff --git a/app/Service/v3/Implementations/CouponRecService.php b/app/Service/v3/Implementations/CouponRecService.php index b6cb27a..692bc52 100644 --- a/app/Service/v3/Implementations/CouponRecService.php +++ b/app/Service/v3/Implementations/CouponRecService.php @@ -422,7 +422,12 @@ class CouponRecService implements CouponRecServiceInterface Db::transaction(function () use ($ids,$receiveType,$userId,&$success,&$fail,$now) { //读写锁,完全控制,性能底 - $cps = Coupon::whereIn('id', $ids)->lockForUpdate()->get(); + + $builder = Coupon::whereIn('id', $ids); + if (env('SUB_CHANNEL') == 1) { + $builder->where('receive_type',$receiveType); + } + $cps = $builder->lockForUpdate()->get(); //写锁,可能在高并发下,读取到脏数据,写入可能有超发情况 //$cps = Coupon::whereIn('id', $ids)->sharedLock()->get(); @@ -432,12 +437,12 @@ class CouponRecService implements CouponRecServiceInterface 'coupon_id' => $cp->id, ]; - if (env('SUB_CHANNEL') == 1) { - $where['receive_type'] = $receiveType; - } - $oneReceiveNumber = CouponRecType::where($where)->value('one_receive_number'); - + $couponInventory = $cp->inventory - $cp->inventory_use; + + if($couponInventory < $oneReceiveNumber){ + continue; + } $cr = new CouponRec; $cr->user_id = $userId; $cr->coupon_id = $cp->id;