CategoryService.php 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  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()->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. "thumb" => $model->thumb,
  28. "level" => $model->level,
  29. "parent_id" => optional($model->parent)->id ?? 0,
  30. "parent_name" => optional($model->parent)->name ?? "",
  31. "child_category_count" => 1, #TODO
  32. "child_goods_count" => 1, #TODO
  33. "weight" => $model->weight,
  34. "index_weight" => $model->index_weight,
  35. "category_weight" => $model->category_weight,
  36. "search_weight" => $model->search_weight,
  37. "created_at" => $model->created_at->format("Y-m-d"),
  38. ];
  39. }, $p->items()),
  40. ];
  41. }
  42. public function store($params): Category
  43. {
  44. $id = $params['id'] ?? 0;
  45. if ($id) {
  46. $model = Category::find($id);
  47. } else {
  48. $model = new Category();
  49. }
  50. $model->name = Arr::get($params, 'name');
  51. $model->thumb = Arr::get($params, 'thumb');
  52. $model->index_weight = Arr::get($params, 'index_weight');
  53. $model->category_weight = Arr::get($params, 'category_weight');
  54. $model->search_weight = Arr::get($params, 'search_weight');
  55. $model->parent_id = Arr::get($params, 'parent_id', 0);
  56. $model->level = 1;
  57. if ($model->parent_id) {
  58. if ($model->parent_id == $model->id) {
  59. throw new ClientException("上级类目不能是当前类目");
  60. }
  61. $parent = Category::findOrFail($model->parent_id);
  62. $model->level = $parent->level + 1;
  63. }
  64. $model->save();
  65. return $model;
  66. }
  67. public function del($params)
  68. {
  69. $category = Category::findOrFail($params['id']);
  70. if ($count = Category::where("parent_id", $category->id)->count() > 0) {
  71. throw new ClientException("该类目下还有{$count}个子分类,请先删除子类目");
  72. }
  73. if ($count = GoodsCategoryMap::where("category_id", $category->id)->count()) {
  74. throw new ClientException("该类目下有{$count}个商品,请先修改对应商品");
  75. }
  76. $category->delete();
  77. }
  78. }