This commit is contained in:
阿不叮咚
2025-09-24 16:37:22 +08:00
parent 998f8d8356
commit 1f6b60ee0f
8 changed files with 217 additions and 360 deletions

View File

@@ -5,11 +5,9 @@ declare(strict_types=1);
namespace App\Controller;
use Aether\AetherController;
use Aether\AetherCrudService;
use Aether\AetherResponse;
use App\Exception\BusinessException;
use Aether\Campus;
use Aether\CampusValidator;
use Exception;
use App\JsonRpc\Service\CampusService;
use Hyperf\Di\Annotation\Inject;
use Hyperf\HttpServer\Annotation\Controller;
use Hyperf\HttpServer\Annotation\DeleteMapping;
@@ -17,61 +15,23 @@ 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 CampusValidator $validator;
protected CampusService $service;
#[Inject]
protected RequestInterface $request;
/**
* 校区列表.
*/
#[GetMapping(path: 'campus/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,
]);
return AetherResponse::success($this->service->list($request->all()));
}
/**
@@ -80,13 +40,7 @@ class CampusController extends AetherController
#[GetMapping(path: 'campus/provinces')]
public function provinces(): array
{
$provinces = Campus::level(1)
->enabled()
->orderBy('name')
->get(['id', 'name', 'province'])
->toArray();
return AetherResponse::success($provinces);
return AetherResponse::success($this->service->getProvinces());
}
/**
@@ -95,38 +49,17 @@ class CampusController extends AetherController
#[GetMapping(path: 'campus/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);
return AetherResponse::success($this->service->getCitiesByProvince($request->input('province_id')));
}
/**
* 创建校区.
* @throws Throwable
*/
#[PostMapping(path: 'campus')]
public function create(RequestInterface $request): array
public function create(): 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(), '校区创建成功');
return AetherResponse::success($this->service->create($this->request->all()), '校区创建成功');
}
/**
@@ -135,52 +68,31 @@ class CampusController extends AetherController
#[GetMapping(path: 'campus/{id}')]
public function get(int $id): array
{
$campus = Campus::find($id);
if (! $campus) {
throw new BusinessException(10001);
}
return AetherResponse::success($campus->toArray());
return AetherResponse::success($this->service->detail($id));
}
/**
* 更新校区.
* @throws Throwable
*/
#[PutMapping(path: 'campus/{id}')]
public function update(int $id, RequestInterface $request): array
public function update(int $id): 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(), '校区更新成功');
return AetherResponse::success($this->service->update($id, $this->request->all()), '校区更新成功');
}
/**
* 删除校区.
* @throws Exception
* @throws Throwable
*/
#[DeleteMapping(path: 'campus/{id}')]
public function delete(int $id): array
{
$campus = Campus::find($id);
if (! $campus) {
throw new BusinessException(10001);
}
return AetherResponse::success($this->service->delete($id), '校区删除成功');
}
$campus->delete();
return AetherResponse::success(null, '校区删除成功');
protected function getService(): AetherCrudService
{
return $this->service;
}
}