查询优化
This commit is contained in:
parent
e709eb5d16
commit
babb1fac79
@ -63,43 +63,46 @@ class CardService extends Service
|
|||||||
$limit = $conditions['limit'] ?? 20;
|
$limit = $conditions['limit'] ?? 20;
|
||||||
$conditions['type'] = [0, 1, 2];
|
$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']) {
|
switch ($conditions['card_status']) {
|
||||||
|
|
||||||
case 0:
|
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');
|
$relation->whereNull('cancelled_at');
|
||||||
})->where('created_at', '<', Carbon::now()->subMonths(6))->paginate($limit);
|
})->where('created_at', '<', Carbon::now()->subMonths(6));
|
||||||
break;
|
break;
|
||||||
|
|
||||||
case 1:
|
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');
|
$relation->whereNull('cancelled_at');
|
||||||
})->where('created_at', '>=', Carbon::now()->subMonths(6))->paginate($limit);
|
})->where('created_at', '>=', Carbon::now()->subMonths(6));
|
||||||
break;
|
break;
|
||||||
|
|
||||||
case 2:
|
case 2:
|
||||||
$res = $query->whereNotNull('service_start_at')
|
$havingRaw = sprintf("MAX(service_end_at) >= '%s'", date('Y-m-d H:i:s'));
|
||||||
->where('service_start_at', '<=', date('Y-m-d H:i:s'))
|
|
||||||
->where('service_end_at', '>=', date('Y-m-d H:i:s'))
|
$query = $query->whereNotNull('service_start_at')
|
||||||
|
->groupBy('sim')->havingRaw($havingRaw)
|
||||||
->whereHas('card', function ($relation) {
|
->whereHas('card', function ($relation) {
|
||||||
$relation->whereNull('cancelled_at');
|
$relation->whereNull('cancelled_at');
|
||||||
})->paginate($limit);
|
});
|
||||||
break;
|
break;
|
||||||
|
|
||||||
case 3:
|
case 3:
|
||||||
$res = $query->whereNotNull('service_start_at')
|
$havingRaw = sprintf("MAX(service_end_at) < '%s'", date('Y-m-d H:i:s'));
|
||||||
->where('service_start_at', '<=', date('Y-m-d H:i:s'))
|
|
||||||
->where('service_end_at', '>=', date('Y-m-d H:i:s'))
|
$query = $query->whereNotNull('service_start_at')
|
||||||
|
->groupBy('sim')->havingRaw($havingRaw)
|
||||||
->whereHas('card', function ($relation) {
|
->whereHas('card', function ($relation) {
|
||||||
$relation->whereNull('cancelled_at');
|
$relation->whereNull('cancelled_at');
|
||||||
})->orderBy('created_at', 'desc')->paginate($limit);
|
});
|
||||||
break;
|
break;
|
||||||
|
|
||||||
case 4:
|
case 4:
|
||||||
$res = $query->whereNotNull('service_start_at')->whereHas('card', function ($relation) {
|
$query = $query->whereHas('card', function ($relation) {
|
||||||
$relation->whereNotNull('cancelled_at');
|
$relation->whereNotNull('cancelled_at');
|
||||||
})->paginate($limit);
|
});
|
||||||
break;
|
break;
|
||||||
|
|
||||||
default:
|
default:
|
||||||
@ -107,7 +110,7 @@ class CardService extends Service
|
|||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
|
||||||
return $res;
|
return $query->paginate($limit);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
Loading…
x
Reference in New Issue
Block a user