From ad712eaabcc9cb974288ebe7721535e79162fb42 Mon Sep 17 00:00:00 2001 From: liangyuyan <1103300295@qq.com> Date: Wed, 5 Aug 2020 17:23:59 +0800 Subject: [PATCH 1/4] =?UTF-8?q?=E4=BC=98=E6=83=A0=E5=88=B8=E8=BF=94?= =?UTF-8?q?=E5=88=B8=E6=B4=BB=E5=8A=A8--=E9=A2=86=E5=8F=96=E6=8E=A5?= =?UTF-8?q?=E5=8F=A3=EF=BC=8C=E4=B8=80=E6=AC=A1=E5=8F=AF=E9=A2=86=E5=8F=96?= =?UTF-8?q?=E5=A4=9A=E5=BC=A0=E4=BC=98=E6=83=A0=E5=88=B8?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/Request/CouponRebateReceiveRequest.php | 6 +- app/Service/CouponRebateService.php | 171 +++++++++++---------- 2 files changed, 97 insertions(+), 80 deletions(-) diff --git a/app/Request/CouponRebateReceiveRequest.php b/app/Request/CouponRebateReceiveRequest.php index 3169c3e..4c4beed 100644 --- a/app/Request/CouponRebateReceiveRequest.php +++ b/app/Request/CouponRebateReceiveRequest.php @@ -24,7 +24,7 @@ class CouponRebateReceiveRequest extends FormRequest return [ 'user_id' => 'required|nonempty|integer|exists_enable:ims_cjdc_user,id', 'receive_type' => 'required|nonempty|integer', - 'id' => 'required|nonempty', + 'ids' => 'required|nonempty', ]; } @@ -33,7 +33,7 @@ class CouponRebateReceiveRequest extends FormRequest return [ 'user_id.*' => ':attribute信息不正确', 'receive_type.*' => ':attribute必须', - 'id.*' => ':attribute必须', + 'ids.*' => ':attribute必须', ]; } @@ -42,7 +42,7 @@ class CouponRebateReceiveRequest extends FormRequest return [ 'user_id' => '领取用户ID', 'receive_type' => '领取方式', - 'id' => '优惠券', + 'ids' => '优惠券', ]; } } diff --git a/app/Service/CouponRebateService.php b/app/Service/CouponRebateService.php index cdd384c..e0f9007 100644 --- a/app/Service/CouponRebateService.php +++ b/app/Service/CouponRebateService.php @@ -21,113 +21,130 @@ class CouponRebateService implements CouponRebateServiceInterface /** * 领取优惠券 + * 返券活动领取 + * 一次可领取多张优惠券 + * 一个用户不可重复领取 */ public function userReceiveCoupon($params) { $userId = $params["user_id"]; $receiveType = $params["receive_type"]; - $id = $params["id"]; + $ids = $params["ids"]; $sendUserId = $params["send_user_id"]; $phone = $params["phone"]; $now = time(); + $ids = is_array($ids) ? implode(',',$ids) : $ids; - // status : 0 领取成功 >0 领取失败 + // status: 0领取成功 >0领取失败 $result = [ 'status' => 1, - 'data' => [], 'coupon_text' => '继续努力~' ]; // 错误日志记录 $errorData = [ - 'coupon_id' =>$id, + 'coupon_ids' =>$ids, 'user_id' =>$userId, 'receiveType' =>$receiveType, 'sendUserId' =>$sendUserId, 'phone' =>$phone ]; - Db::transaction( function() use ($id,$receiveType,$userId,$sendUserId,$phone,$now,&$result,&$errorData) { + Db::transaction( function() use ($ids,$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'); + try{ + //读写锁,完全控制,性能低 + $coupons = Coupon::whereIn('id', $ids)->lockForUpdate() + ->select('id','title','status','inventory','inventory_use','start_time','end_time','full_amount','discounts') + ->get(); - 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'] = '红包已经领完~'; + foreach($coupons as $coupon){ + $errorData['coupon_id'] = $coupon->id; - $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 ( + $coupon->status == 1 + && + $coupon->inventory > $coupon->inventory_use + && + $coupon->start_time <= $now + && + $coupon->end_time >= $now + ) + { - if(!$isReceive){ + // 查询一次能领取的数量 + $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){ - $cp->inventory_use += $cr->number;//记录已领取的数量 - - if ( $cr->save() && $cp->save() ) { - $result['status'] = 0; - // $result['data'] = $cp; - $result['coupon_text'] = $cp['discounts'].'元红包'; + // 判断是否领取过 存在记录则领取过 + $isReceive = CouponRec::select('id') + ->where('system_coupon_user_id',$ids) + ->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() ) { + $result['status'] = 0; + $result['coupon_text'] = '恭喜您领取成功!'; + } + }else{ + $result['status'] = 2; + $result['coupon_text'] = '您已领取!赶快去下单吧~'; + + $errorData['msg'] = '用户已经领取了优惠券'; + $this->log->event( + LogLabel::COUPON_LOG, + $errorData + ); + } + }else{ + $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{ + $errorData['msg'] = '优惠券已经过期或者被禁用或者已领完'; + $this->log->event( + LogLabel::COUPON_LOG, + $errorData + ); } - }else{ - $errorData['msg'] = '用户已经领取了此优惠券'; - $this->log->event( - LogLabel::COUPON_LOG, - $errorData - ); } + + } catch (Exception $e){ + $errorData['msg'] = $e->getMessage(); + $this->log->event( + LogLabel::COUPON_LOG, + $errorData + ); } - } catch (Exception $e){ - $errorData['msg'] = $e->getMessage(); - $this->log->event( - LogLabel::COUPON_LOG, - $errorData - ); - } }); return $result; 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 2/4] =?UTF-8?q?=E4=BC=98=E6=83=A0=E5=88=B8=E8=BF=94?= =?UTF-8?q?=E5=88=B8=E6=B4=BB=E5=8A=A8-=E9=A2=86=E5=8F=96=E6=88=90?= =?UTF-8?q?=E5=8A=9F=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; } 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 3/4] =?UTF-8?q?=E4=BC=98=E6=83=A0=E5=88=B8=E8=BF=94?= =?UTF-8?q?=E5=88=B8=E6=B4=BB=E5=8A=A8--=E4=BF=AE=E6=94=B9=E9=A2=86?= =?UTF-8?q?=E5=8F=96=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, From e0ed0eca9657d3529ca8728301fc191bd568a26a Mon Sep 17 00:00:00 2001 From: liangyuyan <1103300295@qq.com> Date: Thu, 6 Aug 2020 15:07:47 +0800 Subject: [PATCH 4/4] =?UTF-8?q?=E4=BC=98=E6=83=A0=E5=88=B8=E8=BF=94?= =?UTF-8?q?=E5=88=B8=E6=B4=BB=E5=8A=A8--=E4=BF=AE=E6=94=B9=E5=8F=AF?= =?UTF-8?q?=E9=A2=86=E5=8F=96=E4=BC=98=E6=83=A0=E5=88=B8=E6=8E=A5=E5=8F=A3?= =?UTF-8?q?getSystemCouponUserList=EF=BC=8C=E5=A2=9E=E5=8A=A0=E4=B8=80?= =?UTF-8?q?=E4=B8=AA=E7=BB=91=E5=AE=9A=E4=BC=98=E6=83=A0=E5=88=B8=E6=B4=BB?= =?UTF-8?q?=E5=8A=A8=E6=8E=A5=E5=8F=A3?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/Constants/SsdbKeysPrefix.php | 10 ++ app/Controller/CouponController.php | 79 ++--------- app/Controller/CouponRebateController.php | 15 +- app/Request/CouponGetListRequest.php | 45 ++++++ app/Request/CouponRebateTieRequest.php | 48 +++++++ app/Service/CouponRebateService.php | 54 ++++++- app/Service/CouponRebateServiceInterface.php | 2 + app/Service/CouponService.php | 141 +++++++++++++++++++ app/Service/CouponServiceInterface.php | 18 +++ config/routes.php | 1 + 10 files changed, 345 insertions(+), 68 deletions(-) create mode 100644 app/Request/CouponGetListRequest.php create mode 100644 app/Request/CouponRebateTieRequest.php create mode 100644 app/Service/CouponService.php create mode 100644 app/Service/CouponServiceInterface.php diff --git a/app/Constants/SsdbKeysPrefix.php b/app/Constants/SsdbKeysPrefix.php index 2cac620..2550f84 100644 --- a/app/Constants/SsdbKeysPrefix.php +++ b/app/Constants/SsdbKeysPrefix.php @@ -21,4 +21,14 @@ class SsdbKeysPrefix extends AbstractConstants * @Message("Coupon rebate Key Prefix") */ const COUPON_REBATE_RECEIVE = 'coupon_rebate_receive_'; + + /** + * @Message("Coupon rebate Key Prefix") + */ + const COUPON_REBATE_FORWARD = 'coupon_rebate_forward_'; + + /** + * @Message("Coupon rebate Key Prefix") + */ + const COUPON_REBATE_REPAY = 'coupon_rebate_REPAY_'; } diff --git a/app/Controller/CouponController.php b/app/Controller/CouponController.php index 5152e63..6212782 100644 --- a/app/Controller/CouponController.php +++ b/app/Controller/CouponController.php @@ -12,86 +12,33 @@ declare(strict_types=1); namespace App\Controller; +use Hyperf\Di\Annotation\Inject; use App\Model\CouponUserRecType; use App\Model\Coupon; use App\Model\CouponRec; use Hyperf\DbConnection\Db; use Hyperf\Redis\Redis; use Hyperf\Utils\ApplicationContext; +use App\Request\CouponGetListRequest; +use App\Service\CouponService; class CouponController extends BaseController { + /** + * @Inject + * @var CouponService + */ + protected $couponService; /** * 获取用户可领取优惠卷接口 */ - public function getSystemCouponUserList() + public function getSystemCouponUserList(CouponGetListRequest $validator) { - $user_id = $this->request->input('user_id', 0); - $receive_type = $this->request->input('receive_type', 0); - - if ($this->empty($user_id) || $this->empty($receive_type)) { - return $this->success(['not_reveive' => []]); - } - - $c_ids = []; - // 渠道开启,查询该渠道可以领取的优惠券ID - // 渠道未开启,查询所有优惠券 - if (env('SUB_CHANNEL') == 1) { - $c_ids = CouponUserRecType::where('receive_type', $receive_type)->pluck('system_coupon_user_id'); - } else { - $c_ids = Coupon::pluck('id'); - } - - $nowTime = time(); - - $where = [ - ['user_id',"=",$user_id] - ]; - - // 渠道开启,查询该用户在此渠道领过的优惠券ID - if (env('SUB_CHANNEL') == 1) { - array_push($where, ['receive_type', "=", $receive_type]); - } - - $cr_ids = CouponRec::where($where)->pluck('system_coupon_user_id'); - - //领过券的ID - $c_ids = $c_ids->toArray(); - $cr_ids = $cr_ids->toArray(); - - // 当前用户可领的优惠券ID - $couponIds = array_diff($c_ids, $cr_ids); - - $whereC = [ - ['u.end_time','>',$nowTime], - ['u.start_time','<=',$nowTime], - ['u.status','=',1], - ['u.active_type','=',1], - ]; - - if (env('SUB_CHANNEL') == 1) { - array_push($whereC, ['type.receive_type','=',$receive_type]); - } - - $c = Db::table('ims_system_coupon_user as u') - ->where($whereC) - ->join('ims_system_coupon_user_receivetype as type', 'u.id', '=', 'type.system_coupon_user_id') - ->whereRaw('u.inventory_use <= u.inventory and u.inventory-u.inventory_use > type.one_receive_number') - // ->whereIn('u.id',$c_ids) - // ->whereNotIn('u.id',$cr_ids) - ->whereIn('u.id', $couponIds) - ->select('u.*','type.one_receive_number') - ->orderBy('u.weigh','desc') - // ->orderByRaw('FIELD(u.id, '.implode(", " , $ids).')') - ->limit(4) - ->get(); - foreach ($c as $k => &$v){ - if($v->discount_type == 2){ - $v->discounts = floatval($v->discounts); - } - } - return $this->success(['not_reveive'=>$c]); + $userId = $this->request->input('user_id', 0); + $receiveType = $this->request->input('receive_type', 0); + $res = $this->couponService->getSystemCouponUserList($userId,$receiveType); + return $this->success($res); } //统计用户 diff --git a/app/Controller/CouponRebateController.php b/app/Controller/CouponRebateController.php index c1b2b93..41b8fdb 100644 --- a/app/Controller/CouponRebateController.php +++ b/app/Controller/CouponRebateController.php @@ -22,6 +22,7 @@ use App\Model\CouponUserRecType; use App\Model\CouponRec; use App\Request\CouponRebateReceiveRequest; use App\Service\CouponRebateService; +use App\Request\CouponRebateTieRequest; class CouponRebateController extends BaseController { @@ -53,10 +54,22 @@ class CouponRebateController extends BaseController /** * 用户领取优惠券 - * 2020.08.04 只领一张优惠券 */ public function userReceiveCoupon(CouponRebateReceiveRequest $validator) { return $this->success($this->CouponRebateService->userReceiveCoupon($this->request->all())); } + + /** + * 将优惠券绑定活动 + */ + public function tieCouponActive(CouponRebateTieRequest $validator) + { + $couponForward = $this->request->input('coupon_forward_ids',[]); + $couponForward = is_array($couponForward) ? $couponForward : explode(',',$couponForward) ; + $couponRepay = $this->request->input('coupon_repay_id',0); + $couponActivity = $this->request->input('coupon_activity',0); + $res = $this->CouponRebateService->tieCouponActive($couponActivity,$couponForward,$couponRepay); + return $this->success($res); + } } diff --git a/app/Request/CouponGetListRequest.php b/app/Request/CouponGetListRequest.php new file mode 100644 index 0000000..252a45c --- /dev/null +++ b/app/Request/CouponGetListRequest.php @@ -0,0 +1,45 @@ + 'required|nonempty|integer|exists_enable:ims_cjdc_user,id', + 'receive_type' => 'required|nonempty|integer' + ]; + } + + public function messages(): array + { + return [ + 'user_id.*' => ':attribute信息不正确', + 'receive_type.*' => ':attribute必须' + ]; + } + + public function attributes(): array + { + return [ + 'user_id' => '用户ID', + 'receive_type' => '领取方式' + ]; + } +} diff --git a/app/Request/CouponRebateTieRequest.php b/app/Request/CouponRebateTieRequest.php new file mode 100644 index 0000000..39a5863 --- /dev/null +++ b/app/Request/CouponRebateTieRequest.php @@ -0,0 +1,48 @@ + 'required|nonempty', + 'coupon_forward_ids' => 'required|nonempty', + 'coupon_repay_id' => 'required|nonempty', + ]; + } + + public function messages(): array + { + return [ + 'coupon_activity.*' => ':attribute必须', + 'coupon_forward_ids.*' => ':attribute必须', + 'coupon_repay_id.*' => ':attribute必须', + ]; + } + + public function attributes(): array + { + return [ + 'coupon_activity' => '优惠券活动', + 'coupon_forward_ids' => '领取类型优惠券', + 'coupon_repay_id' => '返还类型优惠券', + ]; + } +} diff --git a/app/Service/CouponRebateService.php b/app/Service/CouponRebateService.php index 6d488c1..c7dad5b 100644 --- a/app/Service/CouponRebateService.php +++ b/app/Service/CouponRebateService.php @@ -176,7 +176,7 @@ class CouponRebateService implements CouponRebateServiceInterface } // 记录到ssdb $ssdb = ApplicationContext::getContainer()->get(SSDBTask::class); - if(false === $ssdb->exec('hset',SsdbKeysPrefix::COUPON_REBATE_RECEIVE.$userId,$saveSsdb)){ + if(false === $ssdb->exec('multi_hset',SsdbKeysPrefix::COUPON_REBATE_RECEIVE.$userId,$saveSsdb)){ $errorData['msg'] = '记录领取优惠券到ssdb失败'; $this->log->event( LogLabel::COUPON_LOG, @@ -216,5 +216,57 @@ class CouponRebateService implements CouponRebateServiceInterface ->get(); return $res; } + + /** + * 将优惠券绑定活动 + * 领取优惠券 COUPON_REBATE_FORWARD 可多张 + * 返还优惠券 COUPON_REBATE_REPAY 只一张 + */ + public function tieCouponActive($couponActivity,$couponForward,$couponRepay) + { + $result = [ + 'forward' => true, + 'repay' => true, + ]; + + $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]; + // 记录领取类型优惠券 + $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('set',SsdbKeysPrefix::COUPON_REBATE_REPAY.$couponActivity,$couponRepay)){ + + $this->log->event( + LogLabel::COUPON_LOG, + [ + 'coupon_activity' => $couponActivity, + 'coupon_Repay' => $couponRepay, + 'msg' => '绑定-返还-优惠券到ssdb失败' + ] + ); + $result['repay'] = false; + }; + return $result; + } } \ No newline at end of file diff --git a/app/Service/CouponRebateServiceInterface.php b/app/Service/CouponRebateServiceInterface.php index cc8c0ef..cc8fd86 100644 --- a/app/Service/CouponRebateServiceInterface.php +++ b/app/Service/CouponRebateServiceInterface.php @@ -13,4 +13,6 @@ interface CouponRebateServiceInterface public function getActiveInfo(); + public function tieCouponActive($couponActivity,$couponForward,$couponRepay); + } \ No newline at end of file diff --git a/app/Service/CouponService.php b/app/Service/CouponService.php new file mode 100644 index 0000000..d980954 --- /dev/null +++ b/app/Service/CouponService.php @@ -0,0 +1,141 @@ + 1, + 'not_receive' => [], + 'jump_data' => [ + 'src' => "/zh_cjdianc/pages/couponrebate/index?activity_type=".$activityType, + 'src' => "/zh_cjdianc/pages/couponrebate/index?activity_type=".$activityType, + 'coupons' => [] + ] + ]; + $nowTime = time(); + $c_ids = []; +$result['active_type'] = 1; + // 渠道开启,查询该渠道可以领取的优惠券ID + // 渠道未开启,查询所有优惠券 + if (env('SUB_CHANNEL') == 1) { + $c_ids = CouponUserRecType::where('receive_type', $receiveType)->pluck('system_coupon_user_id'); + } else { + $c_ids = Coupon::pluck('id'); + } + + $couponReceive = CouponRec::where('user_id',$userId); + + // 渠道开启,查询该用户在此渠道领过的优惠券ID + if (env('SUB_CHANNEL') == 1) { + $couponReceive->where('receive_type', $receiveType); + } + $cr_ids = $couponReceive->pluck('system_coupon_user_id'); + + // 可领取的券ID + $c_ids = $c_ids->toArray(); + // 已经领取的券ID + $cr_ids = $cr_ids->toArray(); + + // 当前用户可领的优惠券ID + $couponIds = array_diff($c_ids, $cr_ids); + + // 获取领取型优惠券 + $ssdb = ApplicationContext::getContainer()->get(SSDBTask::class); + $couponRebateIds = $ssdb->exec('hgetall',SsdbKeysPrefix::COUPON_REBATE_FORWARD.$activityType); + // $result['active_type'] = $couponRebateIds; + // return $result; + $couponIds = ($couponRebateIds === false || empty($couponRebateIds)) ? $couponIds : array_merge($couponIds,$couponRebateIds) ; + + $whereC = [ + ['u.end_time','>',$nowTime], + ['u.start_time','<=',$nowTime], + ['u.status','=',1] + ]; + + if (env('SUB_CHANNEL') == 1) { + array_push($whereC, ['type.receive_type','=', $receiveType]); + } + + $coupons = Db::table('ims_system_coupon_user as u') + ->join('ims_system_coupon_user_receivetype as type', 'u.id', '=', 'type.system_coupon_user_id') + ->whereIn('u.id', $couponIds) + ->where($whereC) + ->whereRaw('u.inventory_use < u.inventory and u.inventory-u.inventory_use >= type.one_receive_number') + ->select('u.*','type.one_receive_number') + ->orderBy('u.weigh','desc') + ->limit(4) + ->get(); + + foreach ($coupons as $k => &$v){ + if($v['active_type'] == 1){ + $result['not_receive'] = $v; + }else if($v['active_type'] == 2){ + $result['jump_data']['coupons'][] = $v; + } + if($v->discount_type == 2){ + $v->discounts = floatval($v->discounts); + } + } + + $result['active_type'] = count($result['jump_data']['coupons']) > 0 ? 2 : $result['active_type'] ; + + return $result; + } + + //统计用户 + public function userCouponAccount() + { + + } + + /** + * 用户领取优惠卷 + */ + public function userReceiveCoupon() + { + + } + + /** + * 获取用户已经领取的优惠卷列表 + */ + public function getUserReceiveCouponList() + { + + } + + /** + * 获取用户当前订单可用的优惠券列表 + * 按分类(1订单 等优惠)分组返回 + */ + public function getUserAvailableCoupons() + { + + } + +} \ No newline at end of file diff --git a/app/Service/CouponServiceInterface.php b/app/Service/CouponServiceInterface.php new file mode 100644 index 0000000..b096ecd --- /dev/null +++ b/app/Service/CouponServiceInterface.php @@ -0,0 +1,18 @@ +