From 6a441bca74a0a40ae524c545cde12779c3858b2c Mon Sep 17 00:00:00 2001 From: denghy Date: Fri, 10 May 2019 16:56:18 +0800 Subject: [PATCH] =?UTF-8?q?=E6=9F=A5=E8=AF=A2=E4=BC=98=E5=8C=96?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/Domains/Virtual/Services/CardService.php | 35 +++++++++++--------- 1 file changed, 19 insertions(+), 16 deletions(-) diff --git a/app/Domains/Virtual/Services/CardService.php b/app/Domains/Virtual/Services/CardService.php index 30f59491..f1b0d514 100644 --- a/app/Domains/Virtual/Services/CardService.php +++ b/app/Domains/Virtual/Services/CardService.php @@ -63,43 +63,46 @@ class CardService extends Service $limit = $conditions['limit'] ?? 20; $conditions['type'] = [0, 1, 2]; - $query = $this->orderCardPartitionRepository->selectRaw('DISTINCT(sim) sim, created_at')->withConditions($conditions)->applyConditions(); + $query = $this->orderCardPartitionRepository->selectRaw('sim')->withConditions($conditions); switch ($conditions['card_status']) { + case 0: - $res = $query->whereNull('service_start_at')->whereHas('card', function ($relation) { + $query = $query->whereNull('service_start_at')->whereHas('card', function ($relation) { $relation->whereNull('cancelled_at'); - })->where('created_at', '<', Carbon::now()->subMonths(6))->paginate($limit); + })->where('created_at', '<', Carbon::now()->subMonths(6)); break; case 1: - $res = $query->whereNull('service_start_at')->whereHas('card', function ($relation) { + $query = $query->whereNull('service_start_at')->whereHas('card', function ($relation) { $relation->whereNull('cancelled_at'); - })->where('created_at', '>=', Carbon::now()->subMonths(6))->paginate($limit); + })->where('created_at', '>=', Carbon::now()->subMonths(6)); break; case 2: - $res = $query->whereNotNull('service_start_at') - ->where('service_start_at', '<=', date('Y-m-d H:i:s')) - ->where('service_end_at', '>=', date('Y-m-d H:i:s')) + $havingRaw = sprintf("MAX(service_end_at) >= '%s'", date('Y-m-d H:i:s')); + + $query = $query->whereNotNull('service_start_at') + ->groupBy('sim')->havingRaw($havingRaw) ->whereHas('card', function ($relation) { $relation->whereNull('cancelled_at'); - })->paginate($limit); + }); break; case 3: - $res = $query->whereNotNull('service_start_at') - ->where('service_start_at', '<=', date('Y-m-d H:i:s')) - ->where('service_end_at', '>=', date('Y-m-d H:i:s')) + $havingRaw = sprintf("MAX(service_end_at) < '%s'", date('Y-m-d H:i:s')); + + $query = $query->whereNotNull('service_start_at') + ->groupBy('sim')->havingRaw($havingRaw) ->whereHas('card', function ($relation) { $relation->whereNull('cancelled_at'); - })->orderBy('created_at', 'desc')->paginate($limit); + }); break; case 4: - $res = $query->whereNotNull('service_start_at')->whereHas('card', function ($relation) { + $query = $query->whereHas('card', function ($relation) { $relation->whereNotNull('cancelled_at'); - })->paginate($limit); + }); break; default: @@ -107,7 +110,7 @@ class CardService extends Service break; } - return $res; + return $query->paginate($limit); } /**