From 0c9b3f76453a1cf5f97d3116f854f16cdb707827 Mon Sep 17 00:00:00 2001 From: liangyuyan <1103300295@qq.com> Date: Wed, 5 Aug 2020 14:20:24 +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--=E5=B0=81=E7=AC=AC=E4=B8=80=E7=89=88?= =?UTF-8?q?=EF=BC=88=E9=A2=86=E5=8F=96=E5=8F=AA=E9=A2=86=E4=B8=80=E5=BC=A0?= =?UTF-8?q?=E5=88=B8=EF=BC=89?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/Constants/LogLabel.php | 2 + app/Controller/CouponRebateController.php | 79 ++--------- app/Service/CouponRebateService.php | 135 +++++++++++++++++++ app/Service/CouponRebateServiceInterface.php | 11 ++ 4 files changed, 157 insertions(+), 70 deletions(-) create mode 100644 app/Service/CouponRebateService.php create mode 100644 app/Service/CouponRebateServiceInterface.php diff --git a/app/Constants/LogLabel.php b/app/Constants/LogLabel.php index 2265cf4..c09c8df 100644 --- a/app/Constants/LogLabel.php +++ b/app/Constants/LogLabel.php @@ -16,4 +16,6 @@ class LogLabel extends AbstractConstants * @Message("Ssdb Log Label") */ const SSDB_LOG = 'ssdb_log'; + + const COUPON_LOG = 'coupon_log'; } diff --git a/app/Controller/CouponRebateController.php b/app/Controller/CouponRebateController.php index 1c856f3..51f8b78 100644 --- a/app/Controller/CouponRebateController.php +++ b/app/Controller/CouponRebateController.php @@ -21,6 +21,7 @@ use App\Model\Coupon; use App\Model\CouponUserRecType; use App\Model\CouponRec; use App\Request\CouponRebateReceiveRequest; +use App\Service\CouponRebateService; class CouponRebateController extends BaseController { /** @@ -28,6 +29,13 @@ class CouponRebateController extends BaseController * @var CouponRebateInterface */ protected $CouponRebate; + + /** + * @Inject + * @var CouponRebateService + */ + protected $CouponRebateService; + /** * 用户是否领取过领取优惠券 */ @@ -54,75 +62,6 @@ class CouponRebateController extends BaseController */ public function userReceiveCoupon(CouponRebateReceiveRequest $validator) { - $userId = $this->request->input("user_id", 0); - $receiveType = $this->request->input("receive_type", 0); - $id = $this->request->input("id", 0); - $sendUserId = $this->request->input("send_user_id", 0); - $phone = $this->request->input("phone", ''); - $now = time(); - - // status : 0 领取成功 >0 领取失败 - $return = [ - 'status' => 1, - 'data' => [], - 'coupon_text' => '继续努力~' - ]; - - Db::transaction( function () use ($id,$receiveType,$userId,$sendUserId,$phone,&$return,$now) { - //读写锁,完全控制,性能低 - $cp = Coupon::where('id', $id)->lockForUpdate() - ->select('id','title','status','inventory','inventory_use','start_time','end_time','full_amount','discounts') - ->first(); - - $where = [ - 'system_coupon_user_id' => $cp->id, - ]; - - if (env('SUB_CHANNEL') == 1) { - $where['receive_type'] = $receiveType; - } - // 查询领取类型一次能领取的数量 - $crt = CouponUserRecType::where($where)->first(); - - $cr = new CouponRec; - $cr->user_id = $userId; - $cr->system_coupon_user_id = $cp->id; - $cr->order_main_id = 0; - $cr->receive_time = $now; - $cr->number = $crt->one_receive_number; - $cr->number_remain = $crt->one_receive_number; - $cr->status = 0; - $cr->update_time = $now; - $cr->receive_type = $receiveType; - $cr->send_user_id = $sendUserId; - $cr->phone = $phone; - - //如果优惠卷库存小于等于已领取的数量 或者 未在活动时间内, 则返回领取失败的优惠券 - if ( - $cp->status != 1 - || - $cp->inventory <= $cp->inventory_use - || - $cp->inventory < ($cp->inventory_use+$cr->number) - || - $cp->start_time > $now - || - $cp->end_time < $now - ) - { - $return['status'] = 2; - $return['coupon_text'] = '红包已经领完~'; - }else{ - $cp->inventory_use += $cr->number;//记录已领取的数量 - if ( $cr->save() && $cp->save() ) { - $return['status'] = 0; - $return['data'] = $cp; - $return['coupon_text'] = $cp['discounts'].'元红包'; - } - } - - }); - - return $this->success($return); + return $this->success($this->CouponRebateService->userReceiveCoupon($this->request->all())); } } diff --git a/app/Service/CouponRebateService.php b/app/Service/CouponRebateService.php new file mode 100644 index 0000000..cdd384c --- /dev/null +++ b/app/Service/CouponRebateService.php @@ -0,0 +1,135 @@ +0 领取失败 + $result = [ + 'status' => 1, + 'data' => [], + 'coupon_text' => '继续努力~' + ]; + + // 错误日志记录 + $errorData = [ + 'coupon_id' =>$id, + 'user_id' =>$userId, + 'receiveType' =>$receiveType, + 'sendUserId' =>$sendUserId, + 'phone' =>$phone + ]; + + Db::transaction( function() use ($id,$receiveType,$userId,$sendUserId,$phone,$now,&$result,&$errorData) { + + try{ + //读写锁,完全控制,性能低 + $cp = Coupon::where('id', $id)->lockForUpdate() + ->select('id','title','status','inventory','inventory_use','start_time','end_time','full_amount','discounts') + ->first(); + + $couponReceiveType = CouponUserRecType::where('system_coupon_user_id',$cp->id)->select('one_receive_number'); + + if (env('SUB_CHANNEL') == 1) { + $couponReceiveType->where('receive_type',$receiveType); + } + // 查询一次能领取的数量 + $crt = $couponReceiveType->first(); + + $cr = new CouponRec; + $cr->user_id = $userId; + $cr->system_coupon_user_id = $cp->id; + $cr->order_main_id = 0; + $cr->receive_time = $now; + $cr->number = $crt->one_receive_number; + $cr->number_remain = $crt->one_receive_number; + $cr->status = 0; + $cr->update_time = $now; + $cr->receive_type = $receiveType; + $cr->send_user_id = $sendUserId; + $cr->phone = $phone; + + //如果优惠卷库存小于等于已领取的数量 或者 未在活动时间内, 则返回领取失败的优惠券 + if ( + $cp->status != 1 + || + $cp->inventory <= $cp->inventory_use + || + $cp->inventory < ($cp->inventory_use+$cr->number) + || + $cp->start_time > $now + || + $cp->end_time < $now + ) + { + $result['status'] = 2; + $result['coupon_text'] = '红包已经领完~'; + + $errorData['msg'] = '优惠券已经过期或者被禁用或者已领完'; + $this->log->event( + LogLabel::COUPON_LOG, + $errorData + ); + }else{ + // 判断是否领取过 存在记录则领取过 + $isReceive = CouponRec::select('id') + ->where('system_coupon_user_id',$id) + ->where('user_id',$userId) + ->exists(); + + if(!$isReceive){ + + $cp->inventory_use += $cr->number;//记录已领取的数量 + + if ( $cr->save() && $cp->save() ) { + $result['status'] = 0; + // $result['data'] = $cp; + $result['coupon_text'] = $cp['discounts'].'元红包'; + } + }else{ + $errorData['msg'] = '用户已经领取了此优惠券'; + $this->log->event( + LogLabel::COUPON_LOG, + $errorData + ); + } + } + } catch (Exception $e){ + $errorData['msg'] = $e->getMessage(); + $this->log->event( + LogLabel::COUPON_LOG, + $errorData + ); + } + }); + + return $result; + } +} \ No newline at end of file diff --git a/app/Service/CouponRebateServiceInterface.php b/app/Service/CouponRebateServiceInterface.php new file mode 100644 index 0000000..1ba93d0 --- /dev/null +++ b/app/Service/CouponRebateServiceInterface.php @@ -0,0 +1,11 @@ +