LocationLogRepository.php 2.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  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\Maps\CacheMap;
  10. use App\Models\LocationLogMongodb;
  11. use App\Models\Parking;
  12. use Carbon\Carbon;
  13. use Illuminate\Support\Facades\Cache;
  14. class LocationLogRepository extends BaseRepository
  15. {
  16. public function __construct(LocationLogMongodb $model)
  17. {
  18. $this->model = $model;
  19. }
  20. /**
  21. * 获取车的最后定位
  22. * @param $id
  23. * @return mixed
  24. * User: Mead
  25. */
  26. public function byBikeNoGetLastLocation($bike_no, $is_open = false)
  27. {
  28. if ($is_open === true) {
  29. return [
  30. 'lat' => 0,
  31. 'lng' => 0,
  32. 'mileage' => 0,
  33. 'is_yundong' => 0,
  34. 'speed' => 0
  35. ];
  36. }
  37. $location = Cache::remember(CacheMap::BIKE_LOCATION_MONGODB . $bike_no, Carbon::now()->addSeconds(10), function () use ($bike_no) {
  38. return $this->model->where('bike_no', $bike_no)->select(['latitude', 'longitude', 'mileage', 'speed', 'is_yundong'])->orderByDesc('box_time')->first();
  39. });
  40. return [
  41. 'lat' => $location['latitude'],
  42. 'lng' => $location['longitude'],
  43. 'mileage' => $location['mileage'],
  44. 'is_yundong' => $location['is_yundong'],
  45. 'speed' => $location['speed']
  46. ];
  47. }
  48. /**
  49. * 根据订单显示获取最后位置
  50. * @param $id
  51. * @return array
  52. * User: Mead
  53. */
  54. public function byOrderIdGetLastLocation($id, $is_open = false)
  55. {
  56. if ($is_open === true) {
  57. return [
  58. 'lat' => 0,
  59. 'lng' => 0,
  60. 'mileage' => 0,
  61. 'is_yundong' => 0,
  62. 'speed' => 0
  63. ];
  64. }
  65. $location = Cache::remember(CacheMap::BIKE_LOCATION_MONGODB . $id, Carbon::now()->addSeconds(10), function () use ($id) {
  66. return $this->model->where('order_id', $id)->select(['latitude', 'longitude', 'mileage', 'speed', 'is_yundong'])->orderByDesc('box_time')->first();
  67. });
  68. return [
  69. 'lat' => $location['latitude'],
  70. 'lng' => $location['longitude'],
  71. 'mileage' => $location['mileage'],
  72. 'is_yundong' => $location['is_yundong'],
  73. 'speed' => $location['speed']
  74. ];
  75. }
  76. }