OrderRentController.php 25 KB

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