ProjectGanttService.php 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. <?php
  2. namespace App\Services\Project;
  3. use App\Http\Resources\API\ProjectGanttResource;
  4. use App\Http\Resources\API\ProjectGroupViewTaskResource;
  5. use App\Models\Enums\ProjectStatus;
  6. use App\Models\Project;
  7. use App\Models\Requirement;
  8. use App\Models\Task;
  9. use App\Models\User;
  10. use Illuminate\Support\Collection;
  11. class ProjectGanttService
  12. {
  13. public function gantt(Project $project, string $group): array
  14. {
  15. $groupTasks = $this->getGroupTask($project, $group);
  16. $groups = $groupTasks->keys()->filter();
  17. $groupNamesKeyBy = $this->getGroupNamesKeyBy($groups, $group);
  18. $items = [];
  19. foreach(["", ...$groups] as $groupKey) {
  20. if (! isset($groupTasks[$groupKey])) {
  21. continue;
  22. }
  23. $items[] = [
  24. 'group' => $group,
  25. 'group_label' => $groupKey == "" ? ['id' => "", "name" => ""] : $groupNamesKeyBy[$groupKey],
  26. 'tasks' => ProjectGanttResource::collection($groupTasks[$groupKey]),
  27. ];
  28. }
  29. return $items;
  30. }
  31. protected function getGroupNamesKeyBy(Collection $groups, string $group): array
  32. {
  33. $groupsFormat = $groups->map(fn($group) => ['id' => $group, 'name' => $group]);
  34. $groupNames = match ($group) {
  35. "requirement_id" => Requirement::query()->whereIn("id", $groups)->selectRaw("id,title as name")->get(),
  36. "task_type" => $groupsFormat,
  37. "assign" => User::query()->whereIn("id", $groups)->get(['id', 'name']),
  38. };
  39. return $groupNames->keyBy("id")->toArray();
  40. }
  41. protected function getGroupTask(
  42. Project $project,
  43. string $group,
  44. ): \Illuminate\Database\Eloquent\Collection|\Illuminate\Support\Collection|array
  45. {
  46. return Task::query()
  47. ->with(['assignTo', 'finishedBy'])
  48. ->where("project_id", $project->id)
  49. ->get()
  50. ->groupBy($group);
  51. }
  52. }