diff --git a/app/Controller/v3/CouponController.php b/app/Controller/v3/CouponController.php index a32542b..2048fbe 100644 --- a/app/Controller/v3/CouponController.php +++ b/app/Controller/v3/CouponController.php @@ -69,8 +69,8 @@ class CouponController extends BaseController * $type unused 未使用 used 已使用 expired已失效 */ - $res = $this->couponRecService->getListByUser($userId,$type,$page,$pagesize); - + $res['coupon_list'] = $this->couponRecService->getListByUser($userId,$type,$page,$pagesize); + $res['statistics'] = $this->couponRecService->statistics($userId); return $this->success($res); } diff --git a/app/Controller/v3/HomeController.php b/app/Controller/v3/HomeController.php index 0cce913..448f41d 100644 --- a/app/Controller/v3/HomeController.php +++ b/app/Controller/v3/HomeController.php @@ -178,7 +178,8 @@ class HomeController extends BaseController } $data['user']['collection_count'] = $this->collectStoreService->countByUser($params['user_id']); - $data['user']['coupon_count'] = $this->couponRecService->countAvailableByUser($params['user_id']); + $couponStatistics = $this->couponRecService->statistics($params['user_id']); + $data['user']['coupon_count'] = $couponStatistics['unused']; $data['user']['role'] = $roles; $data['badge'] = $this->badgeService->allByUserOrder($params['user_id']); diff --git a/app/Service/v3/Implementations/CouponRecService.php b/app/Service/v3/Implementations/CouponRecService.php index 6791074..a23f09b 100644 --- a/app/Service/v3/Implementations/CouponRecService.php +++ b/app/Service/v3/Implementations/CouponRecService.php @@ -155,7 +155,7 @@ class CouponRecService implements CouponRecServiceInterface //拼接时间文字提示 $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']]; + return ['has_more_pages' => $paginate->hasMorePages(), 'list' => $couponList['data']]; } /** @@ -285,12 +285,29 @@ class CouponRecService implements CouponRecServiceInterface } /** - * @inheritDoc + * @param $userId + * @return mixed */ - public function countAvailableByUser($userId) + public function statistics($userId) { - $coupons = $this->getListByUser($userId,'unused',1,9999); - $numberRemain= (array_column($coupons['coupon_list'], 'number_remain')); - return array_sum($numberRemain); + //未使用 + $res['unused'] = 0; + //已使用 + $res['used'] = 0; + //已过期 + $res['expired'] = 0; + $coupons = CouponRec::query() + ->with('coupon') + ->where('user_id',$userId) + ->get(); + foreach ($coupons as $coupon){ + if($coupon->coupon->usable_end_time < time() || $coupon->coupon->status != 1){ + $res['expired'] += $coupon->number_remain; + }else{ + $res['unused'] += $coupon->number_remain; + } + $res['used'] += $coupon->used_num; + } + return $res; } } \ No newline at end of file diff --git a/app/Service/v3/Interfaces/CouponRecServiceInterface.php b/app/Service/v3/Interfaces/CouponRecServiceInterface.php index c6d1b53..b686672 100644 --- a/app/Service/v3/Interfaces/CouponRecServiceInterface.php +++ b/app/Service/v3/Interfaces/CouponRecServiceInterface.php @@ -15,5 +15,5 @@ interface CouponRecServiceInterface * @param $userId * @return mixed */ - public function countAvailableByUser($userId); + public function statistics($userId); } \ No newline at end of file