Pārlūkot izejas kodu

给资产列表返回计划数和需求数

kely 1 gadu atpakaļ
vecāks
revīzija
d4bcb846ee

+ 1 - 0
app/Http/Resources/API/AssetRequirementResource.php

@@ -21,6 +21,7 @@ class AssetRequirementResource extends JsonResource
             'requirement_group' => new RequirementGroupParentResource($this->group),
             'title' => $this->title,
             'created_by' => new UserProfileResource($this->createdBy),
+            'reviewed_by' =>  new UserProfileResource($this->reviewedBy),
             'plan' => new SimplePlanResource($this->plan),
             'status' => $this->status,
             'asset_id' => $this->asset_id,

+ 3 - 0
app/Http/Resources/API/AssetResource.php

@@ -31,6 +31,9 @@ class AssetResource extends JsonResource
             'longitude' => $this->longitude,
             'parent_id' => $this->parent_id,
             'level' => count(explode(",", trim($this->path, ","))),
+            'requirement_total' => $this->total_requirements_count,
+            'plan_total' => $this->total_plans_count,
+            'created_at'=> $this->created_at,
             'children' =>  $this->when($this->children->isNotEmpty(), function () {
                 return $this->children->map(function ($child) {
                     return new AssetResource($child, $this->level);

+ 24 - 0
app/Models/Asset.php

@@ -56,4 +56,28 @@ class Asset extends Model
         return $this->belongsToMany(Project::class,'project_asset');
     }
 
+    //获取子级需求数量
+    public function getTotalRequirementsCountAttribute()
+    {
+        $totalRequirementsCount = $this->requirements()->count(); // 当前资产的需求数量
+
+        foreach ($this->children as $child) {
+            $totalRequirementsCount += $child->getTotalRequirementsCountAttribute(); // 递归调用子资产的方法
+        }
+
+        return  $totalRequirementsCount;
+    }
+
+    //获取子级计划数量
+    public function getTotalPlansCountAttribute()
+    {
+        $totalPlansCount = $this->plans()->count(); // 当前资产的需求数量
+
+        foreach ($this->children as $child) {
+            $totalPlansCount += $child->getTotalPlansCountAttribute(); // 递归调用子资产的方法
+        }
+
+        return  $totalPlansCount;
+    }
+
 }