12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182 |
- <?php
- namespace App\Http\Resources;
- use App\Models\Order;
- use App\Models\OrderRent;
- use Carbon\Carbon;
- use Illuminate\Http\Resources\Json\JsonResource;
- use Illuminate\Support\Facades\Log;
- class OrderResource extends JsonResource
- {
- /**
- * Transform the resource into an array.
- *
- * @param \Illuminate\Http\Request $request
- * @return array
- */
- public function toArray($request)
- {
- $model = $this->resource;
- $now = Carbon::now();
- $carbon_start_use_bike_time = Carbon::parse($model->start_use_bike_time);
- $real_use_bike_time_length = 0;
- $real_use_bike_time_length_name = 0;
- switch ($model->status) {
- case Order::STATUS_PAUSE_BIKE: // 临时停车
- case Order::STATUS_RIDE_BIKE : // 骑行中
- $real_use_bike_time_length = $now->diffInMinutes($carbon_start_use_bike_time);
- break;
- case Order::STATUS_CLOSE_ORDER: // 订单关闭
- case Order::STATUS_CLOSE_BIKE: // 待支付
- case Order::STATUS_COMPLETE_ORDER: // 订单完成
- $real_use_bike_time_length = $model->use_bike_time_length;
- break;
- default :
- $real_use_bike_time_length = 0;
- }
- $real_use_bike_time_length_time = $real_use_bike_time_length;
- if ($real_use_bike_time_length > 60) {
- $real_use_bike_time_length = floor($real_use_bike_time_length / 60) . 'h' . ($real_use_bike_time_length % 60) . 'm';
- $real_use_bike_time_length_name = floor($real_use_bike_time_length_time / 60) . '小时' . ($real_use_bike_time_length_time % 60) . '分钟';
- } else {
- $real_use_bike_time_length = $real_use_bike_time_length . 'm';
- $real_use_bike_time_length_name = $real_use_bike_time_length_time . '分钟';
- }
- $order_create_time = Carbon::now()->diffInHours($model->pay_time);
- $is_return = true;
- if (abs($order_create_time) > 24 * 5) {
- $is_return = false;
- }
- if ($model->pay_money == 0) {
- $is_return = false;
- }
- $is_huisu = false;
- $order_end_time = Carbon::now()->diffInHours($model->end_use_bike_time);
- if ($model->status == Order::STATUS_CLOSE_BIKE && $order_end_time < 4) {
- // 4小时内可回溯
- $is_huisu = true;
- }
- $data = [
- 'nickname' => $model->users->nickname ? ($model->users->nickname . ($model->users->truename ? "(" . $model->users->truename . ")" : '')) : '',
- 'mobile' => $model->users->mobile ?? '',
- 'order_id' => $model->id,
- 'orders_status' => Order::$statusMaps[$model->status],
- 'area_name' => $model->area->name,
- 'real_use_bike_time_length' => $real_use_bike_time_length,
- 'real_use_bike_time_length_name' => $real_use_bike_time_length_name,
- 'is_return' => $is_return,
- 'settle_type' => Order::$adminSettleOrderMaps[$model->is_admin_settle_order],
- 'pay_type_name' => Order::$payTypeMaps[$model->pay_type],
- 'is_huisu' => $is_huisu,
- 'preferential_type_name' => $model->preferential_type_name,
- 'is_coupon_name' => Order::$couponMaps[$model->is_coupon],
- 'is_riding_card_name' => Order::$ridingCardMaps[$model->is_riding_card],
- ];
- return array_merge(parent::toArray($request), $data);
- }
- }
|