校区服务
This commit is contained in:
40
app/JsonRpc/Contract/CampusServiceInterface.php
Normal file
40
app/JsonRpc/Contract/CampusServiceInterface.php
Normal file
@@ -0,0 +1,40 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace App\JsonRpc\Contract;
|
||||
|
||||
interface CampusServiceInterface
|
||||
{
|
||||
/**
|
||||
* 获取单个校区详情.
|
||||
* @param int $id 校区ID
|
||||
*/
|
||||
public function getCampus(int $id): array;
|
||||
|
||||
/**
|
||||
* 批量获取校区信息.
|
||||
* @param array $ids 校区ID列表
|
||||
*/
|
||||
public function batchGetCampus(array $ids): array;
|
||||
|
||||
/**
|
||||
* 按省份获取校区列表.
|
||||
* @param string $province 省份名称
|
||||
*/
|
||||
public function getCampusByProvince(string $province): array;
|
||||
|
||||
/**
|
||||
* 按城市获取校区列表.
|
||||
* @param string $province 省份
|
||||
* @param string $city 城市
|
||||
*/
|
||||
public function getCampusByCity(string $province, string $city): array;
|
||||
|
||||
/**
|
||||
* 获取校区完整层级路径.
|
||||
* @param int $campusId 校区ID
|
||||
* @return array 如:[省份, 城市, 校区]
|
||||
*/
|
||||
public function getCampusHierarchy(int $campusId): array;
|
||||
}
|
||||
83
app/JsonRpc/Service/CampusService.php
Normal file
83
app/JsonRpc/Service/CampusService.php
Normal file
@@ -0,0 +1,83 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace App\JsonRpc\Service;
|
||||
|
||||
use App\JsonRpc\Contract\CampusServiceInterface;
|
||||
use App\Model\Campus;
|
||||
use Hyperf\RpcServer\Annotation\RpcService;
|
||||
use RuntimeException;
|
||||
|
||||
#[RpcService(
|
||||
name: 'DataCampus',
|
||||
server: 'jsonrpc-http',
|
||||
protocol: 'jsonrpc-http',
|
||||
publishTo: 'nacos'
|
||||
)]
|
||||
class CampusService implements CampusServiceInterface
|
||||
{
|
||||
public function getCampus(int $id): array
|
||||
{
|
||||
$campus = Campus::find($id);
|
||||
if (! $campus) {
|
||||
throw new RuntimeException("校区不存在: {$id}");
|
||||
}
|
||||
return $campus->toArray();
|
||||
}
|
||||
|
||||
public function batchGetCampus(array $ids): array
|
||||
{
|
||||
if (empty($ids)) {
|
||||
return [];
|
||||
}
|
||||
$campuses = Campus::whereIn('id', $ids)->get()->toArray();
|
||||
return array_column($campuses, null, 'id');
|
||||
}
|
||||
|
||||
public function getCampusByProvince(string $province): array
|
||||
{
|
||||
return Campus::province($province)
|
||||
->level(3)
|
||||
->get()
|
||||
->toArray();
|
||||
}
|
||||
|
||||
public function getCampusByCity(string $province, string $city): array
|
||||
{
|
||||
return Campus::province($province)
|
||||
->city($city)
|
||||
->level(3)
|
||||
->get()
|
||||
->toArray();
|
||||
}
|
||||
|
||||
public function getCampusHierarchy(int $campusId): array
|
||||
{
|
||||
$hierarchy = [];
|
||||
$current = Campus::find($campusId);
|
||||
|
||||
if (! $current) {
|
||||
return $hierarchy;
|
||||
}
|
||||
|
||||
// 从校区向上追溯层级
|
||||
while ($current) {
|
||||
array_unshift($hierarchy, [
|
||||
'id' => $current->id,
|
||||
'name' => $current->name,
|
||||
'level' => $current->level,
|
||||
'province' => $current->province,
|
||||
'city' => $current->city,
|
||||
]);
|
||||
|
||||
if ($current->parent_id === 0) {
|
||||
break;
|
||||
}
|
||||
|
||||
$current = Campus::find($current->parent_id);
|
||||
}
|
||||
|
||||
return $hierarchy;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user