基础数据-校区服务
This commit is contained in:
@@ -10,37 +10,16 @@ use Hyperf\Database\Model\Builder;
|
||||
use Throwable;
|
||||
|
||||
/**
|
||||
* 抽象CRUD服务基类,封装通用逻辑
|
||||
* 抽象CRUD服务基类,封装通用逻辑.
|
||||
*/
|
||||
abstract class AetherCrudService extends AetherService implements AetherCrudInterface
|
||||
{
|
||||
protected array $search = [];
|
||||
|
||||
protected function getSearch(): array
|
||||
{
|
||||
return $this->search;
|
||||
}
|
||||
|
||||
protected array $ignoreSearchFields = [];
|
||||
|
||||
protected function getIgnoreSearchFields(): array
|
||||
{
|
||||
return $this->ignoreSearchFields;
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取当前服务对应的模型实例(由子类实现)
|
||||
*/
|
||||
protected abstract function getModel(): AetherModel;
|
||||
|
||||
/**
|
||||
* 获取当前服务对应的验证器实例(由子类实现)
|
||||
*/
|
||||
protected abstract function getValidator(): AetherValidator;
|
||||
|
||||
|
||||
/**
|
||||
* 通用列表查询(支持分页和树形结构)
|
||||
* 通用列表查询(支持分页和树形结构).
|
||||
*/
|
||||
public function list(array $params = []): array
|
||||
{
|
||||
@@ -63,8 +42,8 @@ abstract class AetherCrudService extends AetherService implements AetherCrudInte
|
||||
|
||||
// 存在分页参数(page或size)则进行分页查询
|
||||
if (isset($params['page']) || isset($params['size'])) {
|
||||
$page = (int)($params['page'] ?? 1);
|
||||
$size = (int)($params['size'] ?? 10);
|
||||
$page = (int) ($params['page'] ?? 1);
|
||||
$size = (int) ($params['size'] ?? 10);
|
||||
// 确保分页参数合法性
|
||||
$page = max(1, $page);
|
||||
$size = max(1, min(100, $size)); // 限制最大页大小为100
|
||||
@@ -72,7 +51,7 @@ abstract class AetherCrudService extends AetherService implements AetherCrudInte
|
||||
$result = $query->paginate($size, ['*'], 'page', $page);
|
||||
return [
|
||||
'total' => $result->total(),
|
||||
'list' => $result->items()
|
||||
'list' => $result->items(),
|
||||
];
|
||||
}
|
||||
|
||||
@@ -81,22 +60,23 @@ abstract class AetherCrudService extends AetherService implements AetherCrudInte
|
||||
|
||||
// 若模型支持树形结构则构建树形,否则返回普通数组
|
||||
if ($model instanceof TreeableInterface) {
|
||||
return $model::buildTree($items, (int)($params['parent_id'] ?? 0));
|
||||
return $model::buildTree($items, (int) ($params['parent_id'] ?? 0));
|
||||
}
|
||||
|
||||
return $items;
|
||||
}
|
||||
|
||||
/**
|
||||
* 通用详情查询
|
||||
* 通用详情查询.
|
||||
*/
|
||||
public function detail(int $id): object
|
||||
{var_dump('detail');
|
||||
{
|
||||
var_dump('detail');
|
||||
return $this->getModel()->findOrFailById($id);
|
||||
}
|
||||
|
||||
/**
|
||||
* 通用创建逻辑
|
||||
* 通用创建逻辑.
|
||||
* @throws BusinessException|Throwable
|
||||
*/
|
||||
public function create(array $data): int
|
||||
@@ -108,14 +88,14 @@ abstract class AetherCrudService extends AetherService implements AetherCrudInte
|
||||
$model = $this->getModel()->createOne($data);
|
||||
$this->logger()->info('创建资源', [
|
||||
'id' => $model->id,
|
||||
'code' => $data['code'] ?? $model->code
|
||||
'code' => $data['code'] ?? $model->code,
|
||||
]);
|
||||
return $model->id;
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* 通用更新逻辑
|
||||
* 通用更新逻辑.
|
||||
* @throws BusinessException|Throwable
|
||||
*/
|
||||
public function update(int $id, array $data): bool
|
||||
@@ -134,14 +114,14 @@ abstract class AetherCrudService extends AetherService implements AetherCrudInte
|
||||
$result = $this->getModel()->updateById($id, $data);
|
||||
$this->logger()->info('更新资源', [
|
||||
'id' => $id,
|
||||
'data' => $data
|
||||
'data' => $data,
|
||||
]);
|
||||
return $result;
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* 通用删除逻辑
|
||||
* 通用删除逻辑.
|
||||
* @throws BusinessException|Throwable
|
||||
*/
|
||||
public function delete(int $id): bool
|
||||
@@ -162,7 +142,134 @@ abstract class AetherCrudService extends AetherService implements AetherCrudInte
|
||||
}
|
||||
|
||||
/**
|
||||
* 钩子方法:更新时的特殊逻辑(子类可重写)
|
||||
* 根据模型的$search配置,自动应用搜索条件到查询构建器.
|
||||
*/
|
||||
public function applySearch(Builder $query, array $params): void
|
||||
{
|
||||
foreach ($this->search as $field => $rule) {
|
||||
// 跳过未传递的参数
|
||||
if (! isset($params[$field])) {
|
||||
continue;
|
||||
}
|
||||
|
||||
$value = $params[$field];
|
||||
$this->applySearchRule($query, $field, $value, $rule);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 软删除恢复.
|
||||
* @throws BusinessException|Throwable
|
||||
*/
|
||||
public function restore(int $id): bool
|
||||
{
|
||||
$model = $this->getModel();
|
||||
// 必须使用withTrashed()才能查询到已删除记录
|
||||
$resource = $model->newQuery()->withTrashed()->find($id);
|
||||
$this->checkResourceExists($resource, '恢复的资源不存在');
|
||||
|
||||
return $this->transaction(function () use ($id) {
|
||||
$result = $this->getModel()->newQuery()->withTrashed()
|
||||
->where('id', $id)->restore();
|
||||
|
||||
$this->logger()->info('恢复软删除资源', ['id' => $id]);
|
||||
return $result;
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* 批量软删除.
|
||||
* @throws BusinessException|Throwable
|
||||
*/
|
||||
public function batchDelete(array $ids): bool
|
||||
{
|
||||
if (empty($ids)) {
|
||||
throw new BusinessException('请选择要删除的记录', 400);
|
||||
}
|
||||
|
||||
$model = $this->getModel();
|
||||
$exists = $model->whereIn('id', $ids)->exists();
|
||||
if (! $exists) {
|
||||
throw new BusinessException('部分记录不存在', 404);
|
||||
}
|
||||
|
||||
return $this->transaction(function () use ($ids) {
|
||||
$result = $this->getModel()->whereIn('id', $ids)->delete();
|
||||
$this->logger()->info('批量软删除资源', ['ids' => $ids]);
|
||||
return $result > 0;
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* 批量恢复软删除.
|
||||
* @throws BusinessException|Throwable
|
||||
*/
|
||||
public function batchRestore(array $ids): bool
|
||||
{
|
||||
if (empty($ids)) {
|
||||
throw new BusinessException('请选择要恢复的记录', 400);
|
||||
}
|
||||
|
||||
$model = $this->getModel();
|
||||
$exists = $model->newQuery()->withTrashed()
|
||||
->whereIn('id', $ids)
|
||||
->whereNotNull('deleted_at')
|
||||
->exists();
|
||||
|
||||
if (! $exists) {
|
||||
throw new BusinessException('部分记录不存在或未被删除', 404);
|
||||
}
|
||||
|
||||
return $this->transaction(function () use ($ids) {
|
||||
$result = $this->getModel()->newQuery()->withTrashed()
|
||||
->whereIn('id', $ids)->restore();
|
||||
|
||||
$this->logger()->info('批量恢复资源', ['ids' => $ids]);
|
||||
return $result > 0;
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* 永久删除(物理删除).
|
||||
* @throws BusinessException|Throwable
|
||||
*/
|
||||
public function forceDelete(int $id): bool
|
||||
{
|
||||
$model = $this->getModel();
|
||||
$resource = $model->newQuery()->withTrashed()->find($id);
|
||||
$this->checkResourceExists($resource, '要删除的资源不存在');
|
||||
|
||||
return $this->transaction(function () use ($id) {
|
||||
$result = $this->getModel()->newQuery()->withTrashed()
|
||||
->where('id', $id)->forceDelete();
|
||||
|
||||
$this->logger()->info('永久删除资源', ['id' => $id]);
|
||||
return $result;
|
||||
});
|
||||
}
|
||||
|
||||
protected function getSearch(): array
|
||||
{
|
||||
return $this->search;
|
||||
}
|
||||
|
||||
protected function getIgnoreSearchFields(): array
|
||||
{
|
||||
return $this->ignoreSearchFields;
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取当前服务对应的模型实例(由子类实现).
|
||||
*/
|
||||
abstract protected function getModel(): AetherModel;
|
||||
|
||||
/**
|
||||
* 获取当前服务对应的验证器实例(由子类实现).
|
||||
*/
|
||||
abstract protected function getValidator(): AetherValidator;
|
||||
|
||||
/**
|
||||
* 钩子方法:更新时的特殊逻辑(子类可重写).
|
||||
*/
|
||||
protected function handleUpdateSpecialLogic(int $id, array &$data): void
|
||||
{
|
||||
@@ -173,7 +280,7 @@ abstract class AetherCrudService extends AetherService implements AetherCrudInte
|
||||
}
|
||||
|
||||
/**
|
||||
* 钩子方法:删除前检查子级(子类可重写)
|
||||
* 钩子方法:删除前检查子级(子类可重写).
|
||||
*/
|
||||
protected function checkChildrenBeforeDelete(int $id): void
|
||||
{
|
||||
@@ -181,23 +288,9 @@ abstract class AetherCrudService extends AetherService implements AetherCrudInte
|
||||
}
|
||||
|
||||
/**
|
||||
* 根据模型的$search配置,自动应用搜索条件到查询构建器
|
||||
*/
|
||||
public function applySearch(Builder $query, array $params): void
|
||||
{
|
||||
foreach ($this->search as $field => $rule) {
|
||||
// 跳过未传递的参数
|
||||
if (!isset($params[$field])) {
|
||||
continue;
|
||||
}
|
||||
|
||||
$value = $params[$field];
|
||||
$this->applySearchRule($query, $field, $value, $rule);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 应用单个搜索规则
|
||||
* 应用单个搜索规则.
|
||||
* @param mixed $value
|
||||
* @param mixed $rule
|
||||
*/
|
||||
protected function applySearchRule(Builder $query, string $field, $value, $rule): void
|
||||
{
|
||||
@@ -221,98 +314,7 @@ abstract class AetherCrudService extends AetherService implements AetherCrudInte
|
||||
call_user_func($config['handler'], $query, $value);
|
||||
}
|
||||
break;
|
||||
// 可扩展其他类型:in、>、< 等
|
||||
// 可扩展其他类型:in、>、< 等
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 软删除恢复
|
||||
* @throws BusinessException|Throwable
|
||||
*/
|
||||
public function restore(int $id): bool
|
||||
{
|
||||
$model = $this->getModel();
|
||||
// 必须使用withTrashed()才能查询到已删除记录
|
||||
$resource = $model->newQuery()->withTrashed()->find($id);
|
||||
$this->checkResourceExists($resource, '恢复的资源不存在');
|
||||
|
||||
return $this->transaction(function () use ($id) {
|
||||
$result = $this->getModel()->newQuery()->withTrashed()
|
||||
->where('id', $id)->restore();
|
||||
|
||||
$this->logger()->info('恢复软删除资源', ['id' => $id]);
|
||||
return $result;
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* 批量软删除
|
||||
* @throws BusinessException|Throwable
|
||||
*/
|
||||
public function batchDelete(array $ids): bool
|
||||
{
|
||||
if (empty($ids)) {
|
||||
throw new BusinessException('请选择要删除的记录', 400);
|
||||
}
|
||||
|
||||
$model = $this->getModel();
|
||||
$exists = $model->whereIn('id', $ids)->exists();
|
||||
if (!$exists) {
|
||||
throw new BusinessException('部分记录不存在', 404);
|
||||
}
|
||||
|
||||
return $this->transaction(function () use ($ids) {
|
||||
$result = $this->getModel()->whereIn('id', $ids)->delete();
|
||||
$this->logger()->info('批量软删除资源', ['ids' => $ids]);
|
||||
return $result > 0;
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* 批量恢复软删除
|
||||
* @throws BusinessException|Throwable
|
||||
*/
|
||||
public function batchRestore(array $ids): bool
|
||||
{
|
||||
if (empty($ids)) {
|
||||
throw new BusinessException('请选择要恢复的记录', 400);
|
||||
}
|
||||
|
||||
$model = $this->getModel();
|
||||
$exists = $model->newQuery()->withTrashed()
|
||||
->whereIn('id', $ids)
|
||||
->whereNotNull('deleted_at')
|
||||
->exists();
|
||||
|
||||
if (!$exists) {
|
||||
throw new BusinessException('部分记录不存在或未被删除', 404);
|
||||
}
|
||||
|
||||
return $this->transaction(function () use ($ids) {
|
||||
$result = $this->getModel()->newQuery()->withTrashed()
|
||||
->whereIn('id', $ids)->restore();
|
||||
|
||||
$this->logger()->info('批量恢复资源', ['ids' => $ids]);
|
||||
return $result > 0;
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* 永久删除(物理删除)
|
||||
* @throws BusinessException|Throwable
|
||||
*/
|
||||
public function forceDelete(int $id): bool
|
||||
{
|
||||
$model = $this->getModel();
|
||||
$resource = $model->newQuery()->withTrashed()->find($id);
|
||||
$this->checkResourceExists($resource, '要删除的资源不存在');
|
||||
|
||||
return $this->transaction(function () use ($id) {
|
||||
$result = $this->getModel()->newQuery()->withTrashed()
|
||||
->where('id', $id)->forceDelete();
|
||||
|
||||
$this->logger()->info('永久删除资源', ['id' => $id]);
|
||||
return $result;
|
||||
});
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user