| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192 |
- <?php
- namespace App\Module\Article\Logics;
- use App\Module\Article\Models\Article;
- use Illuminate\Support\Facades\Log;
- /**
- * 文章逻辑类
- *
- * 处理文章相关的业务逻辑
- */
- class ArticleLogic
- {
- /**
- * 创建文章
- *
- * @param array $data 文章数据
- * @return Article
- */
- public static function createArticle(array $data): Article
- {
- // 检查事务是否已开启
- \UCore\Db\Helper::check_tr();
- try {
- return Article::create($data);
- } catch (\Exception $e) {
- Log::error('创建文章失败', [
- 'error' => $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;
- }
- }
- }
|