This commit is contained in:
Aether
2025-09-26 15:36:09 +08:00
parent 0c7142ad46
commit f3a672b5c9
6 changed files with 295 additions and 46 deletions

View File

@@ -4,6 +4,7 @@ declare(strict_types=1);
namespace Aether;
use Aether\Contract\TreeableInterface;
use Closure;
use DateTime;
use Exception;
@@ -91,6 +92,60 @@ abstract class AetherModel extends HyperfModel implements CacheableInterface
return null;
}
/**
* 列表查询.
*/
// public function list(array $params = []): array
// {
// $query = $this->newQuery();
//
// // 通过模型配置自动应用所有搜索条件
// $this->applySearch($query, $params);
//
// // 动态应用排序
// $sortConfig = $this->getSortConfig();
// if ($sortConfig) {
// $query->orderBy($sortConfig['field'], $sortConfig['direction']);
// }
//
// $withDeleted = filter_var($params['with_deleted'] ?? false, FILTER_VALIDATE_BOOLEAN);
// if ($withDeleted) {
// $query->withTrashed();
// }
//
// // 存在分页参数page或size则进行分页查询
// if (isset($params['page']) || isset($params['size'])) {
// $page = (int) ($params['page'] ?? 1);
// $size = (int) ($params['size'] ?? 10);
// $page = max(1, $page);
// $size = max(1, min(100, $size));
// $result = $query->paginate($size, ['*'], 'page', $page);
// return [
// 'total' => $result->total(),
// 'list' => $result->items(),
// ];
// }
//
// // 无分页参数时返回完整数据集合
// $items = $query->get()->toArray();
//
// // 若模型支持树形结构则构建树形,否则返回普通数组
// if ($this instanceof TreeableInterface) {
// return $this::buildTree($items, (int) ($params['parent_id'] ?? 0));
// }
//
// return $items;
// }
/**
* 列表查询.
*/
public function list(array $params = []): array
{
$query = $this->buildQueryFromParams($params);
return $this->listResult($query, $params);
}
/**
* 快捷创建.
*/
@@ -238,6 +293,76 @@ abstract class AetherModel extends HyperfModel implements CacheableInterface
return Db::transaction($closure);
}
/**
* 根据参数构建查询.
*/
protected function buildQueryFromParams(array $params = []): Builder
{
// 创建查询构建器
$query = static::query();
// 应用搜索条件
// if (isset($params['search'])) {
// $this->applySearch($query, $params['search']);
// }
$this->applySearch($query, $params);
// 应用排序
$this->applySorting($query, $params);
// 处理软删除
if (isset($params['withTrashed']) && $params['withTrashed']) {
$query->withTrashed();
}
return $query;
}
/**
* 应用排序.
*/
protected function applySorting(Builder $query, array $params = []): void
{
// 优先使用传入的排序参数
if (isset($params['sort_field'], $params['sort_direction'])) {
$query->orderBy($params['sort_field'], $params['sort_direction']);
return;
}
// 使用模型配置的排序
$sortConfig = $this->getSortConfig();
if (! empty($sortConfig)) {
$query->orderBy($sortConfig['field'], $sortConfig['direction']);
}
}
/**
* 根据分页参数获取结果.
*/
protected function listResult(Builder $query, array $params = []): array
{
// 分页处理
if (isset($params['page'], $params['size'])) {
$page = max(1, (int) $params['page']);
$size = max(1, min(100, (int) $params['size']));
$result = $query->paginate($size, ['*'], 'page', $page);
return [
'list' => $result->items(),
'total' => $result->total(),
];
}
// 获取所有数据
$result = $query->get()->toArray();
// 如果实现了树结构接口,构建树
if ($this instanceof TreeableInterface) {
return $this->buildTree($result, $params['parent_id'] ?? 0);
}
return $result;
}
/**
* 初始化模型.
*/