Browse Source

优惠券校验

master
Lemon 5 years ago
parent
commit
4f700689e5
  1. 17
      app/Service/v3/Implementations/CouponRecService.php

17
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;

Loading…
Cancel
Save