基础数据-校区服务

This commit is contained in:
Aether
2025-09-19 15:14:20 +08:00
parent 824510d57c
commit b69f14ded2
16 changed files with 757 additions and 300 deletions

View File

@@ -0,0 +1,45 @@
<?php
declare(strict_types=1);
namespace Aether;
use Hyperf\Di\Annotation\Inject;
use Hyperf\HttpServer\Contract\RequestInterface;
use Hyperf\HttpServer\Contract\ResponseInterface;
abstract class AetherController
{
#[Inject]
protected RequestInterface $request;
#[Inject]
protected ResponseInterface $response;
/**
* 获取请求参数.
* @param string $key
* @param mixed|null $default
* @return mixed
*/
public function requestParam(string $key, mixed $default = null): mixed
{
return $this->request->input($key, $default);
}
/**
* 获取所有请求参数.
*/
public function requestParams(): array
{
return $this->request->all();
}
/**
* 返回JSON响应.
*/
public function json(array $data): \Psr\Http\Message\ResponseInterface
{
return $this->response->json($data);
}
}

View File

@@ -1,40 +1,33 @@
<?php
declare(strict_types=1);
namespace Aether;
interface AetherCrudInterface
{
/**
* 列表
* @return array
* 列表.
*/
public function list(): array;
/**
* 查询
* @param int $id
* @return object
* 查询.
*/
public function detail(int $id): object;
/**
* 新增
* @param array $data
* @return int
* 新增.
*/
public function create(array $data): int;
/**
* 更新
* @param int $id
* @param array $data
* @return bool
* 更新.
*/
public function update(int $id, array $data): bool;
/**
* 删除
* @param int $id
* @return bool
* 删除.
*/
public function delete(int $id): bool;
}
}

View File

@@ -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;
});
}
}
}

View File

@@ -0,0 +1,56 @@
<?php
declare(strict_types=1);
namespace Aether;
class AetherResponse
{
/**
* 成功响应.
* @param null|mixed $data 数据
* @param string $message 消息
*/
public static function success(mixed $data = null, string $message = '操作成功'): array
{
return [
'code' => 0,
'message' => $message,
'data' => $data,
'timestamp' => time(),
];
}
/**
* 错误响应.
* @param int $code 错误码
* @param string $message 错误消息
* @param null|mixed $data 附加数据
*/
public static function error(int $code, string $message = '', mixed $data = null): array
{
return [
'code' => $code,
'message' => $message ?: self::getDefaultMessage($code),
'data' => $data,
'timestamp' => time(),
];
}
/**
* 获取默认错误消息.
*/
private static function getDefaultMessage(int $code): string
{
$messages = [
400 => '请求参数错误',
401 => '未授权',
403 => '禁止访问',
404 => '资源不存在',
500 => '服务器内部错误',
10001 => '校区不存在',
];
return $messages[$code] ?? '未知错误';
}
}

View File

@@ -5,9 +5,9 @@ declare(strict_types=1);
namespace Aether;
use Aether\Exception\BusinessException;
use Hyperf\DbConnection\Db;
use Hyperf\Di\Annotation\Inject;
use Hyperf\Logger\LoggerFactory;
use Hyperf\DbConnection\Db;
use Psr\Log\LoggerInterface;
use Throwable;
@@ -17,18 +17,16 @@ abstract class AetherService
protected LoggerFactory $loggerFactory;
/**
* 获取当前服务日志器
* 获取当前服务日志器.
*/
protected function logger(): LoggerInterface
{
// return $this->loggerFactory->get(substr(strrchr(static::class, '\\'), 1));
// 提取类名如ExamTypeService作为日志通道名
$className = substr(strrchr(static::class, '\\'), 1);
return $this->loggerFactory->get($className);
}
/**
* 事务处理
* 事务处理.
* @throws Throwable
*/
protected function transaction(callable $callback)
@@ -37,7 +35,7 @@ abstract class AetherService
}
/**
* 检查资源是否存在(不存在则抛出异常)
* 检查资源是否存在(不存在则抛出异常).
* @throws BusinessException
*/
protected function checkResourceExists(?object $resource, string $message = '资源不存在'): void
@@ -46,4 +44,4 @@ abstract class AetherService
throw new BusinessException($message, BusinessException::RESOURCE_NOT_FOUND);
}
}
}
}

View File

