更新功能:数据字典和定时任务

This commit is contained in:
2026-02-18 10:43:25 +08:00
parent 6623c656f4
commit 790b3140a7
15 changed files with 2847 additions and 25 deletions

View File

@@ -20,7 +20,8 @@ class DictionaryService
});
}
if (isset($params['status']) && $params['status'] !== '') {
// 处理状态筛选只接受布尔值或数字1/0
if (array_key_exists('status', $params) && is_bool($params['status'])) {
$query->where('status', $params['status']);
}
@@ -37,20 +38,48 @@ class DictionaryService
public function getAll(): array
{
return Dictionary::where('status', true)
->orderBy('sort')
->get()
->toArray();
$cacheKey = 'system:dictionaries:all';
$dictionaries = Cache::get($cacheKey);
if ($dictionaries === null) {
$dictionaries = Dictionary::where('status', true)
->orderBy('sort')
->get()
->toArray();
Cache::put($cacheKey, $dictionaries, 3600);
}
return $dictionaries;
}
public function getById(int $id): ?Dictionary
{
return Dictionary::with('items')->find($id);
$cacheKey = 'system:dictionaries:' . $id;
$dictionary = Cache::get($cacheKey);
if ($dictionary === null) {
$dictionary = Dictionary::with('items')->find($id);
if ($dictionary) {
Cache::put($cacheKey, $dictionary->toArray(), 3600);
}
}
return $dictionary ? $dictionary : null;
}
public function getByCode(string $code): ?Dictionary
{
return Dictionary::where('code', $code)->first();
$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);
}
}
return $dictionary;
}
public function getItemsByCode(string $code): array
@@ -125,11 +154,26 @@ class DictionaryService
return true;
}
private function clearCache(): void
private function clearCache($dictionaryId = null): void
{
$codes = Dictionary::pluck('code')->toArray();
foreach ($codes as $code) {
Cache::forget('system:dictionary:' . $code);
// 清理所有字典列表缓存
Cache::forget('system:dictionaries: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:' . $dictionary->code);
}
} else {
// 清理所有字典缓存
$codes = Dictionary::pluck('code')->toArray();
foreach ($codes as $code) {
Cache::forget('system:dictionary:' . $code);
Cache::forget('system:dictionaries:code:' . $code);
}
}
}
@@ -141,7 +185,8 @@ class DictionaryService
$query->where('dictionary_id', $params['dictionary_id']);
}
if (isset($params['status']) && $params['status'] !== '') {
// 处理状态筛选只接受布尔值或数字1/0
if (array_key_exists('status', $params) && is_bool($params['status'])) {
$query->where('status', $params['status']);
}
@@ -167,7 +212,7 @@ class DictionaryService
])->validate();
$item = DictionaryItem::create($data);
$this->clearCache();
$this->clearCache($data['dictionary_id']);
return $item;
}
@@ -182,29 +227,46 @@ class DictionaryService
])->validate();
$item->update($data);
$this->clearCache();
$this->clearCache($item->dictionary_id);
return $item;
}
public function deleteItem(int $id): bool
{
$item = DictionaryItem::findOrFail($id);
$dictionaryId = $item->dictionary_id;
$item->delete();
$this->clearCache();
$this->clearCache($dictionaryId);
return true;
}
public function batchDeleteItems(array $ids): bool
{
$items = DictionaryItem::whereIn('id', $ids)->get();
$dictionaryIds = $items->pluck('dictionary_id')->unique()->toArray();
DictionaryItem::whereIn('id', $ids)->delete();
$this->clearCache();
// 清理相关字典的缓存
foreach ($dictionaryIds as $dictionaryId) {
$this->clearCache($dictionaryId);
}
return true;
}
public function batchUpdateItemsStatus(array $ids, bool $status): bool
{
$items = DictionaryItem::whereIn('id', $ids)->get();
$dictionaryIds = $items->pluck('dictionary_id')->unique()->toArray();
DictionaryItem::whereIn('id', $ids)->update(['status' => $status]);
$this->clearCache();
// 清理相关字典的缓存
foreach ($dictionaryIds as $dictionaryId) {
$this->clearCache($dictionaryId);
}
return true;
}
}