BikeController.php 54 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370
  1. <?php
  2. namespace App\Http\Controllers\App;
  3. use App\Filters\BikeFilter;
  4. use App\Handlers\Aes128Handler;
  5. use App\Handlers\BikeControl;
  6. use App\Handlers\BikeStatusInfoSyncHandler;
  7. use App\Http\Resources\App\BikeOnlineResource;
  8. use App\Http\Resources\App\BikeResource;
  9. use App\Http\Resources\App\OrderRentResource;
  10. use App\Http\Resources\App\OrderResource;
  11. use App\Models\AdminUser;
  12. use App\Models\Area;
  13. use App\Models\Bike;
  14. // use App\Models\BikeTrouble;
  15. use App\Models\BoxBinding;
  16. use App\Models\LocationsLog;
  17. use App\Models\Order;
  18. use App\Models\OrderBikeOperate;
  19. use App\Models\OrderRent;
  20. use App\Models\User;
  21. use App\Models\WorkerBikeOperate;
  22. use App\Models\WorkOrder;
  23. use App\Utils\Admin;
  24. use App\Utils\GaodeMaps;
  25. use App\Utils\Runtime;
  26. use Carbon\Carbon;
  27. use Illuminate\Http\Request;
  28. use App\Http\Controllers\Controller;
  29. use Illuminate\Support\Facades\DB;
  30. use Illuminate\Support\Facades\Log;
  31. use Illuminate\Support\Facades\Redis;
  32. use Matrix\Exception;
  33. class BikeController extends AppBaseController
  34. {
  35. /**
  36. * getBikeInfoByBikeNo 根据车辆编号查找车辆基础信息
  37. *
  38. * @param Request $request
  39. * @return \Illuminate\Http\JsonResponse
  40. * @author Fx
  41. *
  42. */
  43. public function getBikeInfoByBikeNo(Request $request)
  44. {
  45. $bike_no = $request->get('bike_no') ?? '';
  46. if (empty($bike_no)) return $this->error('请输入正确车牌号');
  47. $bikeInfo = Bike::query()->where('bike_no', $bike_no)->whereIn('put_area_id', self::$areaIds)->first();
  48. if (empty($bikeInfo)) return $this->error('找不到车辆信息,请检查车牌号');
  49. $data = [];
  50. $model = $bikeInfo;
  51. $tab_name = '';
  52. if (!(bool)$model->is_link) {
  53. // 离线
  54. $tab_name = '中控离线';
  55. } elseif ((bool)$model->is_trouble) {
  56. // 故障上线
  57. if ((bool)$model->put_status) {
  58. $tab_name = '故障';
  59. } else {
  60. // 故障下线
  61. $tab_name = '故障下线';
  62. }
  63. } elseif (!(bool)$model->is_low_battery_power) {
  64. // 低电量
  65. $tab_name = '低电量';
  66. } elseif (!(bool)$model->put_status) {
  67. // 下线 未投放
  68. $tab_name = '未投放';
  69. } elseif ((bool)$model->is_riding) {
  70. //骑行中
  71. $tab_name = '骑行中';
  72. } elseif (!(bool)$model->is_in_parking) {
  73. // 不在停车区
  74. $tab_name = '不在停车区';
  75. } else {
  76. // 未骑行
  77. $tab_name = '未骑行';
  78. }
  79. $lastLocation = LocationsLog::getNewestLocationTimeByBikeNo($bike_no);
  80. $workInfo = [];
  81. if (app()->redis->hexists(BikeStatusInfoSyncHandler::REDIS_RIDE_BIKE_WORKER_ORDERS_TAG, $bikeInfo->bike_no)) {
  82. $work_id = app()->redis->hget(BikeStatusInfoSyncHandler::REDIS_RIDE_BIKE_WORKER_ORDERS_TAG, $bikeInfo->bike_no);
  83. $work = AdminUser::find($work_id);
  84. $workInfo = [
  85. 'name' => $work->name,
  86. 'phone' => $work->phone,
  87. ];
  88. }
  89. // 车辆信息
  90. $data['bike_info']['bike_no'] = $bike_no;
  91. $data['bike_info']['box_no'] = $bikeInfo->box_no;
  92. $data['bike_info']['id'] = $bikeInfo->id;
  93. $data['bike_info']['is_lock'] = $bikeInfo->is_lock;
  94. $data['bike_info']['battery_power'] = $bikeInfo->battery_power;
  95. $data['bike_info']['is_trouble'] = $bikeInfo->is_trouble; // 是否故障
  96. $data['bike_info']['is_link'] = $bikeInfo->put_status; // 是否在线
  97. $data['bike_info']['area_name'] = Area::query()->find($bikeInfo->put_area_id)->name ?? ''; //车辆区域地址
  98. $data['bike_info']['tab_name'] = $tab_name; //状态
  99. $data['bike_info']['last_location'] = GaodeMaps::getAddress([$lastLocation['location']['lng'], $lastLocation['location']['lat']]); //最后定位位置
  100. $data['bike_info']['last_time'] = $lastLocation['time']; //最后定位时间
  101. $data['bike_info']['workInfo'] = $workInfo; //最后定位时间
  102. //最近订单 取最
  103. $order = Order::query()->where('bike_id', $bikeInfo->id)->orderByDesc('id')->with('users')->first();
  104. $orderRent = OrderRent::query()->where('bike_id', $bikeInfo->id)->orderByDesc('id')->with('users')->first();
  105. $data2 = [];
  106. if (!empty($order) && !empty($orderRent)) {
  107. if ($orderRent->start_use_bike_time > $order->start_use_bike_time) {
  108. // $user = User::query()->find($v->user_id);
  109. $data1['bike_no'] = $orderRent->bike_no;
  110. $data1['username'] = $orderRent->users->nickname . "(" . $orderRent->users->truename . ")" ?? '';
  111. $data1['mobile'] = $orderRent->users->mobile ?? '';
  112. $data1['order_status'] = OrderRent::$statusMaps[$orderRent->status];
  113. $data1['start_use_bike_time'] = $orderRent->start_use_bike_time;
  114. $data1['end_use_bike_time'] = $orderRent->end_use_bike_time;
  115. $data1['start_use_bike_time_app'] = date('m/d H:i', strtotime($orderRent->start_use_bike_time));
  116. $data1['end_use_bike_time_app'] = $orderRent->end_use_bike_time ? date('m/d H:i', strtotime($orderRent->end_use_bike_time)) : '';
  117. } else {
  118. $data1['bike_no'] = $order->bike_no;
  119. $data1['username'] = $order->users->nickname . "(" . $order->users->truename . ")" ?? '';
  120. $data1['mobile'] = $order->users->mobile ?? '';
  121. $data1['order_status'] = Order::$statusMaps[$order->status];
  122. $data1['start_use_bike_time'] = $order->start_use_bike_time;
  123. $data1['end_use_bike_time'] = $order->end_use_bike_time;
  124. $data1['start_use_bike_time_app'] = date('m/d H:i', strtotime($order->start_use_bike_time));
  125. $data1['end_use_bike_time_app'] = $order->end_use_bike_time ? date('m/d H:i', strtotime($order->end_use_bike_time)) : '';
  126. }
  127. $data2[] = $data1;
  128. } else if (!empty($order)) {
  129. $data1['bike_no'] = $order->bike_no;
  130. $data1['username'] = $order->users->nickname . "(" . $order->users->truename . ")" ?? '';
  131. $data1['mobile'] = $order->users->mobile ?? '';
  132. $data1['order_status'] = Order::$statusMaps[$order->status];
  133. $data1['start_use_bike_time'] = $order->start_use_bike_time;
  134. $data1['end_use_bike_time'] = $order->end_use_bike_time;
  135. $data1['start_use_bike_time_app'] = date('m/d H:i', strtotime($order->start_use_bike_time));
  136. $data1['end_use_bike_time_app'] = $order->end_use_bike_time ? date('m/d H:i', strtotime($order->end_use_bike_time)) : '';
  137. $data2[] = $data1;
  138. } else if (!empty($orderRent)) {
  139. $data1['bike_no'] = $orderRent->bike_no;
  140. $data1['username'] = $orderRent->users->nickname . "(" . $orderRent->users->truename . ")" ?? '';
  141. $data1['mobile'] = $orderRent->users->mobile ?? '';
  142. $data1['order_status'] = OrderRent::$statusMaps[$orderRent->status];
  143. $data1['start_use_bike_time'] = $orderRent->start_use_bike_time;
  144. $data1['end_use_bike_time'] = $orderRent->end_use_bike_time;
  145. $data1['start_use_bike_time_app'] = date('m/d H:i', strtotime($orderRent->start_use_bike_time));
  146. $data1['end_use_bike_time_app'] = $orderRent->end_use_bike_time ? date('m/d H:i', strtotime($orderRent->end_use_bike_time)) : '';
  147. $data2[] = $data1;
  148. }
  149. $data['bike_order'] = $data2;
  150. return $this->ok($data);
  151. }
  152. /**
  153. * updateBikeInfo 根据车辆id 修改车辆信息
  154. *
  155. * @param Request $request
  156. * @return \Illuminate\Http\JsonResponse
  157. * @author Fx
  158. *
  159. */
  160. public function updateBikeInfo(Request $request)
  161. {
  162. $admin_id = Admin::user()->id;
  163. if (!Admin::isAdministrator()) {
  164. return $this->error('暂无权限');
  165. }
  166. $bike_id = $request->get('bike_id') ?? '';
  167. if (empty($bike_id)) return $this->error('参数错误');
  168. $bike = Bike::query()->whereIn('put_area_id', self::$areaIds)->find($bike_id);
  169. if (empty($bike)) return $this->error('找不到该车辆信息,请检查车牌号');
  170. $bike_no = $request->get('bike_no') ?? '';
  171. $box_no = $request->get('box_no') ?? '';
  172. $bike_remarks = $request->get('bike_remarks') ?? '';
  173. if (empty($bike_no) && empty($box_no) && empty($bike_remarks)) {
  174. // 都为空
  175. return $this->error('请输入要修改的内容');
  176. }
  177. // 不为空修改
  178. if (!empty($bike_no)) {
  179. $verifyBikeNo = Bike::query()->where('bike_no', $bike_no)->first();
  180. if (!empty($verifyBikeNo)) {
  181. if ($verifyBikeNo->id !== $bike->id) {
  182. return $this->error('车辆编号已存在,不可修改');
  183. }
  184. }
  185. $bike->bike_no = $bike_no;
  186. }
  187. $date = Carbon::now();
  188. if (!empty($bike_remarks)) {
  189. $bike->bike_remarks = $bike_remarks . $date;
  190. }
  191. if (!empty($box_no)) {
  192. $verifyBikeNo = Bike::query()->where('box_no', $box_no)->first();
  193. if (!empty($verifyBikeNo)) {
  194. if ($verifyBikeNo->id !== $bike->id) {
  195. return $this->error('中控编号已存在,不可修改');
  196. }
  197. }
  198. // 中控绑定操作
  199. $box = BoxBinding::query()->where('box_no', $box_no)->first();
  200. if (empty($box)) return $this->error('找不到此设备信息,请联系管理员');
  201. if ($box->is_binding == BoxBinding::BINDING_YES) return $this->error('此设备已经绑定过');
  202. // 蓝牙信息'TBIT_WA205-7HBLE';
  203. $blu_key = config('systemConfig.blu_key');
  204. try {
  205. $blu_ase_key = Aes128Handler::genKey($blu_key, $box_no);
  206. } catch (\Exception $e) {
  207. return $this->error($e->getMessage());
  208. }
  209. $inputs['blu_key'] = $blu_key;
  210. $inputs['blu_ase_key'] = $blu_ase_key;
  211. try {
  212. DB::beginTransaction();
  213. $bike->blu_key = $blu_key;
  214. $bike->blu_ase_key = $blu_ase_key;
  215. $box->is_binding = BoxBinding::BINDING_YES;
  216. $box->save();
  217. $bike->save();
  218. DB::commit();
  219. return $this->ok(BikeResource::make($bike));
  220. } catch (\Exception $e) {
  221. DB::rollBack();
  222. Log::error($e->getMessage());
  223. return $this->error('修改失败,请联系管理员');
  224. }
  225. }
  226. $bool = $bike->save();
  227. if ($bool) {
  228. return $this->ok('修改成功');
  229. } else {
  230. return $this->error('修改失败,请联系管理员');
  231. }
  232. }
  233. /**
  234. * bikeLocation 车辆位置轨迹
  235. *
  236. * @param Request $request
  237. * @return \Illuminate\Http\JsonResponse
  238. * @author Fx
  239. *
  240. */
  241. public function bikeLocation(Request $request)
  242. {
  243. $bike_no = $request->get('bike_no') ?? '';
  244. $time_between = $request->get('time_between') ?? []; // time_between 时间区间
  245. if (empty($bike_no) || empty($time_between)) return $this->error('参数错误');
  246. $locationLog = LocationsLog::where('bike_no', $bike_no)
  247. ->where('created_at', '>=', Carbon::parse($time_between[0])->toDateString())
  248. ->where('created_at', '<=', Carbon::parse($time_between[1])->toDateString())
  249. ->whereBetween('latitude', [3, 53])->whereBetween('longitude', [73, 136])->orderBy('created_at', 'asc')
  250. ->get(['longitude', 'latitude', 'type']);
  251. $data = [];
  252. $userLocation = [];
  253. $workerLocation = [];
  254. $noLocatioin = [];
  255. $old_type = '';
  256. $item = [];
  257. $Locations = [];
  258. foreach ($locationLog as $v) {
  259. $data[] = [$v->longitude, $v->latitude];
  260. if (empty($old_type)) {
  261. $old_type = $v->type;
  262. }
  263. if ($v->type !== $old_type) {
  264. if ($old_type == LocationsLog::USER_LOCATION) {
  265. $userLocation[] = $item;
  266. $Locations[] = ['data' => $item, 'role' => LocationsLog::USER_LOCATION];
  267. } else if ($old_type == LocationsLog::WORKER_LOCATION) {
  268. $workerLocation[] = $item;
  269. $Locations[] = ['data' => $item, 'role' => LocationsLog::WORKER_LOCATION];
  270. } else {
  271. $noLocatioin[] = $item;
  272. $Locations[] = ['data' => $item, 'role' => LocationsLog::NO_LOCATION];
  273. }
  274. $old_type = '';
  275. $item = [];
  276. $item[] = ['longitude' => $v->longitude, 'latitude' => $v->latitude];
  277. } else {
  278. $item[] = ['longitude' => $v->longitude, 'latitude' => $v->latitude];
  279. }
  280. unset($v->_id);
  281. }
  282. if (empty($data)) return $this->ok($data);
  283. $wx_data = $locationLog->toArray();
  284. $res = [
  285. 'locations' => $data,
  286. 'start_location' => $data[0],
  287. 'end_location' => end($data),
  288. 'wx_locations' => $wx_data,
  289. 'wx_start_location' => $wx_data[0],
  290. 'wx_end_location' => end($wx_data),
  291. 'user_location' => $userLocation,
  292. 'worker_location' => $workerLocation,
  293. 'no_locatioin' => $noLocatioin,
  294. 'all_locations' => $Locations
  295. ];
  296. return $this->ok($res);
  297. }
  298. /**
  299. * bikePosition 获取车辆真实位置点(mongodb)
  300. *
  301. * @param Request $request
  302. * @return \Illuminate\Http\JsonResponse
  303. * @author Fx
  304. *
  305. */
  306. public function bikePosition(Request $request)
  307. {
  308. $bike_no = $request->get('bike_no') ?? '';
  309. if (empty($bike_no)) return $this->error('参数错误');
  310. $position = LocationsLog::getNewestLocationByBikeNo($bike_no);
  311. if (empty($position)) return $this->ok([]);
  312. return $this->ok([$position['lng'], $position['lat']]);
  313. }
  314. /**
  315. * sweepCodeLocation 扫码位置 近200条
  316. *
  317. * @param Request $request
  318. * @return \Illuminate\Http\JsonResponse
  319. * @author Fx
  320. *
  321. */
  322. public function sweepCodeLocation(Request $request)
  323. {
  324. $bike_id = $request->get('bike_id') ?? '';
  325. if (empty($bike_id)) return $this->error('参数错误');
  326. $threeAgo = Carbon::today()->subDays(3);
  327. $order = Order::query()
  328. ->where('bike_id', $bike_id)
  329. // ->where('created_at','>',$threeAgo)
  330. ->orderByDesc('id')
  331. ->limit(100)
  332. ->get(['start_use_bike_time', 'start_use_bike_location']);
  333. $data = [];
  334. if (!empty($order)) {
  335. foreach ($order as $v) {
  336. $dataItem['time'] = $v->start_use_bike_time;
  337. $obj = json_decode($v->start_use_bike_location);
  338. $dataItem['location'] = [$obj->longitude, $obj->latitude];
  339. $data[] = $dataItem;
  340. }
  341. }
  342. $orderRent = OrderRent::query()
  343. ->where('bike_id', $bike_id)
  344. // ->where('created_at','>',$threeAgo)
  345. ->orderByDesc('id')
  346. ->limit(100)
  347. ->get(['start_use_bike_time', 'start_use_bike_location']);
  348. $dataRent = [];
  349. if (!empty($orderRent)) {
  350. foreach ($orderRent as $v) {
  351. $dataRentItem['time'] = $v->start_use_bike_time;
  352. $objRent = json_decode($v->start_use_bike_location);
  353. $dataRentItem['location'] = [$objRent->longitude, $objRent->latitude];
  354. $dataRent[] = $dataRentItem;
  355. }
  356. }
  357. return $this->ok(array_merge($data, $dataRent));
  358. }
  359. /**
  360. * latelyOrders 车辆最近订单
  361. *
  362. * @param Request $request
  363. * @return \Illuminate\Http\JsonResponse
  364. * @author Fx
  365. *
  366. */
  367. public function latelyOrders(Request $request)
  368. {
  369. $bike_id = $request->get('bike_id') ?? '';
  370. if (empty($bike_id)) return $this->error('参数错误');
  371. $order = Order::query()
  372. ->where('bike_id', $bike_id)
  373. ->with('users')
  374. ->orderByDesc('id')
  375. ->paginate();
  376. // $data = [];
  377. // if (!empty($order)) {
  378. // foreach ($order as $v) {
  379. // // $user = User::query()->find($v->user_id);
  380. // $data1['bike_no'] = $v->bike_no;
  381. // $data1['username'] = $v->users->nickname ?? '';
  382. // $data1['mobile'] = $v->users->mobile ?? '';
  383. // $data1['order_status'] = Order::$statusMaps[$v->status];
  384. // $data1['start_use_bike_time'] = $v->start_use_bike_time;
  385. // $data1['end_use_bike_time'] = $v->end_use_bike_time;
  386. // $data[] = $data1;
  387. // }
  388. // }
  389. return $this->ok(OrderResource::collection($order));
  390. }
  391. /**
  392. * latelyOrderRent 最近日租订单
  393. *
  394. * @param Request $request
  395. * @return \Illuminate\Http\JsonResponse
  396. * @author Fx
  397. *
  398. */
  399. public function latelyOrderRent(Request $request)
  400. {
  401. $bike_id = $request->get('bike_id') ?? '';
  402. if (empty($bike_id)) return $this->error('参数错误');
  403. $order = OrderRent::query()
  404. ->where('bike_id', $bike_id)
  405. ->with('users')
  406. ->orderByDesc('id')
  407. ->paginate();
  408. return $this->ok(OrderRentResource::collection($order));
  409. }
  410. /**
  411. * locksLog 车辆开锁关锁记录 近3天
  412. *
  413. * @param Request $request
  414. * @return \Illuminate\Http\JsonResponse
  415. * @author Fx
  416. *
  417. */
  418. public function locksLog(Request $request)
  419. {
  420. $bike_id = $request->get('bike_id') ?? '';
  421. if (empty($bike_id)) return $this->error('参数错误');
  422. $typeArr = [OrderBikeOperate::TYPE_OPEN_BIKE,
  423. OrderBikeOperate::TYPE_CLONE_BIKE,
  424. OrderBikeOperate::TYPE_PAUSE_OPEN_BIKE,
  425. OrderBikeOperate::TYPE_PAUSE_CLONE_BIKE];
  426. // 普通用户操作
  427. $threeAgo = Carbon::today()->subDays(3);
  428. $lock1 = OrderBikeOperate::query()
  429. ->whereIn('type', $typeArr)
  430. ->where('is_admin', OrderBikeOperate::IS_ADMIN_NO)
  431. ->where('bike_id', $bike_id)
  432. ->where('created_at', '>', $threeAgo)
  433. ->with('users')
  434. ->orderByDesc('id')
  435. ->get();
  436. $data1 = [];
  437. $open_bike = [];
  438. $close_bike = [];
  439. $open_pause_bike = [];
  440. $close_pause_bike = [];
  441. if (!empty($lock1)) {
  442. foreach ($lock1 as $v) {
  443. // $user = User::query()->find($v->user_id);
  444. $dataItem1['username'] = $v->users->nickname ?? '';
  445. $dataItem1['mobile'] = $v->users->mobile ?? '';
  446. $dataItem1['operate_time'] = date("Y/m/d H:i:s", strtotime($v->created_at));
  447. $dataItem1['name'] = $v->name;
  448. $dataItem1['location'] = [$v->longitude, $v->latitude];
  449. if ($v->type == OrderBikeOperate::TYPE_OPEN_BIKE) {
  450. $open_bike[] = $dataItem1;
  451. } elseif ($v->type == OrderBikeOperate::TYPE_CLONE_BIKE) {
  452. $close_bike[] = $dataItem1;
  453. } elseif ($v->type == OrderBikeOperate::TYPE_PAUSE_OPEN_BIKE) {
  454. $open_pause_bike[] = $dataItem1;
  455. } elseif ($v->type == OrderBikeOperate::TYPE_PAUSE_CLONE_BIKE) {
  456. $close_pause_bike[] = $dataItem1;
  457. }
  458. }
  459. }
  460. // 管理员操作
  461. $lock2 = OrderBikeOperate::query()
  462. ->whereIn('type', $typeArr)
  463. ->where('is_admin', OrderBikeOperate::IS_ADMIN_YES)
  464. ->where('bike_id', $bike_id)
  465. ->where('created_at', '>', $threeAgo)
  466. ->with('adminUsers')
  467. ->orderByDesc('id')
  468. ->get();
  469. $data2 = [];
  470. if (!empty($lock2)) {
  471. foreach ($lock2 as $v) {
  472. // $user = AdminUser::query()->find($v->user_id);
  473. $dataItem2['username'] = $v->adminUsers->name ?? '';
  474. $dataItem2['mobile'] = $v->adminUsers->phone ?? '';
  475. $dataItem2['operate_time'] = date("Y/m/d H:i:s", strtotime($v->created_at));
  476. $dataItem2['name'] = $v->name;
  477. $dataItem2['location'] = [$v->longitude, $v->latitude];
  478. if ($v->type == OrderBikeOperate::TYPE_OPEN_BIKE) {
  479. $open_bike[] = $dataItem2;
  480. } elseif ($v->type == OrderBikeOperate::TYPE_CLONE_BIKE) {
  481. $close_bike[] = $dataItem2;
  482. } elseif ($v->type == OrderBikeOperate::TYPE_PAUSE_OPEN_BIKE) {
  483. $open_pause_bike[] = $dataItem2;
  484. } elseif ($v->type == OrderBikeOperate::TYPE_PAUSE_CLONE_BIKE) {
  485. $close_pause_bike[] = $dataItem2;
  486. }
  487. }
  488. }
  489. // $data = array_merge($data1, $data2);
  490. $data = [
  491. 'open_bike' => $open_bike,
  492. 'close_bike' => $close_bike,
  493. 'open_pause_bike' => $open_pause_bike,
  494. 'close_pause_bike' => $close_pause_bike
  495. ];
  496. return $this->ok($data);
  497. }
  498. /**
  499. * bikeOnLineLog 车辆上线下线记录
  500. *
  501. * @param Request $request
  502. * @return \Illuminate\Http\JsonResponse
  503. * @author Fx
  504. *
  505. */
  506. public function bikeOnLineLog(Request $request)
  507. {
  508. $bike_id = $request->get('bike_id') ?? '';
  509. if (empty($bike_id)) return $this->error('参数错误');
  510. $bikeOnLine = WorkerBikeOperate::query()
  511. ->where('bike_id', $bike_id)
  512. ->whereIn('type', [WorkerBikeOperate::TYPE_ONLINE, WorkerBikeOperate::TYPE_OFFLINE])
  513. ->with('workers')
  514. ->orderByDesc('id')
  515. ->paginate();
  516. // $data = [];
  517. // if (!empty($bikeOnLine)) {
  518. // foreach ($bikeOnLine as $v) {
  519. // $item['reason'] = $v->reason;
  520. // $item['name'] = $v->name;
  521. // $item['created_at'] = date("Y/m/d H:i:s", strtotime($v->created_at));
  522. // $item['username'] = $v->workers->name;
  523. // $item['mobile'] = $v->workers->phone;
  524. // $data[] = $item;
  525. // }
  526. // }
  527. return $this->ok(BikeOnlineResource::collection($bikeOnLine));
  528. }
  529. /**
  530. * repairOptions 可报修选项
  531. *
  532. * @return \Illuminate\Http\JsonResponse
  533. * @author Fx
  534. *
  535. */
  536. public function repairOptions()
  537. {
  538. // $repair_part = BikeTrouble::$troublePartMaps;
  539. $repair_part = WorkOrder::$typeMaps;
  540. $data = [];
  541. foreach ($repair_part as $k => $v) {
  542. $dateItem['id'] = $k;
  543. $dateItem['name'] = $v;
  544. $dateItem['select'] = false;
  545. $data[] = $dateItem;
  546. }
  547. return $this->ok($data);
  548. }
  549. /**
  550. * bikeRepair 车辆报修
  551. *
  552. * @param Request $request
  553. * @return \Illuminate\Http\JsonResponse
  554. * @author Fx
  555. *
  556. */
  557. public function bikeRepair(Request $request)
  558. {
  559. $bike_no = $request->get('bike_no') ?? '';
  560. $box_no = $request->get('box_no') ?? '';
  561. $trouble_part = $request->get('trouble_part') ?? ''; // 报修部位
  562. $reason = $request->get('reason') ?? ''; // 原因
  563. if (empty($trouble_part)) return $this->error('请选择报修部位');
  564. if (empty($reason) || empty($bike_no)) return $this->error('请填写报修原因');
  565. if (!empty($bike_no)) {
  566. $bike = Bike::query()->where('bike_no', $bike_no)->first();
  567. if (!empty($bike)) {
  568. if ($bike->put_status == Bike::PUT_STATUS_NO) {
  569. return $this->error('车辆已下线,无需报修,如需报修请先手动上线');
  570. }
  571. DB::beginTransaction();
  572. try {
  573. // 更新车辆状态 下线
  574. $bike->put_status = Bike::PUT_STATUS_NO;
  575. $bike->is_trouble = Bike::TROUBLE_YES;
  576. $bool1 = $bike->save();
  577. // 更新redis
  578. (new BikeStatusInfoSyncHandler())->toBikeOffLineStatus($bike->bike_no);
  579. if (!$bool1) {
  580. DB::rollBack();
  581. Log::error('更新车辆状态失败');
  582. }
  583. // 插入下线记录
  584. $worker_bike_operate = new WorkerBikeOperate();
  585. $worker_bike_operate->type = WorkerBikeOperate::TYPE_OFFLINE;
  586. $worker_bike_operate->name = WorkerBikeOperate::$typeMaps[WorkerBikeOperate::TYPE_OFFLINE];
  587. $worker_bike_operate->bike_id = $bike->id;
  588. $last_location = json_decode($bike->last_location);
  589. $worker_bike_operate->latitude = empty($last_location['lat']) ? 0 : $last_location['lat'];
  590. $worker_bike_operate->longitude = empty($last_location['lng']) ? 0 : $last_location['lng'];
  591. $worker_bike_operate->worker_id = Admin::user()->id;
  592. // foreach ($trouble_part as $v){
  593. // $reason .= BikeTrouble::$troublePartMaps[$v] . ',';
  594. // }
  595. $worker_bike_operate->reason = $reason;
  596. $bool2 = $worker_bike_operate->save();
  597. if (!$bool2) {
  598. DB::rollBack();
  599. Log::error('插入下线记录失败');
  600. }
  601. // 插入报修信息
  602. // $bike_trouble = new BikeTrouble();
  603. // $bike_trouble->trouble_no = 'T-'.$this->generateTroubleNo(); //故障号
  604. // $bike_trouble->user_id = Admin::user()->id;
  605. // $bike_trouble->bike_id = $bike->id;
  606. // $bike_trouble->area_id = $bike->put_area_id;// 区域id
  607. // $bike_trouble->trouble_part = json_encode($trouble_part);
  608. // $bike_trouble->trouble_grade = count($trouble_part)==1 ? BikeTrouble::getTroubleGrade($trouble_part[0]): BikeTrouble::TROUBLE_GRADE_TWO ;// 故障等级 故障部位大于1 默认高级故障
  609. // // $bike_trouble->up_trouble_type = 0 // 上报问题类型
  610. // $bool3 = $bike_trouble->save();
  611. //生成工单
  612. //插入工单
  613. $work_order_data = [
  614. 'work_no' => WorkOrder::makeWorkNo(),
  615. 'type' => $trouble_part,
  616. 'type_name' => WorkOrder::$typeMaps[$trouble_part],
  617. 'bike_no' => $bike->bike_no,
  618. 'bike_id' => $bike->id,
  619. 'source' => WorkOrder::SOURCE_REPAIR,
  620. 'area_id' => $bike->put_area_id,
  621. 'admin_id' => Admin::user()->id,
  622. 'reason' => $reason,
  623. ];
  624. $work_order_verify = [
  625. 'bike_id' => $bike->id,
  626. 'type' => $trouble_part, // 工单类型
  627. 'status' => WorkOrder::STATUS_NO
  628. ];
  629. WorkOrder::firstOrCreate($work_order_verify, $work_order_data);
  630. DB::commit();
  631. return $this->ok('报修成功');
  632. } catch (\Exception $exception) {
  633. DB::rollBack();
  634. Log::info($exception->getMessage());
  635. return $this->error('操作失败请联系管理员');
  636. }
  637. }
  638. }
  639. if (!empty($box_no)) {
  640. $bike = Bike::query()->where('box_no', $box_no)->first();
  641. if (!empty($bike)) {
  642. try {
  643. // 更新车辆状态 下线
  644. $bike->put_status = Bike::PUT_STATUS_NO;
  645. $bike->is_trouble = Bike::TROUBLE_YES;
  646. $bool1 = $bike->save();
  647. // 更新redis
  648. (new BikeStatusInfoSyncHandler())->toBikeOffLineStatus($bike->bike_no);
  649. if (!$bool1) {
  650. DB::rollBack();
  651. Log::error('更新车辆状态失败');
  652. }
  653. // 插入下线记录
  654. $worker_bike_operate = new WorkerBikeOperate();
  655. $worker_bike_operate->type = WorkerBikeOperate::TYPE_OFFLINE;
  656. $worker_bike_operate->name = WorkerBikeOperate::$typeMaps[WorkerBikeOperate::TYPE_OFFLINE];
  657. $worker_bike_operate->bike_id = $bike->id;
  658. $last_location = json_decode($bike->last_location);
  659. $worker_bike_operate->latitude = empty($last_location['lat']) ? 0 : $last_location['lat'];
  660. $worker_bike_operate->longitude = empty($last_location['lng']) ? 0 : $last_location['lng'];
  661. $worker_bike_operate->worker_id = Admin::user()->id;
  662. $worker_bike_operate->reason = $reason;
  663. $bool2 = $worker_bike_operate->save();
  664. if (!$bool2) {
  665. DB::rollBack();
  666. Log::error('插入下线记录失败');
  667. }
  668. // 插入报修信息
  669. // $bike_trouble = new BikeTrouble();
  670. // $bike_trouble->trouble_no = 'T-' . $this->generateTroubleNo(); //故障号
  671. // $bike_trouble->user_id = Admin::user()->id;
  672. // $bike_trouble->bike_id = $bike->id;
  673. // $bike_trouble->area_id = $bike->put_area_id;// 区域id
  674. // $bike_trouble->trouble_part = json_encode($trouble_part);
  675. // $bike_trouble->trouble_grade = 3; // 故障等级
  676. // // $bike_trouble->up_trouble_type = 0 // 上报问题类型
  677. // $bool3 = $bike_trouble->save();
  678. //生成工单
  679. // 插入工单
  680. $work_order_data = [
  681. 'work_no' => WorkOrder::makeWorkNo(),
  682. 'type' => $trouble_part,
  683. 'type_name' => WorkOrder::$typeMaps[$trouble_part],
  684. 'bike_no' => $bike->bike_no,
  685. 'bike_id' => $bike->id,
  686. 'source' => WorkOrder::SOURCE_REPAIR,
  687. 'area_id' => $bike->put_area_id,
  688. 'admin_id' => Admin::user()->id,
  689. 'reason' => $reason,
  690. ];
  691. $work_order_verify = [
  692. 'bike_id' => $bike->id,
  693. 'type' => $trouble_part,
  694. 'status' => WorkOrder::STATUS_NO
  695. ];
  696. WorkOrder::firstOrCreate($work_order_verify, $work_order_data);
  697. DB::commit();
  698. return $this->ok('报修成功');
  699. } catch (\Exception $exception) {
  700. DB::rollBack();
  701. Log::info($exception->getMessage());
  702. return $this->error('操作失败请联系管理员');
  703. }
  704. } else {
  705. return $this->error('找不到该车辆信息');
  706. }
  707. } else {
  708. return $this->error('参数错误');
  709. }
  710. }
  711. /**
  712. * generateTroubleNo 生成故障号
  713. *
  714. * @return string
  715. * @author Fx
  716. *
  717. */
  718. private function generateTroubleNo()
  719. {
  720. $time = date('YmdHis', time());
  721. $randNum = mt_rand(1111, 9999);
  722. return $time . $randNum;
  723. }
  724. /**
  725. * bikeList 车辆列表
  726. *
  727. * @param BikeFilter $filter
  728. * @return \Illuminate\Http\JsonResponse
  729. * @author Fx
  730. *
  731. */
  732. public function bikeList(BikeFilter $filter)
  733. {
  734. $area_ids = self::$areaIds;
  735. $bikelist = Bike::query()
  736. ->whereIn('put_area_id', $area_ids)
  737. ->whereNotNull('box_no')
  738. ->filter($filter)
  739. ->orderByDesc('id')
  740. ->paginate();
  741. return $this->ok(BikeResource::collection($bikelist));
  742. }
  743. /**
  744. * bikeStatistics 车辆统计
  745. *
  746. * @param BikeFilter $filter
  747. * @return \Illuminate\Http\JsonResponse
  748. * @author Fx
  749. *
  750. */
  751. public function bikeStatistics(BikeFilter $filter)
  752. {
  753. $data = [];
  754. $area_ids = self::$areaIds;
  755. // 总车辆数
  756. $data['total_bikes'] = Bike::query()
  757. ->whereIn('put_area_id', $area_ids)
  758. ->filter($filter)
  759. ->count('id');
  760. // 上线车辆数
  761. $data['put_total_bikes'] = Bike::query()
  762. ->whereIn('put_area_id', $area_ids)
  763. ->filter($filter)
  764. ->where('put_status', Bike::PUT_STATUS_YES)
  765. ->count('id');
  766. // 在线车辆数
  767. $data['line_total_bikes'] = Bike::query()
  768. ->whereIn('put_area_id', $area_ids)
  769. ->filter($filter)
  770. ->where('is_link', Bike::LINK_ONLINE)
  771. ->count('id');
  772. //故障车辆
  773. $data['trouble_total_bikes'] = Bike::query()
  774. ->whereIn('put_area_id', $area_ids)
  775. ->filter($filter)
  776. ->where('is_trouble', Bike::TROUBLE_YES)
  777. ->count('id');
  778. //今日使用
  779. $data['today_use_total_bikes'] = Bike::query()
  780. ->whereIn('put_area_id', $area_ids)
  781. ->filter($filter)
  782. ->where('last_use_bike_end_time', '>', Carbon::today())
  783. ->count('id');
  784. //低电量
  785. $data['low_battery_total_bikes'] = Bike::query()
  786. ->whereIn('put_area_id', $area_ids)
  787. ->filter($filter)
  788. ->where('is_low_battery_power', Bike::BATTERY_POWER_LOW)
  789. ->count('id');
  790. //骑行中
  791. $data['riding_total_bikes'] = Bike::query()
  792. ->whereIn('put_area_id', $area_ids)
  793. ->filter($filter)
  794. ->where('is_riding', Bike::RIDING_YES)
  795. ->count('id');
  796. //未使用
  797. $data['not_riding_total_bikes'] = Bike::query()
  798. ->whereIn('put_area_id', $area_ids)
  799. ->filter($filter)
  800. ->where('is_riding', Bike::RIDING_NO)
  801. ->count('id');
  802. return $this->ok($data);
  803. }
  804. /**
  805. * bikeLogin 车辆上线 (投放)
  806. *
  807. * @param Request $request
  808. * @return \Illuminate\Http\JsonResponse
  809. * @author Fx
  810. *
  811. */
  812. public function bikeLogin(Request $request)
  813. {
  814. $bike_id = $request->get('bike_id') ?? '';
  815. if (empty($bike_id)) return $this->error('参数错误');
  816. try {
  817. DB::beginTransaction();
  818. $bike = Bike::query()->find($bike_id);
  819. if (empty($bike)) return $this->error('找不到车辆');
  820. if ($bike->put_status == Bike::PUT_STATUS_YES) return $this->error('车辆已在线');
  821. // 更新车辆上线
  822. $bike->put_status = Bike::PUT_STATUS_YES;
  823. $bike->save();
  824. // 插入上线记录
  825. $worker_bike_operate = new WorkerBikeOperate();
  826. $worker_bike_operate->type = WorkerBikeOperate::TYPE_ONLINE;
  827. $worker_bike_operate->name = WorkerBikeOperate::$typeMaps[WorkerBikeOperate::TYPE_ONLINE];
  828. $worker_bike_operate->reason = "工作人员操作上线";
  829. $last_location = object_array(json_decode($bike->last_location));
  830. $worker_bike_operate->bike_id = $bike_id;
  831. $worker_bike_operate->latitude = empty($last_location['lat']) ? 0 : $last_location['lat'];
  832. $worker_bike_operate->longitude = empty($last_location['lng']) ? 0 : $last_location['lng'];
  833. $worker_bike_operate->worker_id = Admin::user()->id;
  834. $worker_bike_operate->save();
  835. //更新redis
  836. $lastLocation = LocationsLog::getNewestLocationByBikeNo($bike->bike_no);
  837. (new BikeStatusInfoSyncHandler())->toBikeOnLineStatus($bike->bike_no, $lastLocation['lng'], $lastLocation['lat']);
  838. DB::commit();
  839. return $this->ok('操作成功');
  840. } catch (\Exception $e) {
  841. DB::rollBack();
  842. Log::error($e->getMessage());
  843. return $this->error('操作失败,请联系管理员');
  844. }
  845. }
  846. /**
  847. * bikeLogout 车辆下线(不投放)
  848. *
  849. * @param Request $request
  850. * @return \Illuminate\Http\JsonResponse
  851. * @author Fx
  852. *
  853. */
  854. public function bikeLogout(Request $request)
  855. {
  856. $bike_id = $request->get('bike_id') ?? '';
  857. if (empty($bike_id)) return $this->error('参数错误');
  858. try {
  859. DB::beginTransaction();
  860. $bike = Bike::query()->find($bike_id);
  861. if (empty($bike)) return $this->error('找不到车辆');
  862. if ($bike->put_status == Bike::PUT_STATUS_NO) return $this->error('车辆已下线');
  863. // 更新车辆下线
  864. $bike->put_status = Bike::PUT_STATUS_NO;
  865. $bike->save();
  866. // 插入下线记录
  867. $worker_bike_operate = new WorkerBikeOperate();
  868. $worker_bike_operate->type = WorkerBikeOperate::TYPE_OFFLINE;
  869. $worker_bike_operate->name = WorkerBikeOperate::$typeMaps[WorkerBikeOperate::TYPE_OFFLINE];
  870. $worker_bike_operate->reason = "工作人员操作下线";
  871. $last_location = object_array(json_decode($bike->last_location));
  872. $worker_bike_operate->bike_id = $bike_id;
  873. $worker_bike_operate->latitude = empty($last_location['lat']) ? 0 : $last_location['lat'];
  874. $worker_bike_operate->longitude = empty($last_location['lng']) ? 0 : $last_location['lng'];
  875. $worker_bike_operate->worker_id = Admin::user()->id;
  876. $worker_bike_operate->save();
  877. // 更新redis
  878. (new BikeStatusInfoSyncHandler())->toBikeOffLineStatus($bike->bike_no);
  879. DB::commit();
  880. return $this->ok('操作成功');
  881. } catch (\Exception $e) {
  882. DB::rollBack();
  883. Log::error($e->getMessage());
  884. return $this->error('操作失败,请联系管理员');
  885. }
  886. }
  887. /**
  888. * bikeBell 响铃
  889. *
  890. * @param Request $request
  891. * @return \Illuminate\Http\JsonResponse
  892. * @author Fx
  893. *
  894. */
  895. public function bikeBell(Request $request)
  896. {
  897. $bike_id = $request->get('bike_id') ?? '';
  898. if (empty($bike_id)) return $this->error('参数错误');
  899. $bike = Bike::query()->find($bike_id);
  900. if (empty($bike)) return $this->error('找不到车辆');
  901. // $box_no = "003448483"; // 测试写死
  902. $box_no = $bike->box_no;
  903. $bool = BikeControl::bellBike($box_no);
  904. if ($bool) {
  905. return $this->ok('操作成功,请寻找响铃车辆');
  906. } else {
  907. return $this->error('操作失败,请联系管理员');
  908. }
  909. }
  910. /**
  911. * openBikeLock 开锁
  912. *
  913. * @param Request $request
  914. * @return \Illuminate\Http\JsonResponse
  915. * @author Fx
  916. *
  917. */
  918. public function openBikeLock(Request $request)
  919. {
  920. $bike_id = $request->get('bike_id') ?? '';
  921. if (empty($bike_id)) return $this->error('参数错误');
  922. $bike = Bike::query()->find($bike_id);
  923. if (empty($bike)) return $this->error('找不到车辆');
  924. // $box_no = "003448483"; // 测试写死
  925. $box_no = $bike->box_no;
  926. try {
  927. // 增加worker操作车辆记录
  928. $worker_bike_operate = new WorkerBikeOperate();
  929. $worker_bike_operate->type = WorkerBikeOperate::TYPE_OPEN_BIKE_LOCK;
  930. $worker_bike_operate->name = WorkerBikeOperate::$typeMaps[WorkerBikeOperate::TYPE_OPEN_BIKE_LOCK];
  931. $worker_bike_operate->bike_id = $bike_id;
  932. $last_location = object_array(json_decode($bike->last_location));
  933. // Log::info($last_location);
  934. $worker_bike_operate->latitude = empty($last_location['lat']) ? 0 : $last_location['lat'];
  935. $worker_bike_operate->longitude = empty($last_location['lng']) ? 0 : $last_location['lng'];
  936. $worker_bike_operate->reason = "工作人员开锁";
  937. $worker_bike_operate->worker_id = Admin::user()->id;
  938. $worker_bike_operate->save();
  939. } catch (\Exception $e) {
  940. Log::error('开锁记录生成失败');
  941. return $this->error('开锁失败,请联系管理员');
  942. }
  943. $bool = BikeControl::openLock($box_no);
  944. if ($bool) {
  945. // DB::beginTransaction();
  946. try {
  947. $bike->is_lock = Bike::LOCK_NO;
  948. $bike->save();
  949. // 修改redis
  950. $is_riding = $bike->is_riding;
  951. if (!(bool)$is_riding) {
  952. (new BikeStatusInfoSyncHandler())->toBikeRideStatus(BikeStatusInfoSyncHandler::ROLE_WORKER, $bike->bike_no, ['id' => Admin::user()->id, 'area_id' => $bike->put_area_id, 'bike_id' => $bike_id]);
  953. }
  954. /* // 更新orders redis
  955. Redis::connection()->hset('ride_orders', $order->bike_no, serialize([
  956. 'id' => $order->id,
  957. 'bike_no' => $order->bike_no,
  958. 'bike_id' => $order->bike_id,
  959. 'area_id' => $order->area_id
  960. ]));
  961. DB::commit();*/
  962. } catch (\Exception $e) {
  963. // DB::rollBack();
  964. Log::error('车辆状态骑行更新失败');
  965. }
  966. return $this->ok('操作成功,请寻找响铃车辆');
  967. } else {
  968. return $this->error('开锁失败,请联系管理员');
  969. }
  970. }
  971. /**
  972. * closeBikeLock 关锁
  973. *
  974. * @param Request $request
  975. * @return \Illuminate\Http\JsonResponse
  976. * @author Fx
  977. *
  978. */
  979. public function closeBikeLock(Request $request)
  980. {
  981. $bike_id = $request->get('bike_id') ?? '';
  982. if (empty($bike_id)) return $this->error('参数错误');
  983. $bike = Bike::query()->find($bike_id);
  984. if (empty($bike)) return $this->error('找不到车辆');
  985. if ((bool)$bike->is_riding) {
  986. return $this->error('车辆骑行中,不可关锁');
  987. }
  988. try {
  989. // 增加worker操作车辆记录
  990. $worker_bike_operate = new WorkerBikeOperate();
  991. $worker_bike_operate->type = WorkerBikeOperate::TYPE_CLOSE_BIKE_LOCK;
  992. $worker_bike_operate->name = WorkerBikeOperate::$typeMaps[WorkerBikeOperate::TYPE_CLOSE_BIKE_LOCK];
  993. $worker_bike_operate->bike_id = $bike_id;
  994. $last_location = object_array(json_decode($bike->last_location));
  995. $worker_bike_operate->latitude = empty($last_location['lat']) ? 0 : $last_location['lat'];
  996. $worker_bike_operate->longitude = empty($last_location['lng']) ? 0 : $last_location['lng'];
  997. $worker_bike_operate->reason = "工作人员关锁";
  998. $worker_bike_operate->worker_id = Admin::user()->id;
  999. $worker_bike_operate->save();
  1000. } catch (\Exception $e) {
  1001. Log::error('关锁记录生成失败');
  1002. return $this->error('关锁失败,请联系管理员');
  1003. }
  1004. // $box_no = "003448483"; // 测试写死
  1005. $box_no = $bike->box_no;
  1006. //添加工作人员操作车辆记录
  1007. // 删除redis订单
  1008. $bike_location = LocationsLog::getNewestLocationByBikeNo($bike->bike_no);
  1009. (new BikeStatusInfoSyncHandler())->toBikeWaitRideStatus($bike->bike_no, $bike_location['lng'], $bike_location['lat'], $bike->put_status);
  1010. $bool = BikeControl::closeLock($box_no);
  1011. if ($bool) {
  1012. try {
  1013. // 车辆改为未骑行
  1014. // $bike->is_riding = Bike::RIDING_NO;
  1015. $bike->last_use_bike_end_time = Carbon::now();
  1016. $bike->is_lock = Bike::LOCK_YES;
  1017. $bike->save();
  1018. $lastLocation = LocationsLog::getNewestLocationByBikeNo($bike->bike_no);
  1019. // 修改redis
  1020. (new BikeStatusInfoSyncHandler())->toBikeWaitRideStatus($bike->bike_no, $lastLocation['lng'], $lastLocation['lat'], $bike->put_status);
  1021. } catch (\Exception $e) {
  1022. Log::error('车辆状态未骑行更新失败');
  1023. }
  1024. return $this->ok('操作成功');
  1025. } else {
  1026. return $this->error('操作失败,请联系管理员');
  1027. }
  1028. }
  1029. /**
  1030. * openBatteryLock 开电池锁
  1031. *
  1032. * @param Request $request
  1033. * @return \Illuminate\Http\JsonResponse
  1034. * @author Fx
  1035. *
  1036. */
  1037. public function openBatteryLock(Request $request)
  1038. {
  1039. $bike_id = $request->get('bike_id') ?? '';
  1040. if (empty($bike_id)) return $this->error('参数错误');
  1041. $bike = Bike::query()->find($bike_id);
  1042. if (empty($bike)) return $this->error('找不到车辆');
  1043. try {
  1044. // 增加worker操作车辆记录
  1045. $worker_bike_operate = new WorkerBikeOperate();
  1046. $worker_bike_operate->type = WorkerBikeOperate::TYPE_OPEN_BATTERY_LOCK;
  1047. $worker_bike_operate->name = WorkerBikeOperate::$typeMaps[WorkerBikeOperate::TYPE_OPEN_BATTERY_LOCK];
  1048. $worker_bike_operate->bike_id = $bike_id;
  1049. $last_location = object_array(json_decode($bike->last_location));
  1050. $worker_bike_operate->latitude = empty($last_location['lat']) ? 0 : $last_location['lat'];
  1051. $worker_bike_operate->longitude = empty($last_location['lng']) ? 0 : $last_location['lng'];
  1052. $worker_bike_operate->reason = "工作人员开电池锁";
  1053. $worker_bike_operate->worker_id = Admin::user()->id;
  1054. $worker_bike_operate->save();
  1055. } catch (\Exception $e) {
  1056. Log::error('开电池锁记录生成失败');
  1057. return $this->error('开电池锁失败,请联系管理员');
  1058. }
  1059. // $box_no = "003448483"; // 测试写死
  1060. $box_no = $bike->box_no;
  1061. $bool = BikeControl::openBatteryLock($box_no);
  1062. if ($bool) {
  1063. return $this->ok('操作开电池锁成功');
  1064. } else {
  1065. return $this->error('开电池锁失败,请联系管理员');
  1066. }
  1067. }
  1068. /**
  1069. * rebootBox 重启中控
  1070. *
  1071. * @param Request $request
  1072. * @return \Illuminate\Http\JsonResponse
  1073. * @author Fx
  1074. *
  1075. */
  1076. public function rebootBox(Request $request)
  1077. {
  1078. $bike_id = $request->get('bike_id') ?? '';
  1079. if (empty($bike_id)) return $this->error('参数错误');
  1080. $bike = Bike::query()->find($bike_id);
  1081. if (empty($bike)) return $this->error('找不到车辆');
  1082. try {
  1083. // 增加worker操作车辆记录
  1084. $worker_bike_operate = new WorkerBikeOperate();
  1085. $worker_bike_operate->type = WorkerBikeOperate::TYPE_REBOOT_BOX;
  1086. $worker_bike_operate->name = WorkerBikeOperate::$typeMaps[WorkerBikeOperate::TYPE_REBOOT_BOX];
  1087. $worker_bike_operate->bike_id = $bike_id;
  1088. $last_location = object_array(json_decode($bike->last_location));
  1089. $worker_bike_operate->latitude = empty($last_location['lat']) ? 0 : $last_location['lat'];
  1090. $worker_bike_operate->longitude = empty($last_location['lng']) ? 0 : $last_location['lng'];
  1091. $worker_bike_operate->reason = "工作人员重启中控";
  1092. $worker_bike_operate->worker_id = Admin::user()->id;
  1093. $worker_bike_operate->save();
  1094. } catch (\Exception $e) {
  1095. Log::error('工作人员重启中控失败');
  1096. return $this->error('工作人员重启中控失败,请联系管理员');
  1097. }
  1098. // $box_no = "003448483"; // 测试写死
  1099. $box_no = $bike->box_no;
  1100. $bool = BikeControl::rebootBox($box_no);
  1101. if ($bool) {
  1102. return $this->ok('重启中控成功');
  1103. } else {
  1104. return $this->error('重启中控失败,请联系管理员');
  1105. }
  1106. }
  1107. /**
  1108. * repairInParking 设为在停车区
  1109. *
  1110. * @param Request $request
  1111. * @return \Illuminate\Http\JsonResponse
  1112. * @author Fx
  1113. *
  1114. */
  1115. public function repairInParking(Request $request)
  1116. {
  1117. $bike_id = $request->get('bike_id') ?? '';
  1118. if (empty($bike_id)) return $this->error('参数错误');
  1119. $bike = Bike::query()->find($bike_id);
  1120. if (empty($bike)) return $this->error('找不到车辆');
  1121. try {
  1122. $bike->is_in_parking = Bike::IN_PARKING_YES;
  1123. $bike->save();
  1124. return $this->ok('操作成功');
  1125. } catch (\Exception $e) {
  1126. Log::error($e->getMessage());
  1127. return $this->error('操作失败,请联系管理员');
  1128. }
  1129. }
  1130. /**
  1131. * newBikeLocation 立即定位
  1132. *
  1133. * @param Request $request
  1134. * @return \Illuminate\Http\JsonResponse
  1135. * @author Fx
  1136. *
  1137. */
  1138. public function newBikeLocation(Request $request)
  1139. {
  1140. $bike_id = $request->get('bike_id') ?? '';
  1141. if (empty($bike_id)) return $this->error('参数错误');
  1142. $bike = Bike::query()->find($bike_id);
  1143. if (empty($bike)) return $this->error('找不到车辆');
  1144. try {
  1145. // 增加worker操作车辆记录
  1146. $worker_bike_operate = new WorkerBikeOperate();
  1147. $worker_bike_operate->type = WorkerBikeOperate::TYPE_NOW_BIKE_LOCATION;
  1148. $worker_bike_operate->name = WorkerBikeOperate::$typeMaps[WorkerBikeOperate::TYPE_NOW_BIKE_LOCATION];
  1149. $worker_bike_operate->bike_id = $bike_id;
  1150. $last_location = object_array(json_decode($bike->last_location));
  1151. $worker_bike_operate->latitude = empty($last_location['lat']) ? 0 : $last_location['lat'];
  1152. $worker_bike_operate->longitude = empty($last_location['lng']) ? 0 : $last_location['lng'];
  1153. $worker_bike_operate->reason = "工作人员立即定位车辆";
  1154. $worker_bike_operate->worker_id = Admin::user()->id;
  1155. $worker_bike_operate->save();
  1156. } catch (\Exception $e) {
  1157. Log::error('工作人员立即定位车辆失败');
  1158. return $this->error('工作人员立即定位车辆失败,请联系管理员');
  1159. }
  1160. // $box_no = "003448483"; // 测试写死
  1161. $box_no = $bike->box_no;
  1162. $bool = BikeControl::nowBikeLocation($box_no);
  1163. if ($bool) {
  1164. return $this->ok('立即定位车辆成功');
  1165. } else {
  1166. return $this->error('立即定位车辆失败,请联系管理员');
  1167. }
  1168. }
  1169. /**
  1170. * newBikeBatteryMSG 立即更新电池信息
  1171. *
  1172. * @param Request $request
  1173. * @return \Illuminate\Http\JsonResponse
  1174. * @author Fx
  1175. *
  1176. */
  1177. public function newBikeBatteryMSG(Request $request)
  1178. {
  1179. $bike_id = $request->get('bike_id') ?? '';
  1180. if (empty($bike_id)) return $this->error('参数错误');
  1181. $bike = Bike::query()->find($bike_id);
  1182. if (empty($bike)) return $this->error('找不到车辆');
  1183. try {
  1184. // 增加worker操作车辆记录
  1185. $worker_bike_operate = new WorkerBikeOperate();
  1186. $worker_bike_operate->type = WorkerBikeOperate::TYPE_NOW_BIKE_BATTERY_MSG;
  1187. $worker_bike_operate->name = WorkerBikeOperate::$typeMaps[WorkerBikeOperate::TYPE_NOW_BIKE_BATTERY_MSG];
  1188. $worker_bike_operate->bike_id = $bike_id;
  1189. $last_location = object_array(json_decode($bike->last_location));
  1190. $worker_bike_operate->latitude = empty($last_location['lat']) ? 0 : $last_location['lat'];
  1191. $worker_bike_operate->longitude = empty($last_location['lng']) ? 0 : $last_location['lng'];
  1192. $worker_bike_operate->reason = "工作人员立即更新电池信息";
  1193. $worker_bike_operate->worker_id = Admin::user()->id;
  1194. $worker_bike_operate->save();
  1195. } catch (\Exception $e) {
  1196. Log::error('工作人员立即更新电池信息失败');
  1197. return $this->error('工作人员立即更新电池信息失败,请联系管理员');
  1198. }
  1199. // $box_no = "003448483"; // 测试写死
  1200. $box_no = $bike->box_no;
  1201. $bool = BikeControl::nowBikeBatteryMSG($box_no);
  1202. if ($bool) {
  1203. return $this->ok('立即更新电池信息成功');
  1204. } else {
  1205. return $this->error('立即更新电池信息失败,请联系管理员');
  1206. }
  1207. }
  1208. /**
  1209. * closeBatteryLock 关电池锁
  1210. *
  1211. * @param Request $request
  1212. * @return \Illuminate\Http\JsonResponse
  1213. * @author Fx
  1214. *
  1215. */
  1216. public function closeBatteryLock(Request $request)
  1217. {
  1218. $bike_id = $request->get('bike_id') ?? '';
  1219. if (empty($bike_id)) return $this->error('参数错误');
  1220. $bike = Bike::query()->find($bike_id);
  1221. if (empty($bike)) return $this->error('找不到车辆');
  1222. try {
  1223. // 增加worker操作车辆记录
  1224. $worker_bike_operate = new WorkerBikeOperate();
  1225. $worker_bike_operate->type = WorkerBikeOperate::TYPE_CLOSE_BATTERY_LOCK;
  1226. $worker_bike_operate->name = WorkerBikeOperate::$typeMaps[WorkerBikeOperate::TYPE_CLOSE_BATTERY_LOCK];
  1227. $worker_bike_operate->bike_id = $bike_id;
  1228. $last_location = object_array(json_decode($bike->last_location));
  1229. $worker_bike_operate->latitude = empty($last_location['lat']) ? 0 : $last_location['lat'];
  1230. $worker_bike_operate->longitude = empty($last_location['lng']) ? 0 : $last_location['lng'];
  1231. $worker_bike_operate->reason = "工作人员开电池锁";
  1232. $worker_bike_operate->worker_id = Admin::user()->id;
  1233. $worker_bike_operate->save();
  1234. } catch (\Exception $e) {
  1235. Log::error('关电池锁记录生成失败');
  1236. return $this->error('关电池锁失败,请联系管理员');
  1237. }
  1238. // $box_no = "003448483"; // 测试写死
  1239. $box_no = $bike->box_no;
  1240. $bool = BikeControl::closeBatteryLock($box_no);
  1241. if ($bool) {
  1242. return $this->ok('操作关电池锁成功');
  1243. } else {
  1244. return $this->error('关电池锁失败,请联系管理员');
  1245. }
  1246. }
  1247. /**
  1248. * getKey 获取蓝牙密钥
  1249. *
  1250. * @param Request $request
  1251. * @return \Illuminate\Http\JsonResponse
  1252. * @author Fx
  1253. *
  1254. */
  1255. public function getKey(Request $request)
  1256. {
  1257. $box_no = $request->get('box_no');
  1258. $bike = Bike::where('box_no', $box_no)->first();
  1259. $key = $bike->blu_ase_key ?? '';
  1260. return $this->ok(['key' => $key]);
  1261. }
  1262. }