cqp 5 місяців тому
батько
коміт
fd60e0f6f9
2 змінених файлів з 29 додано та 7 видалено
  1. 13 0
      app/Model/ProductCategoryOrderBy.php
  2. 16 7
      app/Service/ProductService.php

+ 13 - 0
app/Model/ProductCategoryOrderBy.php

@@ -0,0 +1,13 @@
+<?php
+
+namespace App\Model;
+
+use Illuminate\Database\Eloquent\Model;
+
+class ProductCategoryOrderBy extends Model
+{
+    protected $table = "product_category_orderby"; //指定表
+    const CREATED_AT = 'crt_time';
+    const UPDATED_AT = 'upd_time';
+    protected $dateFormat = 'U';
+}

+ 16 - 7
app/Service/ProductService.php

@@ -109,9 +109,14 @@ class ProductService extends Service
      */
     public function productCategoryList($data,$user){
         $model = ProductCategory::TopClear($user,$data);
-        $model = $model->where('del_time',0)
-            ->select('title','id','parent_id','is_edit_unit_price')
-            ->orderby('id','asc');
+        $model = $model->leftJoin('product_category_orderby as a', function ($join) {
+                $join->on('product_category.id', '=', 'a.category_id')
+                    ->where('a.del_time',0);
+            })
+            ->where('product_category.del_time',0)
+            ->select('product_category.title','product_category.id','product_category.parent_id','product_category.is_edit_unit_price')
+            ->orderByRaw('IF(a.sort IS NULL, product_category.id, a.sort) ASC'); // 排序逻辑:优先按 a.sort 排序,没有关联时按主表 id 排序
+
         if(! empty($data['title'])) $model->where('title', 'LIKE', '%'.$data['title'].'%');
         if(isset($data['is_edit_unit_price'])) $model->where('is_edit_unit_price', $data['is_edit_unit_price']);
 
@@ -134,10 +139,14 @@ class ProductService extends Service
         $now_top_depart_id = $this->getDepart($user);
 
         //总社分类
-        $head = ProductCategory::where('del_time',0)
-            ->where('top_depart_id', $head_top_depart_id)
-            ->select('title','id','parent_id')
-            ->orderby('id','asc')
+        $head = ProductCategory::leftJoin('product_category_orderby as a', function ($join) {
+                $join->on('product_category.id', '=', 'a.category_id')
+                    ->where('a.del_time',0);
+            })
+            ->where('product_category.del_time',0)
+            ->where('product_category.top_depart_id', $head_top_depart_id)
+            ->select('product_category.title','product_category.id','product_category.parent_id')
+            ->orderByRaw('IF(a.sort IS NULL, product_category.id, a.sort) ASC') // 排序逻辑:优先按 a.sort 排序,没有关联时按主表 id 排序
             ->get()->toArray();
         $head_tree = [];
         if(! empty($head)) {