|
|
|
@ -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,10 +127,6 @@ class CouponController extends BaseController |
|
|
|
$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)){ |
|
|
|
$fail[] = $cp; |
|
|
|
@ -145,8 +139,6 @@ class CouponController extends BaseController |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
} |
|
|
|
}); |
|
|
|
|
|
|
|
return $this->success([ |
|
|
|
|