OrderController.php 29 KB

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