| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184 |
- <?php
- namespace app\api\model\store;
- use app\common\model\store\Store as StoreModel;
- use Symfony\Component\HttpFoundation\File\UploadedFile;
- use think\Cache;
- use think\Db;
- /**
- * 商家门店模型
- */
- class Store extends StoreModel
- {
- /**
- * 隐藏字段
- */
- protected $hidden = [
- 'is_delete',
- 'app_id',
- 'create_time',
- 'update_time'
- ];
- /**
- * 获取门店列表
- */
- public function getList($is_check = null, $longitude = '', $latitude = '', $limit = false, $shop_supplier_id = 0)
- {
- $model = $this;
- // 是否支持自提核销
- $is_check && $model = $model->where('is_check', '=', $is_check);
- // 商家id
- $shop_supplier_id && $model = $model->where('shop_supplier_id', '=', $shop_supplier_id);
- // 获取数量
- $limit != false && $model = $model->limit($limit);
- // 获取门店列表数据
- $data = $model->where('is_delete', '=', '0')
- ->where('status', '=', '1')
- ->order(['sort' => 'asc', 'create_time' => 'desc'])
- ->select();
- // 根据距离排序
- return $this->sortByDistance($data, $longitude, $latitude);
- }
- /**
- * 获取平台门店列表
- */
- public function getChoiceLists($is_check = null, $longitude = '', $latitude = '', $limit = false, $shop_supplier_id = 0)
- {
- $cacheKey = "caches:stores:{$shop_supplier_id}_".intval($limit).'_'.md5("{$is_check}_{$latitude}_{$longitude}");
- // if($list = \think\facade\Cache::get($cacheKey)){
- // return $list;
- // }
- $model = $this;
- // 是否支持自提核销
- $is_check && $model = $model->where('is_check', '=', $is_check);
- // 商家id
- $shop_supplier_id && $model = $model->where('shop_supplier_id', '=', $shop_supplier_id);
- // 获取数量
- $limit != false && $model = $model->limit($limit);
- $sort = ['sort' => 'asc', 'create_time' => 'desc'];
- if (!empty($longitude) && !empty($latitude)) {
- $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");
- $sort['distance'] = 'asc';
- }else {
- $fieldSql = '*';
- }
- // 获取门店列表数据
- $list = $model->with(['logo','supplier'])
- ->where('is_delete', '=', '0')
- ->where('status', '=', '1')
- ->field($fieldSql)
- ->order($sort)
- ->paginate()
- ->each(function ($store, $k){
- $store['distance'] = round($store['distance'], 3);
- $store['shop_business'] = 0;
- $shop_hours = isset($store['shop_hours'])? $store['shop_hours'] : '';
- $shop_hours = explode('~', $shop_hours);
- $start = isset($shop_hours[0])? $shop_hours[0] : '';
- $end = isset($shop_hours[1])? $shop_hours[1] : '';
- if($start && date('H:i') >= $start){
- if($end && date('H:i') <= $end || empty($end)){
- $store['shop_business'] = 1;
- }
- }
- if($store['distance'] > 0){
- if ($store['distance'] >= 1000) {
- $distance = bcdiv($store['distance'], 1000, 2);
- $store['distance_unit'] = $distance . 'km';
- } else
- $store['distance_unit'] = round($store['distance'], 2) . 'm';
- }else{
- $store['distance_unit'] = '未知';
- }
- });
- if($list){
- //\think\facade\Cache::tag('cache')->set($cacheKey, $list, rand(5, 10));
- }
- return $list;
- }
- /**
- * 根据距离排序
- */
- private function sortByDistance(&$data, $longitude, $latitude)
- {
- // 根据距离排序
- $list = $data->isEmpty() ? [] : $data->toArray();
- $sortArr = [];
- foreach ($list as &$store) {
- /* $logo_image_id = isset($store['logo_image_id'])? $store['logo_image_id'] : 0;
- $store['logo_image'] = '';
- if($logo_image_id){
- $store['logo_image'] =
- }*/
- if (!empty($longitude) && !empty($latitude)) {
- // 计算距离
- $distance = self::getDistance($longitude, $latitude, $store['longitude'], $store['latitude']);
- // 排序列
- $sortArr[] = $distance;
- $store['distance'] = $distance;
- if ($distance >= 1000) {
- $distance = bcdiv($distance, 1000, 2);
- $store['distance_unit'] = $distance . 'km';
- } else
- $store['distance_unit'] = $distance . 'm';
- } else {
- $store['distance'] = '0.00';
- $store['distance_unit'] = '未知';
- }
- }
- if (!empty($longitude) && !empty($latitude)) {
- // 根据距离排序
- array_multisort($sortArr, SORT_ASC, $list);
- }
- return $list;
- }
- /**
- * 获取两个坐标点的距离
- */
- private static function getDistance($ulon, $ulat, $slon, $slat)
- {
- // 地球半径
- $R = 6378137;
- // 将角度转为狐度
- $radLat1 = deg2rad($ulat);
- $radLat2 = deg2rad($slat);
- $radLng1 = deg2rad($ulon);
- $radLng2 = deg2rad($slon);
- // 结果
- $s = acos(cos($radLat1) * cos($radLat2) * cos($radLng1 - $radLng2) + sin($radLat1) * sin($radLat2)) * $R;
- // 精度
- $s = round($s * 10000) / 10000;
- return round($s);
- }
- /**
- * 根据门店id集获取门店列表
- */
- public function getListByIds($storeIds)
- {
- $model = $this;
- // 筛选条件
- $filter = ['store_id' => ['in', $storeIds]];
- if (!empty($storeIds)) {
- $model = $model->orderRaw('field(store_id, ' . implode(',', $storeIds) . ')');
- }
- // 获取商品列表数据
- return $model->with(['logo'])
- ->where('is_delete', '=', '0')
- ->where('status', '=', '1')
- ->where($filter)
- ->select();
- }
- }
|