123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159 |
- <?php
- /**
- * Created by PhpStorm.
- * User: Mead
- * Date: 2019/10/8
- * Time: 3:41 PM
- */
- namespace App\Handlers;
- use App\Utils\Admin;
- use Illuminate\Support\Facades\Redis;
- class BikeStatusInfoSyncHandler
- {
- //用户
- const ROLE_USER = 'user';
- //运维人员
- const ROLE_WORKER = 'worker';
- //系统本身
- const ROLE_SERVER = 'server';
- //绑定人员
- const ROLE_BIND = 'bind';
- //还车时过期时间
- const CLOSE_BIKE_TTL = 10;
- private static $redis = '';
- //正常未骑行车的缓存标识
- const REDIS_BIKE_LOCATION_TAG = 'bike_locations';
- const REDIS_RIDE_BIKE_ORDERS_TAG = 'ride_orders:';
- const REDIS_RIDE_BIKE_WORKER_ORDERS_TAG = 'ride_orders_worker';
- public function __construct()
- {
- self::$redis = Redis::connection();
- }
- /**
- * 车辆处于骑行状态 (用户订单时(data: 订单id[id],区域id[area_id],车辆id[bike_id]))
- * @param $bike_no
- * @param array $data
- * User: Mead
- */
- public function toBikeRideStatus($role, $bike_no, $data = [], $is_put_status = 1)
- {
- $data['role'] = $role;
- self::$redis->set(self::REDIS_RIDE_BIKE_ORDERS_TAG . $bike_no, serialize($data));
- //删除未骑行状态
- if ($is_put_status) {
- self::$redis->zrem(self::REDIS_BIKE_LOCATION_TAG, $bike_no);
- }
- if ($role == self::ROLE_WORKER) {
- self::$redis->hset(self::REDIS_RIDE_BIKE_WORKER_ORDERS_TAG, $bike_no, Admin::user()->id);
- }
- }
- /**
- * 车辆处于等待骑行状态
- * @param $bike_no
- * User: Mead
- */
- public function toBikeWaitRideStatus($bike_no, $lng = 0, $lat = 0, $is_put_status = 1)
- {
- // self::$redis->hdel(self::REDIS_RIDE_BIKE_ORDERS_TAG, $bike_no);
- self::$redis->expire(self::REDIS_RIDE_BIKE_ORDERS_TAG . $bike_no, self::CLOSE_BIKE_TTL);
- if ($is_put_status) {
- self::$redis->geoadd(self::REDIS_BIKE_LOCATION_TAG, $lng, $lat, $bike_no);
- }
- // 不存在就忽略
- self::$redis->hdel(self::REDIS_RIDE_BIKE_WORKER_ORDERS_TAG, $bike_no);
- }
- /**
- * 车辆上线
- * @param $bike_no
- * @param int $lat
- * @param int $lng
- * User: Mead
- */
- public function toBikeOnLineStatus($bike_no, $lng = 0, $lat = 0)
- {
- self::$redis->del([self::REDIS_RIDE_BIKE_ORDERS_TAG . $bike_no]);
- self::$redis->geoadd(self::REDIS_BIKE_LOCATION_TAG, $lng, $lat, $bike_no);
- }
- /**
- * 车辆离线
- * @param $bike_no
- * User: Mead
- */
- public function toBikeOffLineStatus($bike_no)
- {
- //删除未骑行状态
- self::$redis->zrem(self::REDIS_BIKE_LOCATION_TAG, $bike_no);
- }
- /**
- * 获取订单信息
- * @param $bike_no
- * @return mixed
- * User: Mead
- */
- public function getRideBikeOrderInfo($bike_no)
- {
- $order = self::$redis->get(self::REDIS_RIDE_BIKE_ORDERS_TAG . $bike_no);
- if (!$order) return false;
- return unserialize($order);
- }
- /**
- * 改变车为订单为开¬车(租车)
- * @param $bike_no
- * User: Mead
- */
- public function toBikeRentRideStatus($bike_no)
- {
- $data = $this->getRideBikeOrderInfo($bike_no);
- $data['is_close_bike'] = 0;
- self::$redis->set(self::REDIS_RIDE_BIKE_ORDERS_TAG . $bike_no, serialize($data));
- }
- /**
- * 改变车为订单为停车(租车)
- * @param $bike_no
- * User: Mead
- */
- public function toBikeRentWaitRideStatus($bike_no)
- {
- $data = $this->getRideBikeOrderInfo($bike_no);
- $data['is_close_bike'] = 1;
- self::$redis->set(self::REDIS_RIDE_BIKE_ORDERS_TAG . $bike_no, serialize($data));
- }
- /**
- * 对超出运营范围的车辆加电
- * @param $bike_no
- * Author: Mead
- */
- public function toBikeGetElectric($bike_no)
- {
- $data = $this->getRideBikeOrderInfo($bike_no);
- $data['is_out_area_lost_electric'] = 0;
- self::$redis->set(self::REDIS_RIDE_BIKE_ORDERS_TAG . $bike_no, serialize($data));
- }
- /**
- * 对低电量车不限制电量
- * @param $bike_no
- * Author: Mead
- */
- public function toBikeNoElectric($bike_no)
- {
- $data = $this->getRideBikeOrderInfo($bike_no);
- $data['is_low_electric_close_bike'] = 0;
- self::$redis->set(self::REDIS_RIDE_BIKE_ORDERS_TAG . $bike_no, serialize($data));
- }
- }
|