OrderRentController.php 29 KB

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