123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397 |
- <?php
- namespace App\Handlers;
- use App\Maps\CacheMap;
- use App\Models\Area;
- use App\Models\AreaSetting;
- use App\Models\Parking;
- use Carbon\Carbon;
- use Illuminate\Support\Facades\Cache;
- use Illuminate\Support\Facades\Redis;
- use Illuminate\Support\Facades\Log;
- class BikeHandler
- {
- const BIKE_TO_USER = 500;
-
- public function byLocationCheckIsInBanStopParking($lat, $lng, $area_id = 0, $user_lat = 0, $user_lng = 0)
- {
- $stop_bike_all_station_db = Cache::remember(Parking::REDIS_BAN_STOP_BIKE_ALL_SITE_TAG . '_' . $area_id, Carbon::now()->addHours(1), function () use ($area_id) {
- return Parking::where('area_id', $area_id)->where('type', Parking::TYPE_NO_STOP_BIKE)->where('status', Parking::STATUS_OK)->get();
- });
- if (count($stop_bike_all_station_db) === 0) {
-
- return false;
- }
- $ConverHandler = new ConvertHandler();
- $parking_fences = $stop_bike_all_station_db->pluck('parking_fence');
- $is_in_station = $ConverHandler->is_point_in_polygons([
- 'latitude' => $lat,
- 'longitude' => $lng
- ], $parking_fences);
- if ($is_in_station) {
- return true;
- }
-
- if ($user_lat != 0) {
- $distance = $ConverHandler->distance($lat, $lng, $user_lat, $user_lng);
- if (self::BIKE_TO_USER > $distance) {
- $is_in_station = $ConverHandler->is_point_in_polygons([
- 'latitude' => $user_lat,
- 'longitude' => $user_lng
- ], $parking_fences);
- if ($is_in_station) {
- return true;
- }
- }
- }
- return false;
- }
-
- public function byLocationCheckIsInStopParking($lat, $lng, $area_id = 0, $user_lat = 0, $user_lng = 0)
- {
- $options = ['SORT' => 'ASC', 'COUNT' => 1, 'WITHDIST' => true];
- $redis = Redis::connection();
- $stop_bike_stations = $redis->georadius(Parking::REDIS_STOP_BIKE_SITE_TAG . "_{$area_id}", $lng, $lat, 20000, 'm', $options);
- if (!count($stop_bike_stations)) {
-
- return [
- 'status' => false,
- 'distance' => 999999
- ];
- }
- $stop_bike_station = $stop_bike_stations[0];
- $distance = $stop_bike_station[1];
- $park_id = $stop_bike_station[0];
- $isOpenStopBikeFushe = Cache::remember(CacheMap::IS_OPEN_STOP_BIKE_FUSHE . ':' . $area_id, Carbon::now()->addSeconds(CacheMap::CACHE_TIME), function () use ($area_id) {
- return AreaSetting::where('area_id', $area_id)->value('is_parking_fushe');
- });
- $ids = array_column($stop_bike_stations, 0);
- $stop_bike_station_db = Cache::remember("PARKINGS_" . $area_id, Carbon::now()->addHours(1), function () use ($ids, $area_id) {
- return Parking::where('area_id', $area_id)->where('type', Parking::TYPE_STOP_BIKE)->where('status', Parking::STATUS_OK)->get(['id', 'parking_fence', 'parking_centre', 'parking_radius'])->keyBy('id');
- });
- $ConverHandler = new ConvertHandler();
- $CheckAreaHandler = '';
- if ($isOpenStopBikeFushe) {
-
-
- $is_in_near_station = $ConverHandler->is_point_in_circles([
- 'latitude' => $lat,
- 'longitude' => $lng
- ], $stop_bike_station_db);
- if ($is_in_near_station) {
- return [
- 'status' => true,
- 'type' => 'circle',
- 'distance' => $distance,
- ];
- }
-
- if ($user_lat != 0) {
- $bike_to_user_distance = $ConverHandler->distance($lat, $lng, $user_lat, $user_lng);
- if (self::BIKE_TO_USER > $bike_to_user_distance) {
- $is_in_near_station = $ConverHandler->is_point_in_circles([
- 'latitude' => $user_lat,
- 'longitude' => $user_lng
- ], $stop_bike_station_db);
- if ($is_in_near_station) {
- return [
- 'status' => true,
- 'type' => 'circle',
- 'distance' => $distance
- ];
- }
- }
- }
- } else {
-
- $prakings = $stop_bike_station_db->pluck('parking_fence', 'id');
- $is_in_station = $ConverHandler->is_point_in_polygons([
- 'latitude' => $lat,
- 'longitude' => $lng
- ], $prakings);
- if ($is_in_station) {
- return [
- 'status' => true,
- 'distance' => $distance
- ];
- }
-
- if ($user_lat != 0) {
- $bike_to_user_distance = $ConverHandler->distance($lat, $lng, $user_lat, $user_lng);
- if (self::BIKE_TO_USER > $bike_to_user_distance) {
- $is_in_near_station = $ConverHandler->is_point_in_polygons([
- 'latitude' => $user_lat,
- 'longitude' => $user_lng
- ], $prakings);
- if ($is_in_near_station) {
- return [
- 'status' => true,
- 'distance' => $distance
- ];
- }
- }
- }
- }
- return [
- 'status' => false,
- 'distance' => $distance
- ];
- }
-
- public function byDistanceGetDistanceMoney($distance, $areaSetting)
- {
- $gl = $areaSetting->min_limit_km * 1000;
- if ($distance <= $gl) {
- return $areaSetting->min_dispatching_fee;
- }
- $over_distance = $distance - $gl;
- $over_money = bcadd($areaSetting->min_dispatching_fee, ceil($over_distance / (1000 * $areaSetting->over_limit_per_km)) * $areaSetting->over_limit_per_km_money, 2);
- if ($over_money < $areaSetting->max_dispatching_fee) {
- return $over_money;
- }
- return $areaSetting->max_dispatching_fee;
- }
- }
|