查询优化

This commit is contained in:
邓皓元 2019-05-10 16:39:49 +08:00
parent c40138c19a
commit 6d7e43e19a
2 changed files with 19 additions and 11 deletions

View File

@ -29,7 +29,8 @@ class CardRepository extends Repository
'created_at' => 'like',
];
public function model() {
public function model()
{
return Model::class;
}

View File

@ -58,32 +58,37 @@ class CardService extends Service
return new LengthAwarePaginator($cards, $total, $limit);
}
public static function listByCardStatus(int $card_status, $limit = 0)
public static function listByCardStatus(array $conditions = [])
{
switch ($card_status) {
$limit = $conditions['limit'] ?? 20;
$conditions['type'] = [0, 1, 2];
$query = $this->orderCardPartitionRepository->selectRaw('DISTINCT(sim) sim, created_at')->withConditions($conditions)->applyConditions();
switch ($conditions['card_status']) {
case 0:
$res = OrderCardPartition::selectRaw('DISTINCT(sim) sim, created_at')->whereIn('type', [0, 1, 2])->whereNull('service_start_at')->whereHas('card', function ($relation) {
$res = $query->whereNull('service_start_at')->whereHas('card', function ($relation) {
$relation->whereNull('cancelled_at');
})->where('created_at', '<', Carbon::now()->subMonths(6))->orderBy('created_at', 'desc')->paginate($limit);
})->where('created_at', '<', Carbon::now()->subMonths(6))->paginate($limit);
break;
case 1:
$res = OrderCardPartition::selectRaw('DISTINCT(sim) sim, created_at')->whereIn('type', [0, 1, 2])->whereNull('service_start_at')->whereHas('card', function ($relation) {
$res = $query->whereNull('service_start_at')->whereHas('card', function ($relation) {
$relation->whereNull('cancelled_at');
})->where('created_at', '>=', Carbon::now()->subMonths(6))->orderBy('created_at', 'desc')->paginate($limit);
})->where('created_at', '>=', Carbon::now()->subMonths(6))->paginate($limit);
break;
case 2:
$res = OrderCardPartition::selectRaw('DISTINCT(sim) sim, created_at')->whereIn('type', [0, 1, 2])->whereNotNull('service_start_at')
$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'))
->whereHas('card', function ($relation) {
$relation->whereNull('cancelled_at');
})->orderBy('created_at', 'desc')->paginate($limit);
})->paginate($limit);
break;
case 3:
$res = OrderCardPartition::selectRaw('DISTINCT(sim) sim, created_at')->whereIn('type', [0, 1, 2])->whereNotNull('service_start_at')
$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'))
->whereHas('card', function ($relation) {
@ -92,7 +97,9 @@ class CardService extends Service
break;
case 4:
$res = Card::selectRaw('sim, created_at')->whereNull('cancelled_at')->orderBy('created_at', 'desc')->paginate($limit);
$res = $query->whereNotNull('service_start_at')->whereHas('card', function ($relation) {
$relation->whereNotNull('cancelled_at');
})->paginate($limit);
break;
default: