ArticleLogic.php 4.6 KB

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