Browse Source

合并

master
parent
commit
678a2efab5
  1. 84
      app/Service/CouponRebateService.php

84
app/Service/CouponRebateService.php

@ -37,13 +37,13 @@ class CouponRebateService implements CouponRebateServiceInterface
$phone = $params["phone"]; $phone = $params["phone"];
$now = time(); $now = time();
$ids = is_array($ids) ? $ids : explode(',',$ids); $ids = is_array($ids) ? $ids : explode(',',$ids);
// status: 0领取成功 >0领取失败 // status: 0领取成功 >0领取失败
$result = [ $result = [
'status' => 1, 'status' => 1,
'coupon_text' => '继续努力~' 'coupon_text' => '继续努力~'
]; ];
// 错误日志记录 // 错误日志记录
$errorData = [ $errorData = [
'coupon_ids' =>$ids, 'coupon_ids' =>$ids,
@ -54,24 +54,24 @@ class CouponRebateService implements CouponRebateServiceInterface
]; ];
$receiveSsdb = []; $receiveSsdb = [];
try{ try{
Db::transaction( function() use ($ids,$receiveType,$userId,$sendUserId,$phone,$now,&$result,&$errorData,&$receiveSsdb) { Db::transaction( function() use ($ids,$receiveType,$userId,$sendUserId,$phone,$now,&$result,&$errorData,&$receiveSsdb) {
$success = []; $success = [];
$fail = []; $fail = [];
$isr = []; $isr = [];
//读写锁,完全控制,性能低 //读写锁,完全控制,性能低
$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')
->get();
->select('id','title','status','inventory','inventory_use','start_time','end_time','full_amount','discounts','active_type')
->get();
foreach($coupons as $coupon){ foreach($coupons as $coupon){
$errorData['coupon_id'] = $coupon->id; $errorData['coupon_id'] = $coupon->id;
//如果优惠卷库存小于等于已领取的数量 或者 未在活动时间内可领取, 否则返回领取失败的优惠券 //如果优惠卷库存小于等于已领取的数量 或者 未在活动时间内可领取, 否则返回领取失败的优惠券
if (
if (
$coupon->status == 1 $coupon->status == 1
&& &&
$coupon->inventory > $coupon->inventory_use $coupon->inventory > $coupon->inventory_use
@ -81,7 +81,7 @@ class CouponRebateService implements CouponRebateServiceInterface
$coupon->end_time >= $now $coupon->end_time >= $now
&& &&
$coupon->active_type == 2 $coupon->active_type == 2
)
)
{ {
// 查询一次能领取的数量 // 查询一次能领取的数量
@ -90,15 +90,15 @@ class CouponRebateService implements CouponRebateServiceInterface
$couponReceiveType->where('receive_type',$receiveType); $couponReceiveType->where('receive_type',$receiveType);
} }
$couponReceiveType = $couponReceiveType->first(); $couponReceiveType = $couponReceiveType->first();
// 优惠券可领取数量 >= 本次领取数量 // 优惠券可领取数量 >= 本次领取数量
if($coupon->inventory - $coupon->inventory_use >= $couponReceiveType->one_receive_number){ if($coupon->inventory - $coupon->inventory_use >= $couponReceiveType->one_receive_number){
// 判断是否领取过 存在记录则领取过 // 判断是否领取过 存在记录则领取过
$isReceive = CouponRec::select('id') $isReceive = CouponRec::select('id')
->where('system_coupon_user_id',$coupon->id)
->where('user_id',$userId)
->exists();
->where('system_coupon_user_id',$coupon->id)
->where('user_id',$userId)
->exists();
if(!$isReceive){ if(!$isReceive){
//记录已领取的数量 //记录已领取的数量
@ -116,7 +116,7 @@ class CouponRebateService implements CouponRebateServiceInterface
$couponReceive->receive_type = $receiveType; $couponReceive->receive_type = $receiveType;
$couponReceive->send_user_id = $sendUserId; $couponReceive->send_user_id = $sendUserId;
$couponReceive->phone = $phone; $couponReceive->phone = $phone;
// if ( $couponReceive->save() && $coupon->save() ) { // if ( $couponReceive->save() && $coupon->save() ) {
// $success[] = $coupon; // $success[] = $coupon;
// $receiveSsdb[] = $coupon->id;; // $receiveSsdb[] = $coupon->id;;
@ -126,12 +126,12 @@ class CouponRebateService implements CouponRebateServiceInterface
$receiveSsdb[] = $coupon->id; $receiveSsdb[] = $coupon->id;
$result['status'] = 2; $result['status'] = 2;
$result['coupon_text'] = '您已领取!赶快去下单吧~'; $result['coupon_text'] = '您已领取!赶快去下单吧~';
$errorData['msg'] = '用户已经领取了优惠券'; $errorData['msg'] = '用户已经领取了优惠券';
$this->log->event( $this->log->event(
LogLabel::COUPON_LOG, LogLabel::COUPON_LOG,
$errorData $errorData
);
);
} }
}else{ }else{
$fail[] = $coupon; $fail[] = $coupon;
@ -161,6 +161,13 @@ class CouponRebateService implements CouponRebateServiceInterface
$result['coupon_text'] = '恭喜您领取成功!'; $result['coupon_text'] = '恭喜您领取成功!';
} }
}); });
} catch (Exception $e){
$errorData['msg'] = $e->getMessage();
$this->log->event(
LogLabel::COUPON_LOG,
$errorData
);
}
if(count($receiveSsdb) > 0){ if(count($receiveSsdb) > 0){
$saveSsdb = []; $saveSsdb = [];
foreach($receiveSsdb as $kssdb => $vssdb){ foreach($receiveSsdb as $kssdb => $vssdb){
@ -186,11 +193,11 @@ class CouponRebateService implements CouponRebateServiceInterface
public function isCouponRebate($user_id) public function isCouponRebate($user_id)
{ {
$res = Db::table('ims_system_coupon_user as u') $res = Db::table('ims_system_coupon_user as u')
->leftjoin('ims_system_coupon_user_receive as r','u.id','=','r.system_coupon_user_id')
->leftjoin('ims_system_coupon_user_receive as r', 'u.id', '=', 'r.system_coupon_user_id')
->where([ ->where([
['r.user_id' ,'=', $user_id],
['r.receive_type','=', 4],
['u.active_type' ,'=', 2],
['r.user_id', '=', $user_id],
['r.receive_type', '=', 4],
['u.active_type', '=', 2],
]) ])
->select('r.id') ->select('r.id')
->first(); ->first();
@ -205,13 +212,13 @@ class CouponRebateService implements CouponRebateServiceInterface
$time = time(); $time = time();
$res = Db::table('ims_system_coupon_user') $res = Db::table('ims_system_coupon_user')
->where([ ->where([
['status' ,'=', 1],
['active_type' ,'=', 2],
['start_time' ,'<=', $time],
['end_time' ,'>', $time],
['status', '=', 1],
['active_type', '=', 2],
['start_time', '<=', $time],
['end_time', '>', $time],
]) ])
->whereRaw('inventory > inventory_use') ->whereRaw('inventory > inventory_use')
->orderBy('addtime','desc')
->orderBy('addtime', 'desc')
->get(); ->get();
return $res; return $res;
} }
@ -220,47 +227,47 @@ class CouponRebateService implements CouponRebateServiceInterface
* 将优惠券绑定活动 * 将优惠券绑定活动
* 领取优惠券 COUPON_REBATE_FORWARD 可多张 * 领取优惠券 COUPON_REBATE_FORWARD 可多张
* 返还优惠券 COUPON_REBATE_REPAY 只一张 * 返还优惠券 COUPON_REBATE_REPAY 只一张
*/
public function tieCouponActive($couponActivity,$couponForward,$couponRepay)
*/
public function tieCouponActive($couponActivity, $couponForward, $couponRepay)
{ {
$result = [ $result = [
'forward' => true, 'forward' => true,
'repay' => true,
'repay' => 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('get',SsdbKeysPrefix::COUPON_REBATE_REPAY.$couponActivity);
return [$rrss1,$rrss2];
$rrss1 = $ssdb->exec('hgetall', SsdbKeysPrefix::COUPON_REBATE_FORWARD . $couponActivity);
$rrss2 = $ssdb->exec('get', SsdbKeysPrefix::COUPON_REBATE_REPAY . $couponActivity);
return [$rrss1, $rrss2];
// 记录领取类型优惠券 // 记录领取类型优惠券
$forwardData = []; $forwardData = [];
foreach($couponForward as $kForward => $vForward){
foreach ($couponForward as $kForward => $vForward) {
$forwardData[] = $kForward; $forwardData[] = $kForward;
$forwardData[] = $vForward; $forwardData[] = $vForward;
} }
if(false === $ssdb->exec('multi_hset',SsdbKeysPrefix::COUPON_REBATE_FORWARD.$couponActivity,$forwardData)){
if (false === $ssdb->exec('multi_hset', SsdbKeysPrefix::COUPON_REBATE_FORWARD . $couponActivity, $forwardData)) {
$this->log->event( $this->log->event(
LogLabel::COUPON_LOG, LogLabel::COUPON_LOG,
[ [
'coupon_activity' => $couponActivity, 'coupon_activity' => $couponActivity,
'coupon_forward' => $forwardData,
'msg' => '绑定-领取-优惠券到ssdb失败'
'coupon_forward' => $forwardData,
'msg' => '绑定-领取-优惠券到ssdb失败'
] ]
); );
$result['forward'] = false; $result['forward'] = false;
}; };
// 记录返还类型优惠券 // 记录返还类型优惠券
if(false === $ssdb->exec('set',SsdbKeysPrefix::COUPON_REBATE_REPAY.$couponActivity,$couponRepay)){
if (false === $ssdb->exec('set', SsdbKeysPrefix::COUPON_REBATE_REPAY . $couponActivity, $couponRepay)) {
$this->log->event( $this->log->event(
LogLabel::COUPON_LOG, LogLabel::COUPON_LOG,
[ [
'coupon_activity' => $couponActivity, 'coupon_activity' => $couponActivity,
'coupon_Repay' => $couponRepay,
'msg' => '绑定-返还-优惠券到ssdb失败'
'coupon_Repay' => $couponRepay,
'msg' => '绑定-返还-优惠券到ssdb失败'
] ]
); );
$result['repay'] = false; $result['repay'] = false;
@ -344,4 +351,5 @@ class CouponRebateService implements CouponRebateServiceInterface
return '返券成功'; return '返券成功';
}}
}
}
Loading…
Cancel
Save