diff --git a/app/Controller/v3/CouponController.php b/app/Controller/v3/CouponController.php index 5b6ad2e..4ba427c 100644 --- a/app/Controller/v3/CouponController.php +++ b/app/Controller/v3/CouponController.php @@ -67,17 +67,9 @@ class CouponController extends BaseController /** * $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; - } + + $res = $this->couponRecService->getListByUser($userId,$type,$page,$pagesize); + return $this->success($res); } diff --git a/app/Service/v3/Implementations/CouponRecService.php b/app/Service/v3/Implementations/CouponRecService.php index 02f9d7d..f639bd4 100644 --- a/app/Service/v3/Implementations/CouponRecService.php +++ b/app/Service/v3/Implementations/CouponRecService.php @@ -78,102 +78,48 @@ class CouponRecService implements CouponRecServiceInterface } /** - * 用户未使用优惠券列表 + * 用户优惠券列表 */ - public function getUnusedListByUser($userId,$page = 1,$pagesize = 5) + public function getListByUser($userId,$type,$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'])."折"; - } + /** + * $type unused 未使用 used 已使用 expired 已失效 + */ + switch ($type){ + case 'unused': + $builder = $builder->where([ + ['lanzu_coupon.usable_end_time' ,'>', time()], + ['lanzu_coupon_receive.number_remain' ,'>', 0] + ]) + ->whereIn('lanzu_coupon_receive.status',[0,1]); + break; + case 'used': + $builder = $builder->whereIn('lanzu_coupon_receive.status',[1,2]); + break; + case 'expired': + $builder = $builder->where(function ($query) { + $query->where('lanzu_coupon.usable_end_time', '<', time()) + ->orWhere('lanzu_coupon.status', '<>', 1); + }); + break; } - 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) - { - //查询失效优惠券 - $builder = CouponRec::query()->join('lanzu_coupon', 'lanzu_coupon.id', '=', 'lanzu_coupon_receive.coupon_id') - ->where('lanzu_coupon_receive.user_id',$userId) - ->where(function ($query) { - $query->where('lanzu_coupon.usable_end_time', '<', time()) - ->orWhere('lanzu_coupon.status', '<>', 1); - }) - ->select( + $builder = $builder->select( 'lanzu_coupon.title', 'lanzu_coupon.discounts', 'lanzu_coupon.full_amount', 'lanzu_coupon.discount_type', 'lanzu_coupon.introduce', + 'lanzu_coupon.usable_start_time', 'lanzu_coupon.usable_end_time', 'lanzu_coupon_receive.number', 'lanzu_coupon_receive.number_remain' - ) - ->orderBy('lanzu_coupon.weigh','desc'); + ) + ->orderBy('lanzu_coupon.weigh','desc'); $paginate = $builder->paginate($pagesize); $couponList = $paginate->toArray(); foreach ($couponList['data'] as $key => &$coupon) { @@ -185,6 +131,8 @@ class CouponRecService implements CouponRecServiceInterface }elseif($coupon['discount_type'] == 2){ $coupon['discounts_text'] = floatval($coupon['discounts'])."折"; } + //拼接时间文字提示 + $coupon['time_text'] = date("Y-m-d H:i:s",$coupon['usable_start_time']). ' - ' .date("Y-m-d H:i:s",$coupon['usable_end_time']); } return ['has_more_pages' => $paginate->hasMorePages(), 'coupon_list' => $couponList['data']]; } diff --git a/app/Service/v3/Interfaces/CouponRecServiceInterface.php b/app/Service/v3/Interfaces/CouponRecServiceInterface.php index c0e5743..34ebfaa 100644 --- a/app/Service/v3/Interfaces/CouponRecServiceInterface.php +++ b/app/Service/v3/Interfaces/CouponRecServiceInterface.php @@ -8,7 +8,5 @@ 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); + public function getListByUser($userId,$type,$page = 1,$pagesize = 5); } \ No newline at end of file