116 lines
4.5 KiB
PHP
116 lines
4.5 KiB
PHP
<?php
|
|
|
|
namespace App\Repositories\Module\Vmware;
|
|
|
|
use App\Models\ModuleVmware;
|
|
use App\Repositories\BaseRepository;
|
|
|
|
class VmwareRepository extends BaseRepository implements VmwareRepositoryInterface
|
|
{
|
|
public function getModel()
|
|
{
|
|
return ModuleVmware::class;
|
|
}
|
|
public function filter(array $data = [], array $relationships = [], array $withCount = [], array $select = ['*'])
|
|
{
|
|
$model = $this->model->with($relationships)->select($select);
|
|
if (isset($data['relations'])) {
|
|
foreach ($data['relations'] as $key => $value) {
|
|
$model = $model->whereHas($key, function ($query) use ($value) {
|
|
if (isset($value['condition'])) {
|
|
switch (strtoupper($value['condition'])) {
|
|
case "IN":
|
|
$query->whereIn($value['key'], $value['value']);
|
|
break;
|
|
case "NotIn":
|
|
$query->whereNotIn($value['key'], $value['value']);
|
|
break;
|
|
default:
|
|
$query->where($value['key'], $value['condition'], $value['value']);
|
|
break;
|
|
}
|
|
} else {
|
|
$query->where($value['key'], $value['value']);
|
|
}
|
|
});
|
|
}
|
|
}
|
|
if (isset($data['status'])) {
|
|
if (is_array($data['status'])) {
|
|
$model = $model->where(function ($query) use ($data) {
|
|
if (isset($data['status']['condition'])) {
|
|
switch (strtoupper($data['status']['condition'])) {
|
|
case "IN":
|
|
$query->whereIn($data['status']['key'], $data['status']['value']);
|
|
break;
|
|
case "NOTIN":
|
|
$query->whereNotIn($data['status']['key'], $data['status']['value']);
|
|
break;
|
|
default:
|
|
$query->where($data['status']['key'], $data['status']['condition'], $data['status']['value']);
|
|
break;
|
|
}
|
|
} else {
|
|
$query->where($data['status']['key'], $data['status']['value']);
|
|
}
|
|
});
|
|
} else {
|
|
$model = $model->where("status", $data['status']);
|
|
}
|
|
}
|
|
if (count($withCount) > 0) {
|
|
$model = $model->withCount($withCount);
|
|
}
|
|
if (isset($data['page'])) {
|
|
return $model->paginate(15);
|
|
}
|
|
return $model->get();
|
|
}
|
|
public function findWithRelationships(int $id = 0, array $relationships = [], array $withCount = [], array $withSum = [])
|
|
{
|
|
$model = $this->model;
|
|
if (count($relationships) > 0) {
|
|
foreach ($relationships as $relationship) {
|
|
if (method_exists($model, $relationship)) {
|
|
$model = $model->with($relationship);
|
|
}
|
|
}
|
|
}
|
|
// Xử lý withCount
|
|
if (count($withCount) > 0) {
|
|
foreach ($withCount as $key => $items) {
|
|
if (is_array($items)) {
|
|
foreach ($items as $alias => $item) {
|
|
$model = $model->withCount([
|
|
$key . ' as ' . $alias => function ($query) use ($item) {
|
|
$query->where($item['key'], $item['condition'], $item['value']);
|
|
}
|
|
]);
|
|
}
|
|
} else {
|
|
$model = $model->withCount($key);
|
|
}
|
|
}
|
|
}
|
|
|
|
// Xử lý withSum
|
|
if (count($withSum) > 0) {
|
|
foreach ($withSum as $key => $items) {
|
|
if (is_array($items)) {
|
|
foreach ($items as $alias => $item) {
|
|
$model = $model->withSum([
|
|
$key . ' as ' . $alias => function ($query) use ($item) {
|
|
$query->where($item['key'], $item['condition'], $item['value']);
|
|
}
|
|
], $item['sum']);
|
|
}
|
|
} else {
|
|
$model = $model->withSum($key);
|
|
}
|
|
}
|
|
}
|
|
|
|
return $model->find($id);
|
|
}
|
|
}
|