调整数据库表的单复数
This commit is contained in:
@@ -12,27 +12,18 @@ class CityService
|
||||
{
|
||||
$query = City::query();
|
||||
|
||||
if (!empty($params['parent_id'])) {
|
||||
$query->where('parent_id', $params['parent_id']);
|
||||
}
|
||||
|
||||
if (!empty($params['level'])) {
|
||||
$query->where('level', $params['level']);
|
||||
if (!empty($params['parent_code'])) {
|
||||
$query->where('parent_code', $params['parent_code']);
|
||||
}
|
||||
|
||||
if (!empty($params['keyword'])) {
|
||||
$query->where(function ($q) use ($params) {
|
||||
$q->where('name', 'like', '%' . $params['keyword'] . '%')
|
||||
->orWhere('code', 'like', '%' . $params['keyword'] . '%')
|
||||
->orWhere('pinyin', 'like', '%' . $params['keyword'] . '%');
|
||||
$q->where('title', 'like', '%' . $params['keyword'] . '%')
|
||||
->orWhere('code', 'like', '%' . $params['keyword'] . '%');
|
||||
});
|
||||
}
|
||||
|
||||
if (isset($params['status']) && $params['status'] !== '') {
|
||||
$query->where('status', $params['status']);
|
||||
}
|
||||
|
||||
$query->orderBy('sort')->orderBy('id');
|
||||
$query->orderBy('id');
|
||||
|
||||
$pageSize = $params['page_size'] ?? 20;
|
||||
$list = $query->paginate($pageSize);
|
||||
@@ -47,14 +38,13 @@ class CityService
|
||||
|
||||
public function getTree(): array
|
||||
{
|
||||
return $this->buildTree(City::where('status', true)->orderBy('sort')->get());
|
||||
return $this->buildTree(City::orderBy('id')->get());
|
||||
}
|
||||
|
||||
public function getChildren(int $parentId): array
|
||||
public function getChildren(string $parentCode): array
|
||||
{
|
||||
return City::where('parent_id', $parentId)
|
||||
->where('status', true)
|
||||
->orderBy('sort')
|
||||
return City::where('parent_code', $parentCode)
|
||||
->orderBy('id')
|
||||
->get()
|
||||
->toArray();
|
||||
}
|
||||
@@ -69,21 +59,12 @@ class CityService
|
||||
return City::find($id);
|
||||
}
|
||||
|
||||
public function getByPinyin(string $pinyin): array
|
||||
{
|
||||
return City::where('pinyin', 'like', '%' . $pinyin . '%')
|
||||
->where('status', true)
|
||||
->get()
|
||||
->toArray();
|
||||
}
|
||||
|
||||
public function create(array $data): City
|
||||
{
|
||||
Validator::make($data, [
|
||||
'name' => 'required|string|max:100',
|
||||
'code' => 'required|string|max:50|unique:system_cities,code',
|
||||
'level' => 'required|integer|in:1,2,3',
|
||||
'parent_id' => 'sometimes|exists:system_cities,id',
|
||||
'title' => 'required|string|max:100',
|
||||
'code' => 'required|string|max:50|unique:system_city,code',
|
||||
'parent_code' => 'sometimes|nullable|exists:system_city,code',
|
||||
])->validate();
|
||||
|
||||
$city = City::create($data);
|
||||
@@ -96,12 +77,16 @@ class CityService
|
||||
$city = City::findOrFail($id);
|
||||
|
||||
Validator::make($data, [
|
||||
'name' => 'sometimes|required|string|max:100',
|
||||
'code' => 'sometimes|required|string|max:50|unique:system_cities,code,' . $id,
|
||||
'level' => 'sometimes|required|integer|in:1,2,3',
|
||||
'parent_id' => 'sometimes|exists:system_cities,id',
|
||||
'title' => 'sometimes|required|string|max:100',
|
||||
'code' => 'sometimes|required|string|max:50|unique:system_city,code,' . $id,
|
||||
'parent_code' => 'sometimes|nullable|exists:system_city,code',
|
||||
])->validate();
|
||||
|
||||
// 防止循环引用
|
||||
if (isset($data['parent_code']) && $data['parent_code'] === $city->code) {
|
||||
throw new \Exception('不能将城市设置为自己的子级');
|
||||
}
|
||||
|
||||
$city->update($data);
|
||||
$this->clearCache();
|
||||
return $city;
|
||||
@@ -127,19 +112,69 @@ class CityService
|
||||
return true;
|
||||
}
|
||||
|
||||
public function batchUpdateStatus(array $ids, bool $status): bool
|
||||
/**
|
||||
* 获取所有顶级城市(省/直辖市/自治区)
|
||||
*/
|
||||
public function getTopLevel(): array
|
||||
{
|
||||
City::whereIn('id', $ids)->update(['status' => $status]);
|
||||
$this->clearCache();
|
||||
return true;
|
||||
return City::whereNull('parent_code')
|
||||
->orderBy('id')
|
||||
->get()
|
||||
->toArray();
|
||||
}
|
||||
|
||||
private function buildTree(array $cities, int $parentId = 0): array
|
||||
/**
|
||||
* 根据父级编码获取城市
|
||||
*/
|
||||
public function getByParentCode(string $parentCode): array
|
||||
{
|
||||
return City::where('parent_code', $parentCode)
|
||||
->orderBy('id')
|
||||
->get()
|
||||
->toArray();
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取城市的完整路径(从顶级到当前)
|
||||
*/
|
||||
public function getPath(string $code): array
|
||||
{
|
||||
$path = [];
|
||||
$city = $this->getByCode($code);
|
||||
|
||||
while ($city) {
|
||||
array_unshift($path, $city);
|
||||
$city = $city->parent;
|
||||
}
|
||||
|
||||
return $path;
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取城市的所有子孙
|
||||
*/
|
||||
public function getDescendants(string $code): array
|
||||
{
|
||||
$descendants = [];
|
||||
$this->collectDescendants($code, $descendants);
|
||||
return $descendants;
|
||||
}
|
||||
|
||||
private function collectDescendants(string $parentCode, array &$result): void
|
||||
{
|
||||
$children = City::where('parent_code', $parentCode)->get();
|
||||
foreach ($children as $child) {
|
||||
$result[] = $child;
|
||||
$this->collectDescendants($child->code, $result);
|
||||
}
|
||||
}
|
||||
|
||||
private function buildTree($cities, string $parentCode = ''): array
|
||||
{
|
||||
$tree = [];
|
||||
foreach ($cities as $city) {
|
||||
if ($city['parent_id'] == $parentId) {
|
||||
$children = $this->buildTree($cities, $city['id']);
|
||||
if ($city->parent_code == $parentCode) {
|
||||
$children = $this->buildTree($cities, $city->code);
|
||||
if (!empty($children)) {
|
||||
$city['children'] = $children;
|
||||
}
|
||||
@@ -167,32 +202,36 @@ class CityService
|
||||
return $tree;
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取省份(兼容旧接口)
|
||||
*/
|
||||
public function getProvinces(): array
|
||||
{
|
||||
return City::where('level', 1)
|
||||
->where('status', true)
|
||||
->orderBy('sort')
|
||||
->get()
|
||||
->toArray();
|
||||
return $this->getTopLevel();
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取城市(兼容旧接口)
|
||||
*/
|
||||
public function getCities(int $provinceId): array
|
||||
{
|
||||
return City::where('parent_id', $provinceId)
|
||||
->where('level', 2)
|
||||
->where('status', true)
|
||||
->orderBy('sort')
|
||||
->get()
|
||||
->toArray();
|
||||
// 由于新结构使用code关联,这里需要根据id找到对应的code
|
||||
$province = City::find($provinceId);
|
||||
if (!$province) {
|
||||
return [];
|
||||
}
|
||||
return $this->getByParentCode($province->code);
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取区县(兼容旧接口)
|
||||
*/
|
||||
public function getDistricts(int $cityId): array
|
||||
{
|
||||
return City::where('parent_id', $cityId)
|
||||
->where('level', 3)
|
||||
->where('status', true)
|
||||
->orderBy('sort')
|
||||
->get()
|
||||
->toArray();
|
||||
$city = City::find($cityId);
|
||||
if (!$city) {
|
||||
return [];
|
||||
}
|
||||
return $this->getByParentCode($city->code);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user