LocationLogMongodb.php 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  1. <?php
  2. namespace App\Models;
  3. use App\Maps\CacheMap;
  4. use Carbon\Carbon;
  5. use Illuminate\Support\Facades\Cache;
  6. use Illuminate\Support\Facades\Log;
  7. class LocationLogMongodb extends \Jenssegers\Mongodb\Eloquent\Model
  8. {
  9. protected $connection = 'mongodb';
  10. protected $primaryKey = 'id'; //设置主键
  11. protected $table = 'location_logs';
  12. protected $guarded = [];
  13. const STATUS_OK = 1;
  14. const STATUS_PAUSE = 0;
  15. public static $statusMaps = [
  16. self::STATUS_OK => '正常',
  17. self::STATUS_PAUSE => '暂停'
  18. ];
  19. const RENT_YES = 1;
  20. const RENT_NO = 0;
  21. public static $rentMaps = [
  22. self::RENT_NO => '非日租订单',
  23. self::RENT_YES => '日租订单',
  24. ];
  25. /**
  26. * 获取车辆最新定位
  27. *
  28. * @param $bike_no string 车辆编号
  29. *
  30. * @return $newestLngLat array 车辆最新定位
  31. * */
  32. public static function getNewestLocationByBikeNo($bike_no)
  33. {
  34. $newestLocation = Cache::remember(CacheMap::BIKE_REFRESH_LOCATION_MONGODB . $bike_no, Carbon::now()->addSeconds(30), function () use ($bike_no) {
  35. return self::query()->where('bike_no', (string)$bike_no)->orderByDesc('box_time')->first();
  36. });
  37. // $newestLocation = self::query()->where('bike_no', (string)$bike_no)->orderByDesc('box_time')->first();
  38. $newestLngLat = ['longitude' => 0, 'latitude' => 0, 'battery_power' => 0];
  39. if (CacheMap::IS_OPEN_MONGODB_DUG) {
  40. return $newestLngLat;
  41. }
  42. if (empty($newestLocation)) {
  43. return $newestLngLat;
  44. } else {
  45. $newestLngLat['longitude'] = $newestLocation->longitude;
  46. $newestLngLat['latitude'] = $newestLocation->latitude;
  47. $newestLngLat['battery_power'] = $newestLocation->battery_power;
  48. return $newestLngLat;
  49. }
  50. }
  51. }