Browse Source

修改领取优惠卷可能会超发的情况

master
Mike 5 years ago
parent
commit
cf5603b94c
  1. 10
      app/Controller/CouponController.php

10
app/Controller/CouponController.php

@ -100,10 +100,17 @@ class CouponController extends BaseController
$test = $this->request->input("test", 0); $test = $this->request->input("test", 0);
$ids = explode(',', $ids); $ids = explode(',', $ids);
$cps = Coupon::whereIn('id', $ids)->get();
$now = time(); $now = time();
$success = []; $success = [];
$fail = []; $fail = [];
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) { foreach ($cps as $key => $cp) {
$crt = CouponUserRecType::where( $crt = CouponUserRecType::where(
[ [
@ -140,6 +147,7 @@ class CouponController extends BaseController
} }
} }
});
return $this->success([ return $this->success([
'success' => $success, 'success' => $success,

Loading…
Cancel
Save