diff --git a/app/common/dao/store/product/SpuDao.php b/app/common/dao/store/product/SpuDao.php index 2470a86b..0ff7eda5 100644 --- a/app/common/dao/store/product/SpuDao.php +++ b/app/common/dao/store/product/SpuDao.php @@ -20,6 +20,9 @@ use crmeb\services\VicWordService; class SpuDao extends BaseDao { + + public $orderField; + public function getModel(): string { return Spu::class; @@ -43,6 +46,9 @@ class SpuDao extends BaseDao $order = 'S.'. (($where['order'] !== '') ?$where['order']: 'star' ).' DESC'; } } + if (!empty($this->orderField)) { + $order = "field(P.product_id,{$this->orderField})"; + } $order .= ',S.create_time DESC'; if(isset($where['order']) && $where['order'] === 'none'){ $order = ''; @@ -183,7 +189,8 @@ class SpuDao extends BaseDao ->when(isset($where['svip']) && $where['svip'] !== '',function($query)use($where){ $query->where('svip_price_type','>',0)->where('mer_svip_status',1); }); - return $query->order($order); + + return $query->orderRaw($order); } public function findOrCreateAll(array $where) diff --git a/app/common/repositories/store/product/SpuRepository.php b/app/common/repositories/store/product/SpuRepository.php index d7745db0..a360d174 100644 --- a/app/common/repositories/store/product/SpuRepository.php +++ b/app/common/repositories/store/product/SpuRepository.php @@ -38,6 +38,7 @@ class SpuRepository extends BaseRepository public $productFiled = 'P.bar_code,S.product_id,S.store_name,S.image,activity_id,S.keyword,S.price,S.mer_id,spu_id,S.status,store_info,brand_id,cate_id,unit_name,S.star,S.rank,S.sort,sales,S.product_type,rate,reply_count,extension_type,S.sys_labels,S.mer_labels,P.delivery_way,P.delivery_free,P.ot_price,svip_price_type,stock,mer_svip_status'; public $userInfo; + public $orderField; public function __construct(SpuDao $dao) { @@ -143,6 +144,9 @@ class SpuRepository extends BaseRepository } } $where = array_merge(Product::searchEnable(), $where); + if (!empty($this->orderField)) { + $this->dao->orderField = $this->orderField; + } $query = $this->dao->search($where); $query->with([ diff --git a/app/controller/api/store/product/CloudWarehouse.php b/app/controller/api/store/product/CloudWarehouse.php index eb1cb8d4..87f7cac0 100644 --- a/app/controller/api/store/product/CloudWarehouse.php +++ b/app/controller/api/store/product/CloudWarehouse.php @@ -76,7 +76,7 @@ class CloudWarehouse extends BaseController $cloud_product_arr = $query->where(function($query){ $query->where('mer_labels', '') ->whereOr('mer_labels',',5,'); - })->page($page)->field('product_id,mer_labels')->select(); + })->page($page)->field("product_id,mer_labels,st_distance_sphere(point(`long`,`lat`), point({$lng}, {$lat})) as distance")->order('distance')->select(); $cloud_product = []; foreach ($cloud_product_arr as $key => $value) { $cloud_product[] = $value['product_id']; @@ -93,10 +93,7 @@ class CloudWarehouse extends BaseController if (!$cloud_product) { return app('json')->success(['count' => 0, 'list' => []]); } - $count = $query->where(function($query){ - $query->whereOr('mer_labels', '') - ->whereOr('mer_labels',',5,'); - })->count(); + $this->spuRepository->orderField = implode(',', $cloud_product); $products = $this->spuRepository->getApiSearch($where, $page, 0, false, true); if ($products['list']) { $list = $products['list']; @@ -112,7 +109,7 @@ class CloudWarehouse extends BaseController } } } - return app('json')->success(['count' => $count, 'list' => $list]); + return app('json')->success(['count' => $products['count'], 'list' => $list]); } /**