SearchService.php 4.0 KB

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