CustomerService.php 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. <?php
  2. namespace App\Services;
  3. use App\Customer;
  4. use App\Services\common\QueryService;
  5. use Illuminate\Database\Eloquent\Builder;
  6. use App\Traits\ServiceAppAop;
  7. class CustomerService
  8. {
  9. use ServiceAppAop;
  10. protected $modelClass=Customer::class;
  11. public function getSelection($column = ['id', 'name'])
  12. {
  13. return Customer::query()->select($column)->get();
  14. }
  15. public function paginate(array $params, array $withs=[])
  16. {
  17. $query = Customer::query()->with($withs)->orderByDesc('id');
  18. if ($params["tags"] ?? false){
  19. $query->whereHas("tags",function ($builder)use($params){
  20. /** @var Builder $builder */
  21. $builder->whereIn("id",explode(",",$params["tags"]));
  22. });
  23. unset($params["tags"]);
  24. }
  25. $columnQueryRules=[
  26. 'company_name' => ['like' => ''],
  27. 'contact_man' => ['like' => ''],
  28. 'phone' => ['like' => ''],
  29. ];
  30. /** @var Builder $query */
  31. $query = app(QueryService::class)->query($params,$query,$columnQueryRules,"customers");
  32. return $query->paginate($params["paginate"] ?? 50);
  33. }
  34. public function create(array $params)
  35. {
  36. return Customer::query()->create($params);
  37. }
  38. public function find($id)
  39. {
  40. return Customer::query()->find($id);
  41. }
  42. public function update(array $params, array $values)
  43. {
  44. $query = Customer::query();
  45. foreach ($params as $column => $value){
  46. $query->where($column,$value);
  47. }
  48. return $query->update($values);
  49. }
  50. public function destroy($id)
  51. {
  52. return Customer::destroy($id);
  53. }
  54. }