178 lines
7.1 KiB
PHP
178 lines
7.1 KiB
PHP
<?php
|
|
|
|
namespace App\Domains\Virtual\Repositories\Concerns;
|
|
|
|
use Illuminate\Support\Carbon;
|
|
use App\Models\Virtual\OrderCardPartition;
|
|
|
|
trait OrderCardConcern
|
|
{
|
|
/**
|
|
* 查询条件
|
|
*
|
|
* @return void
|
|
*/
|
|
public function withConditions(array $conditions = [])
|
|
{
|
|
$this->model = $this->model->where(function ($query) use ($conditions) {
|
|
if (isset($conditions['id'])) {
|
|
$conditions['id'] = array_wrap($conditions['id']);
|
|
$query->whereIn('id', $conditions['id']);
|
|
}
|
|
|
|
if (isset($conditions['type'])) {
|
|
$conditions['type'] = array_wrap($conditions['type']);
|
|
$query->whereIn('type', $conditions['type']);
|
|
}
|
|
|
|
if (isset($conditions['order_id'])) {
|
|
$conditions['order_id'] = array_wrap($conditions['order_id']);
|
|
$query->whereIn('order_id', $conditions['order_id']);
|
|
}
|
|
|
|
if (isset($conditions['sim'])) {
|
|
$conditions['sim'] = array_wrap($conditions['sim']);
|
|
$conditions['sim'] = array_map('intval', $conditions['sim']);
|
|
$query->whereIn('sim', $conditions['sim']);
|
|
}
|
|
|
|
if (isset($conditions['company_id'])) {
|
|
$conditions['company_id'] = array_wrap($conditions['company_id']);
|
|
$query->whereIn('company_id', $conditions['company_id']);
|
|
}
|
|
|
|
if (isset($conditions['package_id'])) {
|
|
$conditions['package_id'] = array_wrap($conditions['package_id']);
|
|
$query->whereIn('package_id', $conditions['package_id']);
|
|
}
|
|
|
|
if (isset($conditions['carrier_operator'])) {
|
|
$query->whereHas('package', function ($relation) use ($conditions) {
|
|
$relation->withTrashed()->where('carrier_operator', $conditions['carrier_operator']);
|
|
});
|
|
}
|
|
|
|
if (isset($conditions['pay_channel'])) {
|
|
$query->where('pay_channel', $conditions['pay_channel']);
|
|
}
|
|
|
|
if (isset($conditions['unit_price'])) {
|
|
$query->where('unit_price', $conditions['unit_price']);
|
|
}
|
|
|
|
if (!empty($conditions['company_name'])) {
|
|
$query->whereHas('company', function ($relation) use ($conditions) {
|
|
$relation->withTrashed()->where('name', $conditions['company_name']);
|
|
});
|
|
}
|
|
|
|
if (!empty($conditions['package_name'])) {
|
|
$query->whereHas('package', function ($relation) use ($conditions) {
|
|
$relation->withTrashed()->where('name', $conditions['package_name']);
|
|
});
|
|
}
|
|
|
|
if (isset($conditions['starttime'])) {
|
|
$query->where('created_at', '>=', Carbon::parse($conditions['starttime']));
|
|
}
|
|
|
|
if (isset($conditions['endtime'])) {
|
|
$query->where('created_at', '<=', Carbon::parse($conditions['endtime']));
|
|
}
|
|
|
|
if (isset($conditions['service_start_starttime'])) {
|
|
$query->where('service_start_at', '>=', Carbon::parse($conditions['service_start_starttime']));
|
|
}
|
|
|
|
if (isset($conditions['service_start_endtime'])) {
|
|
$query->where('service_start_at', '<=', Carbon::parse($conditions['service_start_endtime']));
|
|
}
|
|
|
|
if (isset($conditions['service_end_starttime'])) {
|
|
$query->where('service_end_at', '>=', Carbon::parse($conditions['service_end_starttime']));
|
|
}
|
|
|
|
if (isset($conditions['service_end_endtime'])) {
|
|
$query->where('service_end_at', '<=', Carbon::parse($conditions['service_end_endtime']));
|
|
}
|
|
|
|
if (isset($conditions['month'])) {
|
|
$time = Carbon::parse($conditions['month'])->format('Y-m-d H:i:s');
|
|
$query->where(function ($subQuery) use ($time) {
|
|
$subQuery->where('service_start_at', '<=', $time)->where('service_end_at', '>=', $time);
|
|
});
|
|
|
|
// $conditions['month'] = (int)Carbon::parse($conditions['month'])->format('Ym');
|
|
// $query->whereRaw("timelines_index(id, sim) @> '{{$conditions['month']}}'");
|
|
}
|
|
|
|
if (isset($conditions['activated_starttime']) && isset($conditions['activated_endtime'])) {
|
|
$query->whereHas('card', function ($relation) use ($conditions) {
|
|
$relation->where('activated_at', '>=', Carbon::parse($conditions['activated_starttime']))
|
|
->where('activated_at', '<=', Carbon::parse($conditions['activated_endtime']));
|
|
});
|
|
}
|
|
|
|
if (isset($conditions['sn'])) {
|
|
$query->whereHas('order', function ($relation) use ($conditions) {
|
|
$relation->where('sn', 'like', "%{$conditions['sn']}%");
|
|
});
|
|
}
|
|
|
|
if (isset($conditions['transaction_no'])) {
|
|
$query->whereHas('order', function ($relation) use ($conditions) {
|
|
$relation->where('transaction_no', 'like', "%{$conditions['transaction_no']}%");
|
|
});
|
|
}
|
|
});
|
|
|
|
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:
|
|
$havingRaw = sprintf("MAX(service_end_at) >= '%s'", date('Y-m-d H:i:s'));
|
|
|
|
$this->model = $this->model->whereNotNull('service_start_at')
|
|
->groupBy('sim')->havingRaw($havingRaw)
|
|
->whereHas('card', function ($relation) {
|
|
$relation->whereNull('cancelled_at');
|
|
});
|
|
break;
|
|
|
|
case 3:
|
|
$havingRaw = sprintf("MAX(service_end_at) < '%s'", date('Y-m-d H:i:s'));
|
|
|
|
$this->model = $this->model->whereNotNull('service_start_at')
|
|
->groupBy('sim')->havingRaw($havingRaw)
|
|
->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;
|
|
}
|
|
}
|