Browse Source

调整打开文件夹的返回数量

kely 10 months ago
parent
commit
758c8e63be
1 changed files with 22 additions and 5 deletions
  1. 22 5
      app/Http/Controllers/API/FolderController.php

+ 22 - 5
app/Http/Controllers/API/FolderController.php

@@ -245,17 +245,34 @@ class FolderController extends Controller
 
         $folderObjectType->modelBuilderAllowed()->findOrFail($objectId);
 
+        $foldsCount = Folder::query()
+            ->where('parent_id',0)
+            ->where('object_type',$objectType)
+            ->whereIn("object_id", $folderId)
+            ->selectRaw("count(*) as cut, object_id")
+            ->groupBy("object_id")
+            ->pluck("cut", "object_id");
+
+        $fileCount = File::query()
+            ->where('object_type',$objectType)
+            ->whereIn('object_id',$folderId)
+            ->where("is_latest_version", 1)
+            ->selectRaw("count(*) as cut, object_id")
+            ->groupBy("object_id")
+            ->pluck("cut", "object_id");
+
+
         $objectWhere = ['object_type' => $objectType, 'object_id' => $objectId,];
         $folders = Folder::query()
             ->where($objectWhere)
             ->when($folderId, fn($query) => $query->where("parent_id", $folderId))
             ->when(! $folderId, fn($query) => $query->where("parent_id", 0))
-            ->get(['id', 'name'])->each(function ($folders)use ($folderId){
-                $folderCount=Folder::query()->where('parent_id',$folders->id)->count();
-                $filesCount=File::query()->where('folder_id',$folderId>0?$folders->id:$folderId)->where("is_latest_version", 1)->count();
-                $folders->itemCount=$folderCount+$filesCount;
-            });
+            ->get(['id', 'name']);
 
+          $folders = $folders->map(function (Folder $folder) use ($foldsCount,$fileCount) {
+              $folder->itemCount =$foldsCount->get($folder->id, 0)+$fileCount->get($folder->id, 0);
+              return $folder;
+          });
 
         $files = File::query()->where($objectWhere)
             ->where("folder_id", $folderId)