This commit is contained in:
Aether
2025-09-26 10:41:43 +08:00
parent 26ee79b951
commit 0c7142ad46
14 changed files with 157 additions and 427 deletions

View File

@@ -1,98 +0,0 @@
<?php
declare(strict_types=1);
namespace App\Controller;
use Aether\AetherController;
use Aether\AetherCrudService;
use Aether\AetherResponse;
use App\JsonRpc\Service\CampusService;
use Hyperf\Di\Annotation\Inject;
use Hyperf\HttpServer\Annotation\Controller;
use Hyperf\HttpServer\Annotation\DeleteMapping;
use Hyperf\HttpServer\Annotation\GetMapping;
use Hyperf\HttpServer\Annotation\PostMapping;
use Hyperf\HttpServer\Annotation\PutMapping;
use Hyperf\HttpServer\Contract\RequestInterface;
use Throwable;
#[Controller(prefix: '/api/v1')]
class CampusController extends AetherController
{
#[Inject]
protected CampusService $service;
#[Inject]
protected RequestInterface $request;
/**
* 校区列表.
*/
#[GetMapping(path: 'campus/list')]
public function list(RequestInterface $request): array
{
return AetherResponse::success($this->service->list($request->all()));
}
/**
* 获取省份列表.
*/
#[GetMapping(path: 'campus/provinces')]
public function provinces(): array
{
return AetherResponse::success($this->service->getProvinces());
}
/**
* 获取城市列表.
*/
#[GetMapping(path: 'campus/cities')]
public function cities(RequestInterface $request): array
{
return AetherResponse::success($this->service->getCitiesByProvince($request->input('province_id')));
}
/**
* 创建校区.
* @throws Throwable
*/
#[PostMapping(path: 'campus')]
public function create(): array
{
return AetherResponse::success($this->service->create($this->request->all()), '校区创建成功');
}
/**
* 获取校区详情.
*/
#[GetMapping(path: 'campus/{id}')]
public function get(int $id): array
{
return AetherResponse::success($this->service->detail($id));
}
/**
* 更新校区.
* @throws Throwable
*/
#[PutMapping(path: 'campus/{id}')]
public function update(int $id): array
{
return AetherResponse::success($this->service->update($id, $this->request->all()), '校区更新成功');
}
/**
* 删除校区.
* @throws Throwable
*/
#[DeleteMapping(path: 'campus/{id}')]
public function delete(int $id): array
{
return AetherResponse::success($this->service->delete($id), '校区删除成功');
}
protected function getService(): AetherCrudService
{
return $this->service;
}
}

View File

@@ -1,81 +0,0 @@
<?php
declare(strict_types=1);
namespace App\Controller;
use Aether\AetherController;
use Aether\AetherCrudService;
use Aether\AetherResponse;
use App\JsonRpc\Service\TeacherService;
use Hyperf\Di\Annotation\Inject;
use Hyperf\HttpServer\Annotation\Controller;
use Hyperf\HttpServer\Annotation\DeleteMapping;
use Hyperf\HttpServer\Annotation\GetMapping;
use Hyperf\HttpServer\Annotation\PostMapping;
use Hyperf\HttpServer\Annotation\PutMapping;
use Hyperf\HttpServer\Contract\RequestInterface;
use Throwable;
#[Controller(prefix: '/api/v1')]
class TeacherController extends AetherController
{
#[Inject]
protected TeacherService $service;
#[Inject]
protected RequestInterface $request;
/**
* 教师列表.
*/
#[GetMapping(path: 'teacher/list')]
public function list(RequestInterface $request): array
{
return AetherResponse::success($this->service->list($request->all()));
}
/**
* 创建教师.
* @throws Throwable
*/
#[PostMapping(path: 'teacher')]
public function create(): array
{
return AetherResponse::success($this->service->create($this->request->all()), '教师创建成功');
}
/**
* 获取教师详情.
*/
#[GetMapping(path: 'teacher/{id}')]
public function get(int $id): array
{
return AetherResponse::success($this->service->detail($id));
}
/**
* 更新教师.
* @throws Throwable
*/
#[PutMapping(path: 'teacher/{id}')]
public function update(int $id): array
{
return AetherResponse::success($this->service->update($id, $this->request->all()), '教师更新成功');
}
/**
* 删除教师.
* @throws Throwable
*/
#[DeleteMapping(path: 'teacher/{id}')]
public function delete(int $id): array
{
return AetherResponse::success($this->service->delete($id), '教师删除成功');
}
protected function getService(): AetherCrudService
{
return $this->service;
}
}

