This commit is contained in:
Aether
2025-09-29 10:15:17 +08:00
parent e7c5d7d098
commit 9a1c120c4f
7 changed files with 107 additions and 183 deletions

View File

@@ -8,6 +8,8 @@ use Aether\Exception\BusinessException;
use App\Model\Campus;
use App\Model\Teacher;
use App\Validator\CampusValidator;
use App\Validator\TeacherValidator;
use Exception;
use Hyperf\Di\Annotation\Inject;
use Hyperf\RpcServer\Annotation\RpcService;
use MicroService\Contract\DataServiceInterface;
@@ -21,12 +23,24 @@ class DataService implements DataServiceInterface
#[Inject]
protected CampusValidator $campusValidator;
#[Inject]
protected Teacher $teacherModel;
#[Inject]
protected TeacherValidator $teacherValidator;
/**
* 获取校区列表.
*/
public function getCampuses(array $data): array
{
// TODO list 方法存在问题
return $this->campusModel->list($data);
}
/**
* 获取校区详情.
* @throws BusinessException
*/
public function getCampusBy(int $id): array
{
$campus = $this->campusModel->find($id);
@@ -36,6 +50,10 @@ class DataService implements DataServiceInterface
return $campus->toArray();
}
/**
* 创建校区.
* @throws Exception
*/
public function createCampus(array $data): int
{
$this->campusValidator->scene('create', $data)->check();
@@ -43,54 +61,44 @@ class DataService implements DataServiceInterface
return $campus->id;
}
/**
* 更新校区.
* @throws Exception
*/
public function updateCampus(int $id, array $data): int
{
$this->campusValidator->scene('update', $data)->check();
return $this->campusModel->updateById($id, $data);
}
/**
* 删除校区.
* @throws Exception
*/
public function deleteCampus(int $id): bool
{
return $this->campusModel->deleteById($id);
}
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)
return $this->campusModel->level(1)
->enabled()
->orderBy('name')
->get(['id', 'name', 'province'])
->toArray();
}
/**
* 根据省份获取城市列表.
* @throws BusinessException
*/
public function getCitiesByProvince(string $province): array
{
return Campus::level(2)
return $this->campusModel->level(2)
->province($province)
->enabled()
->orderBy('name')
@@ -98,97 +106,54 @@ class DataService implements DataServiceInterface
->toArray();
}
public function getTeacherById(int $id): array
/**
* 获取教师列表.
*/
public function getTeachers(array $data): array
{
$teacher = Teacher::find($id);
return $this->teacherModel->list($data);
}
/**
* 获取教师详情.
* @throws BusinessException
*/
public function getTeacherBy(int $id): array
{
$teacher = $this->teacherModel->find($id);
if (! $teacher || $teacher->status != 1) {
throw new BusinessException('教师不存在或已禁用', 10101);
throw new BusinessException('教师不存在或已禁用', 10001);
}
return $teacher->toArray();
}
public function getTeachersByIds(array $ids): array
{
return Teacher::whereIn('id', $ids)
->enabled()
->orderBy('sort_order', 'asc')
->get()
->toArray();
}
public function getTeachersByCampusId(int $campusId, int $page = 1, int $size = 20): array
{
$query = Teacher::campusId($campusId)
->enabled()
->orderBy('sort_order', 'asc');
$total = $query->count();
$list = $query->forPage($page, $size)->get()->toArray();
return [
'total' => $total,
'page' => $page,
'size' => $size,
'list' => $list,
];
}
public function getTeachersBySubject(string $subject, int $page = 1, int $size = 20): array
{
$query = Teacher::subject($subject)
->enabled()
->orderBy('sort_order', 'asc');
$total = $query->count();
$list = $query->forPage($page, $size)->get()->toArray();
return [
'total' => $total,
'page' => $page,
'size' => $size,
'list' => $list,
];
}
public function searchTeachers(string $keyword, int $page = 1, int $size = 20): array
{
$query = Teacher::search($keyword)
->enabled()
->orderBy('sort_order', 'asc');
$total = $query->count();
$list = $query->forPage($page, $size)->get()->toArray();
return [
'total' => $total,
'page' => $page,
'size' => $size,
'list' => $list,
];
}
public function getTeachers(): array
{
// TODO: Implement getTeachers() method.
}
public function getTeacherBy(int $id): array
{
// TODO: Implement getTeacherBy() method.
}
/**
* 创建教师.
* @throws Exception
*/
public function createTeacher(array $data): int
{
// TODO: Implement createTeacher() method.
$this->teacherValidator->scene('create', $data)->check();
$teacher = $this->teacherModel->create($data);
return $teacher->id;
}
/**
* 更新教师.
* @throws Exception
*/
public function updateTeacher(int $id, array $data): int
{
// TODO: Implement updateTeacher() method.
$this->teacherValidator->scene('update', $data)->check();
return $this->teacherModel->updateById($id, $data);
}
/**
* 删除教师.
* @throws Exception
*/
public function deleteTeacher(int $id): bool
{
// TODO: Implement deleteTeacher() method.
return $this->teacherModel->deleteById($id);
}
}