This commit is contained in:
Aether
2025-10-16 20:06:23 +08:00
parent 6ab9bd6198
commit 9052c7069f
8 changed files with 153 additions and 20 deletions

View File

@@ -0,0 +1,58 @@
<?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;
class CampusService extends AetherCrudService
{
#[Inject]
protected Campus $campusModel;
#[Inject]
protected CampusValidator $campusValidator;
/**
* 获取省份列表.
*/
public function getProvinces(): array
{
return $this->campusModel->level(1)
->enabled()
->orderBy('name')
->get(['id', 'name', 'province'])
->toArray();
}
/**
* 根据省份获取城市列表.
* @throws BusinessException
*/
public function getCitiesByProvince(string $province): array
{
return $this->campusModel->level(2)
->province($province)
->enabled()
->orderBy('name')
->get(['id', 'name', 'city'])
->toArray();
}
protected function getModel(): AetherModel
{
return $this->campusModel;
}
protected function getValidator(): AetherValidator
{
return $this->campusValidator;
}
}

View File

@@ -4,6 +4,9 @@ declare(strict_types=1);
namespace App\JsonRpc\Service;
use Aether\AetherCrudService;
use Aether\AetherModel;
use Aether\AetherValidator;
use Aether\Exception\BusinessException;
use App\Exception\CampusNotFound;
use App\Exception\TeacherNotFound;
@@ -31,12 +34,18 @@ class DataService implements DataServiceInterface
#[Inject]
protected TeacherValidator $teacherValidator;
#[Inject]
protected CampusService $campusService;
#[Inject]
protected TeacherService $teacherService;
/**
* 获取校区列表.
*/
public function getCampuses(array $data): array
{
return $this->campusModel->list($data);
return $this->campusService->list($data);
}
/**
@@ -108,12 +117,25 @@ class DataService implements DataServiceInterface
->toArray();
}
public function getCampusByIds(array $ids): array
{
if (empty($ids)) {
return [];
}
return $this->campusModel
->whereIn('id', $ids)
->where('status', 1)
->get()
->keyBy('id')
->toArray();
}
/**
* 获取教师列表.
*/
public function getTeachers(array $data): array
{
return $this->teacherModel->list($data);
return $this->teacherService->list($data);
}
/**
@@ -158,4 +180,14 @@ class DataService implements DataServiceInterface
{
return $this->teacherModel->deleteById($id);
}
protected function getModel(): AetherModel
{
return $this->campusModel;
}
protected function getValidator(): AetherValidator
{
return $this->teacherValidator;
}
}

View File

@@ -0,0 +1,28 @@
<?php
namespace App\JsonRpc\Service;
use Aether\AetherCrudService;
use Aether\AetherModel;
use Aether\AetherValidator;
use App\Model\Teacher;
use App\Validator\TeacherValidator;
use Hyperf\Di\Annotation\Inject;
class TeacherService extends AetherCrudService
{
#[Inject]
protected Teacher $teacherModel;
#[Inject]
protected TeacherValidator $teacherValidator;
protected function getModel(): AetherModel
{
return $this->teacherModel;
}
protected function getValidator(): AetherValidator
{
return $this->teacherValidator;
}
}

View File

@@ -5,6 +5,8 @@ declare(strict_types=1);
namespace App\Model;
use Aether\AetherModel;
use Aether\Contract\TreeableInterface;
use Aether\Traits\AetherTree;
use Carbon\Carbon;
use Hyperf\Database\Model\Builder;
use Hyperf\Database\Model\Relations\BelongsTo;
@@ -26,8 +28,9 @@ use Hyperf\Database\Model\Relations\HasMany;
* @property Carbon $updated_at
* @property Carbon $deleted_at
*/
class Campus extends AetherModel
class Campus extends AetherModel implements TreeableInterface
{
use AetherTree;
protected ?string $table = 'da_campus';
protected array $fillable = [
@@ -52,10 +55,12 @@ class Campus extends AetherModel
];
protected array $search = [
'name' => 'like',
'level' => '=',
'parent_id' => '=',
'city' => 'like',
'province' => 'like',
'status' => '=',
];
protected array|bool|string $sortable = false;
@@ -119,4 +124,14 @@ class Campus extends AetherModel
->where('status', 1)
->whereNull('deleted_at');
}
protected function getParentIdField(): string
{
return 'parent_id';
}
protected function getSortField(): string
{
return '';
}
}

View File

@@ -22,7 +22,7 @@ use Hyperf\Database\Model\Relations\BelongsTo;
* @property string $introduction 教师简介
* @property int $campus_id 所属校区ID
* @property int $status 状态0-禁用1-启用
* @property int $sort_order 排序
* @property int $sort 排序
* @property Carbon $created_at
* @property Carbon $updated_at
* @property Carbon $deleted_at

View File

@@ -32,7 +32,7 @@ class CampusValidator extends AetherValidator
],
'update' => [
'rules' => [
'id' => 'required|integer|min:1',
// 'id' => 'required|integer|min:1',
'name' => 'required|string|max:255',
'parent_id' => 'nullable|integer|min:0',
'level' => 'nullable|integer|in:1,2,3',
@@ -41,7 +41,7 @@ class CampusValidator extends AetherValidator
'address' => 'nullable|string|max:500',
],
'messages' => [
'id.required' => '校区ID不能为空',
// 'id.required' => '校区ID不能为空',
'name.required' => '校区名称不能为空',
'level.in' => '层级只能是1、2、3',
],

View File

@@ -32,7 +32,7 @@ class TeacherValidator extends AetherValidator
],
'update' => [
'rules' => [
'id' => 'required|integer|min:1',
// 'id' => 'required|integer|min:1',
'name' => 'nullable|string|max:50',
'age' => 'nullable|integer|min:18|max:65',
'gender' => 'nullable|integer|in:1,2',
@@ -41,7 +41,7 @@ class TeacherValidator extends AetherValidator
'teach_subject' => 'nullable|string|max:100',
],
'messages' => [
'id.required' => '教师ID不能为空',
// 'id.required' => '教师ID不能为空',
'age.min' => '年龄不能小于18岁',
'age.max' => '年龄不能大于65岁',
'gender.in' => '性别只能是1(男)或2(女)',