123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563 |
- <?php
- namespace App\Http\Controllers\App;
- use App\Filters\BikeFilter;
- use App\Handlers\Aes128Handler;
- use App\Handlers\BaseBikeControl;
- use App\Handlers\BikeStatusInfoSyncHandler;
- use App\Http\Requests\RemarkRequest;
- 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\Jobs\AutoCloseBikeLockJob;
- use App\Models\AdminMerchant;
- use App\Models\AdminRole;
- 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)->where(AdminMerchant::getMerchantWhere());
- if (!Admin::isAdministrator()) {
- // if (!Admin::isAdministrator() && !Admin::isNormalAdministrator()) {
- $bikeInfo = $bikeInfo->whereIn('put_area_id', self::$areaIds);
- }
- $bikeInfo = $bikeInfo->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; //最后定位时间
- $data['bike_info']['remark'] = $bikeInfo->remark ?? ''; //最后定位时间
- //最近订单 取最
- $order = Order::query()->where(AdminMerchant::getMerchantWhere())->where('bike_id', $bikeInfo->id)->orderByDesc('id')->with('users')->first();
- $orderRent = OrderRent::query()->where(AdminMerchant::getMerchantWhere())->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() and !Admin::user()->isRole(AdminRole::MERCHANTSADMIN)) {
- return $this->error('暂无权限');
- }
- $bike_id = $request->get('bike_id') ?? '';
- if (empty($bike_id)) return $this->error('参数错误');
- $bike = Bike::query()->where(AdminMerchant::getMerchantWhere())->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(AdminMerchant::getMerchantWhere())->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->remark = $bike_remarks . $date;
- }
- if (!empty($box_no)) {
- $verifyBikeNo = Bike::query()->where(AdminMerchant::getMerchantWhere())->where('box_no', $box_no)->first();
- if (!empty($verifyBikeNo)) {
- if ($verifyBikeNo->id !== $bike->id) {
- return $this->error('中控编号已存在,不可修改');
- }
- }
- // 中控绑定操作
- $box = BoxBinding::query()->where(AdminMerchant::getMerchantWhere())->where('box_no', $box_no)->first();
- if (empty($box)) return $this->error('找不到此设备信息,请联系管理员');
- if ($box->is_binding == BoxBinding::BINDING_YES) return $this->error('此设备已经绑定过');
- $blu_key = $blu_ase_key = '';
- switch ((int)$box->manufacturer) {
- case BoxBinding::TBT:
- $blu_key = config('systemConfig.blu_key');
- try {
- $blu_ase_key = Aes128Handler::genKey($blu_key, $box_no);
- } catch (\Exception $e) {
- return $this->error($e->getMessage());
- }
- break;
- case BoxBinding::WKM:
- $blu_key = $box->bluetooth_token;
- $blu_ase_key = $box->bluetooth_token;
- break;
- }
- $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('box_time', '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[] = [
- 'points' => $item,
- 'role' => LocationsLog::USER_LOCATION,
- 'borderColor' => '#18D3B8',
- 'borderWidth' => '3',
- 'color' => '#18D3B8',
- 'width' => '4'];
- } else if ($old_type == LocationsLog::WORKER_LOCATION) {
- $workerLocation[] = $item;
- $Locations[] = [
- 'role' => LocationsLog::WORKER_LOCATION,
- 'points' => $item,
- 'borderColor' => '#ff6477',
- 'borderWidth' => '3',
- 'color' => '#ff6477',
- 'width' => '4'
- ];
- } else {
- $noLocatioin[] = $item;
- $Locations[] = [
- 'points' => $item,
- 'borderColor' => '#d684dd',
- 'borderWidth' => '3',
- 'color' => '#d684dd',
- 'width' => '4',
- '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();
- $start_point = [
- 'zIndex' => 100,
- 'id' => 'start',
- 'width' => 32,
- 'height' => 32,
- 'iconPath' => 'http://resource.bike.hanyiyun.com/common/start-location-mark.png',
- ];
- $end_point = [
- 'zIndex' => 100,
- 'id' => 'end',
- 'width' => 32,
- 'height' => 32,
- 'iconPath' => 'http://resource.bike.hanyiyun.com/common/end-location-mark.png',
- ];
- $res = [
- 'points' => [array_merge($wx_data[0], $start_point), array_merge(end($wx_data), $end_point)],
- '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(AdminMerchant::getMerchantWhere())
- ->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(AdminMerchant::getMerchantWhere())
- ->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));
- }
- public function updateRemark(RemarkRequest $request, $id)
- {
- //
- $bike = Bike::find($id);
- $inputs = $request->validated();
- // Log::info($id);
- $bike->update($inputs);
- return $this->ok('修改成功');
- }
- /**
- * 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(AdminMerchant::getMerchantWhere())
- ->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(AdminMerchant::getMerchantWhere())
- ->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()
- ->where(AdminMerchant::getMerchantWhere())
- ->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()
- ->where(AdminMerchant::getMerchantWhere())
- ->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(AdminMerchant::getMerchantWhere())
- ->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(AdminMerchant::getMerchantWhere())->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;
- $worker_bike_operate->merchant_id =AdminMerchant::putMerchantId();
- // 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,
- 'merchant_id' =>AdminMerchant::putMerchantId()
- ];
- $work_order_verify = [
- 'bike_id' => $bike->id,
- 'type' => $trouble_part, // 工单类型
- 'status' => WorkOrder::STATUS_NO,
- 'merchant_id' =>AdminMerchant::putMerchantId()
- ];
- 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(AdminMerchant::getMerchantWhere())->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->merchant_id = AdminMerchant::putMerchantId();
- $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,
- 'merchant_id' =>AdminMerchant::putMerchantId()
- ];
- $work_order_verify = [
- 'bike_id' => $bike->id,
- 'type' => $trouble_part,
- 'status' => WorkOrder::STATUS_NO,
- 'merchant_id' =>AdminMerchant::putMerchantId()
- ];
- 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()
- ->where(AdminMerchant::getMerchantWhere())
- ->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()
- ->where(AdminMerchant::getMerchantWhere())
- ->whereIn('put_area_id', $area_ids)
- ->filter($filter)
- ->count('id');
- // 上线车辆数
- $data['put_total_bikes'] = Bike::query()
- ->whereIn('put_area_id', $area_ids)
- ->where(AdminMerchant::getMerchantWhere())
- ->filter($filter)
- ->where('put_status', Bike::PUT_STATUS_YES)
- ->count('id');
- // 在线车辆数
- $data['line_total_bikes'] = Bike::query()
- ->where(AdminMerchant::getMerchantWhere())
- ->whereIn('put_area_id', $area_ids)
- ->filter($filter)
- ->where('is_link', Bike::LINK_ONLINE)
- ->count('id');
- //故障车辆
- $data['trouble_total_bikes'] = Bike::query()
- ->where(AdminMerchant::getMerchantWhere())
- ->whereIn('put_area_id', $area_ids)
- ->filter($filter)
- ->where('is_trouble', Bike::TROUBLE_YES)
- ->count('id');
- //今日使用
- $data['today_use_total_bikes'] = Bike::query()
- ->where(AdminMerchant::getMerchantWhere())
- ->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()
- ->where(AdminMerchant::getMerchantWhere())
- ->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()
- ->where(AdminMerchant::getMerchantWhere())
- ->whereIn('put_area_id', $area_ids)
- ->filter($filter)
- ->where('is_riding', Bike::RIDING_YES)
- ->count('id');
- //未使用
- $data['not_riding_total_bikes'] = Bike::query()
- ->where(AdminMerchant::getMerchantWhere())
- ->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()->where(AdminMerchant::getMerchantWhere())->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->merchant_id =AdminMerchant::putMerchantId();
- $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->merchant_id =AdminMerchant::putMerchantId();
- $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()->where(AdminMerchant::getMerchantWhere())->where('id',$bike_id)->first();
- if (empty($bike)) return $this->error('找不到车辆');
- // $box_no = "003448483"; // 测试写死
- $box_no = $bike->box_no;
- $bool = (new BaseBikeControl($box_no))::bellBike();
- 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->merchant_id = AdminMerchant::getMerchantWhere();
- $worker_bike_operate->save();
- } catch (\Exception $e) {
- Log::error('开锁记录生成失败');
- return $this->error('开锁失败,请联系管理员');
- }
- $bool = (new BaseBikeControl($box_no))::openLock();
- if ($bool) {
- // DB::beginTransaction();
- try {
- $bike->is_lock = Bike::LOCK_NO;
- $bike->save();
- AutoCloseBikeLockJob::dispatch($bike)->delay(Carbon::now()->addMinutes(config('queue.delay.autoCloseBikeLock')));
- // 修改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->merchant_id = AdminMerchant::putMerchantId();
- $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 = (new BaseBikeControl($box_no))::closeLock();
- 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()->where(AdminMerchant::getMerchantWhere())->where('id',$bike_id)->first();
- 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->merchant_id = AdminMerchant::putMerchantId();
- $worker_bike_operate->save();
- } catch (\Exception $e) {
- Log::error('开电池锁记录生成失败');
- return $this->error('开电池锁失败,请联系管理员');
- }
- // $box_no = "003448483"; // 测试写死
- $box_no = $bike->box_no;
- $bool = (new BaseBikeControl($box_no))::openBatteryLock();
- 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);
- $bike = Bike::query()->where(AdminMerchant::getMerchantWhere())->where('id',$bike_id)->first();
- 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->merchant_id = AdminMerchant::putMerchantId();
- $worker_bike_operate->save();
- } catch (\Exception $e) {
- Log::error('工作人员重启中控失败');
- return $this->error('工作人员重启中控失败,请联系管理员');
- }
- // $box_no = "003448483"; // 测试写死
- $box_no = $bike->box_no;
- $bool = (new BaseBikeControl($box_no))::rebootBox();
- 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);
- $bike = Bike::query()->where(AdminMerchant::getMerchantWhere())->where('id',$bike_id)->first();
- 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);
- $bike = Bike::query()->where(AdminMerchant::getMerchantWhere())->where('id',$bike_id)->first();
- 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->merchant_id = AdminMerchant::putMerchantId();
- $worker_bike_operate->save();
- } catch (\Exception $e) {
- Log::error('工作人员立即定位车辆失败');
- return $this->error('工作人员立即定位车辆失败,请联系管理员');
- }
- // $box_no = "003448483"; // 测试写死
- $box_no = $bike->box_no;
- $bool = (new BaseBikeControl($box_no))::nowBikeLocation();
- 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);
- $bike = Bike::query()->where(AdminMerchant::getMerchantWhere())->where('id',$bike_id)->first();
- 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->merchant_id = AdminMerchant::putMerchantId();
- $worker_bike_operate->save();
- } catch (\Exception $e) {
- Log::error('工作人员立即更新电池信息失败');
- return $this->error('工作人员立即更新电池信息失败,请联系管理员');
- }
- // $box_no = "003448483"; // 测试写死
- $box_no = $bike->box_no;
- $bool = (new BaseBikeControl($box_no))::nowBikeBatteryMSG();
- 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);
- $bike = Bike::query()->where(AdminMerchant::getMerchantWhere())->where('id',$bike_id)->first();
- 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->merchant_id = AdminMerchant::putMerchantId();
- $worker_bike_operate->save();
- } catch (\Exception $e) {
- Log::error('关电池锁记录生成失败');
- return $this->error('关电池锁失败,请联系管理员');
- }
- // $box_no = "003448483"; // 测试写死
- $box_no = $bike->box_no;
- $bool = (new BaseBikeControl($box_no))::closeBatteryLock();
- 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)->where(AdminMerchant::getMerchantWhere())->first();
- $key = $bike->blu_ase_key ?? '';
- return $this->ok(['key' => $key]);
- }
- /**
- * orderLocation 订单轨迹
- *
- * @param Request $request
- * @return \Illuminate\Http\JsonResponse
- * @author Fx
- *
- */
- public function getLocationByBikeNoAndTime(Request $request)
- {
- // $order_id = $request->get('order_id') ?? '';
- $bike_no = $request->get('bike_no') ?? '';
- $time_between = $request->get('time_between') ?? []; // time_between 时间区间
- if (empty($bike_no) || empty($time_between)) return $this->error('参数错误');
- // if (empty($order_id)) return $this->error('参数错误');
- $orderLocations = [];
- $locationsTimes = [];
- try {
- $locationsLog = 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();
- // $locationsLog = LocationsLog::query()
- // ->where('order_id', (int)$order_id)
- // ->where('is_rent', LocationsLog::RENT_NO)
- // ->whereBetween('latitude', [3, 53])
- // ->whereBetween('longitude', [73, 136])
- // ->orderBy('created_at', 'asc')->get();
- } catch (\Exception $exception) {
- Log::info($exception->getMessage());
- }
- if (!empty($locationsLog)) {
- // foreach ($locationsLog as $vv) {
- // $orderLocations[] = [$vv->longitude, $vv->latitude];
- // $locationsTimes[] = Carbon::parse($vv->created_at)->format('Y-m-d H:i:s');
- // }
- $data = [];
- $dataTimes = [];
- $userLocation = [];
- $workerLocation = [];
- $noLocatioin = [];
- $old_type = '';
- $item = [];
- $timesData = [];
- $Locations = [];
- foreach ($locationsLog as $v) {
- $data[] = [$v->longitude, $v->latitude];
- $dataTimes[] = Carbon::parse($v->created_at)->format('Y-m-d H:i:s');
- if (empty($old_type)) {
- $old_type = $v->type;
- }
- if ($v->type !== $old_type) {
- if ($old_type == LocationsLog::USER_LOCATION) {
- $userLocation[] = ['locations' => $item, 'times' => $timesData];
- $Locations[] = ['data' => ['locations' => $item, 'times' => $timesData], 'role' => LocationsLog::USER_LOCATION];
- } else if ($old_type == LocationsLog::WORKER_LOCATION) {
- $workerLocation[] = ['locations' => $item, 'times' => $timesData];
- $Locations[] = ['data' => ['locations' => $item, 'times' => $timesData], 'role' => LocationsLog::WORKER_LOCATION];
- } else {
- $noLocatioin[] = ['locations' => $item, 'times' => $timesData];
- $Locations[] = ['data' => ['locations' => $item, 'times' => $timesData], 'role' => LocationsLog::NO_LOCATION];
- }
- $old_type = '';
- $item = [];
- $timesData = [];
- $item[] = [$v->longitude, $v->latitude];
- $timesData[] = Carbon::parse($v->created_at)->format('Y-m-d H:i:s');
- } else {
- $item[] = [$v->longitude, $v->latitude];
- $timesData[] = Carbon::parse($v->created_at)->format('Y-m-d H:i:s');
- }
- unset($v->_id);
- }
- }
- if (empty($data)) return $this->ok($data);
- $wx_data = $locationsLog->toArray();
- $res = [
- 'locations' => $data,
- 'locationsTimes' => $dataTimes,
- '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);
- }
- }
|