CustomerService.php 1.6 KB

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