$e->getMessage(), 'data' => $data ]); throw $e; } } /** * 更新分类 * * @param int $id 分类ID * @param array $data 分类数据 * @return bool */ public static function updateCategory(int $id, array $data): bool { // 检查事务是否已开启 \UCore\Db\Helper::check_tr(); try { $category = ArticleCate::find($id); if (!$category) { return false; } return $category->update($data); } catch (\Exception $e) { Log::error('更新文章分类失败', [ 'error' => $e->getMessage(), 'id' => $id, 'data' => $data ]); return false; } } /** * 删除分类 * * @param int $id 分类ID * @return bool */ public static function deleteCategory(int $id): bool { try { return DB::transaction(function () use ($id) { $category = ArticleCate::find($id); if (!$category) { return false; } // 检查是否可以删除 if (!$category->can_delete) { throw new \Exception('该分类不允许删除'); } // 检查是否有子分类 $childCount = ArticleCate::where('pid', $id)->count(); if ($childCount > 0) { throw new \Exception('该分类下有子分类,不能删除'); } // 检查是否有关联文章 $articleCount = Article::where('category_id', $id)->count(); if ($articleCount > 0) { throw new \Exception('该分类下有文章,不能删除'); } return $category->delete(); }); } catch (\Exception $e) { Log::error('删除文章分类失败', [ 'error' => $e->getMessage(), 'id' => $id ]); throw $e; } } /** * 批量更新分类状态 * * @param array $ids 分类ID数组 * @param string $status 状态 * @return bool */ public static function batchUpdateStatus(array $ids, string $status): bool { // 检查事务是否已开启 \UCore\Db\Helper::check_tr(); try { return ArticleCate::whereIn('id', $ids)->update(['status' => $status]); } catch (\Exception $e) { Log::error('批量更新文章分类状态失败', [ 'error' => $e->getMessage(), 'ids' => $ids, 'status' => $status ]); return false; } } /** * 检查分类标识是否唯一 * * @param string $unid 分类标识 * @param int|null $excludeId 排除的分类ID * @return bool */ public static function isUnidUnique(string $unid, ?int $excludeId = null): bool { $query = ArticleCate::where('unid', $unid); if ($excludeId) { $query->where('id', '!=', $excludeId); } return $query->count() === 0; } }