SystemStore.php 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. <?php
  2. namespace app\models\system;
  3. use app\admin\model\system\SystemAdmin;
  4. use crmeb\traits\ModelTrait;
  5. use crmeb\basic\BaseModel;
  6. /**
  7. * 门店自提 model
  8. * Class SystemStore
  9. * @package app\model\system
  10. */
  11. class SystemStore extends BaseModel
  12. {
  13. const EARTH_RADIUS = 6371;
  14. use ModelTrait;
  15. /**
  16. * 数据表主键
  17. * @var string
  18. */
  19. protected $pk = 'id';
  20. /**
  21. * 模型名称
  22. * @var string
  23. */
  24. protected $name = 'system_store';
  25. public static function getLatlngAttr($value, $data)
  26. {
  27. return $data['latitude'] . ',' . $data['longitude'];
  28. }
  29. public static function verificWhere()
  30. {
  31. return self::where('is_show', 1)->where('is_del', 0);
  32. }
  33. /**
  34. * 获取门店信息
  35. * @param int $id
  36. * @param string $felid
  37. * @return array|mixed|null|string|\think\Model
  38. * @throws \think\db\exception\DataNotFoundException
  39. * @throws \think\db\exception\DbException
  40. * @throws \think\db\exception\ModelNotFoundException
  41. */
  42. public static function getStoreDispose($id = 0, $felid = '')
  43. {
  44. if ($id)
  45. $storeInfo = self::verificWhere()->where('id', $id)->find();
  46. else
  47. $storeInfo = self::verificWhere()->find();
  48. if ($storeInfo) {
  49. $storeInfo['latlng'] = self::getLatlngAttr(null, $storeInfo);
  50. $storeInfo['valid_time'] = $storeInfo['valid_time'] ? explode(' - ', $storeInfo['valid_time']) : [];
  51. $storeInfo['_valid_time'] = str_replace('-', '/', ($storeInfo['valid_time'][0] ?? '') . ' ~ ' . ($storeInfo['valid_time'][1] ?? ""));
  52. $storeInfo['day_time'] = $storeInfo['day_time'] ? str_replace(' - ', ' ~ ', $storeInfo['day_time']) : [];
  53. $storeInfo['_detailed_address'] = $storeInfo['address'] . ' ' . $storeInfo['detailed_address'];
  54. $storeInfo['address'] = $storeInfo['address'] ? explode(',', $storeInfo['address']) : [];
  55. if ($felid) return $storeInfo[$felid] ?? '';
  56. }
  57. return $storeInfo;
  58. }
  59. /**
  60. * 获取排序sql
  61. * @param $latitude
  62. * @param $longitude
  63. * @return mixed
  64. */
  65. public static function distanceSql($latitude, $longitude)
  66. {
  67. $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";
  68. return $field;
  69. }
  70. /**
  71. * 门店列表
  72. * @return mixed
  73. */
  74. public static function lst($latitude, $longitude, $page, $limit)
  75. {
  76. $model = new self();
  77. $model = $model->where('is_del', 0);
  78. $model = $model->where('is_show', 1);
  79. // $model = $model->where('is_triple', 0);
  80. if ($latitude && $longitude) {
  81. $model = $model->field(['*', self::distanceSql($latitude, $longitude)])->order('distance asc');
  82. }
  83. $list = $model->page((int)$page, (int)$limit)
  84. ->select()
  85. ->hidden(['is_show', 'is_del'])
  86. ->toArray();
  87. if ($latitude && $longitude) {
  88. foreach ($list as &$value) {
  89. //计算距离
  90. $value['distance'] = self::getDistance($latitude, $longitude, $value['latitude'], $value['longitude']);
  91. //转换单位
  92. $value['range'] = bcdiv($value['distance'], 1000, 1);
  93. }
  94. }
  95. return $list;
  96. }
  97. public static function getDistance($lat1, $lng1, $lat2, $lng2)
  98. {
  99. $earthRadius = 6367000; //approximate radius of earth in meters
  100. $lat1 = ($lat1 * pi()) / 180;
  101. $lng1 = ($lng1 * pi()) / 180;
  102. $lat2 = ($lat2 * pi()) / 180;
  103. $lng2 = ($lng2 * pi()) / 180;
  104. $calcLongitude = $lng2 - $lng1;
  105. $calcLatitude = $lat2 - $lat1;
  106. $stepOne = pow(sin($calcLatitude / 2), 2) + cos($lat1) * cos($lat2) * pow(sin($calcLongitude / 2), 2);
  107. $stepTwo = 2 * asin(min(1, sqrt($stepOne)));
  108. $calculatedDistance = $earthRadius * $stepTwo;
  109. return round($calculatedDistance);
  110. }
  111. public function amindinfo()
  112. {
  113. return $this->hasOne(SystemAdmin::class, "id", "admin_id");
  114. }
  115. }