From 8e004f23c774b1fabc087dfd116812532e3a416f Mon Sep 17 00:00:00 2001 From: liangyuyan <1103300295@qq.com> Date: Wed, 5 Aug 2020 21:04:52 +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-=E9=A2=86=E5=8F=96=E6=88=90=E5=8A=9F?= =?UTF-8?q?=E8=AE=B0=E5=BD=95=E5=88=B0ssdb?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/Constants/SsdbKeysPrefix.php | 5 +++ app/Service/CouponRebateService.php | 57 ++++++++++++++++++++++------- 2 files changed, 49 insertions(+), 13 deletions(-) diff --git a/app/Constants/SsdbKeysPrefix.php b/app/Constants/SsdbKeysPrefix.php index 158fefe..2cac620 100644 --- a/app/Constants/SsdbKeysPrefix.php +++ b/app/Constants/SsdbKeysPrefix.php @@ -16,4 +16,9 @@ class SsdbKeysPrefix extends AbstractConstants * @Message("Params Token Key Prefix") */ const PARAMS_TOKEN = 'params_token_'; + + /** + * @Message("Coupon rebate Key Prefix") + */ + const COUPON_REBATE_RECEIVE = 'coupon_rebate_receive_'; } diff --git a/app/Service/CouponRebateService.php b/app/Service/CouponRebateService.php index 19d077f..7c06421 100644 --- a/app/Service/CouponRebateService.php +++ b/app/Service/CouponRebateService.php @@ -9,6 +9,9 @@ use App\Model\CouponUserRecType; use App\Model\CouponRec; use App\Constants\LogLabel; use App\Commons\Log; +use Hyperf\Utils\ApplicationContext; +use App\TaskWorker\SSDBTask; +use App\Constants\SsdbKeysPrefix; use Exception; class CouponRebateService implements CouponRebateServiceInterface @@ -40,7 +43,7 @@ class CouponRebateService implements CouponRebateServiceInterface 'status' => 1, 'coupon_text' => '继续努力~' ]; - + // 错误日志记录 $errorData = [ 'coupon_ids' =>$ids, @@ -50,12 +53,17 @@ class CouponRebateService implements CouponRebateServiceInterface 'phone' =>$phone ]; - Db::transaction( function() use ($ids,$receiveType,$userId,$sendUserId,$phone,$now,&$result,&$errorData) { + $receiveSsdb = []; + + try{ + Db::transaction( function() use ($ids,$receiveType,$userId,$sendUserId,$phone,$now,&$result,&$errorData,&$receiveSsdb) { - try{ + $success = []; + $fail = []; + //读写锁,完全控制,性能低 $coupons = Coupon::whereIn('id', $ids)->lockForUpdate() - ->select('id','title','status','inventory','inventory_use','start_time','end_time','full_amount','discounts') + ->select('id','title','status','inventory','inventory_use','start_time','end_time','full_amount','discounts','active_type') ->get(); foreach($coupons as $coupon){ @@ -70,6 +78,8 @@ class CouponRebateService implements CouponRebateServiceInterface $coupon->start_time <= $now && $coupon->end_time >= $now + && + $coupon->active_type == 2 ) { @@ -107,10 +117,12 @@ class CouponRebateService implements CouponRebateServiceInterface $couponReceive->phone = $phone; if ( $couponReceive->save() && $coupon->save() ) { - $result['status'] = 0; - $result['coupon_text'] = '恭喜您领取成功!'; + $success[] = $coupon; + $receiveSsdb[] = $coupon->id;; } }else{ + $fail[] = $coupon; + $receiveSsdb[] = $coupon->id; $result['status'] = 2; $result['coupon_text'] = '您已领取!赶快去下单吧~'; @@ -121,6 +133,7 @@ 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'] = '优惠券剩余数量不足'; @@ -130,6 +143,7 @@ class CouponRebateService implements CouponRebateServiceInterface ); } }else{ + $fail[] = $coupon; $errorData['msg'] = '优惠券已经过期或者被禁用或者已领完'; $this->log->event( LogLabel::COUPON_LOG, @@ -137,16 +151,33 @@ class CouponRebateService implements CouponRebateServiceInterface ); } } - - } catch (Exception $e){ - $errorData['msg'] = $e->getMessage(); + $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){ + // 记录到ssdb + $ssdb = ApplicationContext::getContainer()->get(SSDBTask::class); + if(false === $ssdb->exec('hset',SsdbKeysPrefix::COUPON_REBATE_RECEIVE.$userId,$receiveSsdb)){ + $errorData['msg'] = '记录领取优惠券到ssdb失败'; $this->log->event( LogLabel::COUPON_LOG, $errorData - ); - } - }); - + ); + }; + } return $result; }