123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127 |
- <?php
- /**
- * Created by PhpStorm.
- * User: Mead
- * Date: 2019/9/4
- * Time: 9:36 AM
- */
- namespace App\Models;
- use App\Maps\BikeMap;
- trait BikeTraitModel
- {
- // public static $redis = new \Redis();
- private static $redis_bike_no_tags = 'box_no_and_bike_no';
- private static $redis_bike_no_to_area_id_tags = 'bike_no_to_area_id';
- private static $redis_bike_no_to_merchant_id_tags = 'bike_no_to_merchant_id';
- /**
- * 根据box_no获取车的no
- * @param $box_no
- * @return mixed
- * User: Mead
- */
- public function byBoxNoGetBikeNo($box_no)
- {
- return $this->redis->hget(self::$redis_bike_no_tags, $box_no);
- }
- /**
- * 根据box_no获取车的no
- * @param $box_no
- * @return mixed
- * User: Mead
- */
- public static function byBoxNoGetBikeNoStatic($box_no)
- {
- return self::$redis->hget(self::$redis_bike_no_tags, $box_no);
- }
- /**
- * 根据box_no 获取车的area_id
- * User: Mead
- */
- public function byBoxNoGetAreaId($box_no)
- {
- $area_id = $this->redis->get(self::$redis_bike_no_to_area_id_tags . $box_no);
- if ($area_id) {
- return $area_id;
- }
- $area_id = $this->db->select('put_area_id')->from('bikes')->where("box_no = " . $box_no)->single();
- if (!$area_id) return false;
- $this->redis->setex(self::$redis_bike_no_to_area_id_tags . $box_no, 24 * 60 * 60, $area_id);
- return $area_id;
- }
- /**
- * 获取断电区的还车点
- * @param $area_id
- * @return mixed
- */
- public function byAreaIdGetDuanParkings($area_id)
- {
- return $this->redis->hgetall("stop_duan_bike_sites_{$area_id}");
- }
- /**
- * 获取车辆的商户id
- * @param $bike_no
- * @return bool
- * Author: Mead
- */
- public function byBikeNoGetMerchantId($bike_no)
- {
- $merchant_id = $this->redis->get(self::$redis_bike_no_to_merchant_id_tags . $bike_no);
- if ($merchant_id) {
- return $merchant_id;
- }
- $merchant_id = $this->db->select('merchant_id')->from('bikes')->where("bike_no = " . $bike_no)->single();
- if (!$merchant_id) return false;
- $this->redis->setex(self::$redis_bike_no_to_merchant_id_tags . $bike_no, 24 * 60 * 60, $merchant_id);
- return $merchant_id;
- }
- /**
- * 跟新车辆信息
- * @param $box_no
- * @param array $data
- * @param array $other
- * @param $type
- * @param int $time
- * Author: Mead
- */
- public function updateBikeData($box_no, $type, $data = [], $other = [], $time = 300, $delKeys = [])
- {
- $hash = "{$box_no}:{$type}:" . md5(implode(',', $data));
- if ($this->redis->get($hash)) {
- $this->redis->setex($hash, $time, 1);
- return false;
- } else {
- $keys = $this->redis->keys("{$box_no}:{$type}:*");
- if (count($keys)) {
- foreach ($keys as $key) {
- $this->redis->del($key);
- }
- }
- $this->redis->setex($hash, $time, 1);
- $data = array_merge($data, $other);
- if (count($delKeys)) {
- foreach ($delKeys as $key) {
- unset($data[$key]);
- }
- }
- $data['is_link'] = BikeMap::LINK_ONLINE;
- $this->db->update('bikes')->where("box_no = '{$box_no}'")->cols($data)->query();
- return true;
- }
- }
- }
|