111
liyaozhi
2025-11-09 c13b8914228e6a404bd60ee36bf2479383da8f23
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
<?php
 
namespace app\api\model\store;
 
use app\common\model\store\Store as StoreModel;
 
/**
 * 商家门店模型
 */
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();
        // 根据距离排序
        if (!empty($longitude) && !empty($latitude)) {
            $map = $this->mapChange($latitude,$longitude);
            return $this->sortByDistance($data, $map['lng'], $map['lat']);
        }
        return $data;
    }
 
    /**
     * 根据距离排序
     */
    private function sortByDistance(&$data, $longitude, $latitude)
    {
        // 根据距离排序
        $list = $data->isEmpty() ? [] : $data->toArray();
        $sortArr = [];
        foreach ($list as &$store) {
            // 计算距离
            $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';
            }
        }
        // 根据距离排序
        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();
    }
    
    /**
     * 腾讯地图---->百度地图
     * @param double $lat 纬度
     * @param double $lng 经度
     */
    private function mapChange($lat, $lng)
    {
        $x_pi = 3.14159265358979324 * 3000.0 / 180.0;
        $x = $lng;
        $y = $lat;
        $z = sqrt($x * $x + $y * $y) + 0.00002 * sin($y * $x_pi);
        $theta = atan2($y, $x) + 0.000003 * cos($x * $x_pi);
        $lng = $z * cos($theta) + 0.0065;
        $lat = $z * sin($theta) + 0.006;
        return array('lng' => $lng, 'lat' => $lat);
    }
 
    /**
     * 根据门店id集获取门店列表
     */
    public function getListByStoreIds($storeIds, $longitude, $latitude)
    {
        // 获取列表数据
        $data = $this->with(['logo'])
            ->where('is_delete', '=', '0')
            ->where('status', '=', '1')
            ->where('store_id', 'in', $storeIds)
            ->select();
        // 根据距离排序
        if (!empty($longitude) && !empty($latitude)) {
            return $this->sortByDistance($data, $longitude, $latitude);
        }
        return $data;
    }
 
    /**
     * 资金冻结
     */
    public function freezeMoney($money)
    {
        return $this->save([
            'money' => $this['money'] - $money,
            'freeze_money' => $this['freeze_money'] + $money,
        ]);
    }
 
    /**
     * 门店列表(分页)
     */
    public function storeList($param)
    {
        // 排序规则
        $sort = [];
        if ($param['sortType'] === 'all') {
            $sort = ['s.create_time' => 'desc'];
        } else if ($param['sortType'] === 'letter') {
            $sort = ['letter' => 'asc']; // 首字母
        }
 
        $model = $this;
        if (isset($param['search']) && $param['search']) {
            $model = $model->where('store_name', 'like', '%' . $param['search'] . '%');
        }
 
        // 查询列表数据
        $list = $model->order($sort)
            ->where('is_delete', '=', '0')
            ->where('status', '=', '1')
            ->paginate($param);
        return $list;
    }
 
    /**
     * 根据商户id获取门店id
     */
    public static function getStoreIdBySupplierId($supplierId)
    {
        $model = new static();
        // 获取列表数据
        $data = $model->with(['logo'])
            ->where('is_delete', '=', '0')
            ->where('status', '=', '1')
            ->where('shop_supplier_id', '=', $supplierId)
            ->find();
        return empty($data) ? '' : $data["store_id"];
    }
}