From 1ddf72a15fdbf0b36f2624c114dba1421877cb0d Mon Sep 17 00:00:00 2001 From: liangyuyan <1103300295@qq.com> Date: Wed, 5 Aug 2020 21:28:48 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BC=98=E6=83=A0=E5=88=B8=E8=BF=94=E5=88=B8?= =?UTF-8?q?=E6=B4=BB=E5=8A=A8--=E4=BF=AE=E6=94=B9=E9=A2=86=E5=8F=96?= =?UTF-8?q?=E6=8E=A5=E5=8F=A3?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/Service/CouponRebateService.php | 22 ++++++++++++++-------- 1 file changed, 14 insertions(+), 8 deletions(-) diff --git a/app/Service/CouponRebateService.php b/app/Service/CouponRebateService.php index 7c06421..6d488c1 100644 --- a/app/Service/CouponRebateService.php +++ b/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) ? implode(',',$ids) : $ids; + $ids = is_array($ids) ? $ids : explode(',',$ids); // status: 0领取成功 >0领取失败 $result = [ @@ -60,9 +60,10 @@ class CouponRebateService implements CouponRebateServiceInterface $success = []; $fail = []; + $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(); @@ -95,7 +96,7 @@ class CouponRebateService implements CouponRebateServiceInterface // 判断是否领取过 存在记录则领取过 $isReceive = CouponRec::select('id') - ->where('system_coupon_user_id',$ids) + ->where('system_coupon_user_id',$coupon->id) ->where('user_id',$userId) ->exists(); @@ -116,10 +117,10 @@ class CouponRebateService implements CouponRebateServiceInterface $couponReceive->send_user_id = $sendUserId; $couponReceive->phone = $phone; - if ( $couponReceive->save() && $coupon->save() ) { - $success[] = $coupon; - $receiveSsdb[] = $coupon->id;; - } + // if ( $couponReceive->save() && $coupon->save() ) { + // $success[] = $coupon; + // $receiveSsdb[] = $coupon->id;; + // } }else{ $fail[] = $coupon; $receiveSsdb[] = $coupon->id; @@ -168,9 +169,14 @@ class CouponRebateService implements CouponRebateServiceInterface ); } 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('hset',SsdbKeysPrefix::COUPON_REBATE_RECEIVE.$userId,$receiveSsdb)){ + if(false === $ssdb->exec('hset',SsdbKeysPrefix::COUPON_REBATE_RECEIVE.$userId,$saveSsdb)){ $errorData['msg'] = '记录领取优惠券到ssdb失败'; $this->log->event( LogLabel::COUPON_LOG,