BikeTraitModel.php 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. <?php
  2. /**
  3. * Created by PhpStorm.
  4. * User: Mead
  5. * Date: 2019/9/4
  6. * Time: 9:36 AM
  7. */
  8. namespace App\Models;
  9. use App\Maps\BikeMap;
  10. trait BikeTraitModel
  11. {
  12. // public static $redis = new \Redis();
  13. private static $redis_bike_no_tags = 'box_no_and_bike_no';
  14. private static $redis_bike_no_to_area_id_tags = 'bike_no_to_area_id';
  15. private static $redis_bike_no_to_merchant_id_tags = 'bike_no_to_merchant_id';
  16. /**
  17. * 根据box_no获取车的no
  18. * @param $box_no
  19. * @return mixed
  20. * User: Mead
  21. */
  22. public function byBoxNoGetBikeNo($box_no)
  23. {
  24. return $this->redis->hget(self::$redis_bike_no_tags, $box_no);
  25. }
  26. /**
  27. * 根据box_no获取车的no
  28. * @param $box_no
  29. * @return mixed
  30. * User: Mead
  31. */
  32. public static function byBoxNoGetBikeNoStatic($box_no)
  33. {
  34. return self::$redis->hget(self::$redis_bike_no_tags, $box_no);
  35. }
  36. /**
  37. * 根据box_no 获取车的area_id
  38. * User: Mead
  39. */
  40. public function byBoxNoGetAreaId($box_no)
  41. {
  42. $area_id = $this->redis->get(self::$redis_bike_no_to_area_id_tags . $box_no);
  43. if ($area_id) {
  44. return $area_id;
  45. }
  46. $area_id = $this->db->select('put_area_id')->from('bikes')->where("box_no = " . $box_no)->single();
  47. if (!$area_id) return false;
  48. $this->redis->setex(self::$redis_bike_no_to_area_id_tags . $box_no, 24 * 60 * 60, $area_id);
  49. return $area_id;
  50. }
  51. /**
  52. * 获取断电区的还车点
  53. * @param $area_id
  54. * @return mixed
  55. */
  56. public function byAreaIdGetDuanParkings($area_id)
  57. {
  58. return $this->redis->hgetall("stop_duan_bike_sites_{$area_id}");
  59. }
  60. /**
  61. * 获取车辆的商户id
  62. * @param $bike_no
  63. * @return bool
  64. * Author: Mead
  65. */
  66. public function byBikeNoGetMerchantId($bike_no)
  67. {
  68. $merchant_id = $this->redis->get(self::$redis_bike_no_to_merchant_id_tags . $bike_no);
  69. if ($merchant_id) {
  70. return $merchant_id;
  71. }
  72. $merchant_id = $this->db->select('merchant_id')->from('bikes')->where("bike_no = " . $bike_no)->single();
  73. if (!$merchant_id) return false;
  74. $this->redis->setex(self::$redis_bike_no_to_merchant_id_tags . $bike_no, 24 * 60 * 60, $merchant_id);
  75. return $merchant_id;
  76. }
  77. /**
  78. * 跟新车辆信息
  79. * @param $box_no
  80. * @param array $data
  81. * @param array $other
  82. * @param $type
  83. * @param int $time
  84. * Author: Mead
  85. */
  86. public function updateBikeData($box_no, $type, $data = [], $other = [], $time = 300, $delKeys = [])
  87. {
  88. $hash = "{$box_no}:{$type}:" . md5(implode(',', $data));
  89. if ($this->redis->get($hash)) {
  90. $this->redis->setex($hash, $time, 1);
  91. return false;
  92. } else {
  93. $keys = $this->redis->keys("{$box_no}:{$type}:*");
  94. if (count($keys)) {
  95. foreach ($keys as $key) {
  96. $this->redis->del($key);
  97. }
  98. }
  99. $this->redis->setex($hash, $time, 1);
  100. $data = array_merge($data, $other);
  101. if (count($delKeys)) {
  102. foreach ($delKeys as $key) {
  103. unset($data[$key]);
  104. }
  105. }
  106. $data['is_link'] = BikeMap::LINK_ONLINE;
  107. $this->db->update('bikes')->where("box_no = '{$box_no}'")->cols($data)->query();
  108. return true;
  109. }
  110. }
  111. }