LocationsLog.php 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. <?php
  2. namespace App\Models;
  3. use Carbon\Carbon;
  4. use Illuminate\Database\Eloquent\Model;
  5. class LocationsLog extends \Jenssegers\Mongodb\Eloquent\Model
  6. {
  7. protected $connection = 'mongodb';
  8. protected $primaryKey = 'id'; //设置主键
  9. protected $table = 'location_logs';
  10. protected $guarded = [];
  11. const RENT_YES = 1;
  12. const RENT_NO = 0;
  13. public static $rentMaps = [
  14. self::RENT_NO => '非日租订单',
  15. self::RENT_YES => '日租订单',
  16. ];
  17. const USER_LOCATION = 'user';
  18. const WORKER_LOCATION = 'worker';
  19. const NO_LOCATION = 'no';
  20. /**
  21. * 获取车辆最新定位
  22. *
  23. * @param $bike_no string 车辆编号
  24. *
  25. * @return $newestLngLat array 车辆最新定位
  26. * */
  27. public static function getNewestLocationByBikeNo($bike_no)
  28. {
  29. $newestLocation = self::query()->where('bike_no', (string)$bike_no)->whereBetween('latitude', [3, 53])->whereBetween('longitude', [73, 136])->orderByDesc('created_at')->first();
  30. $newestLngLat = ['lng' => 0, 'lat' => 0];
  31. if (empty($newestLocation)) {
  32. return $newestLngLat;
  33. } else {
  34. $newestLngLat['lng'] = $newestLocation->longitude;
  35. $newestLngLat['lat'] = $newestLocation->latitude;
  36. return $newestLngLat;
  37. }
  38. }
  39. /**
  40. * 获取车辆最新定位 以及时间
  41. *
  42. * @param $bike_no string 车辆编号
  43. *
  44. * @return $newestLngLat array 车辆最新定位 及时间
  45. * */
  46. public static function getNewestLocationTimeByBikeNo($bike_no)
  47. {
  48. $newestLocation = self::query()->where('bike_no', (string)$bike_no)->whereBetween('latitude', [3, 53])->whereBetween('longitude', [73, 136])->orderByDesc('created_at')->first();
  49. $newestLngLat = ['lng' => 0, 'lat' => 0];
  50. if (empty($newestLocation)) {
  51. return ['location' => $newestLngLat, 'time' => '获取失败'];
  52. } else {
  53. $newestLngLat['lng'] = $newestLocation->longitude;
  54. $newestLngLat['lat'] = $newestLocation->latitude;
  55. return ['location' => $newestLngLat, 'time' => Carbon::make($newestLocation->created_at)->format('Y/m/d H:i:s')];
  56. }
  57. }
  58. }