卡查询优化

This commit is contained in:
邓皓元 2019-05-10 16:17:19 +08:00
parent 4c61220f13
commit 2823fd8439
2 changed files with 50 additions and 49 deletions

View File

@ -126,55 +126,6 @@ trait OrderCardConcern
}
});
if (isset($conditions['card_status'])) {
switch ($conditions['card_status']) {
case 0:
$this->model = $this->model->whereNull('service_start_at')->whereHas('card', function ($relation) {
$relation->whereNull('cancelled_at');
})->where('created_at', '<', Carbon::now()->subMonths(6));
break;
case 1:
$this->model = $this->model->whereNull('service_start_at')->whereHas('card', function ($relation) {
$relation->whereNull('cancelled_at');
})->where('created_at', '>=', Carbon::now()->subMonths(6));
break;
case 2:
$this->model = $this->model->whereIn('sim', function ($subQuery) {
$table = with(new OrderCardPartition)->getTable();
$subQuery->from($table)->selectRaw('sim')
->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');
});
break;
case 3:
$this->model = $this->model->whereNotNull('service_start_at')->whereNotIn('sim', function ($subQuery) {
$table = with(new OrderCardPartition)->getTable();
$subQuery->from($table)->selectRaw('sim')
->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');
});
break;
case 4:
$this->model = $this->model->whereHas('card', function ($relation) {
$relation->whereNotNull('cancelled_at');
});
break;
default:
# code...
break;
}
}
return $this;
}
}

View File

@ -8,6 +8,7 @@ use Illuminate\Support\Carbon;
use Illuminate\Support\Facades\DB;
use App\Domains\Card\Repositories\CardRepository;
use App\Domains\Virtual\Repositories\OrderCardPartitionRepository;
use App\Models\Virtual\OrderCardPartition;
class CardService extends Service
{
@ -38,11 +39,60 @@ class CardService extends Service
$limit = $conditions['limit'] ?? 20;
$conditions['type'] = 0;
if (isset($conditions['card_status'])) {
$conditions['sim'] = $this->getSimArrayByCardStatus($conditions['card_status'], $limit);
}
$cards = $this->orderCardPartitionRepository->withConditions($conditions)->applyConditions()->paginate($limit);
return static::transformer($cards);
}
public static function getSimArrayByCardStatus(int $card_status, $limit = 0)
{
switch ($card_status) {
case 0:
$res = OrderCardPartition::selectRaw('DISTINCT(sim) sim, created_at')->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);
break;
case 1:
$res = OrderCardPartition::selectRaw('DISTINCT(sim) sim, created_at')-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);
break;
case 2:
$res = OrderCardPartition::selectRaw('DISTINCT(sim) sim, created_at')->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);
break;
case 3:
$res = OrderCardPartition::selectRaw('DISTINCT(sim) sim, created_at')->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);
break;
case 4:
$res = Card::selectRaw('DISTINCT(sim) sim, created_at')->whereNull('cancelled_at')->orderBy('created_at', 'desc')->paginate($limit);
break;
default:
# code...
break;
}
return $res->items()->pluck('sim')->toArray();
}
/**
* 格式转化
*