ProjectGanttResource.php 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344
  1. <?php
  2. namespace App\Http\Resources\API;
  3. use Illuminate\Http\Request;
  4. use Illuminate\Http\Resources\Json\JsonResource;
  5. class ProjectGanttResource extends JsonResource
  6. {
  7. /**
  8. * Transform the resource into an array.
  9. *
  10. * @return array<string, mixed>
  11. */
  12. public function toArray(Request $request): array
  13. {
  14. if ((strtotime($this->end)-strtotime($this->begin))===0){
  15. $progress=0; //防止分母为0
  16. }
  17. else{
  18. $progress= (time()-strtotime($this->begin))/(strtotime($this->end)-strtotime($this->begin));
  19. if (time()<strtotime($this->begin)) $progress=0; //1.当前时间<开始时间,进度则为0
  20. if (time()>=strtotime($this->end)) $progress=1;//2.分母不为0,当前时间>=结束时间,进度则为1
  21. }
  22. return [
  23. "id" => $this->id,
  24. "name" => $this->name,
  25. "status" => $this->status,
  26. "assign_id" => $this->assignTo?$this->assignTo->id:null,
  27. "assign_name" =>$this->assignTo? $this->assignTo->name:null,
  28. "assign_username" => $this->assignTo?$this->assignTo->username:null,
  29. "begin" => $this->begin,
  30. "end" => $this->end,
  31. "progress" =>$progress,
  32. "parent_id" => $this->group_label_id,
  33. "parent_name" => $this->group_label_name,
  34. "group" => (string)$this->group,
  35. ];
  36. }
  37. }