Jelajahi Sumber

Merge branch 'refs/heads/peterguo-dev' into dev

peterguo 1 bulan lalu
induk
melakukan
2b20e0808c

+ 55 - 0
app/Http/Controllers/API/DocumentController.php

@@ -0,0 +1,55 @@
+<?php
+
+namespace App\Http\Controllers\API;
+
+use App\Http\Controllers\Controller;
+use App\Models\Enums\ActionObjectType;
+use App\Services\Folder\FoldersService;
+use Illuminate\Http\JsonResponse;
+
+class DocumentController extends Controller
+{
+    public function pathInfo(string $objectType, string $objectId): JsonResponse
+    {
+        $data = [
+            'type' => null,
+            'asset_id' => null,
+            'library_id' => null,
+            'container_id' => null,
+            'folder' => null,
+            'object_id' => intval($objectId),
+            'object_type' => $objectType,
+        ];
+        $model = ActionObjectType::from($objectType)->modelBuilder()->findOrFail($objectId);
+
+        switch ($objectType) {
+            case ActionObjectType::CONTAINER_FILE->value:
+                $data['folder'] = (new FoldersService())->getAllParentTree($model->folder_id);
+                $data['container_id'] = $model->container?->id;
+                $data['library_id'] = $model->library?->id;
+                $data['asset_id'] = $model->library?->asset_id;
+                $data['type'] = $model->library?->type;
+                break;
+            case ActionObjectType::FOLDER->value:
+                $data['folder'] = (new FoldersService())->getAllParentTree($model->id);
+                $data['container_id'] = $model->container?->id;
+                $data['library_id'] = $model->library?->id;
+                $data['asset_id'] = $model->library?->asset_id;
+                $data['type'] = $model->library?->type;
+                break;
+            case ActionObjectType::CONTAINER->value:
+                $data['container_id'] = $model->id;
+                $data['library_id'] = $model->library?->id;
+                $data['asset_id'] = $model->library?->asset_id;
+                $data['type'] = $model->library?->type;
+                break;
+            case ActionObjectType::LIBRARY->value:
+                $data['library_id'] = $model->id;
+                $data['asset_id'] = $model->asset_id;
+                $data['type'] = $model->type;
+            default:
+                break;
+        }
+        return $this->success(['data' => $data]);
+    }
+}

+ 5 - 0
app/Models/Enums/ActionObjectType.php

@@ -6,6 +6,7 @@ use App\Models\Asset;
 use App\Models\Container;
 use App\Models\File;
 use App\Models\Folder;
+use App\Models\Library;
 use App\Models\Plan;
 use App\Models\Project;
 use App\Models\Requirement;
@@ -31,6 +32,8 @@ enum ActionObjectType: string
 
     case PLAN = "plan";
 
+    case LIBRARY = "library";
+
     case CONTAINER = "container";
 
     case CONTAINER_CONTENT = "container_content";
@@ -47,6 +50,7 @@ enum ActionObjectType: string
             self::TASK => Task::query(),
             self::PLAN => Plan::query(),
             self::REQUIREMENT => Requirement::query(),
+            self::LIBRARY => Library::query(),
             self::CONTAINER => Container::query(),
             self::CONTAINER_FILE => File::query(),
             self::FOLDER => Folder::query(),
@@ -61,6 +65,7 @@ enum ActionObjectType: string
             self::TASK => Task::query()->allowed($id),
             self::PLAN => Plan::query(),
             self::REQUIREMENT => Requirement::query(),
+            self::LIBRARY => Library::query()->allowed(),
             self::CONTAINER => Container::query()->allowed($id),
             self::CONTAINER_FILE => File::query(),
             self::FOLDER => Folder::query(),

+ 7 - 6
app/Models/File.php

@@ -7,6 +7,8 @@ use App\Models\Scopes\CompanyScope;
 use Illuminate\Database\Eloquent\Factories\HasFactory;
 use Illuminate\Database\Eloquent\Model;
 use Illuminate\Database\Eloquent\Relations\BelongsTo;
