CustomerService.php 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  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. public function getSelection($column = ['id', 'name'])
  11. {
  12. return Customer::query()->select($column)->get();
  13. }
  14. public function paginate(array $params, array $withs=[])
  15. {
  16. $query = Customer::query()->with($withs)->orderByDesc('id');
  17. if ($params["tags"] ?? false){
  18. $query->whereHas("tags",function ($builder)use($params){
  19. /** @var Builder $builder */
  20. $builder->whereIn("id",explode(",",$params["tags"]));
  21. });
  22. unset($params["tags"]);
  23. }
  24. $columnQueryRules=[
  25. 'company_name' => ['like' => ''],
  26. 'contact_man' => ['like' => ''],
  27. 'phone' => ['like' => ''],
  28. ];
  29. /** @var Builder $query */
  30. $query = app(QueryService::class)->query($params,$query,$columnQueryRules,"customers");
  31. return $query->paginate($params["paginate"] ?? 50);
  32. }
  33. public function create(array $params)
  34. {
  35. return Customer::query()->create($params);
  36. }
  37. public function find($id)
  38. {
  39. return Customer::query()->find($id);
  40. }
  41. public function update(array $params, array $values)
  42. {
  43. $query = Customer::query();
  44. foreach ($params as $column => $value){
  45. $query->where($column,$value);
  46. }
  47. return $query->update($values);
  48. }
  49. public function destroy($id)
  50. {
  51. return Customer::destroy($id);
  52. }
  53. }