CategoryService.php 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. <?php
  2. namespace App\Modules\Admin\Services;
  3. use App\Base\BaseService;
  4. use App\Exceptions\ClientException;
  5. use App\Models\Goods\Category;
  6. use App\Models\Goods\GoodsCategoryMap;
  7. use Illuminate\Database\Eloquent\Builder;
  8. use Illuminate\Support\Arr;
  9. class CategoryService extends BaseService
  10. {
  11. public function paginate($params)
  12. {
  13. $p = Category::query()->with(["parent", "childS", "gcMap"])->when($params['level'], function (Builder $query) use ($params) {
  14. return $query->where("level", $params['level']);
  15. })->when($params['name'], function (Builder $query) use ($params) {
  16. return $query->where(function (Builder $query) use ($params) {
  17. return $query->where("name", "like", "%{$params['name']}%");
  18. });
  19. })->orderByDesc("id")->paginate($params['page_size']);
  20. return [
  21. "total" => $p->total(),
  22. "page_total" => $p->lastPage(),
  23. "list" => array_map(function (Category $model) {
  24. return [
  25. "id" => $model->id,
  26. "name" => $model->name,
  27. "en_name" => $model->en_name,
  28. "thumb" => $model->thumb,
  29. "level" => $model->level,
  30. "parent_id" => optional($model->parent)->id ?? 0,
  31. "parent_name" => optional($model->parent)->name ?? "",
  32. "child_category_count" => $model->childS->count(),
  33. "child_goods_count" => $model->gcMap->count(),
  34. "weight" => $model->weight,
  35. "index_weight" => $model->index_weight,
  36. "category_weight" => $model->category_weight,
  37. "search_weight" => $model->search_weight,
  38. "created_at" => $model->created_at->format("Y-m-d"),
  39. ];
  40. }, $p->items()),
  41. ];
  42. }
  43. public function store($params): Category
  44. {
  45. $id = $params['id'] ?? 0;
  46. if ($id) {
  47. $model = Category::find($id);
  48. } else {
  49. $model = new Category();
  50. }
  51. $model->name = Arr::get($params, 'name');
  52. $model->en_name = Arr::get($params, 'en_name');
  53. $model->thumb = Arr::get($params, 'thumb');
  54. $model->index_weight = Arr::get($params, 'index_weight');
  55. $model->category_weight = Arr::get($params, 'category_weight');
  56. $model->search_weight = Arr::get($params, 'search_weight');
  57. $model->parent_id = Arr::get($params, 'parent_id', 0);
  58. $model->level = 1;
  59. if ($model->parent_id) {
  60. if ($model->parent_id == $model->id) {
  61. throw new ClientException("上级类目不能是当前类目");
  62. }
  63. $parent = Category::findOrFail($model->parent_id);
  64. $model->level = $parent->level + 1;
  65. }
  66. $model->save();
  67. return $model;
  68. }
  69. public function del($params)
  70. {
  71. $category = Category::findOrFail($params['id']);
  72. if ($category->level == 1) {
  73. throw new ClientException("一级类目不能删除");
  74. }
  75. if ($count = Category::where("parent_id", $category->id)->count() > 0) {
  76. throw new ClientException("该类目下还有{$count}个子分类,请先删除子类目");
  77. }
  78. if ($count = GoodsCategoryMap::where("category_id", $category->id)->count()) {
  79. throw new ClientException("该类目下有{$count}个商品,请先修改对应商品");
  80. }
  81. $category->delete();
  82. }
  83. }