+use Illuminate\Database\Eloquent\Relations\HasOne;
+use Illuminate\Database\Eloquent\Relations\HasOneThrough;
 use Illuminate\Database\Eloquent\SoftDeletes;
 use Illuminate\Support\Facades\Auth;
 
@@ -59,18 +61,17 @@ class File extends Model
         return $this->belongsTo(Folder::class);
     }
 
-    public function container()
+    public function container(): BelongsTo
     {
-        return $this->belongsTo(Container::class, 'object_id')->where('object_type', FileObjectType::CONTAINER->value);
+        return $this->belongsTo(Container::class, 'object_id');
     }
 
-    public function containerLibrary()
+    public function library(): HasOneThrough
     {
-        return $this->hasOneThrough(Library::class, Container::class, 'id', 'id', 'object_id', 'library_id')
-            ->where('object_type', FileObjectType::CONTAINER->value);
+        return $this->hasOneThrough(Library::class, Container::class, 'id', 'id', 'object_id', 'library_id');
     }
 
-    public function bimFile(): \Illuminate\Database\Eloquent\Relations\HasOne
+    public function bimFile(): HasOne
     {
         return $this->hasOne(BimFile::class);
     }

+ 12 - 0
app/Models/Folder.php

@@ -2,11 +2,13 @@
 
 namespace App\Models;
 
+use App\Models\Enums\FileObjectType;
 use App\Models\Scopes\CompanyScope;
 use Illuminate\Database\Eloquent\Factories\HasFactory;
 use Illuminate\Database\Eloquent\Model;
 use Illuminate\Database\Eloquent\Relations\BelongsTo;
 use Illuminate\Database\Eloquent\Relations\HasMany;
+use Illuminate\Database\Eloquent\Relations\HasOneThrough;
 use Illuminate\Database\Eloquent\SoftDeletes;
 
 class Folder extends Model
@@ -40,4 +42,14 @@ class Folder extends Model
     {
         return $this->hasMany(Folder::class, 'parent_id');
     }
+
+    public function container(): BelongsTo
+    {
+        return $this->belongsTo(Container::class, 'object_id');
+    }
+
+    public function library(): HasOneThrough
+    {
+        return $this->hasOneThrough(Library::class, Container::class, 'id', 'id', 'object_id', 'library_id');
+    }
 }

+ 14 - 0
app/Services/Folder/FoldersService.php

@@ -5,6 +5,7 @@ namespace App\Services\Folder;
 use App\Models\File;
 use App\Models\Folder;
 use App\Models\NamingRule;
+use Illuminate\Database\Eloquent\Collection;
 use Illuminate\Support\Facades\DB;
 
 class FoldersService
@@ -25,4 +26,17 @@ class FoldersService
                 ->update(['files.naming_rule_id' => $namingRule->id]);
         });
     }
+
+    public function getAllParentTree($folderId): Collection|array
+    {
+        $folder = Folder::query() ->select('id', 'parent_id', 'path')->findOrFail($folderId);
+        $folderIds = explode(',', $folder->path);
+
+        $allChildren = Folder::query()
+            ->select('id', 'parent_id')
+            ->whereIn('id', $folderIds ?? [])
+            ->get();
+
+        return make_tree($allChildren->toArray());
+    }
 }

+ 3 - 0
routes/api.php

@@ -206,6 +206,9 @@ Route::middleware(['auth:sanctum','account.limit'])->group(function () {
         Route::get("project-iot/objects-values/{siteName}/{dataKey}", [API\ProjectController::class, "fetchObjectsValueMulti"])
             ->name("project.fetchObjectsValueMulti");
 
+
+        Route::get("document/path-info/{objectType}/{objectId}", [API\DocumentController::class, "pathInfo"])->name("document.pathInfo");
+
 //        Route::get("project/{project}/not-link-asset-requirement", [API\ProjectController::class, "notLinkAssetRequirement"])
 //            ->name("project.not-link-asset-requirement"); //项目未关联的资产需求