1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465 |
- <?php
- namespace App\Models;
- use App\Maps\CacheMap;
- use Carbon\Carbon;
- use Illuminate\Support\Facades\Cache;
- use Illuminate\Support\Facades\Log;
- class LocationLogMongodb extends \Jenssegers\Mongodb\Eloquent\Model
- {
- protected $connection = 'mongodb';
- protected $primaryKey = 'id'; //设置主键
- protected $table = 'location_logs';
- protected $guarded = [];
- const STATUS_OK = 1;
- const STATUS_PAUSE = 0;
- public static $statusMaps = [
- self::STATUS_OK => '正常',
- self::STATUS_PAUSE => '暂停'
- ];
- const RENT_YES = 1;
- const RENT_NO = 0;
- public static $rentMaps = [
- self::RENT_NO => '非日租订单',
- self::RENT_YES => '日租订单',
- ];
- /**
- * 获取车辆最新定位
- *
- * @param $bike_no string 车辆编号
- *
- * @return $newestLngLat array 车辆最新定位
- * */
- public static function getNewestLocationByBikeNo($bike_no)
- {
- $newestLocation = Cache::remember(CacheMap::BIKE_REFRESH_LOCATION_MONGODB . $bike_no, Carbon::now()->addSeconds(30), function () use ($bike_no) {
- return self::query()->where('bike_no', (string)$bike_no)->orderByDesc('box_time')->first();
- });
- // $newestLocation = self::query()->where('bike_no', (string)$bike_no)->orderByDesc('box_time')->first();
- $newestLngLat = ['longitude' => 0, 'latitude' => 0, 'battery_power' => 0];
- if (CacheMap::IS_OPEN_MONGODB_DUG) {
- return $newestLngLat;
- }
- if (empty($newestLocation)) {
- return $newestLngLat;
- } else {
- $newestLngLat['longitude'] = $newestLocation->longitude;
- $newestLngLat['latitude'] = $newestLocation->latitude;
- $newestLngLat['battery_power'] = $newestLocation->battery_power;
- return $newestLngLat;
- }
- }
- }
|