View File

@@ -1,84 +0,0 @@
<?php
declare(strict_types=1);
namespace App\JsonRpc\Service;
use Aether\AetherCrudService;
use Aether\AetherModel;
use Aether\AetherValidator;
use Aether\Exception\BusinessException;
use App\Model\Campus;
use App\Validator\CampusValidator;
use Hyperf\Di\Annotation\Inject;
use Hyperf\RpcServer\Annotation\RpcService;
use MicroService\Contract\CampusServiceInterface;
#[RpcService(
name: 'CampusService',
server: 'jsonrpc-http',
protocol: 'jsonrpc-http',
publishTo: 'nacos'
)]
class CampusService extends AetherCrudService implements CampusServiceInterface
{
#[Inject]
protected Campus $model;
#[Inject]
protected CampusValidator $validator;
public function getCampusById(int $id): array
{
$campus = Campus::find($id);
if (! $campus || $campus->status != 1) {
throw new BusinessException('校区不存在或已禁用', 10001);
}
return $campus->toArray();
}
public function getCampusesByIds(array $ids): array
{
return Campus::whereIn('id', $ids)
->enabled()
->get()
->toArray();
}
public function getCampusesByParentId(int $parentId): array
{
return Campus::where('parent_id', $parentId)
->enabled()
->get()
->toArray();
}
public function getProvinces(): array
{
return Campus::level(1)
->enabled()
->orderBy('name')
->get(['id', 'name', 'province'])
->toArray();
}
public function getCitiesByProvince(string $province): array
{
return Campus::level(2)
->province($province)
->enabled()
->orderBy('name')
->get(['id', 'name', 'city'])
->toArray();
}
protected function getModel(): AetherModel
{
return $this->model;
}
protected function getValidator(): AetherValidator
{
return $this->validator;
}
}

View File

@@ -4,29 +4,66 @@ declare(strict_types=1);
namespace App\JsonRpc\Service;
use Aether\AetherCrudService;
use Aether\AetherModel;
use Aether\AetherValidator;
use Aether\Exception\BusinessException;
use App\Model\Campus;
use App\Model\Teacher;
use App\Validator\TeacherValidator;
use App\Validator\CampusValidator;
use Hyperf\Di\Annotation\Inject;
use Hyperf\RpcServer\Annotation\RpcService;
use MicroService\Contract\TeacherServiceInterface;
use MicroService\Contract\DataServiceInterface;
#[RpcService(
name: 'DataTeacher',
server: 'jsonrpc-http',
protocol: 'jsonrpc-http',
publishTo: 'nacos'
)]
class TeacherService extends AetherCrudService implements TeacherServiceInterface
#[RpcService(name: 'DataService', server: 'jsonrpc-http', protocol: 'jsonrpc-http', publishTo: 'nacos')]
class DataService implements DataServiceInterface
{
#[Inject]
protected Teacher $model;
protected Campus $campusModel;
#[Inject]
protected TeacherValidator $validator;
protected CampusValidator $campusValidator;
public function getCampusById(int $id): array
{
$campus = Campus::find($id);
if (! $campus || $campus->status != 1) {
throw new BusinessException('校区不存在或已禁用', 10001);
}
return $campus->toArray();
}
public function getCampusesByIds(array $ids): array
{
return Campus::whereIn('id', $ids)
->enabled()
->get()
->toArray();
}
public function getCampusesByParentId(int $parentId): array
{
return Campus::where('parent_id', $parentId)
->enabled()
->get()
->toArray();
}
public function getProvinces(): array
{
return Campus::level(1)
->enabled()
->orderBy('name')
->get(['id', 'name', 'province'])
->toArray();
}
public function getCitiesByProvince(string $province): array
{
return Campus::level(2)
->province($province)
->enabled()
->orderBy('name')
->get(['id', 'name', 'city'])
->toArray();
}
public function getTeacherById(int $id): array
{
@@ -96,14 +133,4 @@ class TeacherService extends AetherCrudService implements TeacherServiceInterfac
'list' => $list,
];
}
protected function getModel(): AetherModel
{
return $this->model;
}
protected function getValidator(): AetherValidator
{
return $this->validator;
}
}