Browse Source

优惠券返券活动--优化用户领取优惠券接口

master
liangyuyan 5 years ago
parent
commit
343a4be0b9
  1. 240
      app/Service/CouponRebateService.php

240
app/Service/CouponRebateService.php

@ -36,7 +36,7 @@ class CouponRebateService implements CouponRebateServiceInterface
$sendUserId = $params["send_user_id"];
$phone = $params["phone"];
$now = time();
$ids = is_array($ids) ? $ids : explode(',',$ids);
$idsData = is_array($ids) ? $ids : explode(',',$ids);
// status: 0领取成功 >0领取失败
$result = [
@ -53,81 +53,102 @@ class CouponRebateService implements CouponRebateServiceInterface
'phone' =>$phone
];
$ssdb = ApplicationContext::getContainer()->get(SSDBTask::class);
$receiveSsdb = [];
try{
Db::transaction( function() use ($ids,$receiveType,$userId,$sendUserId,$phone,$now,&$result,&$errorData,&$receiveSsdb) {
$success = [];
$fail = [];
$isr = [];
//读写锁,完全控制,性能低
$coupons = Coupon::whereIn('id', $ids)->lockForUpdate()
->select('id','title','status','inventory','inventory_use','start_time','end_time','full_amount','discounts','active_type')
->get();
foreach($coupons as $coupon){
$errorData['coupon_id'] = $coupon->id;
//如果优惠卷库存小于等于已领取的数量 或者 未在活动时间内可领取, 否则返回领取失败的优惠券
if (
$coupon->status == 1
&&
$coupon->inventory > $coupon->inventory_use
&&
$coupon->start_time <= $now
&&
$coupon->end_time >= $now
&&
$coupon->active_type == 2
)
{
// 查询一次能领取的数量
$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){
// 判断是否领取过 存在记录则领取过
$isReceive = CouponRec::select('id')
->where('system_coupon_user_id',$coupon->id)
->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() ) {
// $success[] = $coupon;
// $receiveSsdb[] = $coupon->id;;
// }
// 判断是否已全部领取
$couponActivity = $ssdb->exec('get', SsdbKeysPrefix::COUPON_REBATE_ACTIVITY);
$userReceiveCouponIds = $ssdb->exec('hgetall', SsdbKeysPrefix::COUPON_REBATE_FORWARD . $couponActivity);
if($userReceiveCouponIds !== false){
$ids = array_diff($idsData, $userReceiveCouponIds);
}else{
$ids = $idsData;
}
if(count($ids) > 0){
try{
Db::transaction( function() use ($ids,$receiveType,$userId,$sendUserId,$phone,$now,&$result,&$errorData,&$receiveSsdb) {
$success = [];
$fail = [];
$isr = [];
//读写锁,完全控制,性能低
$coupons = Coupon::whereIn('id', $ids)->lockForUpdate()
->select('id','title','status','inventory','inventory_use','start_time','end_time','full_amount','discounts','active_type')
->get();
foreach($coupons as $coupon){
$errorData['coupon_id'] = $coupon->id;
//如果优惠卷库存小于等于已领取的数量 或者 未在活动时间内可领取, 否则返回领取失败的优惠券
if (
$coupon->status == 1
&&
$coupon->inventory > $coupon->inventory_use
&&
$coupon->start_time <= $now
&&
$coupon->end_time >= $now
&&
$coupon->active_type == 2
)
{
// 查询一次能领取的数量
$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){
// 判断是否领取过 存在记录则领取过
$isReceive = CouponRec::select('id')
->where('system_coupon_user_id',$coupon->id)
->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() ) {
// $success[] = $coupon;
// $receiveSsdb[] = $coupon->id;;
// }
}else{
$fail[] = $coupon;
$receiveSsdb[] = $coupon->id;
$result['status'] = 2;
$result['coupon_text'] = '您已领取!赶快去下单吧~';
$errorData['msg'] = '用户已经领取了优惠券';
$this->log->event(
LogLabel::COUPON_LOG,
$errorData
);
}
}else{
$fail[] = $coupon;
$receiveSsdb[] = $coupon->id;
$result['status'] = 2;
$result['coupon_text'] = '您已领取!赶快去下单吧~';
$errorData['msg'] = '用户已经领取了优惠券';
$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
@ -135,55 +156,50 @@ class CouponRebateService implements CouponRebateServiceInterface
}
}else{
$fail[] = $coupon;
$errorData['remain_receive_number'] = $coupon->inventory - $coupon->inventory_use;
$errorData['one_receive_number'] = $couponReceiveType->one_receive_number;
$errorData['msg'] = '优惠券剩余数量不足';
$errorData['msg'] = '优惠券已经过期或者被禁用或者已领完';
$this->log->event(
LogLabel::COUPON_LOG,
$errorData
);
}
}else{
$fail[] = $coupon;
$errorData['msg'] = '优惠券已经过期或者被禁用或者已领完';
$this->log->event(
LogLabel::COUPON_LOG,
$errorData
);
}
}
$result['data'] = [
'success' => $success,
'fail' => $fail
];
if(count($success) > 0){
$result['status'] = 0;
$result['coupon_text'] = '恭喜您领取成功!';
}
});
} catch (Exception $e){
$errorData['msg'] = $e->getMessage();
$this->log->event(
LogLabel::COUPON_LOG,
$errorData
);
}
if(count($receiveSsdb) > 0){
$saveSsdb = [];
foreach($receiveSsdb as $kssdb => $vssdb){
$saveSsdb[] = $kssdb;
$saveSsdb[] = $vssdb;
}
// 记录到ssdb
$ssdb = ApplicationContext::getContainer()->get(SSDBTask::class);
if(false === $ssdb->exec('multi_hset',SsdbKeysPrefix::COUPON_REBATE_RECEIVE.$userId,$saveSsdb)){
$errorData['msg'] = '记录领取优惠券到ssdb失败';
$result['data'] = [
'success' => $success,
'fail' => $fail
];
if(count($success) > 0){
$result['status'] = 0;
$result['coupon_text'] = '恭喜您领取成功!';
}
});
} catch (Exception $e){
$errorData['msg'] = $e->getMessage();
$this->log->event(
LogLabel::COUPON_LOG,
$errorData
);
};
}
if(count($receiveSsdb) > 0){
$saveSsdb = [];
foreach($receiveSsdb as $kssdb => $vssdb){
$saveSsdb[] = $kssdb;
$saveSsdb[] = $vssdb;
}
// 记录到ssdb
if(false === $ssdb->exec('multi_hset',SsdbKeysPrefix::COUPON_REBATE_RECEIVE.$userId,$saveSsdb)){
$errorData['msg'] = '记录领取优惠券到ssdb失败';
$this->log->event(
LogLabel::COUPON_LOG,
$errorData
);
};
}
}else{
$result['status'] = 2;
$result['coupon_text'] = '您已领取!赶快去下单吧~';
}
return $result;
}
@ -242,8 +258,8 @@ class CouponRebateService implements CouponRebateServiceInterface
// $rrss3 = $ssdb->exec('get', SsdbKeysPrefix::COUPON_REBATE_ACTIVITY);
// return [$rrss3];
// 记录活动本次 activity_type 活动标志值
if (false === $ssdb->exec('set', SsdbKeysPrefix::COUPON_REBATE_ACTIVITY, $couponActivity)) {
// 记录活动本次 activity_type 活动标志值
if (false === $ssdb->exec('set', SsdbKeysPrefix::COUPON_REBATE_ACTIVITY, $couponActivity)) {
$this->log->event(
LogLabel::COUPON_LOG,

Loading…
Cancel
Save