From 5d9abdeb838ca39b67ba0616ed40d07e26dc67a1 Mon Sep 17 00:00:00 2001 From: Lemon <15040771@qq.com> Date: Sun, 6 Sep 2020 14:55:00 +0800 Subject: [PATCH] =?UTF-8?q?=E8=B4=AD=E7=89=A9=E8=BD=A6=20=E4=BC=98?= =?UTF-8?q?=E6=83=A0=E5=88=B8=E5=88=97=E8=A1=A8?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/Controller/v3/CouponController.php | 85 +++++++++++++++++++ app/Controller/v3/ShopCartController.php | 9 +- app/Model/v3/Coupon.php | 18 ++++ app/Model/v3/CouponRec.php | 9 ++ .../v3/Implementations/CouponRecService.php | 83 ++++++++++++++++++ .../v3/Implementations/OrderListService.php | 2 +- .../Interfaces/CouponRecServiceInterface.php | 3 + config/routes.php | 2 + 8 files changed, 209 insertions(+), 2 deletions(-) create mode 100644 app/Controller/v3/CouponController.php diff --git a/app/Controller/v3/CouponController.php b/app/Controller/v3/CouponController.php new file mode 100644 index 0000000..5b6ad2e --- /dev/null +++ b/app/Controller/v3/CouponController.php @@ -0,0 +1,85 @@ +request->input('user_id'); + $page = $this->request->input('page'); + $pagesize = $this->request->input('pagesize'); + $type = $this->request->input('type','1'); + /** + * $type unused 未使用 used 已使用 expired已失效 + */ + switch ($type){ + case 'unused': + $res = $this->couponRecService->getUnusedListByUser($userId,$page,$pagesize); + break; + case 'used': + $res = $this->couponRecService->getUsedListByUser($userId,$page,$pagesize); + break; + case 'expired': + $res = $this->couponRecService->getExpiredListByUser($userId,$page,$pagesize); + break; + } + + return $this->success($res); + } + +} diff --git a/app/Controller/v3/ShopCartController.php b/app/Controller/v3/ShopCartController.php index 9a6da33..e8d4e64 100644 --- a/app/Controller/v3/ShopCartController.php +++ b/app/Controller/v3/ShopCartController.php @@ -20,7 +20,14 @@ class ShopCartController extends BaseController //获取购物车失效商品信息 $res['store_lists_invalid'] = $this->shopCartService->undo(); //计算购物车价格 - $res['total'] = '820.00'; + $res['total'] = $this->shopCartService->getTotal(); + return $this->success($res); + } + + public function info() + { + $res['shopcart']['count'] = $this->shopCartService->countGoods(); + $res['shopcart']['total'] = $this->shopCartService->getTotal(); return $this->success($res); } } \ No newline at end of file diff --git a/app/Model/v3/Coupon.php b/app/Model/v3/Coupon.php index d798c0d..4d77051 100644 --- a/app/Model/v3/Coupon.php +++ b/app/Model/v3/Coupon.php @@ -11,4 +11,22 @@ class Coupon extends Model protected $table = 'lanzu_coupon'; + protected $appends = [ + 'full_amount_text', + 'discounts_text' + ]; + + public function getFullAmountTextAttribute() + { + return '满'.$this->attributes['full_amount'].'可用'; + } + + public function getDiscountsTextAttribute() + { + if($this->attributes['discount_type'] == 1){ + return '¥'.$this->attributes['discounts']; + }elseif($this->attributes['discount_type'] == 2){ + return floatval($this->attributes['discounts'])."折"; + } + } } diff --git a/app/Model/v3/CouponRec.php b/app/Model/v3/CouponRec.php index a1db5fb..d0679ee 100644 --- a/app/Model/v3/CouponRec.php +++ b/app/Model/v3/CouponRec.php @@ -9,6 +9,15 @@ class CouponRec extends Model protected $table = 'lanzu_coupon_receive'; + protected $appends = [ + 'used_num' + ]; + + public function getUsedNumAttribute() + { + return ($this->attributes['number'] - $this->attributes['number_remain']); + } + public function coupon() { return $this->hasOne(Coupon::class,'id','coupon_id'); diff --git a/app/Service/v3/Implementations/CouponRecService.php b/app/Service/v3/Implementations/CouponRecService.php index 8953662..6f4c2d9 100644 --- a/app/Service/v3/Implementations/CouponRecService.php +++ b/app/Service/v3/Implementations/CouponRecService.php @@ -76,4 +76,87 @@ class CouponRecService implements CouponRecServiceInterface ->get() ->toArray(); } + + /** + * 用户未使用优惠券列表 + */ + public function getUnusedListByUser($userId,$page = 1,$pagesize = 5) + { + //查询可用优惠券 + $builder = CouponRec::query()->join('lanzu_coupon', 'lanzu_coupon.id', '=', 'lanzu_coupon_receive.coupon_id') + ->where([ + ['lanzu_coupon_receive.user_id' ,'=', $userId], + ['lanzu_coupon.usable_end_time' ,'>', time()], + ['lanzu_coupon_receive.number_remain' ,'>', 0] + ]); + $builder = $builder->whereIn('lanzu_coupon_receive.status',[0,1]) + ->select( + 'lanzu_coupon.title', + 'lanzu_coupon.discounts', + 'lanzu_coupon.full_amount', + 'lanzu_coupon.discount_type', + 'lanzu_coupon.introduce', + 'lanzu_coupon.usable_end_time', + 'lanzu_coupon_receive.number', + 'lanzu_coupon_receive.number_remain' + ) + ->orderBy('lanzu_coupon.weigh','desc'); + $paginate = $builder->paginate($pagesize); + $couponList = $paginate->toArray(); + foreach ($couponList['data'] as $key => &$coupon) { + //拼接满减文字提示 + $coupon['full_amount_text'] = '满' . $coupon['full_amount'] . "可用"; + //判断是折扣优惠券还是满减优惠券 + if($coupon['discount_type'] == 1){ + $coupon['discounts_text'] = '¥'.$coupon['discounts']; + }elseif($coupon['discount_type'] == 2){ + $coupon['discounts_text'] = floatval($coupon['discounts'])."折"; + } + } + return ['has_more_pages' => $paginate->hasMorePages(), 'coupon_list' => $couponList['data']]; + } + + /** + * 用户已使用 + */ + public function getUsedListByUser($userId,$page = 1,$pagesize = 5) + { + //查询可用优惠券 + $builder = CouponRec::query()->join('lanzu_coupon', 'lanzu_coupon.id', '=', 'lanzu_coupon_receive.coupon_id') + ->where('lanzu_coupon_receive.user_id' ,$userId) + ->whereIn('lanzu_coupon_receive.status',[1,2]) + ->select( + 'lanzu_coupon.title', + 'lanzu_coupon.discounts', + 'lanzu_coupon.full_amount', + 'lanzu_coupon.discount_type', + 'lanzu_coupon.introduce', + 'lanzu_coupon.usable_end_time', + 'lanzu_coupon_receive.number', + 'lanzu_coupon_receive.number_remain' + ) + ->orderBy('lanzu_coupon_receive.updated_at','desc'); + $paginate = $builder->paginate($pagesize); + $couponList = $paginate->toArray(); + foreach ($couponList['data'] as $key => &$coupon) { + //拼接满减文字提示 + $coupon['full_amount_text'] = '满' . $coupon['full_amount'] . "可用"; + //判断是折扣优惠券还是满减优惠券 + if($coupon['discount_type'] == 1){ + $coupon['discounts_text'] = '¥'.$coupon['discounts']; + }elseif($coupon['discount_type'] == 2){ + $coupon['discounts_text'] = floatval($coupon['discounts'])."折"; + } + } + return ['has_more_pages' => $paginate->hasMorePages(), 'coupon_list' => $couponList['data']]; + } + + /** + * 用户已失效 + */ + public function getExpiredListByUser($userId,$page = 1,$pagesize = 5) + { + + } + } \ No newline at end of file diff --git a/app/Service/v3/Implementations/OrderListService.php b/app/Service/v3/Implementations/OrderListService.php index 16713a3..7bc5b17 100644 --- a/app/Service/v3/Implementations/OrderListService.php +++ b/app/Service/v3/Implementations/OrderListService.php @@ -135,7 +135,7 @@ class OrderListService implements OrderListServiceInterface break; } - if(!is_null($start_time) && !is_null($end_time)){ + if(!empty($start_time) && !empty($end_time)){ $builder->whereBetween('lanzu_order_main.created_at',[strtotime($start_time.' 23:59:59'),strtotime($end_time.' 23:59:59')]); } diff --git a/app/Service/v3/Interfaces/CouponRecServiceInterface.php b/app/Service/v3/Interfaces/CouponRecServiceInterface.php index 1931874..c0e5743 100644 --- a/app/Service/v3/Interfaces/CouponRecServiceInterface.php +++ b/app/Service/v3/Interfaces/CouponRecServiceInterface.php @@ -8,4 +8,7 @@ interface CouponRecServiceInterface public function check(); public function undo(); public function allForOrderOlAvailable($totalAmount,$userId,$marketId,$type,$storeTypeId); + public function getUnusedListByUser($userId,$page = 1,$pagesize = 5); + public function getUsedListByUser($userId,$page = 1,$pagesize = 5); + public function getExpiredListByUser($userId,$page = 1,$pagesize = 5); } \ No newline at end of file diff --git a/config/routes.php b/config/routes.php index 89dca29..89bb6c1 100644 --- a/config/routes.php +++ b/config/routes.php @@ -128,4 +128,6 @@ Router::addGroup('/v3/', function () { Router::post('userAddress/setDefault', 'App\Controller\v3\UserAddressController@setDefault'); Router::post('userAddress/get', 'App\Controller\v3\UserAddressController@get'); Router::post('userAddress/delete', 'App\Controller\v3\UserAddressController@delete'); + Router::post('coupon/getListByUser', 'App\Controller\v3\CouponController@getListByUser'); + Router::post('shopCart/info', 'App\Controller\v3\ShopCartController@info'); },['middleware' => [\App\Middleware\Auth\ApiMiddleware::class, \App\Middleware\Auth\UserMiddleware::class]]); \ No newline at end of file