Files
hyperf_data/app/JsonRpc/Service/DataService.php
Aether 9052c7069f ..
2025-10-16 20:06:23 +08:00

194 lines
4.5 KiB
PHP

<?php
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;
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 DHD\Contract\DataServiceInterface;
#[RpcService(name: 'DataService', server: 'jsonrpc-http', protocol: 'jsonrpc-http', publishTo: 'nacos')]
class DataService implements DataServiceInterface
{
#[Inject]
protected Campus $campusModel;
#[Inject]
protected CampusValidator $campusValidator;
#[Inject]
protected Teacher $teacherModel;
#[Inject]
protected TeacherValidator $teacherValidator;
#[Inject]
protected CampusService $campusService;
#[Inject]
protected TeacherService $teacherService;
/**
* 获取校区列表.
*/
public function getCampuses(array $data): array
{
return $this->campusService->list($data);
}
/**
* 获取校区详情.
* @throws BusinessException
*/
public function getCampusBy(int $id): array
{
$campus = $this->campusModel->find($id);
if (! $campus || $campus->status != 1) {
throw new CampusNotFound(9999, 'Campus not found', ['id' => $id]);
}
return $campus->toArray();
}
/**
* 创建校区.
* @throws Exception
*/
public function createCampus(array $data): int
{
$this->campusValidator->scene('create', $data)->check();
$campus = $this->campusModel->create($data);
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 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();
}
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->teacherService->list($data);
}
/**
* 获取教师详情.
* @throws BusinessException
*/
public function getTeacherBy(int $id): array
{
$teacher = $this->teacherModel->find($id);
if (! $teacher || $teacher->status != 1) {
throw new TeacherNotFound();
}
return $teacher->toArray();
}
/**
* 创建教师.
* @throws Exception
*/
public function createTeacher(array $data): int
{
$this->teacherValidator->scene('create', $data)->check();
$teacher = $this->teacherModel->create($data);
return $teacher->id;
}
/**
* 更新教师.
* @throws Exception
*/
public function updateTeacher(int $id, array $data): int
{
$this->teacherValidator->scene('update', $data)->check();
return $this->teacherModel->updateById($id, $data);
}
/**
* 删除教师.
* @throws Exception
*/
public function deleteTeacher(int $id): bool
{
return $this->teacherModel->deleteById($id);
}
protected function getModel(): AetherModel
{
return $this->campusModel;
}
protected function getValidator(): AetherValidator
{
return $this->teacherValidator;
}
}