|
@@ -0,0 +1,95 @@
|
|
|
+<?php
|
|
|
+
|
|
|
+namespace App\Services\Project;
|
|
|
+
|
|
|
+use App\Http\Resources\API\ProjectGroupViewTaskResource;
|
|
|
+use App\Models\Enums\ProjectStatus;
|
|
|
+use App\Models\Project;
|
|
|
+use App\Models\Requirement;
|
|
|
+use App\Models\Task;
|
|
|
+use App\Models\User;
|
|
|
+use Illuminate\Support\Collection;
|
|
|
+
|
|
|
+class ProjectTaskGroupViewService
|
|
|
+{
|
|
|
+ public function groupView(Project $project, string $group, array $filter = []): array
|
|
|
+ {
|
|
|
+ $groupTasks = $this->getGroupTask($project, $group, $filter);
|
|
|
+
|
|
|
+ $countItems = $this->countItems($project, $group, $filter);
|
|
|
+
|
|
|
+ $groups = $groupTasks->keys()->filter();
|
|
|
+
|
|
|
+ $groupNamesKeyBy = $this->getGroupNamesKeyBy($groups, $group);
|
|
|
+
|
|
|
+ $items = [];
|
|
|
+ foreach(["", ...$groups] as $groupKey) {
|
|
|
+ if (! isset($groupTasks[$groupKey])) {
|
|
|
+ continue;
|
|
|
+ }
|
|
|
+
|
|
|
+ $items[] = [
|
|
|
+ 'group' => $group,
|
|
|
+ 'group_label' => $groupKey == "" ? ['id' => "", "name" => ""] : $groupNamesKeyBy[$groupKey],
|
|
|
+ 'count' => $countItems[$groupKey],
|
|
|
+ 'tasks' => ProjectGroupViewTaskResource::collection($groupTasks[$groupKey]),
|
|
|
+ ];
|
|
|
+ }
|
|
|
+
|
|
|
+ return [
|
|
|
+ 'total' => $countItems->sum("all_count"),
|
|
|
+ 'items' => $items,
|
|
|
+ ];
|
|
|
+ }
|
|
|
+
|
|
|
+ protected function getGroupNamesKeyBy(Collection $groups, string $group): array
|
|
|
+ {
|
|
|
+ $groupsFormat = $groups->map(fn($group) => ['id' => $group, 'name' => $group]);
|
|
|
+ $groupNames = match ($group) {
|
|
|
+ "requirement_id" => Requirement::query()->whereIn("id", $groups)->selectRaw("id,title as name")->get(),
|
|
|
+ "status", "task_type" => $groupsFormat,
|
|
|
+ "assign", "finished_by", "closed_by" => User::query()->whereIn("id", $groups)->get(['id', 'name']),
|
|
|
+ };
|
|
|
+
|
|
|
+ return $groupNames->keyBy("id")->toArray();
|
|
|
+ }
|
|
|
+
|
|
|
+ protected function countItems(
|
|
|
+ Project $project,
|
|
|
+ string $group,
|
|
|
+ array $filter
|
|
|
+ ): \Illuminate\Database\Eloquent\Collection|\Illuminate\Support\Collection|array
|
|
|
+ {
|
|
|
+ return Task::query()
|
|
|
+ ->where("project_id", $project->id)
|
|
|
+ ->filter($filter)
|
|
|
+ ->selectRaw(
|
|
|
+ sprintf("
|
|
|
+ %s,
|
|
|
+ count(*) as all_count,
|
|
|
+ count(case when status = '%s' then 1 else null end) as wait_count,
|
|
|
+ count(case when status = '%s' then 1 else null end) as doing_count",
|
|
|
+ $group,
|
|
|
+ ProjectStatus::WAIT->value,
|
|
|
+ ProjectStatus::DOING->value
|
|
|
+ )
|
|
|
+ )
|
|
|
+ ->groupBy($group)
|
|
|
+ ->get()
|
|
|
+ ->keyBy($group);
|
|
|
+ }
|
|
|
+
|
|
|
+ protected function getGroupTask(
|
|
|
+ Project $project,
|
|
|
+ string $group,
|
|
|
+ array $filter
|
|
|
+ ): \Illuminate\Database\Eloquent\Collection|\Illuminate\Support\Collection|array
|
|
|
+ {
|
|
|
+ return Task::query()
|
|
|
+ ->filter($filter)
|
|
|
+ ->with(['assignTo', 'finishedBy'])
|
|
|
+ ->where("project_id", $project->id)
|
|
|
+ ->get()
|
|
|
+ ->groupBy($group);
|
|
|
+ }
|
|
|
+}
|