1234567891011121314151617181920212223242526272829303132333435363738394041424344 |
- <?php
- namespace App\Http\Resources\API;
- use Illuminate\Http\Request;
- use Illuminate\Http\Resources\Json\JsonResource;
- class ProjectGanttResource extends JsonResource
- {
- /**
- * Transform the resource into an array.
- *
- * @return array<string, mixed>
- */
- public function toArray(Request $request): array
- {
- if ((strtotime($this->end)-strtotime($this->begin))===0){
- $progress=0; //防止分母为0
- }
- else{
- $progress= (time()-strtotime($this->begin))/(strtotime($this->end)-strtotime($this->begin));
- if (time()<strtotime($this->begin)) $progress=0; //1.当前时间<开始时间,进度则为0
- if (time()>=strtotime($this->end)) $progress=1;//2.分母不为0,当前时间>=结束时间,进度则为1
- }
- return [
- "id" => $this->id,
- "name" => $this->name,
- "status" => $this->status,
- "assign_id" => $this->assignTo?$this->assignTo->id:null,
- "assign_name" =>$this->assignTo? $this->assignTo->name:null,
- "assign_username" => $this->assignTo?$this->assignTo->username:null,
- "begin" => $this->begin,
- "end" => $this->end,
- "progress" =>$progress,
- "parent_id" => $this->group_label_id,
- "parent_name" => $this->group_label_name,
- "group" => (string)$this->group,
- ];
- }
- }
|