12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182 |
- <?php
- namespace App\Services\Project;
- use App\Http\Resources\API\ProjectGanttResource;
- 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 ProjectGanttService
- {
- public function gantt(Project $project, string $group): array
- {
- $groupTasks = $this->getGroupTask($project, $group);
- $groups = $groupTasks->keys()->filter();
- $groupNamesKeyBy = $this->getGroupNamesKeyBy($groups, $group);
- $items = [];
- foreach(["", ...$groups] as $groupKey) {
- if (! isset($groupTasks[$groupKey])) {
- continue;
- }
- $group_label = $groupKey == "" ? ['id' => "", "name" => ""] : $groupNamesKeyBy[$groupKey];
- foreach ($groupTasks[$groupKey] as $tasks){
- if ($tasks->parent_id===0){ //若该任务为顶层任务,则取其任务名以及id,否则找该任务的父级id和父级name
- $tasks->group_label_id=$group_label['id'];
- $tasks->group_label_name=$group_label['name'];
- }else{
- $tasks->group_label_id=$tasks->parent_id;
- $tasks->group_label_name=$tasks->parent->name;
- }
- }
- $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 $result;
- }
- 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(),
- "task_type" => $groupsFormat,
- "assign" => User::query()->whereIn("id", $groups)->get(['id', 'name']),
- };
- return $groupNames->keyBy("id")->toArray();
- }
- protected function getGroupTask(
- Project $project,
- string $group,
- ): \Illuminate\Database\Eloquent\Collection|\Illuminate\Support\Collection|array
- {
- return Task::query()
- ->with(['assignTo', 'finishedBy'])
- ->where("project_id", $project->id)
- ->get()
- ->groupBy($group);
- }
- }
|