Browse Source

Merge branch 'develop' of http://120.24.33.109:11081/hyzjshwo/lanzu_api_hyperf into develop

master
parent
commit
df545ad45e
  1. 1
      .gitignore
  2. 54
      app/Controller/CouponController.php
  3. 2
      app/Controller/IndexController.php

1
.gitignore

@ -11,3 +11,4 @@ vendor/
.DS_Store .DS_Store
*.lock *.lock
.phpunit* .phpunit*
/watch

54
app/Controller/CouponController.php

@ -98,35 +98,36 @@ class CouponController extends BaseController
$userId = $this->request->input("user_id"); $userId = $this->request->input("user_id");
$receiveType = $this->request->input("receive_type"); $receiveType = $this->request->input("receive_type");
$ids = $this->request->input("ids"); $ids = $this->request->input("ids");
$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 = [];
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 会有超发情况
Db::transaction(function () use ($ids,$receiveType,$userId,&$success,&$fail,$now) {
//读写锁,完全控制,性能底
$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 ($cp->inventory<=$cp->inventory_use||$cp->inventory<($cp->inventory_use+$cr->number)){ if ($cp->inventory<=$cp->inventory_use||$cp->inventory<($cp->inventory_use+$cr->number)){
$fail[] = $cp; $fail[] = $cp;
@ -139,8 +140,7 @@ class CouponController extends BaseController
} }
} }
} }
}
});
return $this->success([ return $this->success([
'success' => $success, 'success' => $success,

2
app/Controller/IndexController.php

@ -20,7 +20,7 @@ class IndexController extends AbstractController
return [ return [
'method' => $method, 'method' => $method,
'message' => "Hello {$user}.",
'message' => "Hello22222 {$user}.",
]; ];
} }
} }
Loading…
Cancel
Save