CategoryService.php 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  1. <?php
  2. /**
  3. * @Name
  4. * @Description
  5. * @Author 刘学玺
  6. * @Date 2024/8/27 10:52
  7. */
  8. namespace App\Http\Services\Backend\Server\Service;
  9. use App\Http\Services\Service;
  10. use App\Models\Service\Category;
  11. use App\Models\System\Dept;
  12. use App\Models\System\DictData;
  13. use App\Models\System\DictType;
  14. use Symfony\Component\HttpFoundation\Response;
  15. class CategoryService extends Service
  16. {
  17. protected array $simpleColumn = ['id', 'title', 'cover'];
  18. protected array $selectColumn = ['sort', 'status'];
  19. protected array $appendColumn = ['created_at as createTime'];
  20. public function getCategoryList($params)
  21. {
  22. $category = Category::query();
  23. isset($params['title']) && filled($params['title']) && $category->where('title', "%{$params['title']}%");
  24. isset($params['status']) && filled($params['status']) && $category->where('status', $params['status']);
  25. $categoryPage = $category->paginate($params['pageSize'], [...$this->simpleColumn, ...$this->selectColumn, ...$this->appendColumn], 'page', $params['pageNo']);
  26. return ['list' => $categoryPage->items(), 'total' => $categoryPage->total()];
  27. }
  28. public function createCategory(array $data)
  29. {
  30. $category = self::toModel($data, Category::class);
  31. return $category->create($category->getAttributes())->id;
  32. }
  33. public function getCategory(int $id)
  34. {
  35. return Category::query()->select([...$this->simpleColumn, ...$this->selectColumn])->find($id);
  36. }
  37. public function updateCategory(array $data, int $id): void
  38. {
  39. $category = self::toModel($data, Category::class);
  40. $category->where('id',$id)->update($category->getAttributes());
  41. }
  42. public function deleteCategory(int $id)
  43. {
  44. $category = self::toModel(['id' => $id], Category::class);
  45. return $category->delete();
  46. }
  47. }