更新完善字典相关功能

This commit is contained in:
2026-02-18 17:15:33 +08:00
parent 5450777bd7
commit 378b9bd71f
23 changed files with 1657 additions and 572 deletions

View File

@@ -43,6 +43,7 @@ class DictionaryService
if ($dictionaries === null) {
$dictionaries = Dictionary::where('status', true)
->with(['activeItems'])
->orderBy('sort')
->get()
->toArray();
@@ -52,7 +53,7 @@ class DictionaryService
return $dictionaries;
}
public function getById(int $id): ?Dictionary
public function getById(int $id): ?array
{
$cacheKey = 'system:dictionaries:' . $id;
$dictionary = Cache::get($cacheKey);
@@ -60,22 +61,26 @@ class DictionaryService
if ($dictionary === null) {
$dictionary = Dictionary::with('items')->find($id);
if ($dictionary) {
Cache::put($cacheKey, $dictionary->toArray(), 3600);
$dictionary = $dictionary->toArray();
Cache::put($cacheKey, $dictionary, 3600);
}
}
return $dictionary ? $dictionary : null;
return $dictionary ?? null;
}
public function getByCode(string $code): ?Dictionary
public function getByCode(string $code): ?array
{
$cacheKey = 'system:dictionaries:code:' . $code;
$dictionary = Cache::get($cacheKey);
if ($dictionary === null) {
$dictionary = Dictionary::where('code', $code)->first();
if ($dictionary) {
Cache::put($cacheKey, $dictionary->toArray(), 3600);
$dictionaryModel = Dictionary::where('code', $code)->first();
if ($dictionaryModel) {
$dictionary = $dictionaryModel->toArray();
Cache::put($cacheKey, $dictionary, 3600);
} else {
$dictionary = null;
}
}
@@ -269,4 +274,36 @@ class DictionaryService
return true;
}
/**
* 获取所有字典项(按字典分类)
* @return array 按字典code分类的字典项数据
*/
public function getAllItems(): array
{
$cacheKey = 'system:dictionary-items:all';
$allItems = Cache::get($cacheKey);
if ($allItems === null) {
// 获取所有启用的字典
$dictionaries = Dictionary::where('status', true)
->orderBy('sort')
->get();
$result = [];
foreach ($dictionaries as $dictionary) {
$result[] = [
'code' => $dictionary->code,
'name' => $dictionary->name,
'description' => $dictionary->description,
'items' => $dictionary->activeItems->toArray()
];
}
$allItems = $result;
Cache::put($cacheKey, $allItems, 3600);
}
return $allItems;
}
}