|
|
|
@ -21,113 +21,130 @@ class CouponRebateService implements CouponRebateServiceInterface |
|
|
|
|
|
|
|
/** |
|
|
|
* 领取优惠券 |
|
|
|
* 返券活动领取 |
|
|
|
* 一次可领取多张优惠券 |
|
|
|
* 一个用户不可重复领取 |
|
|
|
*/ |
|
|
|
public function userReceiveCoupon($params) |
|
|
|
{ |
|
|
|
$userId = $params["user_id"]; |
|
|
|
$receiveType = $params["receive_type"]; |
|
|
|
$id = $params["id"]; |
|
|
|
$ids = $params["ids"]; |
|
|
|
$sendUserId = $params["send_user_id"]; |
|
|
|
$phone = $params["phone"]; |
|
|
|
$now = time(); |
|
|
|
$ids = is_array($ids) ? implode(',',$ids) : $ids; |
|
|
|
|
|
|
|
// status : 0 领取成功 >0 领取失败
|
|
|
|
// status: 0领取成功 >0领取失败
|
|
|
|
$result = [ |
|
|
|
'status' => 1, |
|
|
|
'data' => [], |
|
|
|
'coupon_text' => '继续努力~' |
|
|
|
]; |
|
|
|
|
|
|
|
// 错误日志记录
|
|
|
|
$errorData = [ |
|
|
|
'coupon_id' =>$id, |
|
|
|
'coupon_ids' =>$ids, |
|
|
|
'user_id' =>$userId, |
|
|
|
'receiveType' =>$receiveType, |
|
|
|
'sendUserId' =>$sendUserId, |
|
|
|
'phone' =>$phone |
|
|
|
]; |
|
|
|
|
|
|
|
Db::transaction( function() use ($id,$receiveType,$userId,$sendUserId,$phone,$now,&$result,&$errorData) { |
|
|
|
Db::transaction( function() use ($ids,$receiveType,$userId,$sendUserId,$phone,$now,&$result,&$errorData) { |
|
|
|
|
|
|
|
try{ |
|
|
|
//读写锁,完全控制,性能低
|
|
|
|
$cp = Coupon::where('id', $id)->lockForUpdate() |
|
|
|
->select('id','title','status','inventory','inventory_use','start_time','end_time','full_amount','discounts') |
|
|
|
->first(); |
|
|
|
|
|
|
|
$couponReceiveType = CouponUserRecType::where('system_coupon_user_id',$cp->id)->select('one_receive_number'); |
|
|
|
try{ |
|
|
|
//读写锁,完全控制,性能低
|
|
|
|
$coupons = Coupon::whereIn('id', $ids)->lockForUpdate() |
|
|
|
->select('id','title','status','inventory','inventory_use','start_time','end_time','full_amount','discounts') |
|
|
|
->get(); |
|
|
|
|
|
|
|
if (env('SUB_CHANNEL') == 1) { |
|
|
|
$couponReceiveType->where('receive_type',$receiveType); |
|
|
|
} |
|
|
|
// 查询一次能领取的数量
|
|
|
|
$crt = $couponReceiveType->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 |
|
|
|
) |
|
|
|
{ |
|
|
|
$result['status'] = 2; |
|
|
|
$result['coupon_text'] = '红包已经领完~'; |
|
|
|
foreach($coupons as $coupon){ |
|
|
|
$errorData['coupon_id'] = $coupon->id; |
|
|
|
|
|
|
|
$errorData['msg'] = '优惠券已经过期或者被禁用或者已领完'; |
|
|
|
$this->log->event( |
|
|
|
LogLabel::COUPON_LOG, |
|
|
|
$errorData |
|
|
|
); |
|
|
|
}else{ |
|
|
|
// 判断是否领取过 存在记录则领取过
|
|
|
|
$isReceive = CouponRec::select('id') |
|
|
|
->where('system_coupon_user_id',$id) |
|
|
|
->where('user_id',$userId) |
|
|
|
->exists(); |
|
|
|
//如果优惠卷库存小于等于已领取的数量 或者 未在活动时间内可领取, 否则返回领取失败的优惠券
|
|
|
|
if ( |
|
|
|
$coupon->status == 1 |
|
|
|
&& |
|
|
|
$coupon->inventory > $coupon->inventory_use |
|
|
|
&& |
|
|
|
$coupon->start_time <= $now |
|
|
|
&& |
|
|
|
$coupon->end_time >= $now |
|
|
|
) |
|
|
|
{ |
|
|
|
|
|
|
|
if(!$isReceive){ |
|
|
|
// 查询一次能领取的数量
|
|
|
|
$couponReceiveType = CouponUserRecType::where('system_coupon_user_id',$coupon->id)->select('one_receive_number'); |
|
|
|
if (env('SUB_CHANNEL') == 1) { |
|
|
|
$couponReceiveType->where('receive_type',$receiveType); |
|
|
|
} |
|
|
|
$couponReceiveType = $couponReceiveType->first(); |
|
|
|
|
|
|
|
// 优惠券可领取数量 >= 本次领取数量
|
|
|
|
if($coupon->inventory - $coupon->inventory_use >= $couponReceiveType->one_receive_number){ |
|
|
|
|
|
|
|
$cp->inventory_use += $cr->number;//记录已领取的数量
|
|
|
|
|
|
|
|
if ( $cr->save() && $cp->save() ) { |
|
|
|
$result['status'] = 0; |
|
|
|
// $result['data'] = $cp;
|
|
|
|
$result['coupon_text'] = $cp['discounts'].'元红包'; |
|
|
|
// 判断是否领取过 存在记录则领取过
|
|
|
|
$isReceive = CouponRec::select('id') |
|
|
|
->where('system_coupon_user_id',$ids) |
|
|
|
->where('user_id',$userId) |
|
|
|
->exists(); |
|
|
|
|
|
|
|
if(!$isReceive){ |
|
|
|
//记录已领取的数量
|
|
|
|
$coupon->inventory_use += $couponReceiveType->number; |
|
|
|
|
|
|
|
$couponReceive = new CouponRec; |
|
|
|
$couponReceive->user_id = $userId; |
|
|
|
$couponReceive->system_coupon_user_id = $coupon->id; |
|
|
|
$couponReceive->order_main_id = 0; |
|
|
|
$couponReceive->receive_time = $now; |
|
|
|
$couponReceive->number = $couponReceiveType->one_receive_number; |
|
|
|
$couponReceive->number_remain = $couponReceiveType->one_receive_number; |
|
|
|
$couponReceive->status = 0; |
|
|
|
$couponReceive->update_time = $now; |
|
|
|
$couponReceive->receive_type = $receiveType; |
|
|
|
$couponReceive->send_user_id = $sendUserId; |
|
|
|
$couponReceive->phone = $phone; |
|
|
|
|
|
|
|
if ( $couponReceive->save() && $coupon->save() ) { |
|
|
|
$result['status'] = 0; |
|
|
|
$result['coupon_text'] = '恭喜您领取成功!'; |
|
|
|
} |
|
|
|
}else{ |
|
|
|
$result['status'] = 2; |
|
|
|
$result['coupon_text'] = '您已领取!赶快去下单吧~'; |
|
|
|
|
|
|
|
$errorData['msg'] = '用户已经领取了优惠券'; |
|
|
|
$this->log->event( |
|
|
|
LogLabel::COUPON_LOG, |
|
|
|
$errorData |
|
|
|
); |
|
|
|
} |
|
|
|
}else{ |
|
|
|
$errorData['remain_receive_number'] = $coupon->inventory - $coupon->inventory_use; |
|
|
|
$errorData['one_receive_number'] = $couponReceiveType->one_receive_number; |
|
|
|
$errorData['msg'] = '优惠券剩余数量不足'; |
|
|
|
$this->log->event( |
|
|
|
LogLabel::COUPON_LOG, |
|
|
|
$errorData |
|
|
|
); |
|
|
|
} |
|
|
|
}else{ |
|
|
|
$errorData['msg'] = '优惠券已经过期或者被禁用或者已领完'; |
|
|
|
$this->log->event( |
|
|
|
LogLabel::COUPON_LOG, |
|
|
|
$errorData |
|
|
|
); |
|
|
|
} |
|
|
|
}else{ |
|
|
|
$errorData['msg'] = '用户已经领取了此优惠券'; |
|
|
|
$this->log->event( |
|
|
|
LogLabel::COUPON_LOG, |
|
|
|
$errorData |
|
|
|
); |
|
|
|
} |
|
|
|
|
|
|
|
} catch (Exception $e){ |
|
|
|
$errorData['msg'] = $e->getMessage(); |
|
|
|
$this->log->event( |
|
|
|
LogLabel::COUPON_LOG, |
|
|
|
$errorData |
|
|
|
); |
|
|
|
} |
|
|
|
} catch (Exception $e){ |
|
|
|
$errorData['msg'] = $e->getMessage(); |
|
|
|
$this->log->event( |
|
|
|
LogLabel::COUPON_LOG, |
|
|
|
$errorData |
|
|
|
); |
|
|
|
} |
|
|
|
}); |
|
|
|
|
|
|
|
return $result; |
|
|
|
|