Browse Source

查询所有的栏目

rkljw 12 hours ago
parent
commit
7fd856ec98
1 changed files with 33 additions and 12 deletions
  1. 33 12
      app/JsonRpc/WebsiteService.php

+ 33 - 12
app/JsonRpc/WebsiteService.php

@@ -49,6 +49,10 @@ use App\Model\WhiteRouter;
 use App\Model\Size;
 use App\Tools\PinyinHelper;
 use Hyperf\HttpServer\Annotation\AutoController;
+use Hyperf\Utils\Parallel;
+use Hyperf\Coroutine\Concurrent;
+use function Hyperf\Coroutine\batch;
+use Swoole\Coroutine;
 #[RpcService(name: "WebsiteService", protocol: "jsonrpc-http", server: "jsonrpc-http")]
 class WebsiteService implements WebsiteServiceInterface
 {
@@ -2425,20 +2429,37 @@ class WebsiteService implements WebsiteServiceInterface
      */
     public function getAllCategory(array $data): array
     {
-        $result = Category::when($data, function ($query) use ($data) {
-            if (isset($data['name']) && !empty($data['name'])) {
-                $query->where('category.name', 'like', '%' . trim($data['name']) . '%');
+        $total = Category::count();
+        $pageSize = 2000;
+        $pages = (int) ceil($total / $pageSize);
+
+        $results = [];
+        $concurrency = 5; // 最大并发数
+        $running = 0;
+        $chan = new \Swoole\Coroutine\Channel($pages);
+
+        for ($i = 0; $i < $pages; $i++) {
+            Coroutine::create(function () use ($i, $pageSize, $chan) {
+                $offset = $i * $pageSize;
+                $data = Category::useIndex('idx_covering')
+                    ->orderBy('id', 'asc')
+                    ->select('id', 'name', 'pid')
+                    ->offset($offset)
+                    ->limit($pageSize)
+                    ->get()
+                    ->toArray();
+                $chan->push($data);
+            });
+        }
+
+        for ($i = 0; $i < $pages; $i++) {
+            $res = $chan->pop();
+            if (is_array($res)) {
+                $results = array_merge($results, $res);
             }
-        })
-            ->useIndex('idx_covering') // 强制使用覆盖索引
-            ->orderBy('id', 'asc')
-            ->select('id','name','pid')
-            ->get();
-        if (empty($result)) {
-            return Result::error("获取失败", 0);
-        } else {
-            return Result::success($result);
         }
+
+        return Result::success($results);
     }
 
     /**