$e->getMessage(), 'data' => $data ]); throw $e; } } /** * 更新文章 * * @param int $id 文章ID * @param array $data 文章数据 * @return bool */ public static function updateArticle(int $id, array $data): bool { // 检查事务是否已开启 \UCore\Db\Helper::check_tr(); try { $article = Article::find($id); if (!$article) { return false; } return $article->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 deleteArticle(int $id): bool { // 检查事务是否已开启 \UCore\Db\Helper::check_tr(); try { $article = Article::find($id); if (!$article) { return false; } return $article->delete(); } catch (\Exception $e) { Log::error('删除文章失败', [ 'error' => $e->getMessage(), 'id' => $id ]); return false; } } /** * 增加文章浏览量 * * @param int $id 文章ID * @return bool */ public static function incrementViewCount(int $id): bool { try { $article = Article::find($id); if (!$article) { return false; } $article->increment('views_count'); return true; } catch (\Exception $e) { Log::error('增加文章浏览量失败', [ 'error' => $e->getMessage(), 'id' => $id ]); return false; } } /** * 批量更新文章状态 * * @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 Article::whereIn('id', $ids)->update(['status' => $status]); } catch (\Exception $e) { Log::error('批量更新文章状态失败', [ 'error' => $e->getMessage(), 'ids' => $ids, 'status' => $status ]); return false; } } /** * 批量更新文章置顶状态 * * @param array $ids 文章ID数组 * @param bool $isTop 是否置顶 * @return bool */ public static function batchUpdateTopStatus(array $ids, bool $isTop): bool { // 检查事务是否已开启 \UCore\Db\Helper::check_tr(); try { return Article::whereIn('id', $ids)->update(['is_top' => $isTop]); } catch (\Exception $e) { Log::error('批量更新文章置顶状态失败', [ 'error' => $e->getMessage(), 'ids' => $ids, 'is_top' => $isTop ]); return false; } } /** * 批量更新文章推荐状态 * * @param array $ids 文章ID数组 * @param bool $isRecommend 是否推荐 * @return bool */ public static function batchUpdateRecommendStatus(array $ids, bool $isRecommend): bool { // 检查事务是否已开启 \UCore\Db\Helper::check_tr(); try { return Article::whereIn('id', $ids)->update(['is_recommend' => $isRecommend]); } catch (\Exception $e) { Log::error('批量更新文章推荐状态失败', [ 'error' => $e->getMessage(), 'ids' => $ids, 'is_recommend' => $isRecommend ]); return false; } } }