This commit is contained in:
2026-02-18 17:54:07 +08:00
parent 378b9bd71f
commit e679a9402f
11 changed files with 739 additions and 5 deletions

View File

@@ -4,11 +4,19 @@ namespace App\Services\System;
use App\Models\System\Dictionary;
use App\Models\System\DictionaryItem;
use App\Services\WebSocket\WebSocketService;
use Illuminate\Support\Facades\Cache;
use Illuminate\Support\Facades\Validator;
class DictionaryService
{
protected $webSocketService;
public function __construct(WebSocketService $webSocketService)
{
$this->webSocketService = $webSocketService;
}
public function getList(array $params): array
{
$query = Dictionary::query();
@@ -62,6 +70,10 @@ class DictionaryService
$dictionary = Dictionary::with('items')->find($id);
if ($dictionary) {
$dictionary = $dictionary->toArray();
// 格式化字典项值
if (!empty($dictionary['items'])) {
$dictionary['items'] = $this->formatItemsByType($dictionary['items'], $dictionary['value_type']);
}
Cache::put($cacheKey, $dictionary, 3600);
}
}
@@ -75,9 +87,13 @@ class DictionaryService
$dictionary = Cache::get($cacheKey);
if ($dictionary === null) {
$dictionaryModel = Dictionary::where('code', $code)->first();
$dictionaryModel = Dictionary::with('items')->where('code', $code)->first();
if ($dictionaryModel) {
$dictionary = $dictionaryModel->toArray();
// 格式化字典项值
if (!empty($dictionary['items'])) {
$dictionary['items'] = $this->formatItemsByType($dictionary['items'], $dictionary['value_type']);
}
Cache::put($cacheKey, $dictionary, 3600);
} else {
$dictionary = null;
@@ -100,6 +116,8 @@ class DictionaryService
->orderBy('sort')
->get()
->toArray();
// 格式化字典项值
$items = $this->formatItemsByType($items, $dictionary->value_type);
Cache::put($cacheKey, $items, 3600);
} else {
$items = [];
@@ -118,6 +136,7 @@ class DictionaryService
$dictionary = Dictionary::create($data);
$this->clearCache();
$this->notifyDictionaryUpdate('create', $dictionary->toArray());
return $dictionary;
}
@@ -132,15 +151,18 @@ class DictionaryService
$dictionary->update($data);
$this->clearCache();
$this->notifyDictionaryUpdate('update', $dictionary->toArray());
return $dictionary;
}
public function delete(int $id): bool
{
$dictionary = Dictionary::findOrFail($id);
$dictionaryData = $dictionary->toArray();
DictionaryItem::where('dictionary_id', $id)->delete();
$dictionary->delete();
$this->clearCache();
$this->notifyDictionaryUpdate('delete', $dictionaryData);
return true;
}
@@ -149,6 +171,7 @@ class DictionaryService
DictionaryItem::whereIn('dictionary_id', $ids)->delete();
Dictionary::whereIn('id', $ids)->delete();
$this->clearCache();
$this->notifyDictionaryUpdate('batch_delete', ['ids' => $ids]);
return true;
}
@@ -156,6 +179,7 @@ class DictionaryService
{
Dictionary::whereIn('id', $ids)->update(['status' => $status]);
$this->clearCache();
$this->notifyDictionaryUpdate('batch_update_status', ['ids' => $ids, 'status' => $status]);
return true;
}
@@ -218,6 +242,7 @@ class DictionaryService
$item = DictionaryItem::create($data);
$this->clearCache($data['dictionary_id']);
$this->notifyDictionaryItemUpdate('create', $item->toArray());
return $item;
}
@@ -233,6 +258,7 @@ class DictionaryService
$item->update($data);
$this->clearCache($item->dictionary_id);
$this->notifyDictionaryItemUpdate('update', $item->toArray());
return $item;
}
@@ -240,8 +266,10 @@ class DictionaryService
{
$item = DictionaryItem::findOrFail($id);
$dictionaryId = $item->dictionary_id;
$itemData = $item->toArray();
$item->delete();
$this->clearCache($dictionaryId);
$this->notifyDictionaryItemUpdate('delete', $itemData);
return true;
}
@@ -257,6 +285,7 @@ class DictionaryService
$this->clearCache($dictionaryId);
}
$this->notifyDictionaryItemUpdate('batch_delete', ['ids' => $ids, 'dictionary_ids' => $dictionaryIds]);
return true;
}
@@ -272,6 +301,7 @@ class DictionaryService
$this->clearCache($dictionaryId);
}
$this->notifyDictionaryItemUpdate('batch_update_status', ['ids' => $ids, 'dictionary_ids' => $dictionaryIds, 'status' => $status]);
return true;
}
@@ -292,11 +322,16 @@ class DictionaryService
$result = [];
foreach ($dictionaries as $dictionary) {
$items = $dictionary->activeItems->toArray();
// 格式化字典项值
$items = $this->formatItemsByType($items, $dictionary->value_type);
$result[] = [
'code' => $dictionary->code,
'name' => $dictionary->name,
'description' => $dictionary->description,
'items' => $dictionary->activeItems->toArray()
'value_type' => $dictionary->value_type,
'items' => $items
];
}
@@ -306,4 +341,76 @@ class DictionaryService
return $allItems;
}
/**
* 通知前端字典分类已更新
*
* @param string $action 操作类型create, update, delete, batch_delete, batch_update_status
* @param array $data 字典数据
*/
private function notifyDictionaryUpdate(string $action, array $data): void
{
$this->webSocketService->broadcast([
'type' => 'dictionary_update',
'data' => [
'action' => $action,
'resource_type' => 'dictionary',
'data' => $data,
'timestamp' => time()
]
]);
}
/**
* 通知前端字典项已更新
*
* @param string $action 操作类型create, update, delete, batch_delete, batch_update_status
* @param array $data 字典项数据
*/
private function notifyDictionaryItemUpdate(string $action, array $data): void
{
$this->webSocketService->broadcast([
'type' => 'dictionary_item_update',
'data' => [
'action' => $action,
'resource_type' => 'dictionary_item',
'data' => $data,
'timestamp' => time()
]
]);
}
/**
* 根据值类型格式化字典项
* @param array $items 字典项数组
* @param string $valueType 值类型string, number, boolean, json
* @return array 格式化后的字典项数组
*/
private function formatItemsByType(array $items, string $valueType): array
{
return array_map(function ($item) use ($valueType) {
switch ($valueType) {
case 'number':
// 数字类型:将值转换为数字
$item['value'] = is_numeric($item['value']) ? (strpos($item['value'], '.') !== false ? (float)$item['value'] : (int)$item['value']) : $item['value'];
break;
case 'boolean':
// 布尔类型:将 '1', 'true', 'yes' 转换为 true其他为 false
$item['value'] = in_array(strtolower($item['value']), ['1', 'true', 'yes', 'on']);
break;
case 'json':
// JSON类型尝试解析JSON字符串失败则保持原值
$decoded = json_decode($item['value'], true);
if (json_last_error() === JSON_ERROR_NONE) {
$item['value'] = $decoded;
}
break;
case 'string':
default:
// 字符串类型:保持原值
break;
}
return $item;
}, $items);
}
}