SystemStore.php 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  1. <?php
  2. namespace app\models\system;
  3. use crmeb\traits\ModelTrait;
  4. use crmeb\basic\BaseModel;
  5. /**
  6. * 门店自提 model
  7. * Class SystemStore
  8. * @package app\model\system
  9. */
  10. class SystemStore extends BaseModel
  11. {
  12. const EARTH_RADIUS = 6371;
  13. use ModelTrait;
  14. /**
  15. * 数据表主键
  16. * @var string
  17. */
  18. protected $pk = 'id';
  19. /**
  20. * 模型名称
  21. * @var string
  22. */
  23. protected $name = 'system_store';
  24. public static function getLatlngAttr($value, $data)
  25. {
  26. return $data['latitude'] . ',' . $data['longitude'];
  27. }
  28. public static function verificWhere()
  29. {
  30. return self::where('is_show', 1)->where('is_del', 0);
  31. }
  32. /*
  33. * 获取门店信息
  34. * @param int $id
  35. * */
  36. public static function getStoreDispose($id = 0, $felid = '')
  37. {
  38. if ($id)
  39. $storeInfo = self::verificWhere()->where('id', $id)->find();
  40. else
  41. $storeInfo = self::verificWhere()->find();
  42. if ($storeInfo) {
  43. $storeInfo['latlng'] = self::getLatlngAttr(null, $storeInfo);
  44. $storeInfo['valid_time'] = $storeInfo['valid_time'] ? explode(' - ', $storeInfo['valid_time']) : [];
  45. $storeInfo['_valid_time'] = str_replace('-', '/', ($storeInfo['valid_time'][0] ?? '') . ' ~ ' . ($storeInfo['valid_time'][1] ?? ""));
  46. $storeInfo['day_time'] = $storeInfo['day_time'] ? str_replace(' - ', ' ~ ', $storeInfo['day_time']) : [];
  47. $storeInfo['_detailed_address'] = $storeInfo['address'] . ' ' . $storeInfo['detailed_address'];
  48. $storeInfo['address'] = $storeInfo['address'] ? explode(',', $storeInfo['address']) : [];
  49. if ($felid) return $storeInfo[$felid] ?? '';
  50. }
  51. return $storeInfo;
  52. }
  53. /**
  54. * 门店列表
  55. * @return mixed
  56. */
  57. // public static function lst()
  58. // {
  59. // $model = new self;
  60. // $model = $model->where('is_show', 1);
  61. // $model = $model->where('is_del', 0);
  62. // $model = $model->order('id DESC');
  63. // return $model->select();
  64. // }
  65. /**
  66. * 计算某个经纬度的周围某段距离的正方形的四个点
  67. *
  68. * @param lng float 经度
  69. * @param lat float 纬度
  70. * @param distance float 该点所在圆的半径,该圆与此正方形内切,默认值为2.5千米
  71. * @return array 正方形的四个点的经纬度坐标
  72. */
  73. public static function returnSquarePoint($lng, $lat, $distance = 200)
  74. {
  75. $dlng = 2 * asin(sin($distance / (2 * self::EARTH_RADIUS)) / cos(deg2rad($lat)));
  76. $dlng = rad2deg($dlng);
  77. $dlat = rad2deg($distance / self::EARTH_RADIUS);
  78. return [
  79. 'left_top' => [
  80. 'lat' => $lat + $dlat,
  81. 'lng' => $lng - $dlng,
  82. ],
  83. 'right_top' => [
  84. 'lat' => $lat + $dlat,
  85. 'lng' => $lng + $dlng
  86. ],
  87. 'left_bottom' => [
  88. 'lat' => $lat - $dlat,
  89. 'lng' => $lng - $dlng
  90. ],
  91. 'right_bottom' => [
  92. 'lat' => $lat - $dlat,
  93. 'lng' => $lng + $dlng
  94. ]
  95. ];
  96. }
  97. /*
  98. 设置where条件
  99. */
  100. public static function nearbyWhere($model = null, $latitude = 0, $longitude = 0)
  101. {
  102. if (!is_object($model)) {
  103. $latitude = $model;
  104. $model = new self();
  105. $longitude = $latitude;
  106. }
  107. $field = "(round(6367000 * 2 * asin(sqrt(pow(sin(((latitude * pi()) / 180 - ({$latitude} * pi()) / 180) / 2), 2) + cos(({$latitude} * pi()) / 180) * cos((latitude * pi()) / 180) * pow(sin(((longitude * pi()) / 180 - ({$longitude} * pi()) / 180) / 2), 2))))) AS distance";
  108. $model->field($field);
  109. return $model;
  110. }
  111. /**
  112. * 获取排序sql
  113. * @param $latitude
  114. * @param $longitude
  115. * @return mixed
  116. */
  117. public static function distanceSql($latitude, $longitude)
  118. {
  119. $field = "(round(6367000 * 2 * asin(sqrt(pow(sin(((latitude * pi()) / 180 - ({$latitude} * pi()) / 180) / 2), 2) + cos(({$latitude} * pi()) / 180) * cos((latitude * pi()) / 180) * pow(sin(((longitude * pi()) / 180 - ({$longitude} * pi()) / 180) / 2), 2))))) AS distance";
  120. return $field;
  121. }
  122. /**
  123. * 门店列表
  124. * @return mixed
  125. */
  126. public static function lst($latitude, $longitude, $page, $limit)
  127. {
  128. $model = new self();
  129. $model = $model->where('is_del', 0);
  130. $model = $model->where('is_show',1);
  131. if ($latitude && $longitude) {
  132. $model = $model->field(['*', self::distanceSql($latitude, $longitude)])->order('distance asc');
  133. }
  134. $list = $model->page((int)$page, (int)$limit)
  135. ->select()
  136. ->hidden(['is_show', 'is_del'])
  137. ->toArray();
  138. if ($latitude && $longitude) {
  139. foreach ($list as &$value) {
  140. //计算距离
  141. $value['distance'] = sqrt((pow((($latitude - $value['latitude']) * 111000), 2)) + (pow((($longitude - $value['longitude']) * 111000), 2)));
  142. //转换单位
  143. $value['range'] = bcdiv($value['distance'], 1000, 1);
  144. }
  145. // $distanceKey = array_column($list, 'distance');
  146. // array_multisort($distanceKey, SORT_ASC, $list);
  147. }
  148. return $list;
  149. }
  150. }