@@ -5,45 +5,44 @@ declare(strict_types=1);
namespace Aether;
use Aether\Exception\ValidationFailedException;
use Hyperf\Context\ApplicationContext;
use Hyperf\Di\Annotation\Inject;
use Hyperf\Validation\Contract\ValidatorFactoryInterface;
use Hyperf\Validation\Validator;
use Hyperf\Context\ApplicationContext;
use RuntimeException;
abstract class AetherValidator
{
/**
* 当前场景名.
*/
public ?string $currentScene = null;
#[Inject]
protected ValidatorFactoryInterface $validationFactory;
/**
* 当前场景名
*/
public ?string $currentScene = null;
/**
* 待验证数据
* 待验证数据.
*/
protected array $data = [];
/**
* 自定义验证规则子类可通过该属性注册无需重写registerRules
* 格式:['规则名' => 闭包/类方法]
* 格式:['规则名' => 闭包/类方法].
*/
protected array $customRules = [];
/**
* 静态快捷验证方法(简化调用)
* 静态快捷验证方法(简化调用).
*/
public static function validate(string $scene, array $data = []): array
{
// return (new static())->scene($scene, $data)->check();
// 从容器中获取当前类的实例(确保依赖注入生效)
$instance = ApplicationContext::getContainer()->get(static::class);
return $instance->scene($scene, $data)->check();
}
/**
* 设置验证场景和数据(支持链式调用)
* 设置验证场景和数据(支持链式调用).
*/
public function scene(string $scene, array $data = []): self
{
@@ -53,17 +52,17 @@ abstract class AetherValidator
}
/**
* 执行验证(失败抛出异常)
* 执行验证(失败抛出异常).
*/
public function check(): array
{
if (empty($this->currentScene)) {
throw new \RuntimeException('请先设置验证场景');
throw new RuntimeException('请先设置验证场景');
}
$scenes = $this->scenes();
if (!isset($scenes[$this->currentScene])) {
throw new \RuntimeException("验证场景不存在:{$this->currentScene}");
if (! isset($scenes[$this->currentScene])) {
throw new RuntimeException("验证场景不存在:{$this->currentScene}");
}
$sceneConfig = $scenes[$this->currentScene];
@@ -76,7 +75,30 @@ abstract class AetherValidator
}
/**
* 实际执行验证的逻辑(重命名方法名更清晰)
* 格式化验证错误信息(统一格式,供异常处理器复用).
*/
public function formatValidationErrors(Validator $validator): array
{
$errors = [];
$failedRules = $validator->failed();
$errorMessages = $validator->errors()->getMessages();
$attributes = $validator->attributes();
foreach ($failedRules as $field => $rules) {
$errors[] = [
'field' => $field,
'field_label' => $attributes[$field] ?? $field,
'message' => $errorMessages[$field][0] ?? '',
'rules' => array_keys($rules),
'value' => $validator->getValue($field),
];
}
return $errors;
}
/**
* 实际执行验证的逻辑(重命名方法名更清晰).
*/
protected function validateData(array $data, array $rules, array $messages = [], array $attributes = []): array
{
@@ -95,30 +117,7 @@ abstract class AetherValidator
}
/**
* 格式化验证错误信息(统一格式,供异常处理器复用)
*/
public function formatValidationErrors(Validator $validator): array
{
$errors = [];
$failedRules = $validator->failed();
$errorMessages = $validator->errors()->getMessages();
$attributes = $validator->attributes();
foreach ($failedRules as $field => $rules) {
$errors[] = [
'field' => $field,
'field_label' => $attributes[$field] ?? $field,
'message' => $errorMessages[$field][0] ?? '',
'rules' => array_keys($rules),
'value' => $validator->getValue($field)
];
}
return $errors;
}
/**
* 自动注册自定义规则(优先使用$customRules属性
* 自动注册自定义规则(优先使用$customRules属性.
*/
protected function registerRules(Validator $validator): void
{
@@ -128,7 +127,7 @@ abstract class AetherValidator
}
/**
* 定义场景验证规则(子类实现)
* 定义场景验证规则(子类实现).
*/
abstract protected function scenes(): array;
}
}

View File

@@ -0,0 +1,37 @@
<?php
declare(strict_types=1);
namespace MicroService\Contract;
interface CampusServiceInterface
{
/**
* 获取校区详情.
* @param int $id 校区ID
*/
public function getCampusById(int $id): array;
/**
* 批量获取校区信息.
* @param array $ids 校区ID列表
*/
public function getCampusesByIds(array $ids): array;
/**
* 根据父ID获取子校区.
* @param int $parentId 父级ID
*/
public function getCampusesByParentId(int $parentId): array;
/**
* 获取省份列表.
*/
public function getProvinces(): array;
/**
* 根据省份获取城市列表.
* @param string $province 省份名称
*/
public function getCitiesByProvince(string $province): array;
}