AutoCloseBikeLockJob.php 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. <?php
  2. namespace App\Jobs;
  3. use App\Handlers\BikeControl;
  4. use App\Handlers\BikeStatusInfoSyncHandler;
  5. use App\Models\Bike;
  6. use App\Models\LocationsLog;
  7. use App\Models\WorkerBikeOperate;
  8. use Carbon\Carbon;
  9. use Illuminate\Bus\Queueable;
  10. use Illuminate\Queue\SerializesModels;
  11. use Illuminate\Queue\InteractsWithQueue;
  12. use Illuminate\Contracts\Queue\ShouldQueue;
  13. use Illuminate\Foundation\Bus\Dispatchable;
  14. class AutoCloseBikeLockJob implements ShouldQueue
  15. {
  16. use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;
  17. protected $bike;
  18. public $tries = 5;
  19. /**
  20. * Create a new job instance.
  21. *
  22. * @return void
  23. */
  24. public function __construct(Bike $bike)
  25. {
  26. //
  27. $this->bike = $bike;
  28. }
  29. /**
  30. * Execute the job.
  31. *
  32. * @return
  33. */
  34. public function handle()
  35. {
  36. //
  37. $bike = $this->bike;
  38. $res = app()->redis->hexists(BikeStatusInfoSyncHandler::REDIS_RIDE_BIKE_WORKER_ORDERS_TAG, $bike->bike_no);
  39. if ($res) {
  40. if (!$bike->is_riding) {
  41. $bike_location = LocationsLog::getNewestLocationByBikeNo($bike->bike_no);
  42. (new BikeStatusInfoSyncHandler())->toBikeWaitRideStatus($bike->bike_no, $bike_location['lng'], $bike_location['lat'], $bike->put_status);
  43. $bike->last_use_bike_end_time = Carbon::now();
  44. $bike->is_lock = Bike::LOCK_YES;
  45. $bike->save();
  46. BikeControl::closeLock($bike->box_no);
  47. // 添加记录
  48. $worker_bike_operate = new WorkerBikeOperate();
  49. $worker_bike_operate->type = WorkerBikeOperate::TYPE_CLOSE_BIKE_LOCK;
  50. $worker_bike_operate->name = WorkerBikeOperate::$typeMaps[WorkerBikeOperate::TYPE_CLOSE_BIKE_LOCK];
  51. $worker_bike_operate->bike_id = $bike->id;
  52. $last_location = object_array(json_decode($bike->last_location));
  53. $worker_bike_operate->latitude = empty($last_location['lat']) ? 0 : $last_location['lat'];
  54. $worker_bike_operate->longitude = empty($last_location['lng']) ? 0 : $last_location['lng'];
  55. $worker_bike_operate->reason = "系统自动关锁";
  56. $worker_bike_operate->worker_id = 0;
  57. $worker_bike_operate->save();
  58. }
  59. }
  60. return true;
  61. }
  62. }