Files
hyperf_data/app/JsonRpc/Service/CampusService.php
2025-09-19 15:14:20 +08:00

64 lines
1.5 KiB
PHP

<?php
declare(strict_types=1);
namespace App\JsonRpc\Service;
use Aether\Exception\BusinessException;
use App\Model\Campus;
use Hyperf\RpcServer\Annotation\RpcService;
use MicroService\Contract\CampusServiceInterface;
#[RpcService(
name: 'DataCampus',
server: 'jsonrpc-http',
protocol: 'jsonrpc-http',
publishTo: 'nacos'
)]
class CampusService implements CampusServiceInterface
{
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();
}
}