查询优化

This commit is contained in:
邓皓元 2019-05-10 16:53:45 +08:00
parent e709eb5d16
commit babb1fac79

View File

@ -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)->applyConditions();
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);
}
/**