|
|
@ -14,6 +14,8 @@ use App\TaskWorker\SSDBTask; |
|
|
use App\Constants\SsdbKeysPrefix; |
|
|
use App\Constants\SsdbKeysPrefix; |
|
|
use Exception; |
|
|
use Exception; |
|
|
|
|
|
|
|
|
|
|
|
use function AlibabaCloud\Client\json; |
|
|
|
|
|
|
|
|
class CouponRebateService implements CouponRebateServiceInterface |
|
|
class CouponRebateService implements CouponRebateServiceInterface |
|
|
{ |
|
|
{ |
|
|
/** |
|
|
/** |
|
|
@ -35,7 +37,6 @@ class CouponRebateService implements CouponRebateServiceInterface |
|
|
$ids = $params["ids"]; |
|
|
$ids = $params["ids"]; |
|
|
$sendUserId = $params["send_user_id"]; |
|
|
$sendUserId = $params["send_user_id"]; |
|
|
$phone = $params["phone"]; |
|
|
$phone = $params["phone"]; |
|
|
$now = time(); |
|
|
|
|
|
$idsData = is_array($ids) ? $ids : explode(',',$ids); |
|
|
$idsData = is_array($ids) ? $ids : explode(',',$ids); |
|
|
|
|
|
|
|
|
// status: 0领取成功 >0领取失败
|
|
|
// status: 0领取成功 >0领取失败
|
|
|
@ -54,119 +55,85 @@ class CouponRebateService implements CouponRebateServiceInterface |
|
|
]; |
|
|
]; |
|
|
|
|
|
|
|
|
$ssdb = ApplicationContext::getContainer()->get(SSDBTask::class); |
|
|
$ssdb = ApplicationContext::getContainer()->get(SSDBTask::class); |
|
|
|
|
|
$activity = $ssdb->exec('hget', SsdbKeysPrefix::COUPON_REBATE_ACTIVITY ,'activity'); |
|
|
$receiveSsdb = []; |
|
|
$receiveSsdb = []; |
|
|
|
|
|
|
|
|
// 判断是否已全部领取
|
|
|
|
|
|
$couponActivity = $ssdb->exec('hget', SsdbKeysPrefix::COUPON_REBATE_ACTIVITY,0); |
|
|
|
|
|
$userReceiveCouponIds = $ssdb->exec('hgetall', SsdbKeysPrefix::COUPON_REBATE_RECEIVE . $couponActivity . $userId); |
|
|
|
|
|
if($userReceiveCouponIds !== false){ |
|
|
|
|
|
|
|
|
// 判断是否已全部领取过
|
|
|
|
|
|
$userReceive = $ssdb->exec('hgetall', SsdbKeysPrefix::COUPON_REBATE_RECEIVE . $activity . $userId); |
|
|
|
|
|
if($userReceive === false){ |
|
|
|
|
|
$ids = $idsData; |
|
|
|
|
|
}else{ |
|
|
|
|
|
$userReceiveCouponIds = empty($userReceive) ? [] : $userReceive ; |
|
|
$ids = array_diff($idsData, $userReceiveCouponIds); |
|
|
$ids = array_diff($idsData, $userReceiveCouponIds); |
|
|
$receiveSsdb = $userReceiveCouponIds; |
|
|
$receiveSsdb = $userReceiveCouponIds; |
|
|
}else{ |
|
|
|
|
|
$ids = $idsData; |
|
|
|
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
if(count($ids) > 0){ |
|
|
if(count($ids) > 0){ |
|
|
try{ |
|
|
try{ |
|
|
Db::transaction( function() use ($ids,$receiveType,$userId,$sendUserId,$phone,$now,&$result,&$errorData,&$receiveSsdb) { |
|
|
|
|
|
|
|
|
Db::transaction( function() use ($ids,$receiveType,$userId,$sendUserId,$phone,&$result,&$errorData,&$receiveSsdb) { |
|
|
|
|
|
|
|
|
|
|
|
$now = time(); |
|
|
$success = []; |
|
|
$success = []; |
|
|
$fail = []; |
|
|
|
|
|
|
|
|
|
|
|
//读写锁,完全控制,性能低
|
|
|
|
|
|
|
|
|
//获取优惠券信息 (读写锁,完全控制,性能低)
|
|
|
$coupons = Coupon::whereIn('id', $ids)->lockForUpdate() |
|
|
$coupons = Coupon::whereIn('id', $ids)->lockForUpdate() |
|
|
->select('id','title','status','inventory','inventory_use','start_time','end_time','full_amount','discounts','active_type') |
|
|
|
|
|
|
|
|
->where('active_type',2) |
|
|
|
|
|
->where('status',1) |
|
|
|
|
|
->where('start_time', '<=', $now) |
|
|
|
|
|
->where('end_time', '>=', $now) |
|
|
|
|
|
->whereRaw('inventory > inventory_use') |
|
|
|
|
|
->select('id','title','inventory','inventory_use','full_amount','discounts','active_type') |
|
|
->get(); |
|
|
->get(); |
|
|
|
|
|
|
|
|
foreach($coupons as $coupon){ |
|
|
foreach($coupons as $coupon){ |
|
|
$errorData['coupon_id'] = $coupon->id; |
|
|
$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; |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
// 查询一次能领取的数量
|
|
|
|
|
|
$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{ |
|
|
}else{ |
|
|
$fail[] = $coupon; |
|
|
|
|
|
$receiveSsdb[] = $coupon->id; |
|
|
|
|
|
$result['status'] = 3; |
|
|
|
|
|
$result['coupon_text'] = '您已领取!赶快去下单吧~'; |
|
|
|
|
|
|
|
|
|
|
|
$errorData['msg'] = '用户已经领取了优惠券'; |
|
|
|
|
|
|
|
|
$errorData['msg'] = '添加优惠券到用户领取表或者记录已领取数量失败'; |
|
|
$this->log->event( |
|
|
$this->log->event( |
|
|
LogLabel::COUPON_LOG, |
|
|
LogLabel::COUPON_LOG, |
|
|
$errorData |
|
|
$errorData |
|
|
); |
|
|
); |
|
|
} |
|
|
} |
|
|
}else{ |
|
|
|
|
|
$fail[] = $coupon; |
|
|
|
|
|
$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{ |
|
|
|
|
|
$fail[] = $coupon; |
|
|
|
|
|
$errorData['msg'] = '优惠券已经过期或者被禁用或者已领完'; |
|
|
|
|
|
$this->log->event( |
|
|
|
|
|
LogLabel::COUPON_LOG, |
|
|
|
|
|
$errorData |
|
|
|
|
|
); |
|
|
|
|
|
} |
|
|
} |
|
|
} |
|
|
} |
|
|
$result['data'] = [ |
|
|
|
|
|
'success' => $success, |
|
|
|
|
|
'fail' => $fail |
|
|
|
|
|
]; |
|
|
|
|
|
if(count($success) > 0){ |
|
|
if(count($success) > 0){ |
|
|
$result['status'] = 0; |
|
|
$result['status'] = 0; |
|
|
$result['coupon_text'] = '恭喜您领取成功!'; |
|
|
$result['coupon_text'] = '恭喜您领取成功!'; |
|
|
@ -187,8 +154,8 @@ class CouponRebateService implements CouponRebateServiceInterface |
|
|
$saveSsdb[] = $vssdb; |
|
|
$saveSsdb[] = $vssdb; |
|
|
} |
|
|
} |
|
|
// 记录到ssdb
|
|
|
// 记录到ssdb
|
|
|
if(false === $ssdb->exec('multi_hset',SsdbKeysPrefix::COUPON_REBATE_RECEIVE . $couponActivity . $userId, $saveSsdb)){ |
|
|
|
|
|
$errorData['msg'] = '记录领取优惠券到ssdb失败'; |
|
|
|
|
|
|
|
|
if(false === $ssdb->exec('multi_hset',SsdbKeysPrefix::COUPON_REBATE_RECEIVE . $activity . $userId, $saveSsdb)){ |
|
|
|
|
|
$errorData['msg'] = '记录领取优惠券到ssdb失败'; |
|
|
$this->log->event( |
|
|
$this->log->event( |
|
|
LogLabel::COUPON_LOG, |
|
|
LogLabel::COUPON_LOG, |
|
|
$errorData |
|
|
$errorData |
|
|
@ -257,64 +224,19 @@ class CouponRebateService implements CouponRebateServiceInterface |
|
|
*/ |
|
|
*/ |
|
|
public function tieCouponActive($couponActivity, $couponForward, $couponRepay) |
|
|
public function tieCouponActive($couponActivity, $couponForward, $couponRepay) |
|
|
{ |
|
|
{ |
|
|
$result = [ |
|
|
|
|
|
'forward' => true, |
|
|
|
|
|
'repay' => true, |
|
|
|
|
|
'activity' => true, |
|
|
|
|
|
]; |
|
|
|
|
|
|
|
|
|
|
|
$ssdb = ApplicationContext::getContainer()->get(SSDBTask::class); |
|
|
$ssdb = ApplicationContext::getContainer()->get(SSDBTask::class); |
|
|
// $rrss1 = $ssdb->exec('hgetall', SsdbKeysPrefix::COUPON_REBATE_FORWARD . $couponActivity);
|
|
|
|
|
|
// $rrss2 = $ssdb->exec('hget', SsdbKeysPrefix::COUPON_REBATE_REPAY . $couponActivity,0);
|
|
|
|
|
|
// $rrss3 = $ssdb->exec('hget', SsdbKeysPrefix::COUPON_REBATE_ACTIVITY,0);
|
|
|
|
|
|
// return [$rrss1,$rrss2,$rrss3];
|
|
|
|
|
|
|
|
|
|
|
|
// 记录活动本次 activity_type 活动标志值
|
|
|
|
|
|
if (false === $ssdb->exec('hset', SsdbKeysPrefix::COUPON_REBATE_ACTIVITY, [0,$couponActivity])) { |
|
|
|
|
|
|
|
|
|
|
|
$this->log->event( |
|
|
|
|
|
LogLabel::COUPON_LOG, |
|
|
|
|
|
[ |
|
|
|
|
|
'coupon_activity' => $couponActivity, |
|
|
|
|
|
'msg' => '记录活动标志值(activity_type)到ssdb失败' |
|
|
|
|
|
] |
|
|
|
|
|
); |
|
|
|
|
|
$result['activity'] = false; |
|
|
|
|
|
}; |
|
|
|
|
|
|
|
|
|
|
|
// 记录领取类型优惠券
|
|
|
|
|
|
$forwardData = []; |
|
|
|
|
|
foreach ($couponForward as $kForward => $vForward) { |
|
|
|
|
|
$forwardData[] = $kForward; |
|
|
|
|
|
$forwardData[] = $vForward; |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
if (false === $ssdb->exec('multi_hset', SsdbKeysPrefix::COUPON_REBATE_FORWARD . $couponActivity, $forwardData)) { |
|
|
|
|
|
|
|
|
|
|
|
$this->log->event( |
|
|
|
|
|
LogLabel::COUPON_LOG, |
|
|
|
|
|
[ |
|
|
|
|
|
'coupon_activity' => $couponActivity, |
|
|
|
|
|
'coupon_forward' => $forwardData, |
|
|
|
|
|
'msg' => '绑定-领取-优惠券到ssdb失败' |
|
|
|
|
|
] |
|
|
|
|
|
); |
|
|
|
|
|
$result['forward'] = false; |
|
|
|
|
|
}; |
|
|
|
|
|
|
|
|
|
|
|
// 记录返还类型优惠券
|
|
|
|
|
|
if (false === $ssdb->exec('hset', SsdbKeysPrefix::COUPON_REBATE_REPAY . $couponActivity, [0,$couponRepay])) { |
|
|
|
|
|
|
|
|
|
|
|
$this->log->event( |
|
|
|
|
|
LogLabel::COUPON_LOG, |
|
|
|
|
|
[ |
|
|
|
|
|
'coupon_activity' => $couponActivity, |
|
|
|
|
|
'coupon_Repay' => $couponRepay, |
|
|
|
|
|
'msg' => '绑定-返还-优惠券到ssdb失败' |
|
|
|
|
|
] |
|
|
|
|
|
); |
|
|
|
|
|
$result['repay'] = false; |
|
|
|
|
|
}; |
|
|
|
|
|
|
|
|
$data = [ |
|
|
|
|
|
'activity', $couponActivity, |
|
|
|
|
|
'forward' , $couponForward, |
|
|
|
|
|
'repay' , $couponRepay |
|
|
|
|
|
]; |
|
|
|
|
|
|
|
|
|
|
|
$result = [ |
|
|
|
|
|
'result' => ($ssdb->exec('multi_hset', SsdbKeysPrefix::COUPON_REBATE_ACTIVITY, $data) === false) ? false : true , |
|
|
|
|
|
'data' => $ssdb->exec('hgetall', SsdbKeysPrefix::COUPON_REBATE_ACTIVITY) |
|
|
|
|
|
]; |
|
|
|
|
|
|
|
|
return $result; |
|
|
return $result; |
|
|
} |
|
|
} |
|
|
@ -445,4 +367,22 @@ class CouponRebateService implements CouponRebateServiceInterface |
|
|
); |
|
|
); |
|
|
return '返券成功'; |
|
|
return '返券成功'; |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
/** |
|
|
|
|
|
* 清优惠券领取记录(SSDB) |
|
|
|
|
|
*/ |
|
|
|
|
|
public function clearSsdbCouponReceiveByName($activity, $userId, $get = 0){ |
|
|
|
|
|
|
|
|
|
|
|
$ssdb = ApplicationContext::getContainer()->get(SSDBTask::class); |
|
|
|
|
|
|
|
|
|
|
|
if($get > 0){ |
|
|
|
|
|
return [ |
|
|
|
|
|
$ssdb->exec('hgetAll', SsdbKeysPrefix::COUPON_REBATE_RECEIVE . $activity . $userId ), |
|
|
|
|
|
$ssdb->exec('hgetAll', SsdbKeysPrefix::COUPON_REBATE_RECEIVE . $userId ), |
|
|
|
|
|
SsdbKeysPrefix::COUPON_REBATE_RECEIVE . $activity . $userId |
|
|
|
|
|
]; |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
return ( $ssdb->exec('hclear', SsdbKeysPrefix::COUPON_REBATE_RECEIVE . $activity . $userId ) === false) ? false : true; |
|
|
|
|
|
} |
|
|
} |
|
|
} |