From cf5603b94c823f6e95ec31cf914f516a7027cd56 Mon Sep 17 00:00:00 2001 From: Mike Date: Sun, 19 Jul 2020 09:41:27 +0800 Subject: [PATCH 1/4] =?UTF-8?q?=E4=BF=AE=E6=94=B9=E9=A2=86=E5=8F=96?= =?UTF-8?q?=E4=BC=98=E6=83=A0=E5=8D=B7=E5=8F=AF=E8=83=BD=E4=BC=9A=E8=B6=85?= =?UTF-8?q?=E5=8F=91=E7=9A=84=E6=83=85=E5=86=B5?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/Controller/CouponController.php | 72 ++++++++++++++++------------- 1 file changed, 40 insertions(+), 32 deletions(-) diff --git a/app/Controller/CouponController.php b/app/Controller/CouponController.php index a064f2e..0ea6916 100644 --- a/app/Controller/CouponController.php +++ b/app/Controller/CouponController.php @@ -100,46 +100,54 @@ class CouponController extends BaseController $test = $this->request->input("test", 0); $ids = explode(',', $ids); - $cps = Coupon::whereIn('id', $ids)->get(); $now = time(); $success = []; $fail = []; - foreach ($cps as $key => $cp) { - $crt = CouponUserRecType::where( - [ - 'system_coupon_user_id' => $cp->id, - 'receive_type' => $receiveType - ] - )->first(); - - $cr = new CouponRec; - $cr->user_id = $userId; - $cr->system_coupon_user_id = $cp->id; - $cr->order_main_id = 0; - $cr->receive_time = $now; - $cr->number = $crt->one_receive_number; - $cr->number_remain = $crt->one_receive_number; - $cr->status = 0; - $cr->update_time = $now; - $cr->receive_type = $receiveType; - if ($test && ($cp->id % 2)) { - $fail[] = $cp; - } else { - //TODO 会有超发情况 - //如果优惠卷库存小于等于已领取的数量, 则返回领取失败的优惠券 - if ($cp->inventory<=$cp->inventory_use||$cp->inventory<($cp->inventory_use+$cr->number)){ + + + Db::transaction(function () use ($ids,$receiveType,$userId,&$success,&$fail,$test) { + //读写锁,完全控制,性能底 + $cps = Coupon::whereIn('id', $ids)->lockForUpdate()->get(); + //写锁,可能在高并发下,读取到脏数据,写入可能有超发情况 + //$cps = Coupon::whereIn('id', $ids)->sharedLock()->get(); + + foreach ($cps as $key => $cp) { + $crt = CouponUserRecType::where( + [ + 'system_coupon_user_id' => $cp->id, + 'receive_type' => $receiveType + ] + )->first(); + + $cr = new CouponRec; + $cr->user_id = $userId; + $cr->system_coupon_user_id = $cp->id; + $cr->order_main_id = 0; + $cr->receive_time = $now; + $cr->number = $crt->one_receive_number; + $cr->number_remain = $crt->one_receive_number; + $cr->status = 0; + $cr->update_time = $now; + $cr->receive_type = $receiveType; + if ($test && ($cp->id % 2)) { $fail[] = $cp; - }else{ - $cp->inventory_use += $cr->number;//记录已领取的数量 - if ($cr->save()&&$cp->save()) { - $success[] = $cp; - } else { + } else { + //TODO 会有超发情况 + //如果优惠卷库存小于等于已领取的数量, 则返回领取失败的优惠券 + if ($cp->inventory<=$cp->inventory_use||$cp->inventory<($cp->inventory_use+$cr->number)){ $fail[] = $cp; + }else{ + $cp->inventory_use += $cr->number;//记录已领取的数量 + if ($cr->save()&&$cp->save()) { + $success[] = $cp; + } else { + $fail[] = $cp; + } } } - } - } + } + }); return $this->success([ 'success' => $success, From 5583bd8568b4869d9220586d39d89d238ff461e2 Mon Sep 17 00:00:00 2001 From: weigang Date: Sun, 19 Jul 2020 09:48:51 +0800 Subject: [PATCH 2/4] =?UTF-8?q?=E5=BF=BD=E7=95=A5=E7=9B=91=E5=90=AC?= =?UTF-8?q?=E6=96=87=E4=BB=B6?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .gitignore | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/.gitignore b/.gitignore index c867e8f..9091e68 100644 --- a/.gitignore +++ b/.gitignore @@ -10,4 +10,5 @@ vendor/ .env .DS_Store *.lock -.phpunit* \ No newline at end of file +.phpunit* +/watch From e1b1d8acbe30c8cedca06f31d3d2a4c2e3a6f638 Mon Sep 17 00:00:00 2001 From: lanzu_qsy <334039090@qq.com> Date: Sun, 19 Jul 2020 10:10:52 +0800 Subject: [PATCH 3/4] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=E4=BC=98=E6=83=A0?= =?UTF-8?q?=E5=88=B8=E6=8E=A5=E5=8F=A3=20=E5=8E=BB=E9=99=A4=E6=B5=8B?= =?UTF-8?q?=E8=AF=95=E6=95=B0=E6=8D=AE?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/Controller/IndexController.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/app/Controller/IndexController.php b/app/Controller/IndexController.php index 60c3235..3864423 100644 --- a/app/Controller/IndexController.php +++ b/app/Controller/IndexController.php @@ -20,7 +20,7 @@ class IndexController extends AbstractController return [ 'method' => $method, - 'message' => "Hello {$user}.", + 'message' => "Hello22222 {$user}.", ]; } } From 4122537584dd2650985a8d31b4d1f119fd0a4d62 Mon Sep 17 00:00:00 2001 From: lanzu_qsy <334039090@qq.com> Date: Sun, 19 Jul 2020 10:12:57 +0800 Subject: [PATCH 4/4] =?UTF-8?q?=E5=8E=BB=E9=99=A4=E9=A2=86=E5=8F=96?= =?UTF-8?q?=E4=BC=98=E6=83=A0=E5=88=B8=E6=B5=8B=E8=AF=95=E6=95=B0=E6=8D=AE?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/Controller/CouponController.php | 24 ++++++++---------------- 1 file changed, 8 insertions(+), 16 deletions(-) diff --git a/app/Controller/CouponController.php b/app/Controller/CouponController.php index 0ea6916..4c01b0b 100644 --- a/app/Controller/CouponController.php +++ b/app/Controller/CouponController.php @@ -97,15 +97,13 @@ class CouponController extends BaseController $userId = $this->request->input("user_id"); $receiveType = $this->request->input("receive_type"); $ids = $this->request->input("ids"); - $test = $this->request->input("test", 0); - $ids = explode(',', $ids); $now = time(); $success = []; $fail = []; - Db::transaction(function () use ($ids,$receiveType,$userId,&$success,&$fail,$test) { + Db::transaction(function () use ($ids,$receiveType,$userId,&$success,&$fail,$now) { //读写锁,完全控制,性能底 $cps = Coupon::whereIn('id', $ids)->lockForUpdate()->get(); //写锁,可能在高并发下,读取到脏数据,写入可能有超发情况 @@ -129,23 +127,17 @@ class CouponController extends BaseController $cr->status = 0; $cr->update_time = $now; $cr->receive_type = $receiveType; - if ($test && ($cp->id % 2)) { + //如果优惠卷库存小于等于已领取的数量, 则返回领取失败的优惠券 + if ($cp->inventory<=$cp->inventory_use||$cp->inventory<($cp->inventory_use+$cr->number)){ $fail[] = $cp; - } else { - //TODO 会有超发情况 - //如果优惠卷库存小于等于已领取的数量, 则返回领取失败的优惠券 - if ($cp->inventory<=$cp->inventory_use||$cp->inventory<($cp->inventory_use+$cr->number)){ + }else{ + $cp->inventory_use += $cr->number;//记录已领取的数量 + if ($cr->save()&&$cp->save()) { + $success[] = $cp; + } else { $fail[] = $cp; - }else{ - $cp->inventory_use += $cr->number;//记录已领取的数量 - if ($cr->save()&&$cp->save()) { - $success[] = $cp; - } else { - $fail[] = $cp; - } } } - } });