|
|
|
@ -47,92 +47,82 @@ class CouponRebateController extends BaseController |
|
|
|
return $this->success($res); |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
/** |
|
|
|
* 用户领取优惠券 |
|
|
|
*/ |
|
|
|
* 2020.08.04 只领一张优惠券 |
|
|
|
*/ |
|
|
|
public function userReceiveCoupon(CouponRebateReceiveRequest $validator) |
|
|
|
{ |
|
|
|
$userId = $this->request->input("user_id", 0); |
|
|
|
$receiveType = $this->request->input("receive_type", 0); |
|
|
|
$ids = $this->request->input("ids", ''); |
|
|
|
$id = $this->request->input("id", 0); |
|
|
|
$sendUserId = $this->request->input("send_user_id", 0); |
|
|
|
$phone = $this->request->input("phone", ''); |
|
|
|
$ids = explode(',', $ids); |
|
|
|
$now = time(); |
|
|
|
|
|
|
|
// mock 数据 给前端用
|
|
|
|
$test = $this->request->input("test", 0); |
|
|
|
if($test){ |
|
|
|
$cps = Coupon::whereIn('id', $ids)->lockForUpdate() |
|
|
|
->select('id','title','status','inventory','inventory_use','start_time','end_time') |
|
|
|
->first(); |
|
|
|
return $this->success([ |
|
|
|
'success' => [$cps], |
|
|
|
'fail' => [], |
|
|
|
]); |
|
|
|
} |
|
|
|
// status : 0 领取成功 >0 领取失败
|
|
|
|
$return = [ |
|
|
|
'status' => 1, |
|
|
|
'data' => [], |
|
|
|
'coupon_text' => '继续努力~' |
|
|
|
]; |
|
|
|
|
|
|
|
$success = []; |
|
|
|
$fail = []; |
|
|
|
|
|
|
|
Db::transaction( function () use ($ids,$receiveType,$userId,$sendUserId,$phone,&$success,&$fail,$now) { |
|
|
|
Db::transaction( function () use ($id,$receiveType,$userId,$sendUserId,$phone,&$return,$now) { |
|
|
|
//读写锁,完全控制,性能低
|
|
|
|
$cps = Coupon::whereIn('id', $ids)->lockForUpdate() |
|
|
|
->select('id','title','status','inventory','inventory_use','start_time','end_time') |
|
|
|
->get(); |
|
|
|
|
|
|
|
foreach ($cps as $key => $cp) { |
|
|
|
|
|
|
|
$where = [ |
|
|
|
'system_coupon_user_id' => $cp->id, |
|
|
|
]; |
|
|
|
|
|
|
|
if (env('SUB_CHANNEL') == 1) { |
|
|
|
$where['receive_type'] = $receiveType; |
|
|
|
} |
|
|
|
|
|
|
|
$crt = CouponUserRecType::where($where)->first(); |
|
|
|
$cp = Coupon::where('id', $id)->lockForUpdate() |
|
|
|
->select('id','title','status','inventory','inventory_use','start_time','end_time','full_amount','discounts') |
|
|
|
->first(); |
|
|
|
|
|
|
|
$where = [ |
|
|
|
'system_coupon_user_id' => $cp->id, |
|
|
|
]; |
|
|
|
|
|
|
|
$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; |
|
|
|
$cr->send_user_id = $sendUserId; |
|
|
|
$cr->phone = $phone; |
|
|
|
//如果优惠卷库存小于等于已领取的数量 或者 未在活动时间内, 则返回领取失败的优惠券
|
|
|
|
if ( |
|
|
|
$cp->status != 1 |
|
|
|
|| |
|
|
|
$cp->inventory <= $cp->inventory_use |
|
|
|
|| |
|
|
|
$cp->inventory < ($cp->inventory_use+$cr->number) |
|
|
|
|| |
|
|
|
$cp->start_time < $now |
|
|
|
|| |
|
|
|
$cp->end_time > $now |
|
|
|
) |
|
|
|
{ |
|
|
|
$fail[] = $cp; |
|
|
|
}else{ |
|
|
|
$cp->inventory_use += $cr->number;//记录已领取的数量
|
|
|
|
if ( $cr->save() && $cp->save() ) { |
|
|
|
$success[] = $cp; |
|
|
|
} else { |
|
|
|
$fail[] = $cp; |
|
|
|
} |
|
|
|
if (env('SUB_CHANNEL') == 1) { |
|
|
|
$where['receive_type'] = $receiveType; |
|
|
|
} |
|
|
|
// 查询领取类型一次能领取的数量
|
|
|
|
$crt = CouponUserRecType::where($where)->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; |
|
|
|
$cr->send_user_id = $sendUserId; |
|
|
|
$cr->phone = $phone; |
|
|
|
|
|
|
|
//如果优惠卷库存小于等于已领取的数量 或者 未在活动时间内, 则返回领取失败的优惠券
|
|
|
|
if ( |
|
|
|
$cp->status != 1 |
|
|
|
|| |
|
|
|
$cp->inventory <= $cp->inventory_use |
|
|
|
|| |
|
|
|
$cp->inventory < ($cp->inventory_use+$cr->number) |
|
|
|
|| |
|
|
|
$cp->start_time > $now |
|
|
|
|| |
|
|
|
$cp->end_time < $now |
|
|
|
) |
|
|
|
{ |
|
|
|
$return['status'] = 2; |
|
|
|
$return['coupon_text'] = '红包已经领完~'; |
|
|
|
}else{ |
|
|
|
$cp->inventory_use += $cr->number;//记录已领取的数量
|
|
|
|
if ( $cr->save() && $cp->save() ) { |
|
|
|
$return['status'] = 0; |
|
|
|
$return['data'] = $cp; |
|
|
|
$return['coupon_text'] = $cp['discounts'].'元红包'; |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
}); |
|
|
|
|
|
|
|
return $this->success([ |
|
|
|
'success' => $success, |
|
|
|
'fail' => $fail, |
|
|
|
]); |
|
|
|
return $this->success($return); |
|
|
|
} |
|
|
|
} |