12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370 |
- <?php
- namespace App\Http\Controllers\App;
- use App\Filters\BikeFilter;
- use App\Handlers\Aes128Handler;
- use App\Handlers\BikeControl;
- use App\Handlers\BikeStatusInfoSyncHandler;
- use App\Http\Resources\App\BikeOnlineResource;
- use App\Http\Resources\App\BikeResource;
- use App\Http\Resources\App\OrderRentResource;
- use App\Http\Resources\App\OrderResource;
- use App\Models\AdminUser;
- use App\Models\Area;
- use App\Models\Bike;
- // use App\Models\BikeTrouble;
- use App\Models\BoxBinding;
- use App\Models\LocationsLog;
- use App\Models\Order;
- use App\Models\OrderBikeOperate;
- use App\Models\OrderRent;
- use App\Models\User;
- use App\Models\WorkerBikeOperate;
- use App\Models\WorkOrder;
- use App\Utils\Admin;
- use App\Utils\GaodeMaps;
- use App\Utils\Runtime;
- use Carbon\Carbon;
- use Illuminate\Http\Request;
- use App\Http\Controllers\Controller;
- use Illuminate\Support\Facades\DB;
- use Illuminate\Support\Facades\Log;
- use Illuminate\Support\Facades\Redis;
- use Matrix\Exception;
- class BikeController extends AppBaseController
- {
- /**
- * getBikeInfoByBikeNo 根据车辆编号查找车辆基础信息
- *
- * @param Request $request
- * @return \Illuminate\Http\JsonResponse
- * @author Fx
- *
- */
- public function getBikeInfoByBikeNo(Request $request)
- {
- $bike_no = $request->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]);
- }
- }
|