OrderController.php 26 KB

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