OrderController.php 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690
  1. <?php
  2. namespace App\Http\Controllers\Admin;
  3. use App\Filters\OrderFilter;
  4. use App\Handlers\BaseBikeControl;
  5. use App\Handlers\BikeStatusInfoSyncHandler;
  6. use App\Handlers\OrderHandler;
  7. use App\Http\Requests\OrderRequest;
  8. use App\Http\Resources\OrderResource;
  9. use App\Models\AdminMerchant;
  10. use App\Models\AdminUser;
  11. use App\Models\AdminUserArea;
  12. use App\Models\Area;
  13. use App\Models\AreaSetting;
  14. use App\Models\Bike;
  15. use App\Models\CardRidingUseLog;
  16. use App\Models\CardRidingUserBags;
  17. use App\Models\LocationsLog;
  18. use App\Models\Order;
  19. use App\Models\OrderBikeOperate;
  20. use App\Models\OrderRent;
  21. use App\Models\User;
  22. use App\Models\WalletLog;
  23. use App\Notifications\OrderNoPayNotification;
  24. use App\Notifications\OrderRefundNotification;
  25. use App\Utils\Admin;
  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\Notification;
  32. class OrderController extends Controller
  33. {
  34. /**
  35. * index 订单列表
  36. *
  37. * @param Request $request
  38. * @param OrderFilter $filter
  39. * @return \Illuminate\Http\JsonResponse
  40. * @author Fx
  41. *
  42. */
  43. public function index(Request $request, OrderFilter $filter)
  44. {
  45. $admin_id = Admin::user()->id;
  46. $orders = Order::query();
  47. $status = $request->get('status') ?? '';
  48. if (empty($status)) {
  49. $orders = $orders->where('status', '!=', Order::STATUS_CLOSE_ORDER);
  50. }
  51. $orders = $orders
  52. ->filter($filter)
  53. ->where(AdminMerchant::getMerchantWhere())
  54. ->orderByDesc('id')
  55. ->orderBy('status');
  56. if (!Admin::isAdministrator()) {
  57. $area_ids = AdminUser::getAreaIdsByAdminId($admin_id);
  58. if (count($area_ids) !== 0) {
  59. $orders = $orders->whereIn('area_id', $area_ids);
  60. } else {
  61. $area_id = AdminUserArea::query()->where('admin_id', $admin_id)->first('area_id');
  62. $area_id = $area_id->area_id ?? 0;
  63. $orders = $orders->where('area_id', $area_id);
  64. }
  65. }
  66. $orders = $request->get('all') ? $orders->get() : $orders->paginate();
  67. return $this->ok(OrderResource::collection($orders));
  68. }
  69. /**
  70. * update 更新订单
  71. *
  72. * @param OrderRequest $request
  73. * @param Order $order
  74. * @return \Illuminate\Http\JsonResponse
  75. * @author Fx
  76. *
  77. */
  78. public function update(OrderRequest $request, Order $order)
  79. {
  80. $inputs = $request->validated();
  81. $order->update($inputs);
  82. return $this->ok();
  83. }
  84. /**
  85. * orderLocationsearch 订单轨迹 订单列表
  86. *
  87. * @param Request $request
  88. * @param OrderFilter $filter
  89. * @return \Illuminate\Http\JsonResponse
  90. * @author Fx
  91. *
  92. */
  93. public function orderLocationsearch(Request $request, OrderFilter $filter)
  94. {
  95. $admin_id = Admin::user()->id;
  96. $orders = Order::query();
  97. $status = $request->get('status') ?? '';
  98. if (empty($status)) {
  99. $orders = $orders->where('status', '!=', Order::STATUS_CLOSE_ORDER);
  100. }
  101. $orders = $orders
  102. ->filter($filter)
  103. ->where(AdminMerchant::getMerchantWhere())
  104. ->orderByDesc('id')
  105. ->orderBy('status');
  106. if (!Admin::isAdministrator()) {
  107. $area_ids = AdminUser::getAreaIdsByAdminId($admin_id);
  108. if (count($area_ids) !== 0) {
  109. $orders = $orders->whereIn('area_id', $area_ids);
  110. } else {
  111. $area_id = AdminUserArea::query()->where('admin_id', $admin_id)->first('area_id');
  112. $area_id = $area_id->area_id ?? 0;
  113. $orders = $orders->where('area_id', $area_id);
  114. }
  115. }
  116. $orders = $orders->limit(100)->get();
  117. return $this->ok(OrderResource::collection($orders));
  118. }
  119. /**
  120. * orderLocation 订单轨迹
  121. *
  122. * @param Request $request
  123. * @return \Illuminate\Http\JsonResponse
  124. * @author Fx
  125. *
  126. */
  127. public function orderLocation(Request $request)
  128. {
  129. $order_id = $request->get('order_id') ?? '';
  130. if (empty($order_id)) return $this->error('参数错误');
  131. $orderLocations = [];
  132. $locationsTimes = [];
  133. $dataRoles = [];
  134. try {
  135. $locationsLog = LocationsLog::query()->where('order_id', (int)$order_id)->where('is_rent', LocationsLog::RENT_NO)->whereBetween('latitude', [3, 53])->whereBetween('longitude', [73, 136])->orderBy('box_time', 'asc')->get();
  136. } catch (\Exception $exception) {
  137. Log::info($exception->getMessage());
  138. }
  139. if (!empty($locationsLog)) {
  140. foreach ($locationsLog as $vv) {
  141. if (!empty($vv)) {
  142. $orderLocations[] = [$vv->longitude, $vv->latitude];
  143. $dataRoles[] = $vv->type;
  144. $locationsTimes[] = Carbon::parse($vv->box_time)->format('Y-m-d H:i:s');
  145. }
  146. }
  147. }
  148. $data = [
  149. 'orderLocations' => $orderLocations,
  150. 'locationsTimes' => $locationsTimes,
  151. 'locations_role' => $dataRoles,
  152. ];
  153. return $this->ok($data);
  154. }
  155. /**
  156. * orderStatus 订单状态 订单操作状态
  157. *
  158. * @return \Illuminate\Http\JsonResponse
  159. * @author Fx
  160. *
  161. */
  162. public function orderStatus()
  163. {
  164. $orderStatus = [];
  165. foreach (Order::$statusMaps as $k => $v) {
  166. $arr = [];
  167. $arr['id'] = $k;
  168. $arr['name'] = $v;
  169. $orderStatus[] = $arr;
  170. }
  171. $data['order_status'] = $orderStatus;//订单状态
  172. $orderOperateStatus = [];
  173. foreach (OrderBikeOperate::$typeMaps as $k => $v) {
  174. $arr1 = [];
  175. $arr1['id'] = $k;
  176. $arr1['name'] = $v;
  177. $orderOperateStatus[] = $arr1;
  178. }
  179. $data['order_operate_status'] = $orderOperateStatus;//订单操作状态
  180. return $this->ok($data);
  181. }
  182. /**
  183. * orderBikeOperate 订单操作自行车详情
  184. *
  185. * @param Request $request
  186. * @return \Illuminate\Http\JsonResponse
  187. * @author Fx
  188. *
  189. */
  190. public function orderBikeOperate(Request $request)
  191. {
  192. $order_id = $request->get('order_id');
  193. if (empty($order_id)) return $this->error('参数错误');
  194. $data = OrderBikeOperate::query()
  195. ->where('order_id', $order_id)
  196. ->select(['created_at', 'name'])
  197. ->get();
  198. $data2 = Order::find($order_id)->walletLogs;
  199. return $this->ok(['orderBikeOperate' => $data, 'walletLog' => $data2]);
  200. }
  201. /**
  202. * updateOrderCloseBike 修改订单为待支付
  203. *
  204. * @param Request $request
  205. * @return \Illuminate\Http\JsonResponse
  206. * @author Fx
  207. *
  208. */
  209. public function updateOrderCloseBike(Request $request)
  210. {
  211. $order_id = $request->get('order_id');
  212. if (empty($order_id)) return $this->error('参数错误');
  213. $closeOrderBool = OrderHandler::closeOrder($order_id);
  214. if (!$closeOrderBool) return $this->error('操作失败,请联系管理员');
  215. $order = Order::find($order_id);
  216. $user = User::find($order->user_id);
  217. if (!empty($order)) {
  218. Notification::send($user, new OrderNoPayNotification($user, $order));
  219. }
  220. return $this->ok('关锁成功');
  221. }
  222. /**
  223. * settlementOrder 结算订单
  224. *
  225. * @param Request $request
  226. * @return \Illuminate\Http\JsonResponse
  227. * @author Fx
  228. *
  229. */
  230. public function settlementOrder(Request $request)
  231. {
  232. $order_id = $request->get('order_id');
  233. if (empty($order_id)) return $this->error('参数错误');
  234. $ridding_money = $request->get('ridding_money') ?? 0; //骑行费
  235. $dispatch_money = $request->get('dispatch_money') ?? 0; //调度费
  236. $pay_money = $request->get('pay_money') ?? 0;
  237. $pay_money = ($pay_money < 0) ? 0 : $pay_money;
  238. $ridding_money = ($ridding_money < 0) ? 0 : $ridding_money;
  239. $dispatch_money = ($pay_money < 0) ? 0 : $dispatch_money;
  240. $order = Order::query()->find($order_id);
  241. if (empty($order)) return $this->error('找不到订单信息');
  242. $user_id = $order->user_id;
  243. $area_id = $order->area_id;
  244. if ($order->status == Order::STATUS_CLOSE_BIKE) {
  245. $pay_money = bcadd($dispatch_money, $ridding_money, 2) < 0 ? 0 : bcadd($dispatch_money, $ridding_money, 2);
  246. //1.判断用户钱包余额是否大于支付金额
  247. $user = User::query()->find($user_id);
  248. if (empty($user)) return $this->error('出现错误,请联系管理员');
  249. $wallet_money = $user->wallet_money;
  250. $result = bcsub($wallet_money, $pay_money, 2);
  251. $order->pay_type = Order::PAY_TYPE_ACCOUNT;
  252. try {
  253. DB::beginTransaction();
  254. // 修改调度费 各种费用
  255. $admin_name = Admin::user()->name;
  256. $order->dispatch_money = (float)$dispatch_money;
  257. $order->time_money = (float)$ridding_money;
  258. $order->order_money = $pay_money;
  259. $order->pay_money = $order->order_money;
  260. $order->is_admin_settle_order = Order::ADMIN_SETTLE_ORDER_ADMIN;
  261. $order->total_money = bcadd($ridding_money, $dispatch_money, 2);
  262. // 每次后台修改订单金额 需要回溯骑行卡 前边(api)重新使用 重新计算
  263. OrderHandler::cardRidingHuSu($order->id);
  264. if (bccomp($order->order_money, 0) === 0) {
  265. $order->pay_type = Order::PAY_STATUS_OK;
  266. $order->status = Order::STATUS_COMPLETE_ORDER;
  267. }
  268. $order->save();
  269. //给订单车辆操作表添加一条新纪录
  270. $orderBikeOperate = new OrderBikeOperate();
  271. $orderBikeOperate->name = OrderBikeOperate::$typeMaps[OrderBikeOperate::TYPE_ADMIN_SETTLRMENT] . '(' . $admin_name . '),修改骑行费为' . $ridding_money . ',调度费为' . $dispatch_money;
  272. $orderBikeOperate->type = OrderBikeOperate::TYPE_ADMIN_SETTLRMENT;
  273. $orderBikeOperate->bike_id = $order->bike_id;
  274. $orderBikeOperate->order_id = $order_id;
  275. $orderBikeOperate->user_id = $order->user_id;
  276. $orderBikeOperate->is_admin = OrderBikeOperate::IS_ADMIN_YES;
  277. $orderBikeOperate->admin_id = Admin::user()->id;
  278. $orderBikeOperate->save();
  279. $user = User::find($order->user_id);
  280. if (!empty($order)) {
  281. Notification::send($user, new OrderNoPayNotification($user, $order));
  282. }
  283. DB::commit();
  284. return $this->ok('费用已调整');
  285. } catch (\Exception $e) {
  286. DB::rollBack();
  287. Log::info($e->getMessage());
  288. return $this->error('操作失败请联系管理员');
  289. }
  290. } else {
  291. return $this->error('车辆未关锁,请刷新后重试');
  292. }
  293. }
  294. /**
  295. * orderReturnMoney 订单返还
  296. *
  297. * @param Request $request
  298. * @return \Illuminate\Http\JsonResponse
  299. * @author Fx
  300. *
  301. */
  302. public function orderReturnMoney(Request $request)
  303. {
  304. $order_id = $request->get('order_id');
  305. $return_type = $request->get('return_type') ?? '';
  306. if ($return_type !== 'wallet' && $return_type !== 'wechat') return $this->error('返还类型错误');
  307. if (empty($order_id) || empty($return_type)) return $this->error('参数错误');
  308. $return_ridding_money = $request->get('ridding_money') ?? 0;
  309. $return_dispatch_money = $request->get('dispatch_money') ?? 0;
  310. if ($return_ridding_money == 0 && $return_dispatch_money == 0) {
  311. return $this->error('返还金额为0,请填写返还金额');
  312. }
  313. $order = Order::query()->find($order_id);
  314. if (empty($order)) return $this->error('找不到该订单,请联系管理员');
  315. if ($order->is_refund_money == Order::REFUND_MONEY_OK) {
  316. return $this->error('此订单已返还过了 ');
  317. }
  318. // 大于24 * 5小时 不能返还
  319. $order_create_time = Carbon::now()->diffInHours($order->created_at);
  320. if (abs($order_create_time) > 24 * 5) {
  321. return $this->error('返还已过期,不能进行返还操作');
  322. }
  323. $user_id = $order->user_id;
  324. $area_id = $order->area_id;
  325. $dispatch_money = $order->dispatch_money; //订单调度费
  326. $time_money = $order->time_money; //订单骑行费
  327. $pay_money = $order->pay_money; //订单实际支付费用
  328. if ((float)$dispatch_money - (float)$return_dispatch_money < 0) {
  329. return $this->error('返还调度费不能大于' . $dispatch_money);
  330. }
  331. if ((float)$time_money - (float)$return_ridding_money < 0) {
  332. return $this->error('返还骑行费不能大于' . $time_money);
  333. }
  334. $return_money = bcadd($return_dispatch_money, $return_ridding_money, 2);
  335. if ((float)$pay_money - (float)$return_money < 0) {
  336. return $this->error('返还金额不能大于' . $pay_money);
  337. }
  338. DB::beginTransaction();
  339. try {
  340. $old_pay_money = $order->pay_money;
  341. //1.更新订单
  342. $order->dispatch_money = bcsub($dispatch_money, $return_dispatch_money, 2);
  343. $order->time_money = bcsub($time_money, $return_ridding_money, 2);
  344. $order->pay_money = bcsub($pay_money, $return_money, 2);
  345. $order->total_money = bcadd($order->time_money, $order->dispatch_money, 2);
  346. $order->is_refund_money = Order::REFUND_MONEY_OK;
  347. $res1 = $order->save();
  348. OrderBikeOperate::logs(
  349. OrderBikeOperate::$typeMaps[OrderBikeOperate::TYPE_ADMIN_ORDER_REFUND] . '(' . Admin::user()->name . ')',
  350. OrderBikeOperate::TYPE_ADMIN_ORDER_REFUND,
  351. $order->bike_id,
  352. $order->id,
  353. $order->user_id,
  354. OrderBikeOperate::IS_ADMIN_YES,
  355. Admin::user()->id
  356. );
  357. if (!$res1) {
  358. DB::rollBack();
  359. Log::error('更新订单失败');
  360. return $this->error('操作失败请联系管理员');
  361. }
  362. //2.判断返还类别 更新用户余额 或微信退款
  363. if ($return_type === 'wallet') {
  364. $return_type_name = '退平台钱包';
  365. //3.插入钱包记录
  366. $wallet_log = new WalletLog();
  367. $wallet_log->name = WalletLog::$typeMaps[WalletLog::TYPE_ADMIN_ADD_TO_WALLET];
  368. $wallet_log->type = WalletLog::TYPE_ADMIN_ADD_TO_WALLET;
  369. $wallet_log->operate_type = WalletLog::OPERATE_TYPE_ADD;
  370. $wallet_log->money = (float)$return_money;
  371. $wallet_log->user_id = $user_id;
  372. $wallet_log->area_id = $area_id;
  373. $wallet_log->log_id = $order_id;
  374. $wallet_log->log_type = Order::class;
  375. $res2 = $wallet_log->save();
  376. if (!$res2) {
  377. DB::rollBack();
  378. Log::error('插入钱包记录失败');
  379. return $this->error('操作失败请联系管理员');
  380. }
  381. // 4.退余额
  382. $user = User::query()->find($user_id);
  383. $wallet_money = $user->wallet_money;
  384. $user->wallet_money = (float)$wallet_money + (float)$return_money;
  385. $res3 = $user->save();
  386. if (!$res3) {
  387. DB::rollBack();
  388. Log::error('更新用户余额失败');
  389. return $this->error('操作失败请联系管理员');
  390. }
  391. } else if ($return_type === 'wechat') {
  392. $return_type_name = '原路退回';
  393. //退微信
  394. if ($order->pay_type == Order::PAY_TYPE_ACCOUNT) {
  395. DB::rollBack();
  396. return $this->error('此订单为余额支付 ');
  397. }
  398. //插入钱包记录 增加
  399. $wallet_log_add = new WalletLog();
  400. $wallet_log_add->name = WalletLog::$typeMaps[WalletLog::TYPE_ADD_WECHAT_PAY_ORDER_MONEY];
  401. $wallet_log_add->type = WalletLog::TYPE_ADD_WECHAT_PAY_ORDER_MONEY;
  402. $wallet_log_add->operate_type = WalletLog::OPERATE_TYPE_ADD;
  403. $wallet_log_add->money = (float)$return_money;
  404. $wallet_log_add->user_id = $user_id;
  405. $wallet_log_add->area_id = $area_id;
  406. $wallet_log_add->log_id = $order_id;
  407. $wallet_log_add->log_type = Order::class;
  408. $wallet_log_add->save();
  409. //3.插入钱包记录 减少
  410. $wallet_log = new WalletLog();
  411. $wallet_log->name = WalletLog::$typeMaps[WalletLog::TYPE_SUB_ORDER_MONEY_PAY_WECHAT];
  412. $wallet_log->type = WalletLog::TYPE_SUB_ORDER_MONEY_PAY_WECHAT;
  413. $wallet_log->operate_type = WalletLog::OPERATE_TYPE_SUB;
  414. $wallet_log->money = -(float)$return_money;
  415. $wallet_log->user_id = $user_id;
  416. $wallet_log->area_id = $area_id;
  417. $wallet_log->log_id = $order_id;
  418. $wallet_log->status = WalletLog::STATUS_PAUSE;
  419. $wallet_log->log_type = Order::class;
  420. $res2 = $wallet_log->save();
  421. if (!$res2) {
  422. DB::rollBack();
  423. Log::error('插入钱包记录失败');
  424. return $this->error('操作失败请联系管理员');
  425. }
  426. //退微信
  427. $payment = app('wechat.payment'); // 微信支付
  428. $refund_no = $order->no;
  429. $result = $payment->refund->byOutTradeNumber($order->no, $refund_no, wechat_fee($old_pay_money), wechat_fee($return_money), [
  430. // 可在此处传入其他参数,详细参数见微信支付文档
  431. 'refund_desc' => '退订单支付费用',
  432. 'notify_url' => config('wechat.payment.refund_deposit_notify_url')
  433. ]);
  434. if ($result['return_code'] === 'SUCCESS') {
  435. $user = User::find($order->user_id);
  436. // 返还通知
  437. Notification::send($user, new OrderRefundNotification($user, $order, $return_money, $return_type_name));
  438. DB::commit();
  439. return $this->ok('退还成功');
  440. } else {
  441. Log::error('微信退款接口失败');
  442. DB::rollBack();
  443. return $this->error('退还失败,请联系管理员');
  444. }
  445. } else {
  446. DB::rollBack();
  447. return $this->error('返还类型错误');
  448. }
  449. $user = User::find($order->user_id);
  450. // 返还通知
  451. Notification::send($user, new OrderRefundNotification($user, $order, $return_money, $return_type_name));
  452. DB::commit();
  453. } catch (\Exception $e) {
  454. DB::rollBack();
  455. Log::info($e->getMessage());
  456. return $this->error('操作失败请联系管理员');
  457. }
  458. return $this->ok($res3);
  459. }
  460. /**
  461. * changeOrderRiding 订单回溯到骑行状态
  462. *
  463. * @param Request $request
  464. * @return \Illuminate\Http\JsonResponse
  465. * @author Fx
  466. *
  467. */
  468. public function changeOrderRiding(Request $request)
  469. {
  470. $order_id = $request->get('order_id');
  471. if (empty($order_id)) return $this->error('参数错误');
  472. $order = Order::find($order_id);
  473. if (empty($order)) return $this->error('找不到该订单');
  474. $is_low_electric = $request->get('is_low_electric') ?? false;
  475. $order_end_time = Carbon::now()->diffInHours($order->end_use_bike_time);
  476. if ($order->status == Order::STATUS_CLOSE_BIKE && $order_end_time < 4) {
  477. try {
  478. DB::beginTransaction();
  479. // 车辆
  480. $bike = Bike::find($order->bike_id);
  481. $bike->is_riding = Bike::RIDING_YES;
  482. $bike->save();
  483. //订单
  484. $order->status = Order::STATUS_PAUSE_BIKE;
  485. $order->save();
  486. //优惠回溯
  487. if ($order->is_riding_card = Order::RIDING_CARD_OK) {
  488. //骑行卡回溯
  489. OrderHandler::cardRidingHuSu($order->id);
  490. }
  491. // 增加订单操作记录
  492. $orderBikeOperate = new OrderBikeOperate();
  493. if ($is_low_electric) {
  494. $orderBikeOperate->name = OrderBikeOperate::$typeMaps[OrderBikeOperate::TYPE_ADMIN_ORDER_BACK_LOW_POWER] . '(' . Admin::user()->name . ')';
  495. $orderBikeOperate->type = OrderBikeOperate::TYPE_ADMIN_ORDER_BACK_LOW_POWER;
  496. } else {
  497. $orderBikeOperate->name = OrderBikeOperate::$typeMaps[OrderBikeOperate::TYPE_ADMIN_ORDER_BACK] . '(' . Admin::user()->name . ')';
  498. $orderBikeOperate->type = OrderBikeOperate::TYPE_ADMIN_ORDER_BACK;
  499. }
  500. $orderBikeOperate->bike_id = $order->bike_id;
  501. $orderBikeOperate->order_id = $order_id;
  502. $orderBikeOperate->admin_id = Admin::user()->id;
  503. $orderBikeOperate->user_id = $order->user_id;
  504. $orderBikeOperate->is_admin = OrderBikeOperate::IS_ADMIN_YES;
  505. $orderBikeOperate->save();
  506. DB::commit();
  507. (new BikeStatusInfoSyncHandler())->toBikeRideStatus(BikeStatusInfoSyncHandler::ROLE_USER, $order->bike_no,
  508. [
  509. 'id' => $order_id,
  510. 'area_id' => $order->area_id,
  511. 'bike_id' => $order->bike_id
  512. ]);
  513. (new BaseBikeControl($bike->box_no))::temporaryCloseLock();
  514. if ($is_low_electric) {
  515. (new BikeStatusInfoSyncHandler())->toBikeNoElectric($bike->box_no);
  516. }
  517. return $this->ok('操作成功');
  518. } catch (\Exception $e) {
  519. Log::error($e->getMessage());
  520. return $this->error('操作失败,请联系管理员');
  521. }
  522. } else {
  523. return $this->error('时间过长,不可回溯');
  524. }
  525. }
  526. /**
  527. * 订单位置详情
  528. * @params 订单id
  529. * @return \Illuminate\Http\JsonResponse mongo中最后一个位置 根据订单查Mongo中最后一个位置
  530. * @author Fx
  531. *
  532. * */
  533. public function orderDetailPosition(Request $request)
  534. {
  535. $order_id = $request->get('order_id') ?? '';
  536. if (empty($order_id)) return $this->error('参数错误');
  537. $order = Order::find($order_id);
  538. $orderPosition = LocationsLog::query()->where('order_id', $order_id)
  539. ->where('is_rent', LocationsLog::RENT_NO)
  540. ->whereBetween('latitude', [3, 53])->whereBetween('longitude', [73, 136])->orderByDesc('created_at')->first();
  541. $bike_no = $order->bike_no;
  542. $bikePositioin = LocationsLog::getNewestLocationByBikeNo($bike_no);
  543. $end_use_bike_location = json_decode($order->end_use_bike_location);
  544. $area_id = $order->area_id;
  545. $areaSetting = AreaSetting::where('area_id', $area_id)->first();
  546. $data = [
  547. 'orderPosition' => empty($orderPosition) ? [$bikePositioin['lng'] ?? 0, $bikePositioin['lat'] ?? 0] : [$orderPosition->longitude, $orderPosition->latitude],
  548. 'bikePosition' => [$bikePositioin['lng'] ?? 0, $bikePositioin['lat'] ?? 0],
  549. 'is_whole_area_huanche' => $areaSetting->is_whole_area_huanche == 1 ? true : false,
  550. ];
  551. return $this->ok($data);
  552. }
  553. /**
  554. * orderBikeContro 订单车辆控制
  555. *
  556. * @param Request $request
  557. * @return \Illuminate\Http\JsonResponse
  558. * @author Fx
  559. *
  560. */
  561. public function orderBikeContro(Request $request)
  562. {
  563. $type = $request->get('type') ?? '';
  564. $order_no = $request->get('order_no') ?? '';
  565. if (empty($type) || empty($order_no)) return $this->error('参数错误');
  566. $order = Order::query()->where('no', $order_no)->first();
  567. if (in_array($order->status, [Order::STATUS_RIDE_BIKE, Order::STATUS_PAUSE_BIKE])) {
  568. switch ($type) {
  569. case 'huiDian':
  570. (new BikeStatusInfoSyncHandler())->toBikeGetElectric($order->bike_no);
  571. (new BaseBikeControl($order->bikes->box_no))::outAreaGetElectric();
  572. $res = OrderBikeOperate::logs(
  573. OrderBikeOperate::$typeMaps[OrderBikeOperate::TYPE_ADMIN_ORDER_HUI_DIAN] . '(' . Admin::user()->name . ')',
  574. OrderBikeOperate::TYPE_ADMIN_ORDER_HUI_DIAN,
  575. $order->bike_id,
  576. $order->id,
  577. $order->user_id,
  578. OrderBikeOperate::IS_ADMIN_YES,
  579. Admin::user()->id
  580. );
  581. break;
  582. case 'openLock':
  583. (new BaseBikeControl($order->bikes->box_no))::openLock();
  584. $res = OrderBikeOperate::logs(
  585. OrderBikeOperate::$typeMaps[OrderBikeOperate::TYPE_ADMIN_ORDER_OPEN_LOCK] . '(' . Admin::user()->name . ')',
  586. OrderBikeOperate::TYPE_ADMIN_ORDER_OPEN_LOCK,
  587. $order->bike_id,
  588. $order->id,
  589. $order->user_id,
  590. OrderBikeOperate::IS_ADMIN_YES,
  591. Admin::user()->id
  592. );
  593. break;
  594. default:
  595. return $this->error('参数错误,请检查后重新提交');
  596. }
  597. if ($res) {
  598. return $this->ok('操作成功');
  599. } else {
  600. return $this->error('出现错误,请联系管理员');
  601. }
  602. } else {
  603. return $this->error('订单状态不为在骑行,无法操作车辆');
  604. }
  605. }
  606. public function getOrdersByNo(Request $request)
  607. {
  608. $no = $request->get('no') ?? '';
  609. if (!empty($no)) {
  610. $order = Order::query()->where('no', $no)->with(['users', 'bikes'])->first();
  611. if (empty($order)) {
  612. $order = OrderRent::query()->where('no', $no)->with(['users', 'bikes'])->first();
  613. if (empty($order)) {
  614. return $this->error('找不到此订单号对应得订单');
  615. }
  616. }
  617. }
  618. return $this->ok($order->toArray());
  619. }
  620. }