first commit

This commit is contained in:
2025-09-12 15:23:08 +08:00
commit a80c237bbb
117 changed files with 15628 additions and 0 deletions

View File

@@ -0,0 +1,34 @@
<?php
/**
* This service file is part of item.
*
* @author ctexthuang
* @contact ctexthuang@qq.com
*/
declare(strict_types=1);
namespace App\Repository;
use App\Model\AdminUser;
/**
* Class AdminUserRepository
* @extends BaseRepository<AdminUser>
*/
final class AdminUserRepository extends BaseRepository
{
public function __construct(protected readonly AdminUser $model) {}
/**
* @param string $username
* @return AdminUser|null
*/
public function findByUserName(string $username): AdminUser|null
{
// @phpstan-ignore-next-line
return $this->model->newQuery()
->where('username', $username)
->first();
}
}

View File

@@ -0,0 +1,151 @@
<?php
namespace App\Repository;
use App\Repository\Traits\BootTrait;
use App\Repository\Traits\RepositoryOrderByTrait;
use Hyperf\Collection\Collection;
use Hyperf\Contract\LengthAwarePaginatorInterface;
use Hyperf\Database\Model\Builder;
use Hyperf\Database\Model\Model;
use Hyperf\DbConnection\Traits\HasContainer;
use Hyperf\Paginator\AbstractPaginator;
/**
* @template T of Model
* @property T $model
*/
abstract class BaseRepository
{
use BootTrait;
use HasContainer;
use RepositoryOrderByTrait;
public const string PER_PAGE_PARAM_NAME = 'per_page';
public function handleSearch(Builder $query, array $params): Builder
{
return $query;
}
public function handleItems(Collection $items): Collection
{
return $items;
}
public function handlePage(LengthAwarePaginatorInterface $paginator): array
{
if ($paginator instanceof AbstractPaginator) {
$items = $paginator->getCollection();
} else {
$items = Collection::make($paginator->items());
}
$items = $this->handleItems($items);
return [
'list' => $items->toArray(),
'total' => $paginator->total(),
];
}
public function list(array $params = []): Collection
{
return $this->handleItems($this->perQuery($this->getQuery(), $params)->get());
}
public function count(array $params = []): int
{
return $this->perQuery($this->getQuery(), $params)->count();
}
public function page(array $params = [], ?int $page = null, ?int $pageSize = null): array
{
$result = $this->perQuery($this->getQuery(), $params)->paginate(
perPage: $pageSize,
pageName: static::PER_PAGE_PARAM_NAME,
page: $page,
);
return $this->handlePage($result);
}
/**
* @return T
*/
public function create(array $data): mixed
{
// @phpstan-ignore-next-line
return $this->getQuery()->create($data);
}
public function updateById(mixed $id, array $data): bool
{
return (bool) $this->getQuery()->whereKey($id)->first()?->update($data);
}
/**
* @return null|T
*/
public function saveById(mixed $id, array $data): mixed
{
$model = $this->getQuery()->whereKey($id)->first();
if ($model) {
$model->fill($data)->save();
return $model;
}
return null;
}
public function deleteById(mixed $id): int
{
// @phpstan-ignore-next-line
return $this->model::destroy($id);
}
public function forceDeleteById(mixed $id): bool
{
return (bool) $this->getQuery()->whereKey($id)->forceDelete();
}
/**
* @return null|T
*/
public function findById(mixed $id): mixed
{
return $this->getQuery()->whereKey($id)->first();
}
public function findByField(mixed $id, string $field): mixed
{
return $this->getQuery()->whereKey($id)->value($field);
}
/**
* @return null|T
*/
public function findByFilter(array $params): mixed
{
return $this->perQuery($this->getQuery(), $params)->first();
}
public function perQuery(Builder $query, array $params): Builder
{
$this->startBoot($query, $params);
return $this->handleSearch($query, $params);
}
public function getQuery(): Builder
{
return $this->model->newQuery();
}
public function existsById(mixed $id): bool
{
return (bool) $this->getQuery()->whereKey($id)->exists();
}
/**
* @return T
*/
public function getModel(): Model
{
return $this->model;
}
}

View File

@@ -0,0 +1,20 @@
<?php
namespace App\Repository\Traits;
use function Hyperf\Support\class_basename;
use function Hyperf\Support\class_uses_recursive;
trait BootTrait
{
protected function startBoot(...$params): void
{
$traits = class_uses_recursive(static::class);
foreach ($traits as $trait) {
$method = 'boot' . class_basename($trait);
if (method_exists($this, $method)) {
$this->{$method}(...$params);
}
}
}
}

View File

@@ -0,0 +1,38 @@
<?php
namespace App\Repository\Traits;
use Hyperf\Database\Model\Builder;
trait RepositoryOrderByTrait
{
public function handleOrderBy(Builder $query, $params): Builder
{
if ($this->enablePageOrderBy()) {
$orderByField = $params[$this->getOrderByParamName()] ?? $query->getModel()->getKeyName();
$orderByDirection = $params[$this->getOrderByDirectionParamName()] ?? 'desc';
$query->orderBy($orderByField, $orderByDirection);
}
return $query;
}
protected function bootRepositoryOrderByTrait(Builder $query, array $params): void
{
$this->handleOrderBy($query, $params);
}
protected function getOrderByParamName(): string
{
return 'order_by';
}
protected function getOrderByDirectionParamName(): string
{
return 'order_by_direction';
}
protected function enablePageOrderBy(): bool
{
return true;
}
}