WorkOrderResource.php 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  1. <?php
  2. namespace App\Http\Resources\App;
  3. use App\Models\WorkOrder;
  4. use Carbon\Carbon;
  5. use Illuminate\Http\Resources\Json\JsonResource;
  6. class WorkOrderResource extends JsonResource
  7. {
  8. /**
  9. * Transform the resource into an array.
  10. *
  11. * @param \Illuminate\Http\Request $request
  12. * @return array
  13. */
  14. public function toArray($request)
  15. {
  16. $model = $this->resource;
  17. $reason = $model->reason ?? '';
  18. $type = $model->type;
  19. // if($type === WorkOrder::TYPE_TROUBLE){
  20. $str = '';
  21. if(is_serialized($reason)){
  22. $reasonArr = unserialize($reason);
  23. foreach ($reasonArr as $k=>$v){
  24. if($v == 1){
  25. $str .= WorkOrder::$warningMaps[$k]."、";
  26. }
  27. }
  28. $str = rtrim($str,'、');
  29. $reason = $str;
  30. }
  31. // }
  32. $worker = $model->worker->name ?? '';
  33. if($model->planned == WorkOrder::PLANNED_STATUS_WORKED || $model->planned == WorkOrder::PLANNED_STATUS_OVER){
  34. $worker = $model->worker->name ?? '系统';
  35. }
  36. $data = [
  37. 'id' => $model->id,
  38. 'source' => WorkOrder::$sourceMaps[$model->source],
  39. 'planned' => WorkOrder::$plannedMaps[$model->planned],
  40. 'bike_no' => $model->bike_no,
  41. 'created_at' => Carbon::parse($model->created_at)->format('Y-m-d H:i:s'),
  42. 'updated_at' => Carbon::parse($model->updated_at)->format('Y-m-d H:i:s'),
  43. 'worker' => $worker,
  44. 'type_name' => $model->type_name,
  45. 'type' => $model->type,
  46. 'times' => $model->times,
  47. 'reason' => $reason,
  48. ];
  49. // return parent::toArray($request);
  50. return $data;
  51. }
  52. }