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] =?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=E5=8F=AF=E9=A2=86?= =?UTF-8?q?=E5=8F=96=E4=BC=98=E6=83=A0=E5=88=B8=E6=8E=A5=E5=8F=A3getSystem?= =?UTF-8?q?CouponUserList=EF=BC=8C=E5=A2=9E=E5=8A=A0=E4=B8=80=E4=B8=AA?= =?UTF-8?q?=E7=BB=91=E5=AE=9A=E4=BC=98=E6=83=A0=E5=88=B8=E6=B4=BB=E5=8A=A8?= =?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/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 @@ +