249 lines
12 KiB
PHP
249 lines
12 KiB
PHP
<?php
|
|
|
|
namespace App\Domains\Virtual\Repositories\Concerns;
|
|
|
|
use Illuminate\Support\Carbon;
|
|
use Illuminate\Support\Facades\DB;
|
|
use App\Models\Virtual\OrderCardPartition;
|
|
|
|
trait OrderCardConcern
|
|
{
|
|
public function getCardBasePackages(array $simArray)
|
|
{
|
|
return $this->model->selectRaw('DISTINCT ON (sim) sim, package_id')
|
|
->whereIn('type', [0, 1])
|
|
->whereIn('sim', $simArray)
|
|
->orderBy('sim')
|
|
->orderBy('created_at', 'desc')->get();
|
|
}
|
|
|
|
/**
|
|
* 查询条件
|
|
*
|
|
* @return void
|
|
*/
|
|
public function withConditions(array $conditions = [])
|
|
{
|
|
$orgModel = $this->getModel();
|
|
|
|
$this->model = $this->model->where(function ($query) use ($conditions, $orgModel) {
|
|
//套餐表的流量套餐类型
|
|
if (isset($conditions['flow_type'])){
|
|
$hasRaw = sprintf("exists (SELECT id FROM virtual_packages WHERE %s.package_id = virtual_packages.id AND virtual_packages.flow_type = %d)",
|
|
$orgModel->getTable(),
|
|
$conditions['flow_type']);
|
|
$query->whereRaw($hasRaw);
|
|
}
|
|
|
|
|
|
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['company_and_package_id'])) {
|
|
$conditions['company_and_package_id'] = array_wrap($conditions['company_and_package_id']);
|
|
|
|
$company_and_package_id = implode(',', array_map(function ($value) {
|
|
return "'".str_replace('"', '""', $value)."'";
|
|
}, $conditions['company_and_package_id']));
|
|
|
|
$query->whereRaw("CONCAT(company_id, '_', package_id) IN (" . $company_and_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']) && isset($conditions['service_start_endtime'])) {
|
|
$havingRaw = sprintf(
|
|
"sim in (SELECT sim FROM virtual_order_cards_partition GROUP BY sim HAVING MIN(service_start_at) >= '%s' AND MIN(service_start_at) <= '%s')",
|
|
Carbon::parse($conditions['service_start_starttime']),
|
|
Carbon::parse($conditions['service_start_endtime'])
|
|
);
|
|
$this->model = $this->model->whereRaw($havingRaw);
|
|
|
|
// $query->where('service_start_at', '>=', Carbon::parse($conditions['service_start_starttime']));
|
|
// $query->where('service_start_at', '<=', Carbon::parse($conditions['service_start_endtime']));
|
|
}
|
|
|
|
if (isset($conditions['service_end_starttime']) && isset($conditions['service_end_endtime'])) {
|
|
$havingRaw = sprintf(
|
|
"sim in (SELECT sim FROM virtual_order_cards_partition GROUP BY sim HAVING MAX(service_end_at) >= '%s' AND MAX(service_end_at) <= '%s')",
|
|
Carbon::parse($conditions['service_end_starttime']),
|
|
Carbon::parse($conditions['service_end_endtime'])
|
|
);
|
|
$this->model = $this->model->whereRaw($havingRaw);
|
|
|
|
// $query->where('service_end_at', '>=', Carbon::parse($conditions['service_end_starttime']));
|
|
// $query->where('service_end_at', '<=', Carbon::parse($conditions['service_end_endtime']));
|
|
}
|
|
|
|
if (isset($conditions['refunded_starttime'])) {
|
|
$query->where('refunded_at', '>=', Carbon::parse($conditions['refunded_starttime']));
|
|
}
|
|
|
|
if (isset($conditions['refunded_endtime'])) {
|
|
$query->where('refunded_at', '<=', Carbon::parse($conditions['refunded_endtime']));
|
|
}
|
|
|
|
if (isset($conditions['month'])) {
|
|
$time = Carbon::parse($conditions['month']);
|
|
$query->where(function ($subQuery) use ($time) {
|
|
$subQuery->where('service_start_at', '<=', $time->copy()->endOfMonth())->where('service_end_at', '>=', $time->copy()->startOfMonth());
|
|
});
|
|
}
|
|
|
|
if (isset($conditions['year'])) {
|
|
$year = $conditions['year'];
|
|
$query->where(function ($subQuery) use ($year) {
|
|
$subQuery->where('service_start_at', '<=', "$year-12-31 23:59:59")->where('service_end_at', '>=', "$year-01-01 00:00:00");
|
|
});
|
|
}
|
|
|
|
if (isset($conditions['activated_starttime']) && isset($conditions['activated_endtime'])) {
|
|
$query->where('service_start_at', '>=', Carbon::parse($conditions['activated_starttime']))
|
|
->where('service_start_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['is_refunded'])) {
|
|
$query->whereNotNull('refunded_at');
|
|
}
|
|
|
|
if (isset($conditions['sell_starttime']) && isset($conditions['sell_endtime'])) {
|
|
$query->whereRaw(sprintf("exists (SELECT sim FROM virtual_order_cards_partition AS p WHERE %s.sim = p.sim AND type in (0, 1) AND created_at >= '%s' AND created_at <= '%s' AND deleted_at is null AND refunded_at is null)", $orgModel->getTable(), $conditions['sell_starttime'], $conditions['sell_endtime']));
|
|
}
|
|
|
|
if (isset($conditions['has_type']) && !empty($conditions['has_type'])) {
|
|
$conditions['has_type'] = array_wrap($conditions['has_type']);
|
|
$query->whereRaw(sprintf("exists (SELECT sim from virtual_order_cards_partition AS p WHERE %s.sim = p.sim AND type in (%s))", $orgModel->getTable(), implode(',', $conditions['has_type'])));
|
|
}
|
|
});
|
|
|
|
if (isset($conditions['card_status'])) {
|
|
$conditions['card_status'] = intval($conditions['card_status']);
|
|
$date = isset($conditions['month']) ? Carbon::parse($conditions['month'])->copy()->startOfMonth() : Carbon::now()->startOfMonth();
|
|
|
|
switch ($conditions['card_status']) {
|
|
case 0:
|
|
$hasRaw = sprintf("exists (SELECT sim FROM cards WHERE %s.sim = cards.sim )", $orgModel->getTable(), $date);
|
|
|
|
$this->model = $this->model->whereNull('service_start_at')
|
|
->whereRaw($hasRaw)
|
|
->where('created_at', '<', Carbon::now()->subMonths(6));
|
|
break;
|
|
|
|
case 1:
|
|
$hasRaw = sprintf("exists (SELECT sim FROM cards WHERE %s.sim = cards.sim )", $orgModel->getTable(), $date);
|
|
|
|
$this->model = $this->model->whereNull('service_start_at')
|
|
->whereRaw($hasRaw)
|
|
->where('created_at', '>=', Carbon::now()->subMonths(6));
|
|
break;
|
|
|
|
case 2:
|
|
$havingRaw = sprintf("exists (SELECT sim FROM virtual_order_cards_partition AS p WHERE %s.sim = p.sim GROUP BY sim HAVING MAX(service_end_at) >= '%s')", $orgModel->getTable(), $date);
|
|
$hasRaw = sprintf("exists (SELECT sim FROM cards WHERE %s.sim = cards.sim)", $orgModel->getTable(), $date);
|
|
$this->model = $this->model->whereNotNull('service_start_at')
|
|
->whereRaw($havingRaw)
|
|
->whereRaw($hasRaw);
|
|
break;
|
|
|
|
case 3:
|
|
$havingRaw = sprintf("exists (SELECT sim FROM virtual_order_cards_partition AS p WHERE %s.sim = p.sim GROUP BY sim HAVING MAX(service_end_at) < '%s')", $orgModel->getTable(), $date);
|
|
$hasRaw = sprintf("exists (SELECT sim FROM cards WHERE %s.sim = cards.sim)", $orgModel->getTable(), $date);
|
|
$this->model = $this->model->whereNotNull('service_start_at')
|
|
->whereRaw($havingRaw)
|
|
->whereRaw($hasRaw);
|
|
break;
|
|
|
|
case 4:
|
|
$hasRaw = sprintf("exists (SELECT sim FROM cards WHERE %s.sim = cards.sim AND (cancelled_at IS NOT NULL AND cancelled_at >= '%s'))", $orgModel->getTable(), $date);
|
|
$this->model = $this->model->whereRaw($hasRaw);
|
|
break;
|
|
|
|
default:
|
|
# code...
|
|
break;
|
|
}
|
|
}
|
|
|
|
if (isset($conditions['uncancelled'])) {
|
|
$date = isset($conditions['month']) ? Carbon::parse($conditions['month'])->copy()->startOfMonth() : date('Y-m-d H:i:s');
|
|
$this->model = $this->model->whereHas('card', function ($relation) use ($date) {
|
|
$relation->whereNull('cancelled_at')->orWhere('cancelled_at', ">", $date);
|
|
});
|
|
}
|
|
|
|
return $this;
|
|
}
|
|
}
|