ArticleLogic.php 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  1. <?php
  2. namespace App\Module\Article\Logics;
  3. use App\Module\Article\Models\Article;
  4. use Illuminate\Support\Facades\DB;
  5. use Illuminate\Support\Facades\Log;
  6. /**
  7. * 文章逻辑类
  8. *
  9. * 处理文章相关的业务逻辑
  10. */
  11. class ArticleLogic
  12. {
  13. /**
  14. * 创建文章
  15. *
  16. * @param array $data 文章数据
  17. * @return Article
  18. */
  19. public static function createArticle(array $data): Article
  20. {
  21. try {
  22. return DB::transaction(function () use ($data) {
  23. return Article::create($data);
  24. });
  25. } catch (\Exception $e) {
  26. Log::error('创建文章失败', [
  27. 'error' => $e->getMessage(),
  28. 'data' => $data
  29. ]);
  30. throw $e;
  31. }
  32. }
  33. /**
  34. * 更新文章
  35. *
  36. * @param int $id 文章ID
  37. * @param array $data 文章数据
  38. * @return bool
  39. */
  40. public static function updateArticle(int $id, array $data): bool
  41. {
  42. try {
  43. return DB::transaction(function () use ($id, $data) {
  44. $article = Article::find($id);
  45. if (!$article) {
  46. return false;
  47. }
  48. return $article->update($data);
  49. });
  50. } catch (\Exception $e) {
  51. Log::error('更新文章失败', [
  52. 'error' => $e->getMessage(),
  53. 'id' => $id,
  54. 'data' => $data
  55. ]);
  56. return false;
  57. }
  58. }
  59. /**
  60. * 删除文章
  61. *
  62. * @param int $id 文章ID
  63. * @return bool
  64. */
  65. public static function deleteArticle(int $id): bool
  66. {
  67. try {
  68. return DB::transaction(function () use ($id) {
  69. $article = Article::find($id);
  70. if (!$article) {
  71. return false;
  72. }
  73. return $article->delete();
  74. });
  75. } catch (\Exception $e) {
  76. Log::error('删除文章失败', [
  77. 'error' => $e->getMessage(),
  78. 'id' => $id
  79. ]);
  80. return false;
  81. }
  82. }
  83. /**
  84. * 增加文章浏览量
  85. *
  86. * @param int $id 文章ID
  87. * @return bool
  88. */
  89. public static function incrementViewCount(int $id): bool
  90. {
  91. try {
  92. $article = Article::find($id);
  93. if (!$article) {
  94. return false;
  95. }
  96. $article->increment('views_count');
  97. return true;
  98. } catch (\Exception $e) {
  99. Log::error('增加文章浏览量失败', [
  100. 'error' => $e->getMessage(),
  101. 'id' => $id
  102. ]);
  103. return false;
  104. }
  105. }
  106. /**
  107. * 批量更新文章状态
  108. *
  109. * @param array $ids 文章ID数组
  110. * @param string $status 状态
  111. * @return bool
  112. */
  113. public static function batchUpdateStatus(array $ids, string $status): bool
  114. {
  115. try {
  116. return DB::transaction(function () use ($ids, $status) {
  117. return Article::whereIn('id', $ids)->update(['status' => $status]);
  118. });
  119. } catch (\Exception $e) {
  120. Log::error('批量更新文章状态失败', [
  121. 'error' => $e->getMessage(),
  122. 'ids' => $ids,
  123. 'status' => $status
  124. ]);
  125. return false;
  126. }
  127. }
  128. /**
  129. * 批量更新文章置顶状态
  130. *
  131. * @param array $ids 文章ID数组
  132. * @param bool $isTop 是否置顶
  133. * @return bool
  134. */
  135. public static function batchUpdateTopStatus(array $ids, bool $isTop): bool
  136. {
  137. try {
  138. return DB::transaction(function () use ($ids, $isTop) {
  139. return Article::whereIn('id', $ids)->update(['is_top' => $isTop]);
  140. });
  141. } catch (\Exception $e) {
  142. Log::error('批量更新文章置顶状态失败', [
  143. 'error' => $e->getMessage(),
  144. 'ids' => $ids,
  145. 'is_top' => $isTop
  146. ]);
  147. return false;
  148. }
  149. }
  150. /**
  151. * 批量更新文章推荐状态
  152. *
  153. * @param array $ids 文章ID数组
  154. * @param bool $isRecommend 是否推荐
  155. * @return bool
  156. */
  157. public static function batchUpdateRecommendStatus(array $ids, bool $isRecommend): bool
  158. {
  159. try {
  160. return DB::transaction(function () use ($ids, $isRecommend) {
  161. return Article::whereIn('id', $ids)->update(['is_recommend' => $isRecommend]);
  162. });
  163. } catch (\Exception $e) {
  164. Log::error('批量更新文章推荐状态失败', [
  165. 'error' => $e->getMessage(),
  166. 'ids' => $ids,
  167. 'is_recommend' => $isRecommend
  168. ]);
  169. return false;
  170. }
  171. }
  172. }