调整数据库表的单复数

This commit is contained in:
2026-02-19 10:39:38 +08:00
parent 736a41a718
commit d310a29c03
56 changed files with 45577 additions and 506 deletions

View File

@@ -130,9 +130,9 @@ class City extends Controller
]);
}
public function children(int $parentId)
public function children(string $parentCode)
{
$children = $this->cityService->getChildren($parentId);
$children = $this->cityService->getChildren($parentCode);
return response()->json([
'code' => 200,
'message' => 'success',

View File

@@ -11,7 +11,7 @@ class Department extends Model
{
use ModelTrait, SoftDeletes;
protected $table = 'auth_departments';
protected $table = 'auth_department';
protected $fillable = [
'name',

View File

@@ -11,7 +11,7 @@ class Permission extends Model
{
use ModelTrait, SoftDeletes;
protected $table = 'auth_permissions';
protected $table = 'auth_permission';
protected $fillable = [
'title',

View File

@@ -11,7 +11,7 @@ class Role extends Model
{
use ModelTrait, SoftDeletes;
protected $table = 'auth_roles';
protected $table = 'auth_role';
protected $fillable = [
'name',

View File

@@ -14,7 +14,7 @@ class User extends Authenticatable implements JWTSubject
{
use ModelTrait, SoftDeletes;
protected $table = 'auth_users';
protected $table = 'auth_user';
protected $fillable = [
'username',

View File

@@ -9,40 +9,35 @@ use Illuminate\Database\Eloquent\Relations\HasMany;
class City extends Model
{
use ModelTrait;
protected $table = 'system_cities';
protected $table = 'system_city';
protected $fillable = [
'parent_id',
'name',
'title',
'code',
'pinyin',
'pinyin_short',
'level',
'sort',
'status',
];
protected $casts = [
'parent_id' => 'integer',
'level' => 'integer',
'sort' => 'integer',
'status' => 'boolean',
'parent_code',
];
/**
* 子级城市
*/
public function children(): HasMany
{
return $this->hasMany(City::class, 'parent_id')->orderBy('sort');
}
public function activeChildren(): HasMany
{
return $this->hasMany(City::class, 'parent_id')
->where('status', true)
->orderBy('sort');
return $this->hasMany(City::class, 'parent_code', 'code');
}
/**
* 父级城市
*/
public function parent()
{
return $this->belongsTo(City::class, 'parent_id');
return $this->belongsTo(City::class, 'parent_code', 'code');
}
/**
* 判断是否是顶级城市(省/直辖市/自治区)
*/
public function isTopLevel(): bool
{
return empty($this->parent_code);
}
}

View File

@@ -10,7 +10,7 @@ class Config extends Model
{
use ModelTrait, SoftDeletes;
protected $table = 'system_configs';
protected $table = 'system_setting';
protected $fillable = [
'group',

View File

@@ -11,7 +11,7 @@ class Dictionary extends Model
{
use ModelTrait, SoftDeletes;
protected $table = 'system_dictionaries';
protected $table = 'system_dictionary';
protected $fillable = [
'name',

View File

@@ -9,7 +9,7 @@ use Illuminate\Database\Eloquent\Relations\BelongsTo;
class DictionaryItem extends Model
{
use ModelTrait;
protected $table = 'system_dictionary_items';
protected $table = 'system_dictionary_item';
protected $fillable = [
'dictionary_id',

View File

@@ -9,7 +9,7 @@ use Illuminate\Database\Eloquent\Relations\BelongsTo;
class Log extends Model
{
use ModelTrait;
protected $table = 'system_logs';
protected $table = 'system_log';
protected $fillable = [
'user_id',

View File

@@ -11,7 +11,7 @@ class Notification extends Model
{
use ModelTrait, SoftDeletes;
protected $table = 'system_notifications';
protected $table = 'system_notification';
protected $fillable = [
'user_id',

View File

@@ -10,7 +10,7 @@ class Task extends Model
{
use ModelTrait, SoftDeletes;
protected $table = 'system_tasks';
protected $table = 'system_task';
protected $fillable = [
'name',

View File

@@ -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);
}
}

View File

@@ -88,7 +88,7 @@ class ConfigService
{
Validator::make($data, [
'group' => 'required|string|max:50',
'key' => 'required|string|max:100|unique:system_configs,key',
'key' => 'required|string|max:100|unique:system_setting,key',
'name' => 'required|string|max:100',
'type' => 'required|string|in:string,text,number,boolean,select,radio,checkbox,file,json',
])->validate();
@@ -104,7 +104,7 @@ class ConfigService
Validator::make($data, [
'group' => 'sometimes|required|string|max:50',
'key' => 'sometimes|required|string|max:100|unique:system_configs,key,' . $id,
'key' => 'sometimes|required|string|max:100|unique:system_setting,key,' . $id,
'name' => 'sometimes|required|string|max:100',
'type' => 'sometimes|required|string|in:string,text,number,boolean,select,radio,checkbox,file,json',
])->validate();