ArticleCategoryLogic.php 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  1. <?php
  2. namespace App\Module\Article\Logics;
  3. use App\Module\Article\Models\Article;
  4. use App\Module\Article\Models\ArticleCate;
  5. use Illuminate\Support\Facades\Log;
  6. /**
  7. * 文章分类逻辑类
  8. *
  9. * 处理文章分类相关的业务逻辑
  10. */
  11. class ArticleCategoryLogic
  12. {
  13. /**
  14. * 创建分类
  15. *
  16. * @param array $data 分类数据
  17. * @return ArticleCate
  18. */
  19. public static function createCategory(array $data): ArticleCate
  20. {
  21. // 检查事务是否已开启
  22. \UCore\Db\Helper::check_tr();
  23. try {
  24. return ArticleCate::create($data);
  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 updateCategory(int $id, array $data): bool
  41. {
  42. // 检查事务是否已开启
  43. \UCore\Db\Helper::check_tr();
  44. try {
  45. $category = ArticleCate::find($id);
  46. if (!$category) {
  47. return false;
  48. }
  49. return $category->update($data);
  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 deleteCategory(int $id): bool
  66. {
  67. try {
  68. return DB::transaction(function () use ($id) {
  69. $category = ArticleCate::find($id);
  70. if (!$category) {
  71. return false;
  72. }
  73. // 检查是否可以删除
  74. if (!$category->can_delete) {
  75. throw new \Exception('该分类不允许删除');
  76. }
  77. // 检查是否有子分类
  78. $childCount = ArticleCate::where('pid', $id)->count();
  79. if ($childCount > 0) {
  80. throw new \Exception('该分类下有子分类,不能删除');
  81. }
  82. // 检查是否有关联文章
  83. $articleCount = Article::where('category_id', $id)->count();
  84. if ($articleCount > 0) {
  85. throw new \Exception('该分类下有文章,不能删除');
  86. }
  87. return $category->delete();
  88. });
  89. } catch (\Exception $e) {
  90. Log::error('删除文章分类失败', [
  91. 'error' => $e->getMessage(),
  92. 'id' => $id
  93. ]);
  94. throw $e;
  95. }
  96. }
  97. /**
  98. * 批量更新分类状态
  99. *
  100. * @param array $ids 分类ID数组
  101. * @param string $status 状态
  102. * @return bool
  103. */
  104. public static function batchUpdateStatus(array $ids, string $status): bool
  105. {
  106. // 检查事务是否已开启
  107. \UCore\Db\Helper::check_tr();
  108. try {
  109. return ArticleCate::whereIn('id', $ids)->update(['status' => $status]);
  110. } catch (\Exception $e) {
  111. Log::error('批量更新文章分类状态失败', [
  112. 'error' => $e->getMessage(),
  113. 'ids' => $ids,
  114. 'status' => $status
  115. ]);
  116. return false;
  117. }
  118. }
  119. /**
  120. * 检查分类标识是否唯一
  121. *
  122. * @param string $unid 分类标识
  123. * @param int|null $excludeId 排除的分类ID
  124. * @return bool
  125. */
  126. public static function isUnidUnique(string $unid, ?int $excludeId = null): bool
  127. {
  128. $query = ArticleCate::where('unid', $unid);
  129. if ($excludeId) {
  130. $query->where('id', '!=', $excludeId);
  131. }
  132. return $query->count() === 0;
  133. }
  134. }