Эх сурвалжийг харах

农网导航返回父级拼音

rkljw 1 өдөр өмнө
parent
commit
08ad47db28
1 өөрчлөгдсөн 57 нэмэгдсэн , 54 устгасан
  1. 57 54
      app/JsonRpc/NewsService.php

+ 57 - 54
app/JsonRpc/NewsService.php

@@ -59,6 +59,7 @@ use App\Model\Character;
 use App\Model\Couplet;
 use App\Model\Riddle;
 use App\Model\Idiom;
+use App\Model\WhiteRouter;
 use Illuminate\Support\Facades\Cache;
 
 #[RpcService(name: "NewsService", protocol: "jsonrpc-http", server: "jsonrpc-http")]
@@ -5692,60 +5693,62 @@ class NewsService implements NewsServiceInterface
    * 农网导航首页数据
    */
   public function getWebsiteNwHomeList(array $data): array{
-      $categoryIds =  WebsiteCategory::where(['website_id'=> $data['website_id']])->where("pid","!=",0)
-          ->pluck('category_id')
-          ->toArray();
-      // 使用子查询优化
-      $articlesList = DB::table('article')
-          ->select([
-              'article.id',
-              'article.title',
-              'article.imgurl',
-              'article.created_at',
-              'category.name as category_name',
-              'category.id as category_id',
-              'website_category.aLIas_pinyin as pinyin',
-              DB::raw('100 as type')
-          ])
-          ->join('category', 'article.catid', '=', 'category.id')
-          ->leftJoin('website_category', 'website_category.category_id', '=', 'category.id')
-          ->where('website_category.website_id', $data['website_id'])
-          ->where('article.status', 1)
-          ->whereIn('category.id', $categoryIds)
-          // ->whereRaw('article.created_at >= DATE_SUB(NOW(), INTERVAL 30 DAY)')
-          ->orderBy('article.created_at', 'desc')
-          ->get()
-          ->groupBy('category_id')
-          ->map(function ($group) {
-              // 确保第一条数据有图片
-              $firstWithImage = $group->first(function ($item) {
-                  return !empty($item->imgurl);
-              });
-
-              if ($firstWithImage) {
-                  // 如果有带图片的文章,将其放在第一位
-                  $group = $group->filter(function ($item) use ($firstWithImage) {
-                      return $item->id !== $firstWithImage->id;
-                  })->prepend($firstWithImage);
-              }
+    $categoryIds =  WebsiteCategory::where(['website_id'=> $data['website_id']])->where("pid","!=",0)
+        ->pluck('category_id')
+        ->toArray();
+    // 使用子查询优化
+    $articlesList = DB::table('article')
+        ->select([
+            'article.id',
+            'article.title',
+            'article.imgurl',
+            'article.created_at',
+            'category.name as category_name',
+            'category.id as category_id',
+            'website_category.aLIas_pinyin as pinyin',
+            'parent_wc.aLIas_pinyin as parent_pinyin',
+            DB::raw('100 as type')
+        ])
+        ->join('category', 'article.catid', '=', 'category.id')
+        ->leftJoin('website_category', 'website_category.category_id', '=', 'category.id')
+        ->leftJoin('website_category as parent_wc', function($join) {
+            $join->on('parent_wc.category_id', '=', 'website_category.pid')
+                ->where('parent_wc.website_id', '=', DB::raw('website_category.website_id'));
+        })
+        ->where('website_category.website_id', $data['website_id'])
+        ->where('article.status', 1)
+        ->whereIn('category.id', $categoryIds)
+        ->orderBy('article.created_at', 'desc')
+        ->get()
+        ->groupBy('category_id')
+        ->map(function ($group) {
+            // 确保第一条数据有图片
+            $firstWithImage = $group->first(function ($item) {
+                return !empty($item->imgurl);
+            });
 
-              return [
-                  'category_name' => $group->first()->category_name,
-                  'catid' => $group->first()->category_id,
-                  'pinyin' => $group->first()->pinyin,
-//                  'type' => 100,
-                  'list' => $group->take(4)->map(function ($item) {
-                      return [
-                          'id' => $item->id,
-                          'title' => $item->title,
-//                          'imgurl' => $item->imgurl,
-//                          'created_at' => $item->created_at
-                      ];
-                  })->values()
-              ];
-          })
-          ->values()
-          ->toArray();
-      return Result::success($articlesList);
+            if ($firstWithImage) {
+                // 如果有带图片的文章,将其放在第一位
+                $group = $group->filter(function ($item) use ($firstWithImage) {
+                    return $item->id !== $firstWithImage->id;
+                })->prepend($firstWithImage);
+            }
+
+            return [
+                'category_name' => $group->first()->category_name,
+                'catid' => $group->first()->category_id,
+                'pinyin' => $group->first()->pinyin,
+                'parent_pinyin' => $group->first()->parent_pinyin,
+                'list' => $group->take(4)->map(function ($item) {
+                    return [
+                        'id' => $item->id,
+                        'title' => $item->title,
+                    ];
+                })->values()
+            ];
+        })
+        ->values()
+        ->toArray();
+    return Result::success($articlesList);
   }
 }