LocationLogRepository.php 3.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. <?php
  2. /**
  3. * Created by PhpStorm.
  4. * User: Mead
  5. * Date: 2019/8/6
  6. * Time: 9:16 PM
  7. */
  8. namespace App\Repositories;
  9. use App\Handlers\BikeStatusInfoSyncHandler;
  10. use App\Maps\CacheMap;
  11. use App\Models\LocationLogMongodb;
  12. use App\Models\Parking;
  13. use Carbon\Carbon;
  14. use Illuminate\Support\Facades\Cache;
  15. use Illuminate\Support\Facades\Log;
  16. class LocationLogRepository extends BaseRepository
  17. {
  18. public function __construct(LocationLogMongodb $model)
  19. {
  20. $this->model = $model;
  21. }
  22. /**
  23. * 获取车的最后定位
  24. * @param $id
  25. * @return mixed
  26. * User: Mead
  27. */
  28. public function byBikeNoGetLastLocation($bike_no, $is_open = false)
  29. {
  30. if ($is_open === true) {
  31. return [
  32. 'lat' => 0,
  33. 'lng' => 0,
  34. 'mileage' => 0,
  35. 'is_yundong' => 0,
  36. 'speed' => 0
  37. ];
  38. }
  39. $location = (new BikeStatusInfoSyncHandler())->byBikeNoGetLocation($bike_no);
  40. if ($location['time']) {
  41. $location['speed'] = 0;
  42. return $location;
  43. }
  44. $location = Cache::remember(CacheMap::BIKE_LOCATION_MONGODB . $bike_no, Carbon::now()->addSeconds(3), function () use ($bike_no) {
  45. return $this->model->where('day', date('Ymd'))->where('bike_no', $bike_no)->select(['latitude', 'longitude', 'mileage', 'speed', 'is_yundong'])->orderByDesc('_id')->first();
  46. });
  47. // $location = Cache::remember(CacheMap::BIKE_LOCATION_MONGODB . $bike_no, Carbon::now()->addSeconds(10), function () use ($bike_no) {
  48. // return $this->model->where('bike_no', $bike_no)->select(['latitude', 'longitude', 'mileage', 'speed', 'is_yundong'])->orderByDesc('box_time')->first();
  49. // });
  50. return [
  51. 'lat' => $location['latitude'],
  52. 'lng' => $location['longitude'],
  53. 'mileage' => $location['mileage'],
  54. 'is_yundong' => $location['is_yundong'],
  55. 'speed' => $location['speed']
  56. ];
  57. }
  58. /**
  59. * 根据订单显示获取最后位置
  60. * @param $id
  61. * @return array
  62. * User: Mead
  63. */
  64. public function byOrderIdGetLastLocation($id, $is_open = false)
  65. {
  66. if ($is_open === true) {
  67. return [
  68. 'lat' => 0,
  69. 'lng' => 0,
  70. 'mileage' => 0,
  71. 'is_yundong' => 0,
  72. 'speed' => 0
  73. ];
  74. }
  75. $location = Cache::remember(CacheMap::BIKE_LOCATION_MONGODB . $id, Carbon::now()->addSeconds(10), function () use ($id) {
  76. return $this->model->where('order_id', $id)->select(['latitude', 'longitude', 'mileage', 'speed', 'is_yundong'])->orderByDesc('box_time')->first();
  77. });
  78. return [
  79. 'lat' => $location['latitude'],
  80. 'lng' => $location['longitude'],
  81. 'mileage' => $location['mileage'],
  82. 'is_yundong' => $location['is_yundong'],
  83. 'speed' => $location['speed']
  84. ];
  85. }
  86. }