|
|
@@ -328,4 +328,72 @@ class GhsClass extends BaseClass
|
|
|
util::fail('找不到供货商');
|
|
|
}
|
|
|
|
|
|
+ // 建立附近批发店关系
|
|
|
+ public static function buildNearbyGhsRelations($shopId, $pfShopId, $province, $city, $toLat, $toLnt)
|
|
|
+ {
|
|
|
+ if (empty($toLat) || empty($toLnt)) {
|
|
|
+ util::fail('门店未定位');
|
|
|
+ }
|
|
|
+
|
|
|
+ if (empty($province) || empty($city)) {
|
|
|
+ util::fail('门店未设置所在省市');
|
|
|
+ }
|
|
|
+
|
|
|
+ // 按省市查找
|
|
|
+ $nearbyShops = ShopClass::getAllByCondition(['ptStyle'=>2, 'province'=>$province, 'city'=>$city, 'actTime>=' => strtotime('yesterday')], null, 'id, merchantName, lat, long');
|
|
|
+ if (empty($nearbyShops)) {
|
|
|
+ return;
|
|
|
+ }
|
|
|
+
|
|
|
+ $origins = [];
|
|
|
+ $validNearbyShops = [];
|
|
|
+ foreach ($nearbyShops as $nearbyShop) {
|
|
|
+ if ($nearbyShop['id'] == $pfShopId) {
|
|
|
+ continue;
|
|
|
+ }
|
|
|
+ if ($nearbyShop['lat'] && $nearbyShop['long']) {
|
|
|
+ $origins[] = [
|
|
|
+ 'lat' => $nearbyShop['lat'],
|
|
|
+ 'lng' => $nearbyShop['long'],
|
|
|
+ ];
|
|
|
+ $validNearbyShops[] = $nearbyShop;
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ if (empty($origins)) {
|
|
|
+ return;
|
|
|
+ }
|
|
|
+
|
|
|
+ // 高德批量测距接口最多支持 100 个起点,需分批处理
|
|
|
+ $chunkOrigins = array_chunk($origins, 100);
|
|
|
+ $allDistances = [];
|
|
|
+
|
|
|
+ foreach ($chunkOrigins as $chunk) {
|
|
|
+ $distances = \common\components\mapUtil::calculateBatchDistance($chunk, $toLnt, $toLat);
|
|
|
+ $allDistances = array_merge($allDistances, $distances);
|
|
|
+ }
|
|
|
+
|
|
|
+ // 建立关系前要先执照距离从小到大排序
|
|
|
+ // 注意:array_multisort($allDistances, SORT_ASC, $validNearbyShops) 能保证 $allDistances 与 $validNearbyShops 保持索引同步,
|
|
|
+ // 即排序后 $validNearbyShops[$i] 就是排序后第 $i 小距离的门店信息,$allDistances[$i] 为其对应距离。
|
|
|
+ array_multisort($allDistances, SORT_ASC, $validNearbyShops);
|
|
|
+
|
|
|
+ // 取前6个
|
|
|
+ $allDistances = array_slice($allDistances, 0, 6);
|
|
|
+ $validNearbyShops = array_slice($validNearbyShops, 0, 6);
|
|
|
+
|
|
|
+ // 建立关系并保存距离
|
|
|
+ foreach ($validNearbyShops as $index => $nearbyShop) {
|
|
|
+ $ghsShopId = $nearbyShop['id'];
|
|
|
+ $distance = $allDistances[$index] ?? 0;
|
|
|
+
|
|
|
+ // 调用 GhsClass::build 建立关系 (返回的 $ghs 包含 id 和 customId)
|
|
|
+ $ghs = self::build($ghsShopId, $shopId);
|
|
|
+ if (!empty($ghs) && $distance > 0) {
|
|
|
+ $ghsId = $ghs['id'];
|
|
|
+ self::updateById($ghsId, ['distance' => $distance]);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
}
|