OrderRentResource.php 3.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. <?php
  2. namespace App\Http\Resources;
  3. use App\Models\OrderRent;
  4. use Carbon\Carbon;
  5. use Illuminate\Http\Resources\Json\JsonResource;
  6. class OrderRentResource 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. $now = Carbon::now();
  18. $carbon_start_use_bike_time = Carbon::parse($model->start_use_bike_time);
  19. $real_use_bike_time_length = 0;
  20. switch ($model->status){
  21. case OrderRent::STATUS_RENT_BIKE: // 租车中
  22. $real_use_bike_time_length = $now->diffInMinutes($carbon_start_use_bike_time);
  23. break;
  24. case OrderRent::STATUS_COMPLETE_ORDER: // 订单完成
  25. case OrderRent::STATUS_CLOSE_RENT_BIKE: // 待支付
  26. $real_use_bike_time_length = $model->use_bike_time_length;
  27. break;
  28. default :
  29. $real_use_bike_time_length = 0;
  30. }
  31. $real_use_bike_time_length_time = $real_use_bike_time_length;
  32. if ($real_use_bike_time_length > 60) {
  33. $real_use_bike_time_length = floor($real_use_bike_time_length / 60) . 'h' . ($real_use_bike_time_length % 60) . 'm';
  34. $real_use_bike_time_length_name = floor($real_use_bike_time_length_time / 60) . '小时' . ($real_use_bike_time_length_time % 60) . '分钟';
  35. } else {
  36. $real_use_bike_time_length = $real_use_bike_time_length . 'm';
  37. $real_use_bike_time_length_name = $real_use_bike_time_length_time . '分钟';
  38. }
  39. $order_create_time = Carbon::now()->diffInHours($model->updated_at);
  40. $is_return = true;
  41. // Log::info($order_create_time);
  42. if(abs($order_create_time) > 24 * 5){
  43. $is_return = false;
  44. }
  45. if($model->pay_money == 0){
  46. $is_return = false;
  47. }
  48. $is_huisu = false;
  49. $order_end_time = Carbon::now()->diffInHours($model->end_use_bike_time);
  50. if($model->status == OrderRent::STATUS_CLOSE_RENT_BIKE && $order_end_time <4){
  51. // 4小时内可回溯
  52. $is_huisu = true;
  53. }
  54. $data = [
  55. 'nickname' => $model->users->nickname ? ($model->users->nickname . "(" . $model->users->truename . ")") : '',
  56. 'mobile' => $model->users->mobile ?? '',
  57. 'order_id' => $model->id,
  58. 'area_id' => $model->area_id,
  59. 'orders_status' => OrderRent::$statusMaps[$model->status],
  60. 'area_name' => $model->area->name,
  61. 'real_use_bike_time_length' => $real_use_bike_time_length,
  62. 'is_return' => $is_return,
  63. 'settle_type' => OrderRent::$adminSettleOrderMaps[$model->is_admin_settle_order],
  64. 'no' => $model->no,
  65. 'id' => $model->id,
  66. 'bike_no' => $model->bike_no,
  67. 'pay_money' => $model->pay_money,
  68. 'status' => $model->status,
  69. 'start_use_bike_time' => $model->start_use_bike_time,
  70. 'end_use_bike_time' => $model->end_use_bike_time,
  71. 'order_total_money' => $model->order_total_money,
  72. 'total_money' => $model->total_money,
  73. 'time_money' => $model->time_money,
  74. 'rent_money' => $model->rent_money,
  75. 'dispatch_money' => $model->dispatch_money,
  76. 'use_bike_distance_length' => $model->use_bike_distance_length,
  77. 'pay_type' => $model->pay_type,
  78. 'pay_type_name' => OrderRent::$payTypeMaps[$model->pay_type],
  79. 'is_huisu' => $is_huisu,
  80. 'setting' => $model->setting,
  81. 'phone_detail' => $model->phone_detail ?? '',
  82. 'real_use_bike_time_length_name' => $real_use_bike_time_length_name,
  83. ];
  84. return $data;// array_merge(parent::toArray($request),$data);
  85. }
  86. }