瀏覽代碼

项目甘特图响应若该任务为顶层任务,则取其任务名以及id,否则找该任务的父级id和父级name

langshiyeye 11 月之前
父節點
當前提交
4f99e96143
共有 3 個文件被更改,包括 15 次插入5 次删除
  1. 1 1
      app/Http/Resources/API/ProjectGanttResource.php
  2. 4 1
      app/Models/Task.php
  3. 10 3
      app/Services/Project/ProjectGanttService.php

+ 1 - 1
app/Http/Resources/API/ProjectGanttResource.php

@@ -37,7 +37,7 @@ class ProjectGanttResource extends JsonResource
             "end" => $this->end,
             "progress" =>$progress,
             "parent_id" => $this->group_label_id,
-            "parent_name" => $this->group_label_id,
+            "parent_name" => $this->group_label_name,
             "group" => (string)$this->group,
         ];
     }

+ 4 - 1
app/Models/Task.php

@@ -96,7 +96,10 @@ class Task extends Model
     {
         return $this->hasMany(Task::class, 'parent_id');
     }
-
+    public function parent(): \Illuminate\Database\Eloquent\Relations\HasOne
+    {
+        return $this->hasOne(Task::class, 'id','parent_id');
+    }
     public function closedBy(): \Illuminate\Database\Eloquent\Relations\BelongsTo
     {
         return $this->belongsTo(User::class, 'closed_by');

+ 10 - 3
app/Services/Project/ProjectGanttService.php

@@ -28,13 +28,20 @@ class ProjectGanttService
             }
             $group_label = $groupKey == "" ? ['id' => "", "name" => ""] : $groupNamesKeyBy[$groupKey];
             foreach ($groupTasks[$groupKey] as $tasks){
-                $tasks->group_label_id=$group_label['id'];
-                $tasks->group_label_name=$group_label['name'];
-                $tasks->group=$group;
+                if ($tasks->parent_id===0){ //若该任务为顶层任务,则取其任务名以及id,否则找该任务的父级id和父级name
+                    $tasks->group_label_id=$group_label['id'];
+                    $tasks->group_label_name=$group_label['name'];
+                }else{
+                    $tasks->group_label_id=$tasks->parent_id;
+                    $tasks->group_label_name=$tasks->parent->name;
+
+                }
+
             }
             $items[] = [
 //                'group' => $group,
 //                'group_label' => $groupKey == "" ? ['id' => "", "name" => ""] : $groupNamesKeyBy[$groupKey],
+
                 ProjectGanttResource::collection($groupTasks[$groupKey]),
             ];
         };