SearchService.php 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. <?php
  2. namespace App\Services;
  3. use App\Models\Address;
  4. use App\Models\Client;
  5. use App\Models\Provider;
  6. use App\Rules\ScheduleBusinessRules;
  7. use App\Services\DistanceService;
  8. use Illuminate\Support\Facades\Auth;
  9. use Illuminate\Support\Facades\DB;
  10. class SearchService
  11. {
  12. public function __construct() {}
  13. public function buscaPrestadores(?string $name = null, ?string $date = null): array
  14. {
  15. $user = Auth::user();
  16. $cliente = Client::where('user_id', $user->id)->first();
  17. $blockedProviderIds = ScheduleBusinessRules::getBlockedProviderIdsForClient($cliente->id);
  18. $providersWithWorkingDays = ScheduleBusinessRules::getProviderIdsWithWorkingDays();
  19. $clientPrimaryAddress = Address::where('source', 'client')
  20. ->where('source_id', $cliente->id)
  21. ->orderBy('is_primary', 'desc')
  22. ->first();
  23. $distanceSelect = $this->distanceSelect(
  24. $clientPrimaryAddress?->latitude !== null ? (float) $clientPrimaryAddress->latitude : null,
  25. $clientPrimaryAddress?->longitude !== null ? (float) $clientPrimaryAddress->longitude : null,
  26. );
  27. $baseQuery = Provider::leftJoin('users as provider_user', 'provider_user.id', '=', 'providers.user_id')
  28. ->leftJoin(DB::raw("
  29. (
  30. SELECT DISTINCT ON (source_id)
  31. *
  32. FROM addresses
  33. WHERE source = 'provider'
  34. AND deleted_at IS NULL
  35. ORDER BY source_id, is_primary DESC
  36. ) as provider_address
  37. "), 'provider_address.source_id', '=', 'providers.id')
  38. ->whereNotNull('provider_address.id')
  39. ->whereNotIn('providers.id', $blockedProviderIds)
  40. ->whereIn('providers.id', $providersWithWorkingDays)
  41. ->whereNull('providers.deleted_at')
  42. ->whereNotNull('providers.daily_price_8h')
  43. ->whereNotNull('providers.daily_price_6h')
  44. ->whereNotNull('providers.daily_price_4h')
  45. ->whereNotNull('providers.daily_price_2h')
  46. ->when($name, fn ($q) => $q->where('provider_user.name', 'ILIKE', "%{$name}%"))
  47. ->select(
  48. 'providers.id as provider_id',
  49. 'provider_user.name as provider_name',
  50. 'provider_address.district',
  51. 'provider_address.latitude as provider_latitude',
  52. 'provider_address.longitude as provider_longitude',
  53. 'providers.average_rating',
  54. 'providers.total_services',
  55. 'providers.daily_price_8h',
  56. 'providers.daily_price_6h',
  57. 'providers.daily_price_4h',
  58. 'providers.daily_price_2h',
  59. 'providers.created_at',
  60. DB::raw("(
  61. SELECT COUNT(*)
  62. FROM reviews
  63. LEFT JOIN schedules ON schedules.id = reviews.schedule_id
  64. WHERE reviews.origin = 'provider'
  65. AND schedules.provider_id = providers.id
  66. ) as total_reviews"),
  67. $distanceSelect,
  68. )
  69. ->orderBy('providers.average_rating', 'desc');
  70. $providers = (clone $baseQuery)
  71. ->when(
  72. $clientPrimaryAddress?->city_id,
  73. fn ($query, int $cityId) => $query->where('provider_address.city_id', $cityId)
  74. )
  75. ->get();
  76. if ($providers->isEmpty() && $clientPrimaryAddress?->city_id) {
  77. $providers = $baseQuery->get();
  78. }
  79. return $providers
  80. ->when(
  81. $date,
  82. fn ($collection) => $collection->whereIn(
  83. 'provider_id',
  84. ScheduleBusinessRules::getAvailableProviderIdsForDate(
  85. $date,
  86. $collection->pluck('provider_id')
  87. )->toArray()
  88. )->values()
  89. )
  90. ->toArray();
  91. }
  92. private function distanceSelect(?float $clientLatitude, ?float $clientLongitude): \Illuminate\Contracts\Database\Query\Expression
  93. {
  94. return DistanceService::sqlExpression($clientLatitude, $clientLongitude);
  95. }
  96. }