1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495 |
- <?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);
- }
- }
|