This commit is contained in:
阿不叮咚
2025-09-24 13:39:23 +08:00
parent 5f1676d329
commit ae5b2b5af2
5 changed files with 10 additions and 9 deletions

View File

@@ -0,0 +1,186 @@
<?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\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;
#[Controller(prefix: '/api/v1')]
class CampusController extends AetherController
{
#[Inject]
protected CampusValidator $validator;
/**
* 校区列表.
*/
#[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,
]);
}
/**
* 获取省份列表.
*/
#[GetMapping(path: 'campus/provinces')]
public function provinces(): array
{
$provinces = Campus::level(1)
->enabled()
->orderBy('name')
->get(['id', 'name', 'province'])
->toArray();
return AetherResponse::success($provinces);
}
/**
* 获取城市列表.
*/
#[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);
}
/**
* 创建校区.
*/
#[PostMapping(path: 'campus')]
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: 'campus/{id}')]
public function get(int $id): array
{
$campus = Campus::find($id);
if (! $campus) {
throw new BusinessException(10001);
}
return AetherResponse::success($campus->toArray());
}
/**
* 更新校区.
*/
#[PutMapping(path: 'campus/{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(), '校区更新成功');
}
/**
* 删除校区.
* @throws Exception
*/
#[DeleteMapping(path: 'campus/{id}')]
public function delete(int $id): array
{
$campus = Campus::find($id);
if (! $campus) {
throw new BusinessException(10001);
}
$campus->delete();
return AetherResponse::success(null, '校区删除成功');
}
}