where('status', STATUS::SHOW->value); } return $query->orderBy('sort_order', 'asc')->get(); }); } /** * 获取分类树 * * @param bool $onlyActive 是否只获取激活状态的分类 * @return array */ public static function getCategoryTree(bool $onlyActive = true): array { $cacheKey = 'article_category_tree_' . ($onlyActive ? 'active' : 'all'); return Cache::remember($cacheKey, 3600, function () use ($onlyActive) { $categories = self::getAllCategories($onlyActive); return self::buildCategoryTree($categories); }); } /** * 构建分类树 * * @param Collection $categories 分类集合 * @param int $parentId 父级ID * @return array */ protected static function buildCategoryTree(Collection $categories, int $parentId = 0): array { $tree = []; foreach ($categories as $category) { if ($category->pid == $parentId) { $children = self::buildCategoryTree($categories, $category->id); if ($children) { $category->children = $children; } $tree[] = $category; } } return $tree; } /** * 获取分类详情 * * @param int $id 分类ID * @return ArticleCate|null */ public static function getCategoryDetail(int $id): ?ArticleCate { return ArticleCate::find($id); } /** * 根据标识获取分类 * * @param string $unid 分类标识 * @return ArticleCate|null */ public static function getCategoryByUnid(string $unid): ?ArticleCate { return ArticleCate::where('unid', $unid)->first(); } /** * 创建分类 * * @param array $data 分类数据 * @return ArticleCate */ public static function createCategory(array $data): ArticleCate { $category = ArticleCategoryLogic::createCategory($data); // 清除相关缓存 self::clearCategoryCache(); return $category; } /** * 更新分类 * * @param int $id 分类ID * @param array $data 分类数据 * @return bool */ public static function updateCategory(int $id, array $data): bool { $result = ArticleCategoryLogic::updateCategory($id, $data); // 清除相关缓存 self::clearCategoryCache(); return $result; } /** * 删除分类 * * @param int $id 分类ID * @return bool */ public static function deleteCategory(int $id): bool { $result = ArticleCategoryLogic::deleteCategory($id); // 清除相关缓存 self::clearCategoryCache(); return $result; } /** * 清除分类相关缓存 * * @return void */ public static function clearCategoryCache(): void { Cache::forget('article_categories_active'); Cache::forget('article_categories_all'); Cache::forget('article_category_tree_active'); Cache::forget('article_category_tree_all'); } }