Browse Source

feat(product): 添加商品搜索来源参数并调整排序逻辑

- 在 ProductDao::search 方法中添加 source 参数,默认值为 'api'
- 根据不同来源调整商品排序逻辑:admin 端按 sort 和 create_time 排序
- 非 admin 端保持原有的随机排序方式
- 在 ProductRepository 中调用 search 方法时传入 'admin' 来源参数
shichen 2 months ago
parent
commit
f2d66c226d

+ 7 - 3
app/common/dao/store/product/ProductDao.php

@@ -101,7 +101,7 @@ class ProductDao extends BaseDao
101
      * @param array $where
101
      * @param array $where
102
      * @return mixed
102
      * @return mixed
103
      */
103
      */
104
-    public function search(?int $merId, array $where)
104
+    public function search(?int $merId, array $where, string $source = 'api')
105
     {
105
     {
106
 
106
 
107
         $query = isset($where['soft']) ? $this->getModel()::onlyTrashed() : $this->getModel()::where('product_id','>',0);
107
         $query = isset($where['soft']) ? $this->getModel()::onlyTrashed() : $this->getModel()::where('product_id','>',0);
@@ -167,8 +167,12 @@ class ProductDao extends BaseDao
167
             }, function ($query) use ($where) {
167
             }, function ($query) use ($where) {
168
                 $query->order('rank DESC ,create_time DESC');
168
                 $query->order('rank DESC ,create_time DESC');
169
             });
169
             });
170
-        },function ($query){
171
-            $query->order('sort DESC')->orderRaw("RAND()");
170
+        },function ($query) use ($source){
171
+            if ($source === 'admin') {
172
+                $query->order('sort DESC')->order('create_time DESC');
173
+            } else {
174
+                $query->order('sort DESC')->orderRaw("RAND()");
175
+            }
172
         });
176
         });
173
         return $query;
177
         return $query;
174
     }
178
     }

+ 1 - 1
app/common/repositories/store/product/ProductRepository.php

@@ -737,7 +737,7 @@ class ProductRepository extends BaseRepository
737
      */
737
      */
738
     public function getList(?int $merId, array $where, int $page, int $limit)
738
     public function getList(?int $merId, array $where, int $page, int $limit)
739
     {
739
     {
740
-        $query = $this->dao->search($merId, $where)->with(['merCateId.category', 'storeCategory', 'brand']);
740
+        $query = $this->dao->search($merId, $where,'admin')->with(['merCateId.category', 'storeCategory', 'brand']);
741
         $count = $query->count($this->dao->getPk());
741
         $count = $query->count($this->dao->getPk());
742
         $list = $query->page($page, $limit)->setOption('field', [])->field($this->filed)->select();
742
         $list = $query->page($page, $limit)->setOption('field', [])->field($this->filed)->select();
743
         $list->append(['max_extension', 'min_extension']);
743
         $list->append(['max_extension', 'min_extension']);