This commit is contained in:
pbao9
2026-02-04 11:08:30 +07:00
parent 0b29522bd2
commit 8e4e0f33e3
9 changed files with 156 additions and 110 deletions
@@ -16,19 +16,32 @@ class vCenterCachedRepository extends BaseCachedRepository implements vCenterRep
{
return $this->vCenterRepository->getModel();
}
public function filter(array $data = [], array $relationships = [], array $withCount = [], array $select = ['*'])
{
public function filter(
array $data = [],
array $relationships = [],
array $withCount = [],
array $select = ['*'],
bool $returnQuery = false
) {
if (
$returnQuery ||
!empty($data['paginate'])
) {
return $this->vCenterRepository
->filter($data, $relationships, $withCount, $select, $returnQuery);
}
$cacheKey = $this->getCacheKey(md5(json_encode([
'filterItem' => [
'data' => $data,
'relationships' => $relationships,
'select' => $select,
'withCount' => $withCount
]
'filter' => compact('data', 'relationships', 'withCount', 'select')
])));
return Cache::remember($cacheKey, $this->cacheTime, function () use ($data, $relationships, $select, $withCount) {
return $this->vCenterRepository->filter($data, $relationships, $withCount, $select);
});
return Cache::remember(
$cacheKey,
$this->cacheTime,
fn() =>
$this->vCenterRepository
->filter($data, $relationships, $withCount, $select, $returnQuery)
);
}
public function findByName(string $name = '')
{
@@ -37,7 +50,7 @@ class vCenterCachedRepository extends BaseCachedRepository implements vCenterRep
'name' => $name
]
])));
return Cache::remember($cacheKey, $this->cacheTime, function () use ($name){
return Cache::remember($cacheKey, $this->cacheTime, function () use ($name) {
return $this->vCenterRepository->findByName($name);
});
}
+27 -11
View File
@@ -11,26 +11,42 @@ class vCenterRepository extends BaseRepository implements vCenterRepositoryInter
{
return vCenter::class;
}
public function filter(array $data = [], array $relationships = [], array $withCount = [], array $select = ['*'])
{
$model = $this->model->with($relationships)->select($select);
public function filter(
array $data = [],
array $relationships = [],
array $withCount = [],
array $select = ['*'],
bool $returnQuery = false
) {
$query = $this->model
->select($select)
->with($relationships);
if (isset($data['status'])) {
$model = $model->where("status", $data['status']);
// if (!empty($data['vcenter_id'])) {
// $query->where('vcenter_id', $data['vcenter_id']);
// }
if (!empty($withCount)) {
$query->withCount($withCount);
}
if (count($withCount) > 0) {
$model = $model->withCount($withCount);
if ($returnQuery) {
return $query;
}
if(isset($data['page'])) {
return $model->paginate(15);
if (!empty($data['paginate'])) {
return $query->paginate(
$data['per_page'] ?? 15
);
}
return $model->get();
return $query->get();
}
public function findByName(string $name = '')
{
if (empty($name)) {
return false;
}
return $this->model->where('host','LIKE',"%$name%")->first();
return $this->model->where('host', 'LIKE', "%$name%")->first();
}
}
@@ -3,7 +3,14 @@ namespace App\Repositories\vCenter;
use App\Repositories\BaseRepositoryInterface;
interface vCenterRepositoryInterface extends BaseRepositoryInterface {
public function filter(array $data = [],array $relationships = [],array $withCount = [],array $select = ['*']);
interface vCenterRepositoryInterface extends BaseRepositoryInterface
{
public function filter(
array $data = [],
array $relationships = [],
array $withCount = [],
array $select = ['*'],
bool $returnQuery = false
);
public function findByName(string $name = '');
}