基础数据-校区服务
This commit is contained in:
183
app/Controller/CampusController.php
Normal file
183
app/Controller/CampusController.php
Normal file
@@ -0,0 +1,183 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace App\Controller;
|
||||
|
||||
use Aether\AetherController;
|
||||
use Aether\AetherResponse;
|
||||
use App\Exception\BusinessException;
|
||||
use App\Model\Campus;
|
||||
use App\Validator\CampusValidator;
|
||||
use Exception;
|
||||
use Hyperf\Di\Annotation\Inject;
|
||||
use Hyperf\HttpServer\Contract\RequestInterface;
|
||||
|
||||
/**
|
||||
* @Controller(prefix="/api/v1/campus")
|
||||
*/
|
||||
class CampusController extends AetherController
|
||||
{
|
||||
#[Inject]
|
||||
protected CampusValidator $validator;
|
||||
|
||||
/**
|
||||
* 创建校区.
|
||||
* @PostMapping(path="")
|
||||
*/
|
||||
public function create(RequestInterface $request): array
|
||||
{
|
||||
$data = $request->all();
|
||||
$validator = $this->validator->validateCreate($data);
|
||||
if ($validator->fails()) {
|
||||
throw new BusinessException(400, $validator->errors()->first());
|
||||
}
|
||||
|
||||
$campus = new Campus();
|
||||
$campus->fill($data);
|
||||
$campus->save();
|
||||
|
||||
return AetherResponse::success($campus->toArray(), '校区创建成功');
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取校区详情.
|
||||
* @GetMapping(path="/{id}")
|
||||
*/
|
||||
public function get(int $id): array
|
||||
{
|
||||
$campus = Campus::find($id);
|
||||
if (! $campus) {
|
||||
throw new BusinessException(10001);
|
||||
}
|
||||
|
||||
return AetherResponse::success($campus->toArray());
|
||||
}
|
||||
|
||||
/**
|
||||
* 更新校区.
|
||||
* @PutMapping(path="/{id}")
|
||||
*/
|
||||
public function update(int $id, RequestInterface $request): array
|
||||
{
|
||||
$data = $request->all();
|
||||
$data['id'] = $id;
|
||||
|
||||
$validator = $this->validator->validateUpdate($data);
|
||||
if ($validator->fails()) {
|
||||
throw new BusinessException(400, $validator->errors()->first());
|
||||
}
|
||||
|
||||
$campus = Campus::find($id);
|
||||
if (! $campus) {
|
||||
throw new BusinessException(10001);
|
||||
}
|
||||
|
||||
$campus->fill($data);
|
||||
$campus->save();
|
||||
|
||||
return AetherResponse::success($campus->toArray(), '校区更新成功');
|
||||
}
|
||||
|
||||
/**
|
||||
* 删除校区.
|
||||
* @DeleteMapping(path="/{id}")
|
||||
* @throws Exception
|
||||
*/
|
||||
public function delete(int $id): array
|
||||
{
|
||||
$campus = Campus::find($id);
|
||||
if (! $campus) {
|
||||
throw new BusinessException(10001);
|
||||
}
|
||||
|
||||
$campus->delete();
|
||||
return AetherResponse::success(null, '校区删除成功');
|
||||
}
|
||||
|
||||
/**
|
||||
* 校区列表.
|
||||
* @GetMapping(path="/list")
|
||||
*/
|
||||
public function list(RequestInterface $request): array
|
||||
{
|
||||
$data = $request->all();
|
||||
$validator = $this->validator->validateQuery($data);
|
||||
if ($validator->fails()) {
|
||||
throw new BusinessException(400, $validator->errors()->first());
|
||||
}
|
||||
|
||||
$query = Campus::query();
|
||||
|
||||
if (! empty($data['id'])) {
|
||||
$query->where('id', $data['id']);
|
||||
}
|
||||
if (! empty($data['name'])) {
|
||||
$query->where('name', 'like', "%{$data['name']}%");
|
||||
}
|
||||
if (isset($data['parent_id'])) {
|
||||
$query->where('parent_id', $data['parent_id']);
|
||||
}
|
||||
if (isset($data['level'])) {
|
||||
$query->where('level', $data['level']);
|
||||
}
|
||||
if (! empty($data['province'])) {
|
||||
$query->where('province', $data['province']);
|
||||
}
|
||||
if (! empty($data['city'])) {
|
||||
$query->where('city', $data['city']);
|
||||
}
|
||||
if (isset($data['status'])) {
|
||||
$query->where('status', $data['status']);
|
||||
}
|
||||
|
||||
$page = $data['page'] ?? 1;
|
||||
$size = $data['size'] ?? 20;
|
||||
|
||||
$total = $query->count();
|
||||
$list = $query->forPage($page, $size)->get()->toArray();
|
||||
|
||||
return AetherResponse::success([
|
||||
'total' => $total,
|
||||
'page' => $page,
|
||||
'size' => $size,
|
||||
'list' => $list,
|
||||
]);
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取省份列表.
|
||||
* @GetMapping(path="/provinces")
|
||||
*/
|
||||
public function provinces(): array
|
||||
{
|
||||
$provinces = Campus::level(1)
|
||||
->enabled()
|
||||
->orderBy('name')
|
||||
->get(['id', 'name', 'province'])
|
||||
->toArray();
|
||||
|
||||
return AetherResponse::success($provinces);
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取城市列表.
|
||||
* @GetMapping(path="/cities")
|
||||
*/
|
||||
public function cities(RequestInterface $request): array
|
||||
{
|
||||
$province = $request->input('province');
|
||||
if (empty($province)) {
|
||||
throw new BusinessException(400, '省份不能为空');
|
||||
}
|
||||
|
||||
$cities = Campus::level(2)
|
||||
->province($province)
|
||||
->enabled()
|
||||
->orderBy('name')
|
||||
->get(['id', 'name', 'city'])
|
||||
->toArray();
|
||||
|
||||
return AetherResponse::success($cities);
|
||||
}
|
||||
}
|
||||
45
app/Exception/BusinessException.php
Normal file
45
app/Exception/BusinessException.php
Normal file
@@ -0,0 +1,45 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace App\Exception;
|
||||
|
||||
use Hyperf\Server\Exception\ServerException;
|
||||
use Throwable;
|
||||
|
||||
class BusinessException extends ServerException
|
||||
{
|
||||
/**
|
||||
* 业务异常.
|
||||
* @param int $code 错误码
|
||||
* @param string $message 错误消息
|
||||
*/
|
||||
public function __construct(int $code = 500, string $message = '', ?Throwable $previous = null)
|
||||
{
|
||||
if (empty($message)) {
|
||||
$message = $this->getDefaultMessage($code);
|
||||
}
|
||||
|
||||
parent::__construct($message, $code, $previous);
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取默认错误消息.
|
||||
*/
|
||||
private function getDefaultMessage(int $code): string
|
||||
{
|
||||
$messages = [
|
||||
400 => '请求参数错误',
|
||||
401 => '未授权',
|
||||
403 => '禁止访问',
|
||||
404 => '资源不存在',
|
||||
500 => '服务器内部错误',
|
||||
10001 => '校区不存在',
|
||||
10002 => '校区已存在',
|
||||
10003 => '父级校区不存在',
|
||||
10004 => '层级参数错误',
|
||||
];
|
||||
|
||||
return $messages[$code] ?? '业务处理失败';
|
||||
}
|
||||
}
|
||||
65
app/Exception/Handler/BusinessExceptionHandler.php
Normal file
65
app/Exception/Handler/BusinessExceptionHandler.php
Normal file
@@ -0,0 +1,65 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace App\Exception\Handler;
|
||||
|
||||
use Aether\AetherResponse;
|
||||
use App\Exception\BusinessException;
|
||||
use Hyperf\ExceptionHandler\ExceptionHandler;
|
||||
use Hyperf\HttpMessage\Stream\SwooleStream;
|
||||
use Psr\Http\Message\ResponseInterface;
|
||||
use Throwable;
|
||||
|
||||
class BusinessExceptionHandler extends ExceptionHandler
|
||||
{
|
||||
/**
|
||||
* 显式声明next属性,确保存在.
|
||||
*/
|
||||
protected ?self $next = null;
|
||||
|
||||
/**
|
||||
* 处理异常.
|
||||
*/
|
||||
public function handle(Throwable $throwable, ResponseInterface $response)
|
||||
{
|
||||
// 只处理BusinessException类型的异常
|
||||
if ($throwable instanceof BusinessException) {
|
||||
$result = AetherResponse::error(
|
||||
$throwable->getCode(),
|
||||
$throwable->getMessage() ?: '业务处理异常'
|
||||
);
|
||||
|
||||
$body = json_encode($result, JSON_UNESCAPED_UNICODE);
|
||||
|
||||
return $response
|
||||
->withHeader('Content-Type', 'application/json')
|
||||
->withStatus(200)
|
||||
->withBody(new SwooleStream((string) $body));
|
||||
}
|
||||
|
||||
// 安全处理下一个处理器
|
||||
if ($this->next !== null) {
|
||||
return $this->next->handle($throwable, $response);
|
||||
}
|
||||
|
||||
// 如果没有下一个处理器,直接返回原始响应
|
||||
return $response;
|
||||
}
|
||||
|
||||
/**
|
||||
* 判断是否需要处理该异常.
|
||||
*/
|
||||
public function isValid(Throwable $throwable): bool
|
||||
{
|
||||
return $throwable instanceof BusinessException;
|
||||
}
|
||||
|
||||
/**
|
||||
* 显式实现setNext方法.
|
||||
*/
|
||||
public function setNext(self $handler): void
|
||||
{
|
||||
$this->next = $handler;
|
||||
}
|
||||
}
|
||||
@@ -1,40 +0,0 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace App\JsonRpc\Contract;
|
||||
|
||||
interface CampusServiceInterface
|
||||
{
|
||||
/**
|
||||
* 获取单个校区详情.
|
||||
* @param int $id 校区ID
|
||||
*/
|
||||
public function getCampus(int $id): array;
|
||||
|
||||
/**
|
||||
* 批量获取校区信息.
|
||||
* @param array $ids 校区ID列表
|
||||
*/
|
||||
public function batchGetCampus(array $ids): array;
|
||||
|
||||
/**
|
||||
* 按省份获取校区列表.
|
||||
* @param string $province 省份名称
|
||||
*/
|
||||
public function getCampusByProvince(string $province): array;
|
||||
|
||||
/**
|
||||
* 按城市获取校区列表.
|
||||
* @param string $province 省份
|
||||
* @param string $city 城市
|
||||
*/
|
||||
public function getCampusByCity(string $province, string $city): array;
|
||||
|
||||
/**
|
||||
* 获取校区完整层级路径.
|
||||
* @param int $campusId 校区ID
|
||||
* @return array 如:[省份, 城市, 校区]
|
||||
*/
|
||||
public function getCampusHierarchy(int $campusId): array;
|
||||
}
|
||||
@@ -4,10 +4,10 @@ declare(strict_types=1);
|
||||
|
||||
namespace App\JsonRpc\Service;
|
||||
|
||||
use App\JsonRpc\Contract\CampusServiceInterface;
|
||||
use Aether\Exception\BusinessException;
|
||||
use App\Model\Campus;
|
||||
use Hyperf\RpcServer\Annotation\RpcService;
|
||||
use RuntimeException;
|
||||
use MicroService\Contract\CampusServiceInterface;
|
||||
|
||||
#[RpcService(
|
||||
name: 'DataCampus',
|
||||
@@ -17,67 +17,47 @@ use RuntimeException;
|
||||
)]
|
||||
class CampusService implements CampusServiceInterface
|
||||
{
|
||||
public function getCampus(int $id): array
|
||||
public function getCampusById(int $id): array
|
||||
{
|
||||
$campus = Campus::find($id);
|
||||
if (! $campus) {
|
||||
throw new RuntimeException("校区不存在: {$id}");
|
||||
if (! $campus || $campus->status != 1) {
|
||||
throw new BusinessException('校区不存在或已禁用', 10001);
|
||||
}
|
||||
return $campus->toArray();
|
||||
}
|
||||
|
||||
public function batchGetCampus(array $ids): array
|
||||
public function getCampusesByIds(array $ids): array
|
||||
{
|
||||
if (empty($ids)) {
|
||||
return [];
|
||||
}
|
||||
$campuses = Campus::whereIn('id', $ids)->get()->toArray();
|
||||
return array_column($campuses, null, 'id');
|
||||
}
|
||||
|
||||
public function getCampusByProvince(string $province): array
|
||||
{
|
||||
return Campus::province($province)
|
||||
->level(3)
|
||||
return Campus::whereIn('id', $ids)
|
||||
->enabled()
|
||||
->get()
|
||||
->toArray();
|
||||
}
|
||||
|
||||
public function getCampusByCity(string $province, string $city): array
|
||||
public function getCampusesByParentId(int $parentId): array
|
||||
{
|
||||
return Campus::province($province)
|
||||
->city($city)
|
||||
->level(3)
|
||||
return Campus::where('parent_id', $parentId)
|
||||
->enabled()
|
||||
->get()
|
||||
->toArray();
|
||||
}
|
||||
|
||||
public function getCampusHierarchy(int $campusId): array
|
||||
public function getProvinces(): array
|
||||
{
|
||||
$hierarchy = [];
|
||||
$current = Campus::find($campusId);
|
||||
return Campus::level(1)
|
||||
->enabled()
|
||||
->orderBy('name')
|
||||
->get(['id', 'name', 'province'])
|
||||
->toArray();
|
||||
}
|
||||
|
||||
if (! $current) {
|
||||
return $hierarchy;
|
||||
}
|
||||
|
||||
// 从校区向上追溯层级
|
||||
while ($current) {
|
||||
array_unshift($hierarchy, [
|
||||
'id' => $current->id,
|
||||
'name' => $current->name,
|
||||
'level' => $current->level,
|
||||
'province' => $current->province,
|
||||
'city' => $current->city,
|
||||
]);
|
||||
|
||||
if ($current->parent_id === 0) {
|
||||
break;
|
||||
}
|
||||
|
||||
$current = Campus::find($current->parent_id);
|
||||
}
|
||||
|
||||
return $hierarchy;
|
||||
public function getCitiesByProvince(string $province): array
|
||||
{
|
||||
return Campus::level(2)
|
||||
->province($province)
|
||||
->enabled()
|
||||
->orderBy('name')
|
||||
->get(['id', 'name', 'city'])
|
||||
->toArray();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -7,6 +7,7 @@ namespace App\Model;
|
||||
use Aether\AetherModel;
|
||||
use Carbon\Carbon;
|
||||
use Hyperf\Database\Model\Builder;
|
||||
use Hyperf\Database\Model\Relations\BelongsTo;
|
||||
use Hyperf\Database\Model\Relations\HasMany;
|
||||
|
||||
/**
|
||||
@@ -41,11 +42,26 @@ class Campus extends AetherModel
|
||||
];
|
||||
|
||||
protected array $casts = [
|
||||
'id' => 'integer',
|
||||
'parent_id' => 'integer',
|
||||
'level' => 'integer',
|
||||
'status' => 'integer',
|
||||
'created_at' => 'datetime',
|
||||
'updated_at' => 'datetime',
|
||||
'deleted_at' => 'datetime',
|
||||
];
|
||||
|
||||
/**
|
||||
* 获取父级校区.
|
||||
* @return BelongsTo
|
||||
*/
|
||||
public function parent(): BelongsTo
|
||||
{
|
||||
return $this->belongsTo(Campus::class, 'parent_id', 'id')
|
||||
->where('status', 1)
|
||||
->whereNull('deleted_at');
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取子校区.
|
||||
*/
|
||||
@@ -77,4 +93,12 @@ class Campus extends AetherModel
|
||||
{
|
||||
return $query->where('level', $level);
|
||||
}
|
||||
|
||||
/**
|
||||
* 查询启用的校区.
|
||||
*/
|
||||
public function scopeEnabled(Builder $query): Builder
|
||||
{
|
||||
return $query->where('status', 1);
|
||||
}
|
||||
}
|
||||
|
||||
69
app/Validator/CampusValidator.php
Normal file
69
app/Validator/CampusValidator.php
Normal file
@@ -0,0 +1,69 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace App\Validator;
|
||||
|
||||
use Hyperf\Validation\Contract\ValidatorFactoryInterface;
|
||||
use Hyperf\Validation\Validator;
|
||||
|
||||
class CampusValidator
|
||||
{
|
||||
public function __construct(protected ValidatorFactoryInterface $validationFactory)
|
||||
{
|
||||
}
|
||||
|
||||
public function validateCreate(array $data): Validator
|
||||
{
|
||||
return $this->validationFactory->make($data, [
|
||||
'name' => 'required|string|max:255',
|
||||
'parent_id' => 'required|integer|min:0',
|
||||
'level' => 'required|integer|in:1,2,3',
|
||||
'province' => 'nullable|string|max:100',
|
||||
'city' => 'nullable|string|max:100',
|
||||
'address' => 'nullable|string|max:500',
|
||||
'contact_phone' => 'nullable|string|max:20',
|
||||
'contact_person' => 'nullable|string|max:20',
|
||||
'status' => 'nullable|integer|in:0,1',
|
||||
], [
|
||||
'name.required' => '校区名称不能为空',
|
||||
'parent_id.required' => '父级ID不能为空',
|
||||
'level.required' => '层级不能为空',
|
||||
'level.in' => '层级只能是1、2、3',
|
||||
]);
|
||||
}
|
||||
|
||||
public function validateUpdate(array $data): Validator
|
||||
{
|
||||
return $this->validationFactory->make($data, [
|
||||
'id' => 'required|integer|min:1',
|
||||
'name' => 'nullable|string|max:255',
|
||||
'parent_id' => 'nullable|integer|min:0',
|
||||
'level' => 'nullable|integer|in:1,2,3',
|
||||
'province' => 'nullable|string|max:100',
|
||||
'city' => 'nullable|string|max:100',
|
||||
'address' => 'nullable|string|max:500',
|
||||
'contact_phone' => 'nullable|string|max:20',
|
||||
'contact_person' => 'nullable|string|max:20',
|
||||
'status' => 'nullable|integer|in:0,1',
|
||||
], [
|
||||
'id.required' => '校区ID不能为空',
|
||||
'level.in' => '层级只能是1、2、3',
|
||||
]);
|
||||
}
|
||||
|
||||
public function validateQuery(array $data): Validator
|
||||
{
|
||||
return $this->validationFactory->make($data, [
|
||||
'id' => 'nullable|integer|min:1',
|
||||
'name' => 'nullable|string|max:255',
|
||||
'parent_id' => 'nullable|integer|min:0',
|
||||
'level' => 'nullable|integer|in:1,2,3',
|
||||
'province' => 'nullable|string|max:100',
|
||||
'city' => 'nullable|string|max:100',
|
||||
'status' => 'nullable|integer|in:0,1',
|
||||
'page' => 'nullable|integer|min:1',
|
||||
'size' => 'nullable|integer|min:1|max:100',
|
||||
]);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user