| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153 |
- <?php
- namespace App\Module\Article\Logics;
- use App\Module\Article\Models\Article;
- use App\Module\Article\Models\ArticleCate;
- use Illuminate\Support\Facades\Log;
- /**
- * 文章分类逻辑类
- *
- * 处理文章分类相关的业务逻辑
- */
- class ArticleCategoryLogic
- {
- /**
- * 创建分类
- *
- * @param array $data 分类数据
- * @return ArticleCate
- */
- public static function createCategory(array $data): ArticleCate
- {
- // 检查事务是否已开启
- \UCore\Db\Helper::check_tr();
- try {
- return ArticleCate::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 updateCategory(int $id, array $data): bool
- {
- // 检查事务是否已开启
- \UCore\Db\Helper::check_tr();
- try {
- $category = ArticleCate::find($id);
- if (!$category) {
- return false;
- }
- return $category->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 deleteCategory(int $id): bool
- {
- try {
- return DB::transaction(function () use ($id) {
- $category = ArticleCate::find($id);
- if (!$category) {
- return false;
- }
- // 检查是否可以删除
- if (!$category->can_delete) {
- throw new \Exception('该分类不允许删除');
- }
- // 检查是否有子分类
- $childCount = ArticleCate::where('pid', $id)->count();
- if ($childCount > 0) {
- throw new \Exception('该分类下有子分类,不能删除');
- }
- // 检查是否有关联文章
- $articleCount = Article::where('category_id', $id)->count();
- if ($articleCount > 0) {
- throw new \Exception('该分类下有文章,不能删除');
- }
- return $category->delete();
- });
- } catch (\Exception $e) {
- Log::error('删除文章分类失败', [
- 'error' => $e->getMessage(),
- 'id' => $id
- ]);
- throw $e;
- }
- }
- /**
- * 批量更新分类状态
- *
- * @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 ArticleCate::whereIn('id', $ids)->update(['status' => $status]);
- } catch (\Exception $e) {
- Log::error('批量更新文章分类状态失败', [
- 'error' => $e->getMessage(),
- 'ids' => $ids,
- 'status' => $status
- ]);
- return false;
- }
- }
- /**
- * 检查分类标识是否唯一
- *
- * @param string $unid 分类标识
- * @param int|null $excludeId 排除的分类ID
- * @return bool
- */
- public static function isUnidUnique(string $unid, ?int $excludeId = null): bool
- {
- $query = ArticleCate::where('unid', $unid);
- if ($excludeId) {
- $query->where('id', '!=', $excludeId);
- }
- return $query->count() === 0;
- }
- }
|