更新调整修复

This commit is contained in:
2026-02-19 14:05:30 +08:00
parent f0af965412
commit e26ba12150
7 changed files with 539 additions and 518 deletions

View File

@@ -46,24 +46,24 @@ class DictionaryService
public function getAll(): array
{
$cacheKey = 'system:dictionaries:all';
$dictionaries = Cache::get($cacheKey);
$cacheKey = 'system:dictionary:all';
$dictionary = Cache::get($cacheKey);
if ($dictionaries === null) {
$dictionaries = Dictionary::where('status', true)
if ($dictionary === null) {
$dictionary = Dictionary::where('status', true)
->with(['activeItems'])
->orderBy('sort')
->get()
->toArray();
Cache::put($cacheKey, $dictionaries, 3600);
Cache::put($cacheKey, $dictionary, 3600);
}
return $dictionaries;
return $dictionary;
}
public function getById(int $id): ?array
{
$cacheKey = 'system:dictionaries:' . $id;
$cacheKey = 'system:dictionary:' . $id;
$dictionary = Cache::get($cacheKey);
if ($dictionary === null) {
@@ -83,7 +83,7 @@ class DictionaryService
public function getByCode(string $code): ?array
{
$cacheKey = 'system:dictionaries:code:' . $code;
$cacheKey = 'system:dictionary:code:' . $code;
$dictionary = Cache::get($cacheKey);
if ($dictionary === null) {
@@ -131,7 +131,7 @@ class DictionaryService
{
Validator::make($data, [
'name' => 'required|string|max:100',
'code' => 'required|string|max:50|unique:system_dictionaries,code',
'code' => 'required|string|max:50|unique:system_dictionary,code',
])->validate();
$dictionary = Dictionary::create($data);
@@ -146,7 +146,7 @@ class DictionaryService
Validator::make($data, [
'name' => 'sometimes|required|string|max:100',
'code' => 'sometimes|required|string|max:50|unique:system_dictionaries,code,' . $id,
'code' => 'sometimes|required|string|max:50|unique:system_dictionary,code,' . $id,
])->validate();
$dictionary->update($data);
@@ -186,14 +186,14 @@ class DictionaryService
private function clearCache($dictionaryId = null): void
{
// 清理所有字典列表缓存
Cache::forget('system:dictionaries:all');
Cache::forget('system:dictionary:all');
if ($dictionaryId) {
// 清理特定字典的缓存
$dictionary = Dictionary::find($dictionaryId);
if ($dictionary) {
Cache::forget('system:dictionaries:' . $dictionaryId);
Cache::forget('system:dictionaries:code:' . $dictionary->code);
Cache::forget('system:dictionary:' . $dictionaryId);
Cache::forget('system:dictionary:code:' . $dictionary->code);
Cache::forget('system:dictionary:' . $dictionary->code);
}
} else {
@@ -201,7 +201,7 @@ class DictionaryService
$codes = Dictionary::pluck('code')->toArray();
foreach ($codes as $code) {
Cache::forget('system:dictionary:' . $code);
Cache::forget('system:dictionaries:code:' . $code);
Cache::forget('system:dictionary:code:' . $code);
}
}
}
@@ -235,7 +235,7 @@ class DictionaryService
public function createItem(array $data): DictionaryItem
{
Validator::make($data, [
'dictionary_id' => 'required|exists:system_dictionaries,id',
'dictionary_id' => 'required|exists:system_dictionary,id',
'label' => 'required|string|max:100',
'value' => 'required|string|max:100',
])->validate();
@@ -251,7 +251,7 @@ class DictionaryService
$item = DictionaryItem::findOrFail($id);
Validator::make($data, [
'dictionary_id' => 'sometimes|required|exists:system_dictionaries,id',
'dictionary_id' => 'sometimes|required|exists:system_dictionary,id',
'label' => 'sometimes|required|string|max:100',
'value' => 'sometimes|required|string|max:100',
])->validate();
@@ -316,12 +316,12 @@ class DictionaryService
if ($allItems === null) {
// 获取所有启用的字典
$dictionaries = Dictionary::where('status', true)
$dictionary = Dictionary::where('status', true)
->orderBy('sort')
->get();
$result = [];
foreach ($dictionaries as $dictionary) {
foreach ($dictionary as $dictionary) {
$items = $dictionary->activeItems->toArray();
// 格式化字典项值
$items = $this->formatItemsByType($items, $dictionary->value_type);