SystemStore.php 4.8 KB

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