From cc8562411c2cf8bcdfe2efab0cf4205435318368 Mon Sep 17 00:00:00 2001 From: luofei <604446095@qq.com> Date: Tue, 23 Jan 2024 11:25:44 +0800 Subject: [PATCH] =?UTF-8?q?=E8=B0=83=E6=95=B4=E4=BA=91=E4=BB=93=E5=95=86?= =?UTF-8?q?=E5=93=81=E5=88=97=E8=A1=A8=E6=9F=A5=E8=AF=A2?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../dao/system/merchant/MerchantDao.php | 30 +++++++++++++++++++ .../api/store/product/CloudWarehouse.php | 21 +++++-------- 2 files changed, 38 insertions(+), 13 deletions(-) diff --git a/app/common/dao/system/merchant/MerchantDao.php b/app/common/dao/system/merchant/MerchantDao.php index 19bdd512..c28fc8a8 100644 --- a/app/common/dao/system/merchant/MerchantDao.php +++ b/app/common/dao/system/merchant/MerchantDao.php @@ -15,6 +15,7 @@ namespace app\common\dao\system\merchant; use app\common\dao\BaseDao; +use app\common\model\store\product\CloudProduct; use app\common\model\system\merchant\Merchant; use crmeb\services\VicWordService; use think\db\BaseQuery; @@ -345,4 +346,33 @@ class MerchantDao extends BaseDao return []; } + /** + * 按距离获取有效的商户 + * @param $lat + * @param $lng + * @param $sort 是否按距离排序 + * @param $distance 距离,单位米 + * @return mixed + */ + public function getProductByDistance($lat, $lng, $sort = false, $distance = 10000) + { + $query = CloudProduct::where('type_id', 'IN', ["10", "17"]) + ->whereNotNull('lat') + ->whereNotNull('long') + ->field("product_id,st_distance_sphere(point(`long`,`lat`), point({$lng}, {$lat})) as distance"); + if ($sort) { + $query->order('distance')->limit(100); + } else { + $query->having("distance <= {$distance}")->limit(50); + } + $product = $query->select()->toArray(); + if (empty($product) && $distance < 50000 && !$sort) { + $product = $this->getProductByDistance($lat, $lng, $sort, 50000); + } + if (!empty($product)) { + return array_column($product, 'product_id'); + } + return []; + } + } diff --git a/app/controller/api/store/product/CloudWarehouse.php b/app/controller/api/store/product/CloudWarehouse.php index 1a2ab426..fad2d374 100644 --- a/app/controller/api/store/product/CloudWarehouse.php +++ b/app/controller/api/store/product/CloudWarehouse.php @@ -46,27 +46,22 @@ class CloudWarehouse extends BaseController */ public function index($street_code, $page = 1, $category_id = 0, $cate_pid = 0,$cate_id = 0,$location = '') { - $query = Db::name('cloud_product')->where('status', 1); if (!empty($location) && $location != ',') { [$lat, $lng] = explode(',', $location); - } - if (!empty($street_code) && (empty($location) || $location == ',')) { - - + } elseif (!empty($street_code)) { $location = GeoStreet::where('street_code', $street_code)->field('lng,lat')->find(); if (!empty($location)) { [$lat, $lng] = [$location['lat'], $location['lng']]; } } - if (!empty($lat)) { - $merIds = (new MerchantDao())->getValidMerchantByDistance($lat, $lng,true,50000); - if (!empty($merIds)) { - $query->whereIn('mer_id', $merIds); - } + if (empty($lat) || empty($lng)) { + [$lat, $lng] = ['28.889137', '105.443352']; } - if (empty($merIds) && !empty($lat)) { - $merIds = (new MerchantDao())->getValidMerchantByDistance($lat, $lng, true); - $query->whereIn('mer_id', $merIds); + $query = Db::name('cloud_product')->where('status', 1); + $productIds = (new MerchantDao())->getProductByDistance($lat, $lng,false,50000); + if (empty($productIds)) { + $productIds = (new MerchantDao())->getProductByDistance($lat, $lng, true); + $query->whereIn('product_id', $productIds); } if($cate_pid!=0){ $cate_id=Db::name('store_category')->where('pid',$cate_pid)->where('is_show',1)->column('store_category_id');