195 lines
5.0 KiB
PHP
195 lines
5.0 KiB
PHP
<?php
|
|
|
|
declare(strict_types=1);
|
|
|
|
namespace App\JsonRpc\Service;
|
|
|
|
use Aether\Exception\BusinessException;
|
|
use App\Model\Campus;
|
|
use App\Model\Teacher;
|
|
use App\Validator\CampusValidator;
|
|
use Hyperf\Di\Annotation\Inject;
|
|
use Hyperf\RpcServer\Annotation\RpcService;
|
|
use MicroService\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;
|
|
|
|
public function getCampuses(array $data): array
|
|
{
|
|
// TODO list 方法存在问题
|
|
return $this->campusModel->list($data);
|
|
}
|
|
|
|
public function getCampusBy(int $id): array
|
|
{
|
|
$campus = $this->campusModel->find($id);
|
|
if (! $campus || $campus->status != 1) {
|
|
throw new BusinessException('校区不存在或已禁用', 10001);
|
|
}
|
|
return $campus->toArray();
|
|
}
|
|
|
|
public function createCampus(array $data): int
|
|
{
|
|
$this->campusValidator->scene('create', $data)->check();
|
|
$campus = $this->campusModel->create($data);
|
|
// $this->tran
|
|
return $campus->id;
|
|
}
|
|
|
|
public function updateCampus(int $id, array $data): int
|
|
{
|
|
// TODO: Implement updateCampus() method.
|
|
}
|
|
|
|
public function deleteCampus(int $id): bool
|
|
{
|
|
// TODO: Implement deleteCampus() method.
|
|
}
|
|
|
|
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)
|
|
->enabled()
|
|
->orderBy('name')
|
|
->get(['id', 'name', 'province'])
|
|
->toArray();
|
|
}
|
|
|
|
public function getCitiesByProvince(string $province): array
|
|
{
|
|
return Campus::level(2)
|
|
->province($province)
|
|
->enabled()
|
|
->orderBy('name')
|
|
->get(['id', 'name', 'city'])
|
|
->toArray();
|
|
}
|
|
|
|
public function getTeacherById(int $id): array
|
|
{
|
|
$teacher = Teacher::find($id);
|
|
if (! $teacher || $teacher->status != 1) {
|
|
throw new BusinessException('教师不存在或已禁用', 10101);
|
|
}
|
|
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.
|
|
}
|
|
|
|
public function createTeacher(array $data): int
|
|
{
|
|
// TODO: Implement createTeacher() method.
|
|
}
|
|
|
|
public function updateTeacher(int $id, array $data): int
|
|
{
|
|
// TODO: Implement updateTeacher() method.
|
|
}
|
|
|
|
public function deleteTeacher(int $id): bool
|
|
{
|
|
// TODO: Implement deleteTeacher() method.
|
|
}
|
|
}
|