Store.php 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  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'])
  62. ->with('supplier')
  63. ->where('is_delete', '=', '0')
  64. ->where('status', '=', '1')
  65. ->field($fieldSql)
  66. ->order($sort)
  67. ->paginate()
  68. ->each(function ($store, $k){
  69. $store['distance'] = round($store['distance'], 3);
  70. $store['shop_business'] = 0;
  71. $shop_hours = isset($store['shop_hours'])? $store['shop_hours'] : '';
  72. $shop_hours = explode('~', $shop_hours);
  73. $start = isset($shop_hours[0])? $shop_hours[0] : '';
  74. $end = isset($shop_hours[1])? $shop_hours[1] : '';
  75. if($start && date('H:i') >= $start){
  76. if($end && date('H:i') <= $end || empty($end)){
  77. $store['shop_business'] = 1;
  78. }
  79. }
  80. if($store['distance'] > 0){
  81. if ($store['distance'] >= 1000) {
  82. $distance = bcdiv($store['distance'], 1000, 2);
  83. $store['distance_unit'] = $distance . 'km';
  84. } else
  85. $store['distance_unit'] = round($store['distance'], 2) . 'm';
  86. }else{
  87. $store['distance_unit'] = '未知';
  88. }
  89. });
  90. return $list;
  91. }
  92. /**
  93. * 根据距离排序
  94. */
  95. private function sortByDistance(&$data, $longitude, $latitude)
  96. {
  97. // 根据距离排序
  98. $list = $data->isEmpty() ? [] : $data->toArray();
  99. $sortArr = [];
  100. foreach ($list as &$store) {
  101. /* $logo_image_id = isset($store['logo_image_id'])? $store['logo_image_id'] : 0;
  102. $store['logo_image'] = '';
  103. if($logo_image_id){
  104. $store['logo_image'] =
  105. }*/
  106. if (!empty($longitude) && !empty($latitude)) {
  107. // 计算距离
  108. $distance = self::getDistance($longitude, $latitude, $store['longitude'], $store['latitude']);
  109. // 排序列
  110. $sortArr[] = $distance;
  111. $store['distance'] = $distance;
  112. if ($distance >= 1000) {
  113. $distance = bcdiv($distance, 1000, 2);
  114. $store['distance_unit'] = $distance . 'km';
  115. } else
  116. $store['distance_unit'] = $distance . 'm';
  117. } else {
  118. $store['distance'] = '0.00';
  119. $store['distance_unit'] = '未知';
  120. }
  121. }
  122. if (!empty($longitude) && !empty($latitude)) {
  123. // 根据距离排序
  124. array_multisort($sortArr, SORT_ASC, $list);
  125. }
  126. return $list;
  127. }
  128. /**
  129. * 获取两个坐标点的距离
  130. */
  131. private static function getDistance($ulon, $ulat, $slon, $slat)
  132. {
  133. // 地球半径
  134. $R = 6378137;
  135. // 将角度转为狐度
  136. $radLat1 = deg2rad($ulat);
  137. $radLat2 = deg2rad($slat);
  138. $radLng1 = deg2rad($ulon);
  139. $radLng2 = deg2rad($slon);
  140. // 结果
  141. $s = acos(cos($radLat1) * cos($radLat2) * cos($radLng1 - $radLng2) + sin($radLat1) * sin($radLat2)) * $R;
  142. // 精度
  143. $s = round($s * 10000) / 10000;
  144. return round($s);
  145. }
  146. /**
  147. * 根据门店id集获取门店列表
  148. */
  149. public function getListByIds($storeIds)
  150. {
  151. $model = $this;
  152. // 筛选条件
  153. $filter = ['store_id' => ['in', $storeIds]];
  154. if (!empty($storeIds)) {
  155. $model = $model->orderRaw('field(store_id, ' . implode(',', $storeIds) . ')');
  156. }
  157. // 获取商品列表数据
  158. return $model->with(['logo'])
  159. ->where('is_delete', '=', '0')
  160. ->where('status', '=', '1')
  161. ->where($filter)
  162. ->select();
  163. }
  164. }