where(function ($q) use ($params) { $q->where('name', 'like', '%' . $params['keyword'] . '%') ->orWhere('code', 'like', '%' . $params['keyword'] . '%'); }); } // 处理状态筛选:只接受布尔值或数字1/0 if (array_key_exists('status', $params) && is_bool($params['status'])) { $query->where('status', $params['status']); } $pageSize = $params['page_size'] ?? 20; $list = $query->orderBy('sort')->orderBy('id')->paginate($pageSize); return [ 'list' => $list->items(), 'total' => $list->total(), 'page' => $list->currentPage(), 'page_size' => $list->perPage(), ]; } public function getAll(): array { $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 { $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 { $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 { $cacheKey = 'system:dictionary:' . $code; $items = Cache::get($cacheKey); if ($items === null) { $dictionary = Dictionary::where('code', $code)->first(); if ($dictionary) { $items = DictionaryItem::where('dictionary_id', $dictionary->id) ->where('status', true) ->orderBy('sort') ->get() ->toArray(); Cache::put($cacheKey, $items, 3600); } else { $items = []; } } return $items; } public function create(array $data): Dictionary { Validator::make($data, [ 'name' => 'required|string|max:100', 'code' => 'required|string|max:50|unique:system_dictionaries,code', ])->validate(); $dictionary = Dictionary::create($data); $this->clearCache(); return $dictionary; } public function update(int $id, array $data): Dictionary { $dictionary = Dictionary::findOrFail($id); Validator::make($data, [ 'name' => 'sometimes|required|string|max:100', 'code' => 'sometimes|required|string|max:50|unique:system_dictionaries,code,' . $id, ])->validate(); $dictionary->update($data); $this->clearCache(); return $dictionary; } public function delete(int $id): bool { $dictionary = Dictionary::findOrFail($id); DictionaryItem::where('dictionary_id', $id)->delete(); $dictionary->delete(); $this->clearCache(); return true; } public function batchDelete(array $ids): bool { DictionaryItem::whereIn('dictionary_id', $ids)->delete(); Dictionary::whereIn('id', $ids)->delete(); $this->clearCache(); return true; } public function batchUpdateStatus(array $ids, bool $status): bool { Dictionary::whereIn('id', $ids)->update(['status' => $status]); $this->clearCache(); return true; } private function clearCache($dictionaryId = null): void { // 清理所有字典列表缓存 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); } } } public function getItemsList(array $params): array { $query = DictionaryItem::query(); if (!empty($params['dictionary_id'])) { $query->where('dictionary_id', $params['dictionary_id']); } // 处理状态筛选:只接受布尔值或数字1/0 if (array_key_exists('status', $params) && is_bool($params['status'])) { $query->where('status', $params['status']); } $query->orderBy('sort')->orderBy('id'); $pageSize = $params['page_size'] ?? 20; $list = $query->paginate($pageSize); return [ 'list' => $list->items(), 'total' => $list->total(), 'page' => $list->currentPage(), 'page_size' => $list->perPage(), ]; } public function createItem(array $data): DictionaryItem { Validator::make($data, [ 'dictionary_id' => 'required|exists:system_dictionaries,id', 'label' => 'required|string|max:100', 'value' => 'required|string|max:100', ])->validate(); $item = DictionaryItem::create($data); $this->clearCache($data['dictionary_id']); return $item; } public function updateItem(int $id, array $data): DictionaryItem { $item = DictionaryItem::findOrFail($id); Validator::make($data, [ 'dictionary_id' => 'sometimes|required|exists:system_dictionaries,id', 'label' => 'sometimes|required|string|max:100', 'value' => 'sometimes|required|string|max:100', ])->validate(); $item->update($data); $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($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(); // 清理相关字典的缓存 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]); // 清理相关字典的缓存 foreach ($dictionaryIds as $dictionaryId) { $this->clearCache($dictionaryId); } return true; } }