Store.php 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  1. <?php
  2. namespace app\api\model\store;
  3. use app\common\model\store\Store as StoreModel;
  4. use Symfony\Component\HttpFoundation\File\UploadedFile;
  5. use think\Db;
  6. /**
  7. * 商家门店模型
  8. */
  9. class Store extends StoreModel
  10. {
  11. /**
  12. * 隐藏字段
  13. */
  14. protected $hidden = [
  15. 'is_delete',
  16. 'app_id',
  17. 'create_time',
  18. 'update_time'
  19. ];
  20. /**
  21. * 获取门店列表
  22. */
  23. public function getList($is_check = null, $longitude = '', $latitude = '', $limit = false, $shop_supplier_id = 0)
  24. {
  25. $model = $this;
  26. // 是否支持自提核销
  27. $is_check && $model = $model->where('is_check', '=', $is_check);
  28. // 商家id
  29. $shop_supplier_id && $model = $model->where('shop_supplier_id', '=', $shop_supplier_id);
  30. // 获取数量
  31. $limit != false && $model = $model->limit($limit);
  32. // 获取门店列表数据
  33. $data = $model->where('is_delete', '=', '0')
  34. ->where('status', '=', '1')
  35. ->order(['sort' => 'asc', 'create_time' => 'desc'])
  36. ->select();
  37. // 根据距离排序
  38. return $this->sortByDistance($data, $longitude, $latitude);
  39. }
  40. /**
  41. * 获取平台门店列表
  42. */
  43. public function getChoiceLists($is_check = null, $longitude = '', $latitude = '', $limit = false, $shop_supplier_id = 0)
  44. {
  45. $model = $this;
  46. // 是否支持自提核销
  47. $is_check && $model = $model->where('is_check', '=', $is_check);
  48. // 商家id
  49. $shop_supplier_id && $model = $model->where('shop_supplier_id', '=', $shop_supplier_id);
  50. // 获取数量
  51. $limit != false && $model = $model->limit($limit);
  52. $distanceSql = '';
  53. $sort = ['sort' => 'asc', 'create_time' => 'desc'];
  54. if (!empty($longitude) && !empty($latitude)) {
  55. $fieldSql = "*,".\think\facade\Db::Raw("(6378.138 * 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))) * 1000) as distance");
  56. $sort['distance'] = 'asc';
  57. }else {
  58. $fieldSql = '*';
  59. }
  60. // 获取门店列表数据
  61. $list = $model->with(['logo','supplier'])
  62. ->where('is_delete', '=', '0')
  63. ->where('status', '=', '1')
  64. ->field($fieldSql)
  65. ->order($sort)
  66. ->paginate()
  67. ->each(function ($store, $k){
  68. $store['distance'] = round($store['distance'], 3);
  69. $store['shop_business'] = 0;
  70. $shop_hours = isset($store['shop_hours'])? $store['shop_hours'] : '';
  71. $shop_hours = explode('~', $shop_hours);
  72. $start = isset($shop_hours[0])? $shop_hours[0] : '';
  73. $end = isset($shop_hours[1])? $shop_hours[1] : '';
  74. if($start && date('H:i') >= $start){
  75. if($end && date('H:i') <= $end || empty($end)){
  76. $store['shop_business'] = 1;
  77. }
  78. }
  79. if($store['distance'] > 0){
  80. if ($store['distance'] >= 1000) {
  81. $distance = bcdiv($store['distance'], 1000, 2);
  82. $store['distance_unit'] = $distance . 'km';
  83. } else
  84. $store['distance_unit'] = round($store['distance'], 2) . 'm';
  85. }else{
  86. $store['distance_unit'] = '未知';
  87. }
  88. });
  89. return $list;
  90. }
  91. /**
  92. * 根据距离排序
  93. */
  94. private function sortByDistance(&$data, $longitude, $latitude)
  95. {
  96. // 根据距离排序
  97. $list = $data->isEmpty() ? [] : $data->toArray();
  98. $sortArr = [];
  99. foreach ($list as &$store) {
  100. /* $logo_image_id = isset($store['logo_image_id'])? $store['logo_image_id'] : 0;
  101. $store['logo_image'] = '';
  102. if($logo_image_id){
  103. $store['logo_image'] =
  104. }*/
  105. if (!empty($longitude) && !empty($latitude)) {
  106. // 计算距离
  107. $distance = self::getDistance($longitude, $latitude, $store['longitude'], $store['latitude']);
  108. // 排序列
  109. $sortArr[] = $distance;
  110. $store['distance'] = $distance;
  111. if ($distance >= 1000) {
  112. $distance = bcdiv($distance, 1000, 2);
  113. $store['distance_unit'] = $distance . 'km';
  114. } else
  115. $store['distance_unit'] = $distance . 'm';
  116. } else {
  117. $store['distance'] = '0.00';
  118. $store['distance_unit'] = '未知';
  119. }
  120. }
  121. if (!empty($longitude) && !empty($latitude)) {
  122. // 根据距离排序
  123. array_multisort($sortArr, SORT_ASC, $list);
  124. }
  125. return $list;
  126. }
  127. /**
  128. * 获取两个坐标点的距离
  129. */
  130. private static function getDistance($ulon, $ulat, $slon, $slat)
  131. {
  132. // 地球半径
  133. $R = 6378137;
  134. // 将角度转为狐度
  135. $radLat1 = deg2rad($ulat);
  136. $radLat2 = deg2rad($slat);
  137. $radLng1 = deg2rad($ulon);
  138. $radLng2 = deg2rad($slon);
  139. // 结果
  140. $s = acos(cos($radLat1) * cos($radLat2) * cos($radLng1 - $radLng2) + sin($radLat1) * sin($radLat2)) * $R;
  141. // 精度
  142. $s = round($s * 10000) / 10000;
  143. return round($s);
  144. }
  145. /**
  146. * 根据门店id集获取门店列表
  147. */
  148. public function getListByIds($storeIds)
  149. {
  150. $model = $this;
  151. // 筛选条件
  152. $filter = ['store_id' => ['in', $storeIds]];
  153. if (!empty($storeIds)) {
  154. $model = $model->orderRaw('field(store_id, ' . implode(',', $storeIds) . ')');
  155. }
  156. // 获取商品列表数据
  157. return $model->with(['logo'])
  158. ->where('is_delete', '=', '0')
  159. ->where('status', '=', '1')
  160. ->where($filter)
  161. ->select();
  162. }
  163. }