id; $orders = Order::query(); $status = $request->get('status') ?? ''; if (empty($status)) { $orders = $orders->where('status', '!=', Order::STATUS_CLOSE_ORDER); } $orders = $orders ->filter($filter) ->orderByDesc('id') ->orderBy('status'); if (!Admin::isAdministrator()) { $area_ids = AdminUser::getAreaIdsByAdminId($admin_id); if (count($area_ids) !== 0) { $orders = $orders->whereIn('area_id', $area_ids); } else { $area_id = AdminUserArea::query()->where('admin_id', $admin_id)->first('area_id'); $area_id = $area_id->area_id ?? 0; $orders = $orders->where('area_id', $area_id); } } $orders = $request->get('all') ? $orders->get() : $orders->paginate(); return $this->ok(OrderResource::collection($orders)); } /** * update 更新订单 * * @param OrderRequest $request * @param Order $order * @return \Illuminate\Http\JsonResponse * @author Fx * */ public function update(OrderRequest $request, Order $order) { $inputs = $request->validated(); $order->update($inputs); return $this->ok(); } /** * orderLocationsearch 订单轨迹 订单列表 * * @param Request $request * @param OrderFilter $filter * @return \Illuminate\Http\JsonResponse * @author Fx * */ public function orderLocationsearch(Request $request, OrderFilter $filter) { $admin_id = Admin::user()->id; $orders = Order::query(); $status = $request->get('status') ?? ''; if (empty($status)) { $orders = $orders->where('status', '!=', Order::STATUS_CLOSE_ORDER); } $orders = $orders ->filter($filter) ->orderByDesc('id') ->orderBy('status'); if (!Admin::isAdministrator()) { $area_ids = AdminUser::getAreaIdsByAdminId($admin_id); if (count($area_ids) !== 0) { $orders = $orders->whereIn('area_id', $area_ids); } else { $area_id = AdminUserArea::query()->where('admin_id', $admin_id)->first('area_id'); $area_id = $area_id->area_id ?? 0; $orders = $orders->where('area_id', $area_id); } } $orders = $orders->limit(100)->get(); return $this->ok(OrderResource::collection($orders)); } /** * orderLocation 订单轨迹 * * @param Request $request * @return \Illuminate\Http\JsonResponse * @author Fx * */ public function orderLocation(Request $request) { $order_id = $request->get('order_id') ?? ''; if (empty($order_id)) return $this->error('参数错误'); $orderLocations = []; $locationsTimes = []; try { $locationsLog = LocationsLog::query()->where('order_id', (int)$order_id)->where('is_rent', LocationsLog::RENT_NO)->whereBetween('latitude', [3, 53])->whereBetween('longitude', [73, 136])->orderBy('created_at', 'asc')->get(); } catch (\Exception $exception) { Log::info($exception->getMessage()); } if (!empty($locationsLog)) { foreach ($locationsLog as $vv) { $orderLocations[] = [$vv->longitude, $vv->latitude]; $locationsTimes[] = Carbon::parse($vv->created_at)->format('Y-m-d H:i:s'); } } $data = [ 'orderLocations' => $orderLocations, 'locationsTimes' => $locationsTimes, ]; return $this->ok($data); } /** * orderStatus 订单状态 订单操作状态 * * @return \Illuminate\Http\JsonResponse * @author Fx * */ public function orderStatus() { $orderStatus = []; foreach (Order::$statusMaps as $k => $v) { $arr = []; $arr['id'] = $k; $arr['name'] = $v; $orderStatus[] = $arr; } $data['order_status'] = $orderStatus;//订单状态 $orderOperateStatus = []; foreach (OrderBikeOperate::$typeMaps as $k => $v) { $arr1 = []; $arr1['id'] = $k; $arr1['name'] = $v; $orderOperateStatus[] = $arr1; } $data['order_operate_status'] = $orderOperateStatus;//订单操作状态 return $this->ok($data); } /** * orderBikeOperate 订单操作自行车详情 * * @param Request $request * @return \Illuminate\Http\JsonResponse * @author Fx * */ public function orderBikeOperate(Request $request) { $order_id = $request->get('order_id'); if (empty($order_id)) return $this->error('参数错误'); $data = OrderBikeOperate::query() ->where('order_id', $order_id) ->select(['created_at', 'name']) ->get(); $data2 = Order::find($order_id)->walletLogs; return $this->ok(['orderBikeOperate' => $data, 'walletLog' => $data2]); } /** * updateOrderCloseBike 修改订单为待支付 * * @param Request $request * @return \Illuminate\Http\JsonResponse * @author Fx * */ public function updateOrderCloseBike(Request $request) { $order_id = $request->get('order_id'); if (empty($order_id)) return $this->error('参数错误'); $closeOrderBool = OrderHandler::closeOrder($order_id); if (!$closeOrderBool) return $this->error('操作失败,请联系管理员'); $order = Order::find($order_id); $user = User::find($order->user_id); if (!empty($order)) { Notification::send($user, new OrderNoPayNotification($user, $order)); } return $this->ok('关锁成功'); } /** * settlementOrder 结算订单 * * @param Request $request * @return \Illuminate\Http\JsonResponse * @author Fx * */ public function settlementOrder(Request $request) { $order_id = $request->get('order_id'); if (empty($order_id)) return $this->error('参数错误'); $ridding_money = $request->get('ridding_money') ?? 0; //骑行费 $dispatch_money = $request->get('dispatch_money') ?? 0; //调度费 $pay_money = $request->get('pay_money') ?? 0; $pay_money = ($pay_money < 0) ? 0 : $pay_money; $ridding_money = ($ridding_money < 0) ? 0 : $ridding_money; $dispatch_money = ($pay_money < 0) ? 0 : $dispatch_money; $order = Order::query()->find($order_id); if (empty($order)) return $this->error('找不到订单信息'); $user_id = $order->user_id; $area_id = $order->area_id; if ($order->status == Order::STATUS_CLOSE_BIKE) { // 优惠金额 $preferential_money = $order->preferential_money; $pay_money = bcsub(bcadd($dispatch_money, $ridding_money, 2), $preferential_money, 2) < 0 ? 0 : bcsub(bcadd($dispatch_money, $ridding_money, 2), $preferential_money, 2); //1.判断用户钱包余额是否大于支付金额 $user = User::query()->find($user_id); if (empty($user)) return $this->error('出现错误,请联系管理员'); $wallet_money = $user->wallet_money; $result = (float)$wallet_money - (float)$pay_money; $order->pay_type = Order::PAY_TYPE_ACCOUNT; if ($result < 0) { try { DB::beginTransaction(); // 修改调度费 各种费用 $order->dispatch_money = (float)$dispatch_money; $order->time_money = (float)$ridding_money; $order->total_money = $pay_money; $order->pay_money = $order->total_money; $order->save(); //给订单车辆操作表添加一条新纪录 $orderBikeOperate = new OrderBikeOperate(); $orderBikeOperate->name = OrderBikeOperate::$typeMaps[OrderBikeOperate::TYPE_ADMIN_SETTLRMENT] . ',修改骑行费为' . $ridding_money . ',调度费为' . $dispatch_money; $orderBikeOperate->type = OrderBikeOperate::TYPE_ADMIN_SETTLRMENT; $orderBikeOperate->bike_id = $order->bike_id; $orderBikeOperate->order_id = $order_id; $orderBikeOperate->user_id = $order->user_id; $orderBikeOperate->save(); $user = User::find($order->user_id); if (!empty($order)) { Notification::send($user, new OrderNoPayNotification($user, $order)); } DB::commit(); } catch (\Exception $e) { DB::rollBack(); Log::info($e->getMessage()); return $this->error('操作失败请联系管理员'); } return $this->error('调度费已调整,用户钱包余额不足,无法后台结算'); } // 只有状态为待支付才能后台操作结算 DB::beginTransaction(); try { // 2.更新用户余额 $user->wallet_money = $result; $res1 = $user->save(); if (!$res1) { DB::rollBack(); Log::error('更新用户余额失败'); return $this->error('操作失败请联系管理员'); } // 3.插入钱包消费记录 $wallet_log = new WalletLog(); $wallet_log->name = WalletLog::$typeMaps[WalletLog::TYPE_ADMIN_SUB_BIKE_ORDER]; $wallet_log->type = WalletLog::TYPE_ADMIN_SUB_BIKE_ORDER; $wallet_log->operate_type = WalletLog::OPERATE_TYPE_SUB; $wallet_log->money = (float)$pay_money; $wallet_log->user_id = $user_id; $wallet_log->area_id = $area_id; $wallet_log->log_id = $order_id; $wallet_log->log_type = Order::class; $res2 = $wallet_log->save(); if (!$res2) { DB::rollBack(); Log::error('消费记录插入失败'); return $this->error('操作失败请联系管理员'); } // 3.更新订单 $order->dispatch_money = (float)$dispatch_money; $order->time_money = (float)$ridding_money; $order->total_money = $pay_money; $order->pay_money = $order->total_money; $order->status = Order::STATUS_COMPLETE_ORDER; // 更新订单状态为已完成 $order->pay_status = Order::PAY_STATUS_OK; $order->pay_type = Order::PAY_TYPE_ACCOUNT; $order->is_admin_settle_order = Order::ADMIN_SETTLE_ORDER_ADMIN; $order->end_use_bike_time = Carbon::now(); $res3 = $order->save(); if (!$res3) { DB::rollBack(); Log::error('更新订单失败'); return $this->error('操作失败请联系管理员'); } //4.给订单车辆操作表添加一条新纪录 $orderBikeOperate = new OrderBikeOperate(); $orderBikeOperate->name = OrderBikeOperate::$typeMaps[OrderBikeOperate::TYPE_ADMIN_SETTLRMENT] . ',修改骑行费为' . $ridding_money . ',调度费为' . $dispatch_money; $orderBikeOperate->type = OrderBikeOperate::TYPE_ADMIN_SETTLRMENT; $orderBikeOperate->bike_id = $order->bike_id; $orderBikeOperate->order_id = $order_id; $orderBikeOperate->user_id = $order->user_id; $res4 = $orderBikeOperate->save(); if (!$res4) { DB::rollBack(); Log::error('订单车辆操作插入失败'); return $this->error('操作失败请联系管理员'); } DB::commit(); } catch (\Exception $e) { DB::rollBack(); Log::info($e->getMessage()); return $this->error('操作失败请联系管理员'); } return $this->ok($res3); } else { return $this->error('车辆未关锁,请刷新后重试'); } } /** * orderReturnMoney 订单返还 * * @param Request $request * @return \Illuminate\Http\JsonResponse * @author Fx * */ public function orderReturnMoney(Request $request) { $order_id = $request->get('order_id'); $return_type = $request->get('return_type') ?? ''; if ($return_type !== 'wallet' && $return_type !== 'wechat') return $this->error('返还类型错误'); if (empty($order_id) || empty($return_type)) return $this->error('参数错误'); $return_ridding_money = $request->get('ridding_money') ?? 0; $return_dispatch_money = $request->get('dispatch_money') ?? 0; if ($return_ridding_money == 0 && $return_dispatch_money == 0) { return $this->error('返还金额为0,请填写返还金额'); } $order = Order::query()->find($order_id); if (empty($order)) return $this->error('找不到该订单,请联系管理员'); if ($order->is_refund_money == Order::REFUND_MONEY_OK) { return $this->error('此订单已返还过了 '); } // 大于24 * 5小时 不能返还 $order_create_time = Carbon::now()->diffInHours($order->created_at); if (abs($order_create_time) > 24 * 5) { return $this->error('返还已过期,不能进行返还操作'); } $user_id = $order->user_id; $area_id = $order->area_id; $dispatch_money = $order->dispatch_money; //订单调度费 $time_money = $order->time_money; //订单骑行费 $pay_money = $order->pay_money; //订单实际支付费用 if ((float)$dispatch_money - (float)$return_dispatch_money < 0) { return $this->error('返还调度费不能大于' . $dispatch_money); } if ((float)$time_money - (float)$return_ridding_money < 0) { return $this->error('返还骑行费不能大于' . $time_money); } $return_money = (float)$return_dispatch_money + (float)$return_ridding_money; if ((float)$pay_money - (float)$return_money < 0) { return $this->error('返还金额不能大于' . $pay_money); } DB::beginTransaction(); try { $old_pay_money = $order->pay_money; //1.更新订单 $order->dispatch_money = (float)$dispatch_money - (float)$return_dispatch_money; $order->time_money = (float)$time_money - (float)$return_ridding_money; $order->pay_money = (float)$pay_money - (float)$return_money; $order->is_refund_money = Order::REFUND_MONEY_OK; $res1 = $order->save(); if (!$res1) { DB::rollBack(); Log::error('更新订单失败'); return $this->error('操作失败请联系管理员'); } //2.判断返还类别 更新用户余额 或微信退款 if ($return_type === 'wallet') { $return_type_name = '退平台钱包'; //3.插入钱包记录 $wallet_log = new WalletLog(); $wallet_log->name = WalletLog::$typeMaps[WalletLog::TYPE_ADMIN_ADD_TO_WALLET]; $wallet_log->type = WalletLog::TYPE_ADMIN_ADD_TO_WALLET; $wallet_log->operate_type = WalletLog::OPERATE_TYPE_ADD; $wallet_log->money = (float)$return_money; $wallet_log->user_id = $user_id; $wallet_log->area_id = $area_id; $wallet_log->log_id = $order_id; $wallet_log->log_type = Order::class; $res2 = $wallet_log->save(); if (!$res2) { DB::rollBack(); Log::error('插入钱包记录失败'); return $this->error('操作失败请联系管理员'); } // 4.退余额 $user = User::query()->find($user_id); $wallet_money = $user->wallet_money; $user->wallet_money = (float)$wallet_money + (float)$return_money; $res3 = $user->save(); if (!$res3) { DB::rollBack(); Log::error('更新用户余额失败'); return $this->error('操作失败请联系管理员'); } } else if ($return_type === 'wechat') { $return_type_name = '原路退回'; //退微信 if ($order->pay_type == Order::PAY_TYPE_ACCOUNT) { DB::rollBack(); return $this->error('此订单为余额支付 '); } //插入钱包记录 增加 $wallet_log_add = new WalletLog(); $wallet_log_add->name = WalletLog::$typeMaps[WalletLog::TYPE_ADD_WECHAT_PAY_ORDER_MONEY]; $wallet_log_add->type = WalletLog::TYPE_ADD_WECHAT_PAY_ORDER_MONEY; $wallet_log_add->operate_type = WalletLog::OPERATE_TYPE_ADD; $wallet_log_add->money = (float)$return_money; $wallet_log_add->user_id = $user_id; $wallet_log_add->area_id = $area_id; $wallet_log_add->log_id = $order_id; $wallet_log_add->log_type = Order::class; $wallet_log_add->save(); //3.插入钱包记录 减少 $wallet_log = new WalletLog(); $wallet_log->name = WalletLog::$typeMaps[WalletLog::TYPE_SUB_ORDER_MONEY_PAY_WECHAT]; $wallet_log->type = WalletLog::TYPE_SUB_ORDER_MONEY_PAY_WECHAT; $wallet_log->operate_type = WalletLog::OPERATE_TYPE_SUB; $wallet_log->money = -(float)$return_money; $wallet_log->user_id = $user_id; $wallet_log->area_id = $area_id; $wallet_log->log_id = $order_id; $wallet_log->status = WalletLog::STATUS_PAUSE; $wallet_log->log_type = Order::class; $res2 = $wallet_log->save(); if (!$res2) { DB::rollBack(); Log::error('插入钱包记录失败'); return $this->error('操作失败请联系管理员'); } //退微信 $payment = app('wechat.payment'); // 微信支付 $refund_no = $order->no; $result = $payment->refund->byOutTradeNumber($order->no, $refund_no, wechat_fee($old_pay_money), wechat_fee($return_money), [ // 可在此处传入其他参数,详细参数见微信支付文档 'refund_desc' => '退订单支付费用', ]); if ($result['return_code'] === 'SUCCESS') { $user = User::find($order->user_id); // 返还通知 Notification::send($user, new OrderRefundNotification($user, $order, $return_money, $return_type_name)); DB::commit(); return $this->ok('退还成功'); } else { Log::error('微信退款接口失败'); DB::rollBack(); return $this->error('退还失败,请联系管理员'); } } else { DB::rollBack(); return $this->error('返还类型错误'); } $user = User::find($order->user_id); // 返还通知 Notification::send($user, new OrderRefundNotification($user, $order, $return_money, $return_type_name)); DB::commit(); } catch (\Exception $e) { DB::rollBack(); Log::info($e->getMessage()); return $this->error('操作失败请联系管理员'); } return $this->ok($res3); } /** * changeOrderRiding 订单回溯到骑行状态 * * @param Request $request * @return \Illuminate\Http\JsonResponse * @author Fx * */ public function changeOrderRiding(Request $request) { $order_id = $request->get('order_id'); if (empty($order_id)) return $this->error('参数错误'); $order = Order::find($order_id); if (empty($order)) return $this->error('找不到该订单'); $order_end_time = Carbon::now()->diffInHours($order->end_use_bike_time); if ($order->status == Order::STATUS_CLOSE_BIKE && $order_end_time < 2) { try { DB::beginTransaction(); // 车辆 $bike = Bike::find($order->bike_id); $bike->is_riding = Bike::RIDING_YES; $bike->save(); //订单 $order->status = Order::STATUS_PAUSE_BIKE; $order->save(); //优惠回溯 switch ($order->preferential_type) { case Order::PREFERENTIAL_CARD_RIDING: $card_use_log = CardRidingUseLog::where('order_id', $order->id)->first(); if (empty($card_use_log)) break; $card_id = $card_use_log->card_riding_user_bags_id; $cardRidingUser = CardRidingUserBags::find($card_id); if (!empty($cardRidingUser) && $cardRidingUser->is_limit_times == CardRidingUserBags::LIMIT_TIMES_YES) { $cardRidingUser->can_ridding_times += 1; $cardRidingUser->save(); } $card_use_log->delete(); break; default; } // 增加订单操作记录 $orderBikeOperate = new OrderBikeOperate(); $orderBikeOperate->name = OrderBikeOperate::$typeMaps[OrderBikeOperate::TYPE_ADMIN_ORDER_BACK]; $orderBikeOperate->type = OrderBikeOperate::TYPE_ADMIN_ORDER_BACK; $orderBikeOperate->bike_id = $order->bike_id; $orderBikeOperate->order_id = $order_id; $orderBikeOperate->user_id = $order->user_id; $orderBikeOperate->is_admin = OrderBikeOperate::IS_ADMIN_YES; $orderBikeOperate->save(); DB::commit(); (new BikeStatusInfoSyncHandler())->toBikeRideStatus(BikeStatusInfoSyncHandler::ROLE_USER, $order->bike_no, [ 'id' => $order_id, 'area_id' => $order->area_id, 'bike_id' => $order->bike_id ]); BikeControl::temporaryCloseLock($bike->box_no); return $this->ok('操作成功'); } catch (\Exception $e) { Log::error($e->getMessage()); return $this->error('操作失败,请联系管理员'); } } else { return $this->error('时间过长,不可回溯'); } } /* * 订单位置详情 * @params 订单id * @return mongo中最后一个位置 根据订单查Mongo中最后一个位置 * @author Fx * * */ public function orderDetailPosition(Request $request) { $order_id = $request->get('order_id') ?? ''; if (empty($order_id)) return $this->error('参数错误'); $order = Order::find($order_id); $orderPosition = LocationsLog::query()->where('order_id', $order_id) ->where('is_rent', LocationsLog::RENT_NO) ->whereBetween('latitude', [3, 53])->whereBetween('longitude', [73, 136])->orderByDesc('created_at')->first(); $bike_no = $order->bike_no; $bikePositioin = LocationsLog::getNewestLocationByBikeNo($bike_no); $end_use_bike_location = json_decode($order->end_use_bike_location); $data = [ 'orderPosition' => empty($orderPosition) ? [$bikePositioin['lng'] ?? 0, $bikePositioin['lat'] ?? 0] : [$orderPosition->longitude, $orderPosition->latitude], 'bikePosition' => [$bikePositioin['lng'] ?? 0, $bikePositioin['lat'] ?? 0] ]; return $this->ok($data); } }