get('bike_no') ?? ''; if (empty($bike_no)) return $this->error('请输入正确车牌号'); $bikeInfo = Bike::query()->where('bike_no', $bike_no)->whereIn('put_area_id', self::$areaIds)->first(); if (empty($bikeInfo)) return $this->error('找不到车辆信息,请检查车牌号'); $data = []; $model = $bikeInfo; $tab_name = ''; if (!(bool)$model->is_link) { // 离线 $tab_name = '中控离线'; } elseif ((bool)$model->is_trouble) { // 故障上线 if ((bool)$model->put_status) { $tab_name = '故障'; } else { // 故障下线 $tab_name = '故障下线'; } } elseif (!(bool)$model->is_low_battery_power) { // 低电量 $tab_name = '低电量'; } elseif (!(bool)$model->put_status) { // 下线 未投放 $tab_name = '未投放'; } elseif ((bool)$model->is_riding) { //骑行中 $tab_name = '骑行中'; } elseif (!(bool)$model->is_in_parking) { // 不在停车区 $tab_name = '不在停车区'; } else { // 未骑行 $tab_name = '未骑行'; } $lastLocation = LocationsLog::getNewestLocationTimeByBikeNo($bike_no); $workInfo = []; if (app()->redis->hexists(BikeStatusInfoSyncHandler::REDIS_RIDE_BIKE_WORKER_ORDERS_TAG, $bikeInfo->bike_no)) { $work_id = app()->redis->hget(BikeStatusInfoSyncHandler::REDIS_RIDE_BIKE_WORKER_ORDERS_TAG, $bikeInfo->bike_no); $work = AdminUser::find($work_id); $workInfo = [ 'name' => $work->name, 'phone' => $work->phone, ]; } // 车辆信息 $data['bike_info']['bike_no'] = $bike_no; $data['bike_info']['box_no'] = $bikeInfo->box_no; $data['bike_info']['id'] = $bikeInfo->id; $data['bike_info']['is_lock'] = $bikeInfo->is_lock; $data['bike_info']['battery_power'] = $bikeInfo->battery_power; $data['bike_info']['is_trouble'] = $bikeInfo->is_trouble; // 是否故障 $data['bike_info']['is_link'] = $bikeInfo->put_status; // 是否在线 $data['bike_info']['area_name'] = Area::query()->find($bikeInfo->put_area_id)->name ?? ''; //车辆区域地址 $data['bike_info']['tab_name'] = $tab_name; //状态 $data['bike_info']['last_location'] = GaodeMaps::getAddress([$lastLocation['location']['lng'], $lastLocation['location']['lat']]); //最后定位位置 $data['bike_info']['last_time'] = $lastLocation['time']; //最后定位时间 $data['bike_info']['workInfo'] = $workInfo; //最后定位时间 //最近订单 取最 $order = Order::query()->where('bike_id', $bikeInfo->id)->orderByDesc('id')->with('users')->first(); $orderRent = OrderRent::query()->where('bike_id', $bikeInfo->id)->orderByDesc('id')->with('users')->first(); $data2 = []; if (!empty($order) && !empty($orderRent)) { if ($orderRent->start_use_bike_time > $order->start_use_bike_time) { // $user = User::query()->find($v->user_id); $data1['bike_no'] = $orderRent->bike_no; $data1['username'] = $orderRent->users->nickname . "(" . $orderRent->users->truename . ")" ?? ''; $data1['mobile'] = $orderRent->users->mobile ?? ''; $data1['order_status'] = OrderRent::$statusMaps[$orderRent->status]; $data1['start_use_bike_time'] = $orderRent->start_use_bike_time; $data1['end_use_bike_time'] = $orderRent->end_use_bike_time; $data1['start_use_bike_time_app'] = date('m/d H:i', strtotime($orderRent->start_use_bike_time)); $data1['end_use_bike_time_app'] = $orderRent->end_use_bike_time ? date('m/d H:i', strtotime($orderRent->end_use_bike_time)) : ''; } else { $data1['bike_no'] = $order->bike_no; $data1['username'] = $order->users->nickname . "(" . $order->users->truename . ")" ?? ''; $data1['mobile'] = $order->users->mobile ?? ''; $data1['order_status'] = Order::$statusMaps[$order->status]; $data1['start_use_bike_time'] = $order->start_use_bike_time; $data1['end_use_bike_time'] = $order->end_use_bike_time; $data1['start_use_bike_time_app'] = date('m/d H:i', strtotime($order->start_use_bike_time)); $data1['end_use_bike_time_app'] = $order->end_use_bike_time ? date('m/d H:i', strtotime($order->end_use_bike_time)) : ''; } $data2[] = $data1; } else if (!empty($order)) { $data1['bike_no'] = $order->bike_no; $data1['username'] = $order->users->nickname . "(" . $order->users->truename . ")" ?? ''; $data1['mobile'] = $order->users->mobile ?? ''; $data1['order_status'] = Order::$statusMaps[$order->status]; $data1['start_use_bike_time'] = $order->start_use_bike_time; $data1['end_use_bike_time'] = $order->end_use_bike_time; $data1['start_use_bike_time_app'] = date('m/d H:i', strtotime($order->start_use_bike_time)); $data1['end_use_bike_time_app'] = $order->end_use_bike_time ? date('m/d H:i', strtotime($order->end_use_bike_time)) : ''; $data2[] = $data1; } else if (!empty($orderRent)) { $data1['bike_no'] = $orderRent->bike_no; $data1['username'] = $orderRent->users->nickname . "(" . $orderRent->users->truename . ")" ?? ''; $data1['mobile'] = $orderRent->users->mobile ?? ''; $data1['order_status'] = OrderRent::$statusMaps[$orderRent->status]; $data1['start_use_bike_time'] = $orderRent->start_use_bike_time; $data1['end_use_bike_time'] = $orderRent->end_use_bike_time; $data1['start_use_bike_time_app'] = date('m/d H:i', strtotime($orderRent->start_use_bike_time)); $data1['end_use_bike_time_app'] = $orderRent->end_use_bike_time ? date('m/d H:i', strtotime($orderRent->end_use_bike_time)) : ''; $data2[] = $data1; } $data['bike_order'] = $data2; return $this->ok($data); } /** * updateBikeInfo 根据车辆id 修改车辆信息 * * @param Request $request * @return \Illuminate\Http\JsonResponse * @author Fx * */ public function updateBikeInfo(Request $request) { $admin_id = Admin::user()->id; if (!Admin::isAdministrator()) { return $this->error('暂无权限'); } $bike_id = $request->get('bike_id') ?? ''; if (empty($bike_id)) return $this->error('参数错误'); $bike = Bike::query()->whereIn('put_area_id', self::$areaIds)->find($bike_id); if (empty($bike)) return $this->error('找不到该车辆信息,请检查车牌号'); $bike_no = $request->get('bike_no') ?? ''; $box_no = $request->get('box_no') ?? ''; $bike_remarks = $request->get('bike_remarks') ?? ''; if (empty($bike_no) && empty($box_no) && empty($bike_remarks)) { // 都为空 return $this->error('请输入要修改的内容'); } // 不为空修改 if (!empty($bike_no)) { $verifyBikeNo = Bike::query()->where('bike_no', $bike_no)->first(); if (!empty($verifyBikeNo)) { if ($verifyBikeNo->id !== $bike->id) { return $this->error('车辆编号已存在,不可修改'); } } $bike->bike_no = $bike_no; } $date = Carbon::now(); if (!empty($bike_remarks)) { $bike->bike_remarks = $bike_remarks . $date; } if (!empty($box_no)) { $verifyBikeNo = Bike::query()->where('box_no', $box_no)->first(); if (!empty($verifyBikeNo)) { if ($verifyBikeNo->id !== $bike->id) { return $this->error('中控编号已存在,不可修改'); } } // 中控绑定操作 $box = BoxBinding::query()->where('box_no', $box_no)->first(); if (empty($box)) return $this->error('找不到此设备信息,请联系管理员'); if ($box->is_binding == BoxBinding::BINDING_YES) return $this->error('此设备已经绑定过'); // 蓝牙信息'TBIT_WA205-7HBLE'; $blu_key = config('systemConfig.blu_key'); try { $blu_ase_key = Aes128Handler::genKey($blu_key, $box_no); } catch (\Exception $e) { return $this->error($e->getMessage()); } $inputs['blu_key'] = $blu_key; $inputs['blu_ase_key'] = $blu_ase_key; try { DB::beginTransaction(); $bike->blu_key = $blu_key; $bike->blu_ase_key = $blu_ase_key; $box->is_binding = BoxBinding::BINDING_YES; $box->save(); $bike->save(); DB::commit(); return $this->ok(BikeResource::make($bike)); } catch (\Exception $e) { DB::rollBack(); Log::error($e->getMessage()); return $this->error('修改失败,请联系管理员'); } } $bool = $bike->save(); if ($bool) { return $this->ok('修改成功'); } else { return $this->error('修改失败,请联系管理员'); } } /** * bikeLocation 车辆位置轨迹 * * @param Request $request * @return \Illuminate\Http\JsonResponse * @author Fx * */ public function bikeLocation(Request $request) { $bike_no = $request->get('bike_no') ?? ''; $time_between = $request->get('time_between') ?? []; // time_between 时间区间 if (empty($bike_no) || empty($time_between)) return $this->error('参数错误'); $locationLog = LocationsLog::where('bike_no', $bike_no) ->where('created_at', '>=', Carbon::parse($time_between[0])->toDateString()) ->where('created_at', '<=', Carbon::parse($time_between[1])->toDateString()) ->whereBetween('latitude', [3, 53])->whereBetween('longitude', [73, 136])->orderBy('created_at', 'asc') ->get(['longitude', 'latitude', 'type']); $data = []; $userLocation = []; $workerLocation = []; $noLocatioin = []; $old_type = ''; $item = []; $Locations = []; foreach ($locationLog as $v) { $data[] = [$v->longitude, $v->latitude]; if (empty($old_type)) { $old_type = $v->type; } if ($v->type !== $old_type) { if ($old_type == LocationsLog::USER_LOCATION) { $userLocation[] = $item; $Locations[] = ['data' => $item, 'role' => LocationsLog::USER_LOCATION]; } else if ($old_type == LocationsLog::WORKER_LOCATION) { $workerLocation[] = $item; $Locations[] = ['data' => $item, 'role' => LocationsLog::WORKER_LOCATION]; } else { $noLocatioin[] = $item; $Locations[] = ['data' => $item, 'role' => LocationsLog::NO_LOCATION]; } $old_type = ''; $item = []; $item[] = ['longitude' => $v->longitude, 'latitude' => $v->latitude]; } else { $item[] = ['longitude' => $v->longitude, 'latitude' => $v->latitude]; } unset($v->_id); } if (empty($data)) return $this->ok($data); $wx_data = $locationLog->toArray(); $res = [ 'locations' => $data, 'start_location' => $data[0], 'end_location' => end($data), 'wx_locations' => $wx_data, 'wx_start_location' => $wx_data[0], 'wx_end_location' => end($wx_data), 'user_location' => $userLocation, 'worker_location' => $workerLocation, 'no_locatioin' => $noLocatioin, 'all_locations' => $Locations ]; return $this->ok($res); } /** * bikePosition 获取车辆真实位置点(mongodb) * * @param Request $request * @return \Illuminate\Http\JsonResponse * @author Fx * */ public function bikePosition(Request $request) { $bike_no = $request->get('bike_no') ?? ''; if (empty($bike_no)) return $this->error('参数错误'); $position = LocationsLog::getNewestLocationByBikeNo($bike_no); if (empty($position)) return $this->ok([]); return $this->ok([$position['lng'], $position['lat']]); } /** * sweepCodeLocation 扫码位置 近200条 * * @param Request $request * @return \Illuminate\Http\JsonResponse * @author Fx * */ public function sweepCodeLocation(Request $request) { $bike_id = $request->get('bike_id') ?? ''; if (empty($bike_id)) return $this->error('参数错误'); $threeAgo = Carbon::today()->subDays(3); $order = Order::query() ->where('bike_id', $bike_id) // ->where('created_at','>',$threeAgo) ->orderByDesc('id') ->limit(100) ->get(['start_use_bike_time', 'start_use_bike_location']); $data = []; if (!empty($order)) { foreach ($order as $v) { $dataItem['time'] = $v->start_use_bike_time; $obj = json_decode($v->start_use_bike_location); $dataItem['location'] = [$obj->longitude, $obj->latitude]; $data[] = $dataItem; } } $orderRent = OrderRent::query() ->where('bike_id', $bike_id) // ->where('created_at','>',$threeAgo) ->orderByDesc('id') ->limit(100) ->get(['start_use_bike_time', 'start_use_bike_location']); $dataRent = []; if (!empty($orderRent)) { foreach ($orderRent as $v) { $dataRentItem['time'] = $v->start_use_bike_time; $objRent = json_decode($v->start_use_bike_location); $dataRentItem['location'] = [$objRent->longitude, $objRent->latitude]; $dataRent[] = $dataRentItem; } } return $this->ok(array_merge($data, $dataRent)); } /** * latelyOrders 车辆最近订单 * * @param Request $request * @return \Illuminate\Http\JsonResponse * @author Fx * */ public function latelyOrders(Request $request) { $bike_id = $request->get('bike_id') ?? ''; if (empty($bike_id)) return $this->error('参数错误'); $order = Order::query() ->where('bike_id', $bike_id) ->with('users') ->orderByDesc('id') ->paginate(); // $data = []; // if (!empty($order)) { // foreach ($order as $v) { // // $user = User::query()->find($v->user_id); // $data1['bike_no'] = $v->bike_no; // $data1['username'] = $v->users->nickname ?? ''; // $data1['mobile'] = $v->users->mobile ?? ''; // $data1['order_status'] = Order::$statusMaps[$v->status]; // $data1['start_use_bike_time'] = $v->start_use_bike_time; // $data1['end_use_bike_time'] = $v->end_use_bike_time; // $data[] = $data1; // } // } return $this->ok(OrderResource::collection($order)); } /** * latelyOrderRent 最近日租订单 * * @param Request $request * @return \Illuminate\Http\JsonResponse * @author Fx * */ public function latelyOrderRent(Request $request) { $bike_id = $request->get('bike_id') ?? ''; if (empty($bike_id)) return $this->error('参数错误'); $order = OrderRent::query() ->where('bike_id', $bike_id) ->with('users') ->orderByDesc('id') ->paginate(); return $this->ok(OrderRentResource::collection($order)); } /** * locksLog 车辆开锁关锁记录 近3天 * * @param Request $request * @return \Illuminate\Http\JsonResponse * @author Fx * */ public function locksLog(Request $request) { $bike_id = $request->get('bike_id') ?? ''; if (empty($bike_id)) return $this->error('参数错误'); $typeArr = [OrderBikeOperate::TYPE_OPEN_BIKE, OrderBikeOperate::TYPE_CLONE_BIKE, OrderBikeOperate::TYPE_PAUSE_OPEN_BIKE, OrderBikeOperate::TYPE_PAUSE_CLONE_BIKE]; // 普通用户操作 $threeAgo = Carbon::today()->subDays(3); $lock1 = OrderBikeOperate::query() ->whereIn('type', $typeArr) ->where('is_admin', OrderBikeOperate::IS_ADMIN_NO) ->where('bike_id', $bike_id) ->where('created_at', '>', $threeAgo) ->with('users') ->orderByDesc('id') ->get(); $data1 = []; $open_bike = []; $close_bike = []; $open_pause_bike = []; $close_pause_bike = []; if (!empty($lock1)) { foreach ($lock1 as $v) { // $user = User::query()->find($v->user_id); $dataItem1['username'] = $v->users->nickname ?? ''; $dataItem1['mobile'] = $v->users->mobile ?? ''; $dataItem1['operate_time'] = date("Y/m/d H:i:s", strtotime($v->created_at)); $dataItem1['name'] = $v->name; $dataItem1['location'] = [$v->longitude, $v->latitude]; if ($v->type == OrderBikeOperate::TYPE_OPEN_BIKE) { $open_bike[] = $dataItem1; } elseif ($v->type == OrderBikeOperate::TYPE_CLONE_BIKE) { $close_bike[] = $dataItem1; } elseif ($v->type == OrderBikeOperate::TYPE_PAUSE_OPEN_BIKE) { $open_pause_bike[] = $dataItem1; } elseif ($v->type == OrderBikeOperate::TYPE_PAUSE_CLONE_BIKE) { $close_pause_bike[] = $dataItem1; } } } // 管理员操作 $lock2 = OrderBikeOperate::query() ->whereIn('type', $typeArr) ->where('is_admin', OrderBikeOperate::IS_ADMIN_YES) ->where('bike_id', $bike_id) ->where('created_at', '>', $threeAgo) ->with('adminUsers') ->orderByDesc('id') ->get(); $data2 = []; if (!empty($lock2)) { foreach ($lock2 as $v) { // $user = AdminUser::query()->find($v->user_id); $dataItem2['username'] = $v->adminUsers->name ?? ''; $dataItem2['mobile'] = $v->adminUsers->phone ?? ''; $dataItem2['operate_time'] = date("Y/m/d H:i:s", strtotime($v->created_at)); $dataItem2['name'] = $v->name; $dataItem2['location'] = [$v->longitude, $v->latitude]; if ($v->type == OrderBikeOperate::TYPE_OPEN_BIKE) { $open_bike[] = $dataItem2; } elseif ($v->type == OrderBikeOperate::TYPE_CLONE_BIKE) { $close_bike[] = $dataItem2; } elseif ($v->type == OrderBikeOperate::TYPE_PAUSE_OPEN_BIKE) { $open_pause_bike[] = $dataItem2; } elseif ($v->type == OrderBikeOperate::TYPE_PAUSE_CLONE_BIKE) { $close_pause_bike[] = $dataItem2; } } } // $data = array_merge($data1, $data2); $data = [ 'open_bike' => $open_bike, 'close_bike' => $close_bike, 'open_pause_bike' => $open_pause_bike, 'close_pause_bike' => $close_pause_bike ]; return $this->ok($data); } /** * bikeOnLineLog 车辆上线下线记录 * * @param Request $request * @return \Illuminate\Http\JsonResponse * @author Fx * */ public function bikeOnLineLog(Request $request) { $bike_id = $request->get('bike_id') ?? ''; if (empty($bike_id)) return $this->error('参数错误'); $bikeOnLine = WorkerBikeOperate::query() ->where('bike_id', $bike_id) ->whereIn('type', [WorkerBikeOperate::TYPE_ONLINE, WorkerBikeOperate::TYPE_OFFLINE]) ->with('workers') ->orderByDesc('id') ->paginate(); // $data = []; // if (!empty($bikeOnLine)) { // foreach ($bikeOnLine as $v) { // $item['reason'] = $v->reason; // $item['name'] = $v->name; // $item['created_at'] = date("Y/m/d H:i:s", strtotime($v->created_at)); // $item['username'] = $v->workers->name; // $item['mobile'] = $v->workers->phone; // $data[] = $item; // } // } return $this->ok(BikeOnlineResource::collection($bikeOnLine)); } /** * repairOptions 可报修选项 * * @return \Illuminate\Http\JsonResponse * @author Fx * */ public function repairOptions() { // $repair_part = BikeTrouble::$troublePartMaps; $repair_part = WorkOrder::$typeMaps; $data = []; foreach ($repair_part as $k => $v) { $dateItem['id'] = $k; $dateItem['name'] = $v; $dateItem['select'] = false; $data[] = $dateItem; } return $this->ok($data); } /** * bikeRepair 车辆报修 * * @param Request $request * @return \Illuminate\Http\JsonResponse * @author Fx * */ public function bikeRepair(Request $request) { $bike_no = $request->get('bike_no') ?? ''; $box_no = $request->get('box_no') ?? ''; $trouble_part = $request->get('trouble_part') ?? ''; // 报修部位 $reason = $request->get('reason') ?? ''; // 原因 if (empty($trouble_part)) return $this->error('请选择报修部位'); if (empty($reason) || empty($bike_no)) return $this->error('请填写报修原因'); if (!empty($bike_no)) { $bike = Bike::query()->where('bike_no', $bike_no)->first(); if (!empty($bike)) { if ($bike->put_status == Bike::PUT_STATUS_NO) { return $this->error('车辆已下线,无需报修,如需报修请先手动上线'); } DB::beginTransaction(); try { // 更新车辆状态 下线 $bike->put_status = Bike::PUT_STATUS_NO; $bike->is_trouble = Bike::TROUBLE_YES; $bool1 = $bike->save(); // 更新redis (new BikeStatusInfoSyncHandler())->toBikeOffLineStatus($bike->bike_no); if (!$bool1) { DB::rollBack(); Log::error('更新车辆状态失败'); } // 插入下线记录 $worker_bike_operate = new WorkerBikeOperate(); $worker_bike_operate->type = WorkerBikeOperate::TYPE_OFFLINE; $worker_bike_operate->name = WorkerBikeOperate::$typeMaps[WorkerBikeOperate::TYPE_OFFLINE]; $worker_bike_operate->bike_id = $bike->id; $last_location = json_decode($bike->last_location); $worker_bike_operate->latitude = empty($last_location['lat']) ? 0 : $last_location['lat']; $worker_bike_operate->longitude = empty($last_location['lng']) ? 0 : $last_location['lng']; $worker_bike_operate->worker_id = Admin::user()->id; // foreach ($trouble_part as $v){ // $reason .= BikeTrouble::$troublePartMaps[$v] . ','; // } $worker_bike_operate->reason = $reason; $bool2 = $worker_bike_operate->save(); if (!$bool2) { DB::rollBack(); Log::error('插入下线记录失败'); } // 插入报修信息 // $bike_trouble = new BikeTrouble(); // $bike_trouble->trouble_no = 'T-'.$this->generateTroubleNo(); //故障号 // $bike_trouble->user_id = Admin::user()->id; // $bike_trouble->bike_id = $bike->id; // $bike_trouble->area_id = $bike->put_area_id;// 区域id // $bike_trouble->trouble_part = json_encode($trouble_part); // $bike_trouble->trouble_grade = count($trouble_part)==1 ? BikeTrouble::getTroubleGrade($trouble_part[0]): BikeTrouble::TROUBLE_GRADE_TWO ;// 故障等级 故障部位大于1 默认高级故障 // // $bike_trouble->up_trouble_type = 0 // 上报问题类型 // $bool3 = $bike_trouble->save(); //生成工单 //插入工单 $work_order_data = [ 'work_no' => WorkOrder::makeWorkNo(), 'type' => $trouble_part, 'type_name' => WorkOrder::$typeMaps[$trouble_part], 'bike_no' => $bike->bike_no, 'bike_id' => $bike->id, 'source' => WorkOrder::SOURCE_REPAIR, 'area_id' => $bike->put_area_id, 'admin_id' => Admin::user()->id, 'reason' => $reason, ]; $work_order_verify = [ 'bike_id' => $bike->id, 'type' => $trouble_part, // 工单类型 'status' => WorkOrder::STATUS_NO ]; WorkOrder::firstOrCreate($work_order_verify, $work_order_data); DB::commit(); return $this->ok('报修成功'); } catch (\Exception $exception) { DB::rollBack(); Log::info($exception->getMessage()); return $this->error('操作失败请联系管理员'); } } } if (!empty($box_no)) { $bike = Bike::query()->where('box_no', $box_no)->first(); if (!empty($bike)) { try { // 更新车辆状态 下线 $bike->put_status = Bike::PUT_STATUS_NO; $bike->is_trouble = Bike::TROUBLE_YES; $bool1 = $bike->save(); // 更新redis (new BikeStatusInfoSyncHandler())->toBikeOffLineStatus($bike->bike_no); if (!$bool1) { DB::rollBack(); Log::error('更新车辆状态失败'); } // 插入下线记录 $worker_bike_operate = new WorkerBikeOperate(); $worker_bike_operate->type = WorkerBikeOperate::TYPE_OFFLINE; $worker_bike_operate->name = WorkerBikeOperate::$typeMaps[WorkerBikeOperate::TYPE_OFFLINE]; $worker_bike_operate->bike_id = $bike->id; $last_location = json_decode($bike->last_location); $worker_bike_operate->latitude = empty($last_location['lat']) ? 0 : $last_location['lat']; $worker_bike_operate->longitude = empty($last_location['lng']) ? 0 : $last_location['lng']; $worker_bike_operate->worker_id = Admin::user()->id; $worker_bike_operate->reason = $reason; $bool2 = $worker_bike_operate->save(); if (!$bool2) { DB::rollBack(); Log::error('插入下线记录失败'); } // 插入报修信息 // $bike_trouble = new BikeTrouble(); // $bike_trouble->trouble_no = 'T-' . $this->generateTroubleNo(); //故障号 // $bike_trouble->user_id = Admin::user()->id; // $bike_trouble->bike_id = $bike->id; // $bike_trouble->area_id = $bike->put_area_id;// 区域id // $bike_trouble->trouble_part = json_encode($trouble_part); // $bike_trouble->trouble_grade = 3; // 故障等级 // // $bike_trouble->up_trouble_type = 0 // 上报问题类型 // $bool3 = $bike_trouble->save(); //生成工单 // 插入工单 $work_order_data = [ 'work_no' => WorkOrder::makeWorkNo(), 'type' => $trouble_part, 'type_name' => WorkOrder::$typeMaps[$trouble_part], 'bike_no' => $bike->bike_no, 'bike_id' => $bike->id, 'source' => WorkOrder::SOURCE_REPAIR, 'area_id' => $bike->put_area_id, 'admin_id' => Admin::user()->id, 'reason' => $reason, ]; $work_order_verify = [ 'bike_id' => $bike->id, 'type' => $trouble_part, 'status' => WorkOrder::STATUS_NO ]; WorkOrder::firstOrCreate($work_order_verify, $work_order_data); DB::commit(); return $this->ok('报修成功'); } catch (\Exception $exception) { DB::rollBack(); Log::info($exception->getMessage()); return $this->error('操作失败请联系管理员'); } } else { return $this->error('找不到该车辆信息'); } } else { return $this->error('参数错误'); } } /** * generateTroubleNo 生成故障号 * * @return string * @author Fx * */ private function generateTroubleNo() { $time = date('YmdHis', time()); $randNum = mt_rand(1111, 9999); return $time . $randNum; } /** * bikeList 车辆列表 * * @param BikeFilter $filter * @return \Illuminate\Http\JsonResponse * @author Fx * */ public function bikeList(BikeFilter $filter) { $area_ids = self::$areaIds; $bikelist = Bike::query() ->whereIn('put_area_id', $area_ids) ->whereNotNull('box_no') ->filter($filter) ->orderByDesc('id') ->paginate(); return $this->ok(BikeResource::collection($bikelist)); } /** * bikeStatistics 车辆统计 * * @param BikeFilter $filter * @return \Illuminate\Http\JsonResponse * @author Fx * */ public function bikeStatistics(BikeFilter $filter) { $data = []; $area_ids = self::$areaIds; // 总车辆数 $data['total_bikes'] = Bike::query() ->whereIn('put_area_id', $area_ids) ->filter($filter) ->count('id'); // 上线车辆数 $data['put_total_bikes'] = Bike::query() ->whereIn('put_area_id', $area_ids) ->filter($filter) ->where('put_status', Bike::PUT_STATUS_YES) ->count('id'); // 在线车辆数 $data['line_total_bikes'] = Bike::query() ->whereIn('put_area_id', $area_ids) ->filter($filter) ->where('is_link', Bike::LINK_ONLINE) ->count('id'); //故障车辆 $data['trouble_total_bikes'] = Bike::query() ->whereIn('put_area_id', $area_ids) ->filter($filter) ->where('is_trouble', Bike::TROUBLE_YES) ->count('id'); //今日使用 $data['today_use_total_bikes'] = Bike::query() ->whereIn('put_area_id', $area_ids) ->filter($filter) ->where('last_use_bike_end_time', '>', Carbon::today()) ->count('id'); //低电量 $data['low_battery_total_bikes'] = Bike::query() ->whereIn('put_area_id', $area_ids) ->filter($filter) ->where('is_low_battery_power', Bike::BATTERY_POWER_LOW) ->count('id'); //骑行中 $data['riding_total_bikes'] = Bike::query() ->whereIn('put_area_id', $area_ids) ->filter($filter) ->where('is_riding', Bike::RIDING_YES) ->count('id'); //未使用 $data['not_riding_total_bikes'] = Bike::query() ->whereIn('put_area_id', $area_ids) ->filter($filter) ->where('is_riding', Bike::RIDING_NO) ->count('id'); return $this->ok($data); } /** * bikeLogin 车辆上线 (投放) * * @param Request $request * @return \Illuminate\Http\JsonResponse * @author Fx * */ public function bikeLogin(Request $request) { $bike_id = $request->get('bike_id') ?? ''; if (empty($bike_id)) return $this->error('参数错误'); try { DB::beginTransaction(); $bike = Bike::query()->find($bike_id); if (empty($bike)) return $this->error('找不到车辆'); if ($bike->put_status == Bike::PUT_STATUS_YES) return $this->error('车辆已在线'); // 更新车辆上线 $bike->put_status = Bike::PUT_STATUS_YES; $bike->save(); // 插入上线记录 $worker_bike_operate = new WorkerBikeOperate(); $worker_bike_operate->type = WorkerBikeOperate::TYPE_ONLINE; $worker_bike_operate->name = WorkerBikeOperate::$typeMaps[WorkerBikeOperate::TYPE_ONLINE]; $worker_bike_operate->reason = "工作人员操作上线"; $last_location = object_array(json_decode($bike->last_location)); $worker_bike_operate->bike_id = $bike_id; $worker_bike_operate->latitude = empty($last_location['lat']) ? 0 : $last_location['lat']; $worker_bike_operate->longitude = empty($last_location['lng']) ? 0 : $last_location['lng']; $worker_bike_operate->worker_id = Admin::user()->id; $worker_bike_operate->save(); //更新redis $lastLocation = LocationsLog::getNewestLocationByBikeNo($bike->bike_no); (new BikeStatusInfoSyncHandler())->toBikeOnLineStatus($bike->bike_no, $lastLocation['lng'], $lastLocation['lat']); DB::commit(); return $this->ok('操作成功'); } catch (\Exception $e) { DB::rollBack(); Log::error($e->getMessage()); return $this->error('操作失败,请联系管理员'); } } /** * bikeLogout 车辆下线(不投放) * * @param Request $request * @return \Illuminate\Http\JsonResponse * @author Fx * */ public function bikeLogout(Request $request) { $bike_id = $request->get('bike_id') ?? ''; if (empty($bike_id)) return $this->error('参数错误'); try { DB::beginTransaction(); $bike = Bike::query()->find($bike_id); if (empty($bike)) return $this->error('找不到车辆'); if ($bike->put_status == Bike::PUT_STATUS_NO) return $this->error('车辆已下线'); // 更新车辆下线 $bike->put_status = Bike::PUT_STATUS_NO; $bike->save(); // 插入下线记录 $worker_bike_operate = new WorkerBikeOperate(); $worker_bike_operate->type = WorkerBikeOperate::TYPE_OFFLINE; $worker_bike_operate->name = WorkerBikeOperate::$typeMaps[WorkerBikeOperate::TYPE_OFFLINE]; $worker_bike_operate->reason = "工作人员操作下线"; $last_location = object_array(json_decode($bike->last_location)); $worker_bike_operate->bike_id = $bike_id; $worker_bike_operate->latitude = empty($last_location['lat']) ? 0 : $last_location['lat']; $worker_bike_operate->longitude = empty($last_location['lng']) ? 0 : $last_location['lng']; $worker_bike_operate->worker_id = Admin::user()->id; $worker_bike_operate->save(); // 更新redis (new BikeStatusInfoSyncHandler())->toBikeOffLineStatus($bike->bike_no); DB::commit(); return $this->ok('操作成功'); } catch (\Exception $e) { DB::rollBack(); Log::error($e->getMessage()); return $this->error('操作失败,请联系管理员'); } } /** * bikeBell 响铃 * * @param Request $request * @return \Illuminate\Http\JsonResponse * @author Fx * */ public function bikeBell(Request $request) { $bike_id = $request->get('bike_id') ?? ''; if (empty($bike_id)) return $this->error('参数错误'); $bike = Bike::query()->find($bike_id); if (empty($bike)) return $this->error('找不到车辆'); // $box_no = "003448483"; // 测试写死 $box_no = $bike->box_no; $bool = BikeControl::bellBike($box_no); if ($bool) { return $this->ok('操作成功,请寻找响铃车辆'); } else { return $this->error('操作失败,请联系管理员'); } } /** * openBikeLock 开锁 * * @param Request $request * @return \Illuminate\Http\JsonResponse * @author Fx * */ public function openBikeLock(Request $request) { $bike_id = $request->get('bike_id') ?? ''; if (empty($bike_id)) return $this->error('参数错误'); $bike = Bike::query()->find($bike_id); if (empty($bike)) return $this->error('找不到车辆'); // $box_no = "003448483"; // 测试写死 $box_no = $bike->box_no; try { // 增加worker操作车辆记录 $worker_bike_operate = new WorkerBikeOperate(); $worker_bike_operate->type = WorkerBikeOperate::TYPE_OPEN_BIKE_LOCK; $worker_bike_operate->name = WorkerBikeOperate::$typeMaps[WorkerBikeOperate::TYPE_OPEN_BIKE_LOCK]; $worker_bike_operate->bike_id = $bike_id; $last_location = object_array(json_decode($bike->last_location)); // Log::info($last_location); $worker_bike_operate->latitude = empty($last_location['lat']) ? 0 : $last_location['lat']; $worker_bike_operate->longitude = empty($last_location['lng']) ? 0 : $last_location['lng']; $worker_bike_operate->reason = "工作人员开锁"; $worker_bike_operate->worker_id = Admin::user()->id; $worker_bike_operate->save(); } catch (\Exception $e) { Log::error('开锁记录生成失败'); return $this->error('开锁失败,请联系管理员'); } $bool = BikeControl::openLock($box_no); if ($bool) { // DB::beginTransaction(); try { $bike->is_lock = Bike::LOCK_NO; $bike->save(); // 修改redis $is_riding = $bike->is_riding; if (!(bool)$is_riding) { (new BikeStatusInfoSyncHandler())->toBikeRideStatus(BikeStatusInfoSyncHandler::ROLE_WORKER, $bike->bike_no, ['id' => Admin::user()->id, 'area_id' => $bike->put_area_id, 'bike_id' => $bike_id]); } /* // 更新orders redis Redis::connection()->hset('ride_orders', $order->bike_no, serialize([ 'id' => $order->id, 'bike_no' => $order->bike_no, 'bike_id' => $order->bike_id, 'area_id' => $order->area_id ])); DB::commit();*/ } catch (\Exception $e) { // DB::rollBack(); Log::error('车辆状态骑行更新失败'); } return $this->ok('操作成功,请寻找响铃车辆'); } else { return $this->error('开锁失败,请联系管理员'); } } /** * closeBikeLock 关锁 * * @param Request $request * @return \Illuminate\Http\JsonResponse * @author Fx * */ public function closeBikeLock(Request $request) { $bike_id = $request->get('bike_id') ?? ''; if (empty($bike_id)) return $this->error('参数错误'); $bike = Bike::query()->find($bike_id); if (empty($bike)) return $this->error('找不到车辆'); if ((bool)$bike->is_riding) { return $this->error('车辆骑行中,不可关锁'); } try { // 增加worker操作车辆记录 $worker_bike_operate = new WorkerBikeOperate(); $worker_bike_operate->type = WorkerBikeOperate::TYPE_CLOSE_BIKE_LOCK; $worker_bike_operate->name = WorkerBikeOperate::$typeMaps[WorkerBikeOperate::TYPE_CLOSE_BIKE_LOCK]; $worker_bike_operate->bike_id = $bike_id; $last_location = object_array(json_decode($bike->last_location)); $worker_bike_operate->latitude = empty($last_location['lat']) ? 0 : $last_location['lat']; $worker_bike_operate->longitude = empty($last_location['lng']) ? 0 : $last_location['lng']; $worker_bike_operate->reason = "工作人员关锁"; $worker_bike_operate->worker_id = Admin::user()->id; $worker_bike_operate->save(); } catch (\Exception $e) { Log::error('关锁记录生成失败'); return $this->error('关锁失败,请联系管理员'); } // $box_no = "003448483"; // 测试写死 $box_no = $bike->box_no; //添加工作人员操作车辆记录 // 删除redis订单 $bike_location = LocationsLog::getNewestLocationByBikeNo($bike->bike_no); (new BikeStatusInfoSyncHandler())->toBikeWaitRideStatus($bike->bike_no, $bike_location['lng'], $bike_location['lat'], $bike->put_status); $bool = BikeControl::closeLock($box_no); if ($bool) { try { // 车辆改为未骑行 // $bike->is_riding = Bike::RIDING_NO; $bike->last_use_bike_end_time = Carbon::now(); $bike->is_lock = Bike::LOCK_YES; $bike->save(); $lastLocation = LocationsLog::getNewestLocationByBikeNo($bike->bike_no); // 修改redis (new BikeStatusInfoSyncHandler())->toBikeWaitRideStatus($bike->bike_no, $lastLocation['lng'], $lastLocation['lat'], $bike->put_status); } catch (\Exception $e) { Log::error('车辆状态未骑行更新失败'); } return $this->ok('操作成功'); } else { return $this->error('操作失败,请联系管理员'); } } /** * openBatteryLock 开电池锁 * * @param Request $request * @return \Illuminate\Http\JsonResponse * @author Fx * */ public function openBatteryLock(Request $request) { $bike_id = $request->get('bike_id') ?? ''; if (empty($bike_id)) return $this->error('参数错误'); $bike = Bike::query()->find($bike_id); if (empty($bike)) return $this->error('找不到车辆'); try { // 增加worker操作车辆记录 $worker_bike_operate = new WorkerBikeOperate(); $worker_bike_operate->type = WorkerBikeOperate::TYPE_OPEN_BATTERY_LOCK; $worker_bike_operate->name = WorkerBikeOperate::$typeMaps[WorkerBikeOperate::TYPE_OPEN_BATTERY_LOCK]; $worker_bike_operate->bike_id = $bike_id; $last_location = object_array(json_decode($bike->last_location)); $worker_bike_operate->latitude = empty($last_location['lat']) ? 0 : $last_location['lat']; $worker_bike_operate->longitude = empty($last_location['lng']) ? 0 : $last_location['lng']; $worker_bike_operate->reason = "工作人员开电池锁"; $worker_bike_operate->worker_id = Admin::user()->id; $worker_bike_operate->save(); } catch (\Exception $e) { Log::error('开电池锁记录生成失败'); return $this->error('开电池锁失败,请联系管理员'); } // $box_no = "003448483"; // 测试写死 $box_no = $bike->box_no; $bool = BikeControl::openBatteryLock($box_no); if ($bool) { return $this->ok('操作开电池锁成功'); } else { return $this->error('开电池锁失败,请联系管理员'); } } /** * rebootBox 重启中控 * * @param Request $request * @return \Illuminate\Http\JsonResponse * @author Fx * */ public function rebootBox(Request $request) { $bike_id = $request->get('bike_id') ?? ''; if (empty($bike_id)) return $this->error('参数错误'); $bike = Bike::query()->find($bike_id); if (empty($bike)) return $this->error('找不到车辆'); try { // 增加worker操作车辆记录 $worker_bike_operate = new WorkerBikeOperate(); $worker_bike_operate->type = WorkerBikeOperate::TYPE_REBOOT_BOX; $worker_bike_operate->name = WorkerBikeOperate::$typeMaps[WorkerBikeOperate::TYPE_REBOOT_BOX]; $worker_bike_operate->bike_id = $bike_id; $last_location = object_array(json_decode($bike->last_location)); $worker_bike_operate->latitude = empty($last_location['lat']) ? 0 : $last_location['lat']; $worker_bike_operate->longitude = empty($last_location['lng']) ? 0 : $last_location['lng']; $worker_bike_operate->reason = "工作人员重启中控"; $worker_bike_operate->worker_id = Admin::user()->id; $worker_bike_operate->save(); } catch (\Exception $e) { Log::error('工作人员重启中控失败'); return $this->error('工作人员重启中控失败,请联系管理员'); } // $box_no = "003448483"; // 测试写死 $box_no = $bike->box_no; $bool = BikeControl::rebootBox($box_no); if ($bool) { return $this->ok('重启中控成功'); } else { return $this->error('重启中控失败,请联系管理员'); } } /** * repairInParking 设为在停车区 * * @param Request $request * @return \Illuminate\Http\JsonResponse * @author Fx * */ public function repairInParking(Request $request) { $bike_id = $request->get('bike_id') ?? ''; if (empty($bike_id)) return $this->error('参数错误'); $bike = Bike::query()->find($bike_id); if (empty($bike)) return $this->error('找不到车辆'); try { $bike->is_in_parking = Bike::IN_PARKING_YES; $bike->save(); return $this->ok('操作成功'); } catch (\Exception $e) { Log::error($e->getMessage()); return $this->error('操作失败,请联系管理员'); } } /** * newBikeLocation 立即定位 * * @param Request $request * @return \Illuminate\Http\JsonResponse * @author Fx * */ public function newBikeLocation(Request $request) { $bike_id = $request->get('bike_id') ?? ''; if (empty($bike_id)) return $this->error('参数错误'); $bike = Bike::query()->find($bike_id); if (empty($bike)) return $this->error('找不到车辆'); try { // 增加worker操作车辆记录 $worker_bike_operate = new WorkerBikeOperate(); $worker_bike_operate->type = WorkerBikeOperate::TYPE_NOW_BIKE_LOCATION; $worker_bike_operate->name = WorkerBikeOperate::$typeMaps[WorkerBikeOperate::TYPE_NOW_BIKE_LOCATION]; $worker_bike_operate->bike_id = $bike_id; $last_location = object_array(json_decode($bike->last_location)); $worker_bike_operate->latitude = empty($last_location['lat']) ? 0 : $last_location['lat']; $worker_bike_operate->longitude = empty($last_location['lng']) ? 0 : $last_location['lng']; $worker_bike_operate->reason = "工作人员立即定位车辆"; $worker_bike_operate->worker_id = Admin::user()->id; $worker_bike_operate->save(); } catch (\Exception $e) { Log::error('工作人员立即定位车辆失败'); return $this->error('工作人员立即定位车辆失败,请联系管理员'); } // $box_no = "003448483"; // 测试写死 $box_no = $bike->box_no; $bool = BikeControl::nowBikeLocation($box_no); if ($bool) { return $this->ok('立即定位车辆成功'); } else { return $this->error('立即定位车辆失败,请联系管理员'); } } /** * newBikeBatteryMSG 立即更新电池信息 * * @param Request $request * @return \Illuminate\Http\JsonResponse * @author Fx * */ public function newBikeBatteryMSG(Request $request) { $bike_id = $request->get('bike_id') ?? ''; if (empty($bike_id)) return $this->error('参数错误'); $bike = Bike::query()->find($bike_id); if (empty($bike)) return $this->error('找不到车辆'); try { // 增加worker操作车辆记录 $worker_bike_operate = new WorkerBikeOperate(); $worker_bike_operate->type = WorkerBikeOperate::TYPE_NOW_BIKE_BATTERY_MSG; $worker_bike_operate->name = WorkerBikeOperate::$typeMaps[WorkerBikeOperate::TYPE_NOW_BIKE_BATTERY_MSG]; $worker_bike_operate->bike_id = $bike_id; $last_location = object_array(json_decode($bike->last_location)); $worker_bike_operate->latitude = empty($last_location['lat']) ? 0 : $last_location['lat']; $worker_bike_operate->longitude = empty($last_location['lng']) ? 0 : $last_location['lng']; $worker_bike_operate->reason = "工作人员立即更新电池信息"; $worker_bike_operate->worker_id = Admin::user()->id; $worker_bike_operate->save(); } catch (\Exception $e) { Log::error('工作人员立即更新电池信息失败'); return $this->error('工作人员立即更新电池信息失败,请联系管理员'); } // $box_no = "003448483"; // 测试写死 $box_no = $bike->box_no; $bool = BikeControl::nowBikeBatteryMSG($box_no); if ($bool) { return $this->ok('立即更新电池信息成功'); } else { return $this->error('立即更新电池信息失败,请联系管理员'); } } /** * closeBatteryLock 关电池锁 * * @param Request $request * @return \Illuminate\Http\JsonResponse * @author Fx * */ public function closeBatteryLock(Request $request) { $bike_id = $request->get('bike_id') ?? ''; if (empty($bike_id)) return $this->error('参数错误'); $bike = Bike::query()->find($bike_id); if (empty($bike)) return $this->error('找不到车辆'); try { // 增加worker操作车辆记录 $worker_bike_operate = new WorkerBikeOperate(); $worker_bike_operate->type = WorkerBikeOperate::TYPE_CLOSE_BATTERY_LOCK; $worker_bike_operate->name = WorkerBikeOperate::$typeMaps[WorkerBikeOperate::TYPE_CLOSE_BATTERY_LOCK]; $worker_bike_operate->bike_id = $bike_id; $last_location = object_array(json_decode($bike->last_location)); $worker_bike_operate->latitude = empty($last_location['lat']) ? 0 : $last_location['lat']; $worker_bike_operate->longitude = empty($last_location['lng']) ? 0 : $last_location['lng']; $worker_bike_operate->reason = "工作人员开电池锁"; $worker_bike_operate->worker_id = Admin::user()->id; $worker_bike_operate->save(); } catch (\Exception $e) { Log::error('关电池锁记录生成失败'); return $this->error('关电池锁失败,请联系管理员'); } // $box_no = "003448483"; // 测试写死 $box_no = $bike->box_no; $bool = BikeControl::closeBatteryLock($box_no); if ($bool) { return $this->ok('操作关电池锁成功'); } else { return $this->error('关电池锁失败,请联系管理员'); } } /** * getKey 获取蓝牙密钥 * * @param Request $request * @return \Illuminate\Http\JsonResponse * @author Fx * */ public function getKey(Request $request) { $box_no = $request->get('box_no'); $bike = Bike::where('box_no', $box_no)->first(); $key = $bike->blu_ase_key ?? ''; return $this->ok(['key' => $key]); } }