mirror of
https://gitee.com/ctexthuang/hyperf-micro-svc.git
synced 2026-02-08 10:20:16 +08:00
feat : admin menu
This commit is contained in:
80
app/Repository/AdminMenuRepository.php
Normal file
80
app/Repository/AdminMenuRepository.php
Normal file
@@ -0,0 +1,80 @@
|
||||
<?php
|
||||
/**
|
||||
* This service file is part of item.
|
||||
*
|
||||
* @author ctexthuang
|
||||
* @contact ctexthuang@qq.com
|
||||
*/
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace App\Repository;
|
||||
|
||||
use App\Constants\Model\AdminUser\AdminMenuStatusCode;
|
||||
use App\Model\AdminMenu;
|
||||
use Hyperf\Collection\Arr;
|
||||
use Hyperf\Collection\Collection;
|
||||
use Hyperf\Database\Model\Builder;
|
||||
|
||||
final class AdminMenuRepository extends BaseRepository
|
||||
{
|
||||
/**
|
||||
* @param AdminMenu $model
|
||||
*/
|
||||
public function __construct(protected readonly AdminMenu $model) {}
|
||||
|
||||
/**
|
||||
* @return bool
|
||||
*/
|
||||
public function enablePageOrderBy(): bool
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param array $params
|
||||
* @return Collection
|
||||
*/
|
||||
public function list(array $params = []): Collection
|
||||
{
|
||||
return $this->perQuery($this->getQuery(), $params)->orderBy('sort')->get();
|
||||
}
|
||||
|
||||
/**
|
||||
* @param Builder $query
|
||||
* @param array $params
|
||||
* @return Builder
|
||||
*/
|
||||
public function handleSearch(Builder $query, array $params): Builder
|
||||
{
|
||||
$whereInName = static function (Builder $query, array|string $code) {
|
||||
$query->whereIn('name', Arr::wrap($code));
|
||||
};
|
||||
return $query
|
||||
->when(Arr::get($params, 'sortable'), static function (Builder $query, array $sortable) {
|
||||
$query->orderBy(key($sortable), current($sortable));
|
||||
})
|
||||
->when(Arr::get($params, 'code'), $whereInName)
|
||||
->when(Arr::get($params, 'name'), $whereInName)
|
||||
->when(Arr::get($params, 'children'), static function (Builder $query) {
|
||||
$query->with('children');
|
||||
})->when(Arr::get($params, 'status'), static function (Builder $query, AdminMenuStatusCode $status) {
|
||||
$query->where('status', $status);
|
||||
})
|
||||
->when(Arr::has($params, 'parent_id'), static function (Builder $query) use ($params) {
|
||||
$query->where('parent_id', Arr::get($params, 'parent_id'));
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* @return Builder[]|\Hyperf\Database\Model\Collection
|
||||
*/
|
||||
public function allTree(): \Hyperf\Database\Model\Collection|array
|
||||
{
|
||||
return $this->model
|
||||
->newQuery()
|
||||
->where('parent_id', 0)
|
||||
->with('children')
|
||||
->get();
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user