15313670163 преди 1 месец
родител
ревизия
5eb9f176fe
променени са 1 файла, в които са добавени 28 реда и са изтрити 31 реда
  1. 28 31
      app/JsonRpc/WebsiteService.php

+ 28 - 31
app/JsonRpc/WebsiteService.php

@@ -582,44 +582,41 @@ class WebsiteService implements WebsiteServiceInterface
 
     public function getWebsiteModelCategory(array $data): array 
     {
+        // return Result::success($data);
         $website_id=[ 
-            'website_category.website_id' => $data['website_id'] 
-        ];
-        $placeid=$data['placeid']-1; 
-        $pid=[
-            'website_category.pid' => $data['pid'], 
+            'website_id' => $data['website_id'] 
         ];
-        var_dump( "=======",$pid);
-        $num = $data['num'];
-        // return Result::success($data);
-        $result=WebsiteCategory::where($website_id)
-        ->leftJoin("category",'website_category.category_id','category.id')
-        ->select('website_category.*','category.is_url','category.web_url')
-        ->where($pid)
-        // ->with(['children' => function ($query) use ($data) {
-        //     $query->where('website_category.website_id', $data['website_id']);
-        // }])
-        ->orderBy('website_category.sort')  
-        ->offset($placeid)
-        ->limit($num)
-        ->get();
-        foreach($result as $k=>$v){
-            $child[$k]['children_countent']=WebsiteCategory::where($website_id)
-            ->where('website_category.pid',$v['category_id'])
-            ->count();
-            if(!empty($child[$k]['children_countent'])){
-                $result[$k]['children_count']=$child[$k]['children_countent'];
-            }else{
-                $result[$k]['children_count']=0;
-            }
-        }
-        // return Result::success($result);
+        // 初始化 $pid 数组
+        // $pid = [];
+        // 以下注释掉的代码是之前的逻辑,用于获取非顶级分类的 pid
+        $pid = WebsiteCategory::where($website_id)
+            ->where('pid', '!=', 0)
+            ->pluck('pid');
+        $pid = array_values(array_unique($pid->toArray()));
+
+        // 构建查询语句
+        $query = WebsiteCategory::where($website_id)
+            ->where('pid', $data['pid'])
+            ->offset($data['placeid'])
+            ->limit($data['num'])
+            ->orderBy('sort');
+
+        // 如果 $pid 数组不为空,添加 CASE WHEN 条件
+        if (!empty($pid)) {
+            $placeholders = implode(',', array_fill(0, count($pid), '?'));
+            $query->selectRaw("website_category.*, CASE WHEN website_category.category_id IN ($placeholders) THEN 1 ELSE 0 END AS children_count", $pid);
+        } else {
+            // 如果 $pid 数组为空,不添加 CASE WHEN 条件,添加字段 children_count 并赋值为 0
+            $query->select('website_category.*', DB::raw('0 as children_count'));
+        }
+
+        // 执行查询
+        $result = $query->get();
         if(!empty($result)){ 
             return Result::success($result);  
         }else{ 
             return Result::error("本网站暂无栏目",0);  
         } 
-
     } 
 
     /**