ProjectTaskGroupViewService.php 3.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. <?php
  2. namespace App\Services\Project;
  3. use App\Http\Resources\API\ProjectGroupViewTaskResource;
  4. use App\Models\Enums\ProjectStatus;
  5. use App\Models\Project;
  6. use App\Models\Requirement;
  7. use App\Models\Task;
  8. use App\Models\User;
  9. use Illuminate\Support\Collection;
  10. class ProjectTaskGroupViewService
  11. {
  12. public function groupView(Project $project, string $group, array $filter = []): array
  13. {
  14. $groupTasks = $this->getGroupTask($project, $group, $filter);
  15. $countItems = $this->countItems($project, $group, $filter);
  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. 'count' => $countItems[$groupKey],
  27. 'tasks' => ProjectGroupViewTaskResource::collection($groupTasks[$groupKey]),
  28. ];
  29. }
  30. return [
  31. 'total' => $countItems->sum("all_count"),
  32. 'items' => $items,
  33. ];
  34. }
  35. protected function getGroupNamesKeyBy(Collection $groups, string $group): array
  36. {
  37. $groupsFormat = $groups->map(fn($group) => ['id' => $group, 'name' => $group]);
  38. $groupNames = match ($group) {
  39. "requirement_id" => Requirement::query()->whereIn("id", $groups)->selectRaw("id,title as name")->get(),
  40. "status", "task_type" => $groupsFormat,
  41. "assign", "finished_by", "closed_by" => User::query()->whereIn("id", $groups)->get(['id', 'name']),
  42. };
  43. return $groupNames->keyBy("id")->toArray();
  44. }
  45. protected function countItems(
  46. Project $project,
  47. string $group,
  48. array $filter
  49. ): \Illuminate\Database\Eloquent\Collection|\Illuminate\Support\Collection|array
  50. {
  51. return Task::query()
  52. ->where("project_id", $project->id)
  53. ->filter($filter)
  54. ->selectRaw(
  55. sprintf("
  56. %s,
  57. count(*) as all_count,
  58. count(case when status = '%s' then 1 else null end) as wait_count,
  59. count(case when status = '%s' then 1 else null end) as doing_count",
  60. $group,
  61. ProjectStatus::WAIT->value,
  62. ProjectStatus::DOING->value
  63. )
  64. )
  65. ->groupBy($group)
  66. ->get()
  67. ->keyBy($group);
  68. }
  69. protected function getGroupTask(
  70. Project $project,
  71. string $group,
  72. array $filter
  73. ): \Illuminate\Database\Eloquent\Collection|\Illuminate\Support\Collection|array
  74. {
  75. return Task::query()
  76. ->filter($filter)
  77. ->with(['assignTo', 'finishedBy'])
  78. ->where("project_id", $project->id)
  79. ->get()
  80. ->groupBy($group);
  81. }
  82. }