BikeStatusInfoSyncHandler.php 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  1. <?php
  2. /**
  3. * Created by PhpStorm.
  4. * User: Mead
  5. * Date: 2019/10/8
  6. * Time: 3:41 PM
  7. */
  8. namespace App\Handlers;
  9. use App\Utils\Admin;
  10. use Illuminate\Support\Facades\Redis;
  11. class BikeStatusInfoSyncHandler
  12. {
  13. //用户
  14. const ROLE_USER = 'user';
  15. //运维人员
  16. const ROLE_WORKER = 'worker';
  17. //系统本身
  18. const ROLE_SERVER = 'server';
  19. //绑定人员
  20. const ROLE_BIND = 'bind';
  21. //还车时过期时间
  22. const CLOSE_BIKE_TTL = 10;
  23. private static $redis = '';
  24. //正常未骑行车的缓存标识
  25. const REDIS_BIKE_LOCATION_TAG = 'bike_locations';
  26. const REDIS_RIDE_BIKE_ORDERS_TAG = 'ride_orders:';
  27. const REDIS_RIDE_BIKE_WORKER_ORDERS_TAG = 'ride_orders_worker';
  28. public function __construct()
  29. {
  30. self::$redis = Redis::connection();
  31. }
  32. /**
  33. * 车辆处于骑行状态 (用户订单时(data: 订单id[id],区域id[area_id],车辆id[bike_id]))
  34. * @param $bike_no
  35. * @param array $data
  36. * User: Mead
  37. */
  38. public function toBikeRideStatus($role, $bike_no, $data = [], $is_put_status = 1)
  39. {
  40. $data['role'] = $role;
  41. self::$redis->set(self::REDIS_RIDE_BIKE_ORDERS_TAG . $bike_no, serialize($data));
  42. //删除未骑行状态
  43. if ($is_put_status) {
  44. self::$redis->zrem(self::REDIS_BIKE_LOCATION_TAG, $bike_no);
  45. }
  46. if ($role == self::ROLE_WORKER) {
  47. self::$redis->hset(self::REDIS_RIDE_BIKE_WORKER_ORDERS_TAG, $bike_no, Admin::user()->id);
  48. }
  49. }
  50. /**
  51. * 车辆处于等待骑行状态
  52. * @param $bike_no
  53. * User: Mead
  54. */
  55. public function toBikeWaitRideStatus($bike_no, $lng = 0, $lat = 0, $is_put_status = 1)
  56. {
  57. // self::$redis->hdel(self::REDIS_RIDE_BIKE_ORDERS_TAG, $bike_no);
  58. self::$redis->expire(self::REDIS_RIDE_BIKE_ORDERS_TAG . $bike_no, self::CLOSE_BIKE_TTL);
  59. if ($is_put_status) {
  60. self::$redis->geoadd(self::REDIS_BIKE_LOCATION_TAG, $lng, $lat, $bike_no);
  61. }
  62. // 不存在就忽略
  63. self::$redis->hdel(self::REDIS_RIDE_BIKE_WORKER_ORDERS_TAG, $bike_no);
  64. }
  65. /**
  66. * 车辆上线
  67. * @param $bike_no
  68. * @param int $lat
  69. * @param int $lng
  70. * User: Mead
  71. */
  72. public function toBikeOnLineStatus($bike_no, $lng = 0, $lat = 0)
  73. {
  74. self::$redis->del([self::REDIS_RIDE_BIKE_ORDERS_TAG . $bike_no]);
  75. self::$redis->geoadd(self::REDIS_BIKE_LOCATION_TAG, $lng, $lat, $bike_no);
  76. }
  77. /**
  78. * 车辆离线
  79. * @param $bike_no
  80. * User: Mead
  81. */
  82. public function toBikeOffLineStatus($bike_no)
  83. {
  84. //删除未骑行状态
  85. self::$redis->zrem(self::REDIS_BIKE_LOCATION_TAG, $bike_no);
  86. }
  87. /**
  88. * 获取订单信息
  89. * @param $bike_no
  90. * @return mixed
  91. * User: Mead
  92. */
  93. public function getRideBikeOrderInfo($bike_no)
  94. {
  95. $order = self::$redis->get(self::REDIS_RIDE_BIKE_ORDERS_TAG . $bike_no);
  96. if (!$order) return false;
  97. return unserialize($order);
  98. }
  99. /**
  100. * 改变车为订单为开¬车(租车)
  101. * @param $bike_no
  102. * User: Mead
  103. */
  104. public function toBikeRentRideStatus($bike_no)
  105. {
  106. $data = $this->getRideBikeOrderInfo($bike_no);
  107. $data['is_close_bike'] = 0;
  108. self::$redis->set(self::REDIS_RIDE_BIKE_ORDERS_TAG . $bike_no, serialize($data));
  109. }
  110. /**
  111. * 改变车为订单为停车(租车)
  112. * @param $bike_no
  113. * User: Mead
  114. */
  115. public function toBikeRentWaitRideStatus($bike_no)
  116. {
  117. $data = $this->getRideBikeOrderInfo($bike_no);
  118. $data['is_close_bike'] = 1;
  119. self::$redis->set(self::REDIS_RIDE_BIKE_ORDERS_TAG . $bike_no, serialize($data));
  120. }
  121. /**
  122. * 对超出运营范围的车辆加电
  123. * @param $bike_no
  124. * Author: Mead
  125. */
  126. public function toBikeGetElectric($bike_no)
  127. {
  128. $data = $this->getRideBikeOrderInfo($bike_no);
  129. $data['is_out_area_lost_electric'] = 0;
  130. self::$redis->set(self::REDIS_RIDE_BIKE_ORDERS_TAG . $bike_no, serialize($data));
  131. }
  132. /**
  133. * 对低电量车不限制电量
  134. * @param $bike_no
  135. * Author: Mead
  136. */
  137. public function toBikeNoElectric($bike_no)
  138. {
  139. $data = $this->getRideBikeOrderInfo($bike_no);
  140. $data['is_low_electric_close_bike'] = 0;
  141. self::$redis->set(self::REDIS_RIDE_BIKE_ORDERS_TAG . $bike_no, serialize($data));
  142. }
  143. }