Jelajahi Sumber

Merge branch 'dev' into comment_image

kely 11 bulan lalu
induk
melakukan
f408be7777

+ 0 - 44
app/Http/Resources/API/ProjectGanttResource.php

@@ -1,44 +0,0 @@
-<?php
-
-namespace App\Http\Resources\API;
-
-use Illuminate\Http\Request;
-use Illuminate\Http\Resources\Json\JsonResource;
-
-class ProjectGanttResource extends JsonResource
-{
-    /**
-     * Transform the resource into an array.
-     *
-     * @return array<string, mixed>
-     */
-    public function toArray(Request $request): array
-    {
-        if ((strtotime($this->end)-strtotime($this->begin))===0){
-            $progress=0; //防止分母为0
-        }
-        else{
-            $progress=  (time()-strtotime($this->begin))/(strtotime($this->end)-strtotime($this->begin));
-            if (time()<strtotime($this->begin))  $progress=0; //1.当前时间<开始时间,进度则为0
-            if (time()>=strtotime($this->end)) $progress=1;//2.分母不为0,当前时间>=结束时间,进度则为1
-        }
-
-
-
-
-        return [
-            "id" => $this->id,
-            "name" => $this->name,
-            "status" => $this->status,
-            "assign_id" => $this->assignTo?$this->assignTo->id:null,
-            "assign_name" =>$this->assignTo? $this->assignTo->name:null,
-            "assign_username" => $this->assignTo?$this->assignTo->username:null,
-            "begin" => $this->begin,
-            "end" => $this->end,
-            "progress" =>$progress,
-            "parent_id" => $this->group_label_id,
-            "parent_name" => $this->group_label_id,
-            "group" => (string)$this->group,
-        ];
-    }
-}

+ 70 - 18
app/Services/Project/ProjectGanttService.php

@@ -9,6 +9,7 @@ use App\Models\Project;
 use App\Models\Requirement;
 use App\Models\Task;
 use App\Models\User;
+use Carbon\Carbon;
 use Illuminate\Support\Collection;
 
 class ProjectGanttService
@@ -26,27 +27,78 @@ class ProjectGanttService
             if (! isset($groupTasks[$groupKey])) {
                 continue;
             }
-            $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;
+
+            $treeTasks = make_tree($groupTasks[$groupKey]->toArray());
+
+            $groupUniqueKey = uniqid();
+            $items[] = $this->topGroupFormat(
+                $groupUniqueKey,
+                isset($groupNamesKeyBy[$groupKey]) ? $groupNamesKeyBy[$groupKey]['name'] : "Empty"
+            );
+
+            $tasks = $this->flattenTasks($treeTasks);
+
+            foreach ($tasks as $task) {
+                $items[] = $this->taskFormat($task, $groupUniqueKey);
             }
-            $items[] = [
-//                'group' => $group,
-//                'group_label' => $groupKey == "" ? ['id' => "", "name" => ""] : $groupNamesKeyBy[$groupKey],
-                ProjectGanttResource::collection($groupTasks[$groupKey]),
-            ];
-        };
-        $result=[];
-        foreach ($items as $item){
-            foreach ($item as $it){
-                foreach ($it as $i){
-                    $result[]= $i;
-                }
+        }
+
+        return $items;
+    }
+
+    protected function topGroupFormat(string $id, string $name)
+    {
+        return [
+            'id' => $id,
+            'text' => $name,
+            'parent' => 0,
+            'start_date' => null,
+            'duration' => null,
+            'progress' => 0,
+            'open' => true,
+        ];
+    }
+
+    protected function taskFormat(array $task, string $topId)
+    {
+        $progress = 0;
+        $begin = $task['begin'] ? Carbon::parse($task['begin']) : Carbon::parse($task['created_at']);
+        $end = $task['end'] ? Carbon::parse($task['end']) : $begin->copy()->addYears(2);
+        $now = Carbon::now();
+
+        if ($now->gt($end)) {
+            $progress = 1;
+        }
+
+        if ($now->gt($begin) && $end->gt($now)) {
+            $totalDay = $end->diffInDays($begin);
+            $day = $now->diffInDays($begin);
+            $progress = (float)number_format($day / $totalDay, 4);
+        }
+
+        return [
+            'id' => $task['id'],
+            'text' => $task['name'],
+            'parent' => $task['parent_id'] > 0 ? $task['parent_id'] : $topId ,
+            'start_date' => $task['begin'],
+            'duration' => $end->diffInDays($begin),
+            'progress' => $progress,
+            'open' => true,
+        ];
+    }
+
+    protected function flattenTasks($tasks) {
+        $items = [];
+
+        foreach ($tasks as $task) {
+            $items[] = $task;
+            if (!empty($task['children'])) {
+                $items = array_merge($items, $this->flattenTasks($task['children']));
             }
+            unset($task['children']);
         }
-        return $result;
+
+        return $items;
     }
 
     protected function getGroupNamesKeyBy(Collection $groups, string $group): array