OrderRentHandler.php 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219
  1. <?php
  2. namespace App\Handlers;
  3. use App\Models\AreaSetting;
  4. use App\Models\Bike;
  5. use App\Models\LocationsLog;
  6. use App\Models\OrderBikeOperate;
  7. use App\Models\OrderRent;
  8. use App\Models\OrderRentBikeOperate;
  9. use App\Utils\Admin;
  10. use Carbon\Carbon;
  11. use Illuminate\Support\Facades\DB;
  12. use Illuminate\Support\Facades\Log;
  13. class OrderRentHandler
  14. {
  15. /**
  16. * 关锁还车
  17. * */
  18. public static function closeOrderRent($order_id)
  19. {
  20. $order = OrderRent::find($order_id);
  21. if (empty($order)) {
  22. Log::error('找不到订单信息');
  23. return false;
  24. }
  25. if ($order->status !== OrderRent::STATUS_RENT_BIKE) {
  26. Log::error('订单状态不为租车中,无法改为待结算');
  27. return false;
  28. }
  29. $bike = Bike::query()->find($order->bike_id);
  30. if (empty($bike)) {
  31. Log::error('找不到订单车辆信息');
  32. return false;
  33. }
  34. // 关车
  35. $closeLockBool = (new BaseBikeControl($bike->box_no))::closeLock();
  36. if (!$closeLockBool) {
  37. Log::error('关锁失败');
  38. return false;
  39. }
  40. try {
  41. $lastLocation = LocationsLog::getNewestLocationByBikeNo($bike->bike_no);
  42. $BikeHandler = new BikeHandler();
  43. $is_ban_stop_bike = $BikeHandler->byLocationCheckIsInBanStopParking($lastLocation['lat'], $lastLocation['lng'], $order->area_id);
  44. if ($is_ban_stop_bike) {
  45. Log::error('车辆在禁停区,无法关闭订单还车');
  46. return false;
  47. }
  48. DB::beginTransaction();
  49. // 更新车信息
  50. $bike->is_riding = Bike::RIDING_NO;
  51. $bike->is_lock = Bike::LOCK_YES;
  52. $bike->is_rent = Bike::RIDING_NO;
  53. $bike->last_location = json_encode($lastLocation);
  54. $bike->last_use_bike_end_time = date('Y-m-d H:i:s');
  55. $bike->save();
  56. // 记录关锁日志信息
  57. $orderRentOperateBike = new OrderRentBikeOperate();
  58. $orderRentOperateBike->type = OrderRentBikeOperate::TYPE_CLONE_BIKE;
  59. $orderRentOperateBike->name = OrderRentBikeOperate::$typeMaps[OrderBikeOperate::TYPE_CLONE_BIKE] . ',后台关锁';
  60. $orderRentOperateBike->bike_id = $bike->id;
  61. $orderRentOperateBike->latitude = $lastLocation['lat'];
  62. $orderRentOperateBike->longitude = $lastLocation['lng'];
  63. $orderRentOperateBike->order_id = $order_id;
  64. $orderRentOperateBike->user_id = Admin::user()->id;
  65. $orderRentOperateBike->is_admin = OrderRentBikeOperate::IS_ADMIN_YES;
  66. $orderRentOperateBike->save();
  67. // 删除redis订单
  68. (new BikeStatusInfoSyncHandler())->toBikeWaitRideStatus($bike->bike_no, $lastLocation['lng'], $lastLocation['lat'], $bike->put_status);
  69. $second = Carbon::now()->diffInSeconds(Carbon::parse($order->start_use_bike_time));
  70. // 判断是否经常这样操作(未做)
  71. // 30秒内关车,关闭订单
  72. // if ($second <= 30) {
  73. // // 关闭订单
  74. // $order->status = Order::STATUS_CLOSE_ORDER;
  75. // $order->end_use_bike_time = now();
  76. // $order->end_use_bike_location = [
  77. // 'latitude' => $lastLocation['lat'],
  78. // 'longitude' => $lastLocation['lng'],
  79. // ];
  80. // $order->is_admin_settle_order = Order::ADMIN_SETTLE_ORDER_ADMIN;
  81. // $order->end_use_bike_location = json_encode( $order->end_use_bike_location);
  82. // $order->save();
  83. // DB::commit();
  84. // return true;
  85. // }
  86. // 计算订单数据
  87. $setting = AreaSetting::query()->where('area_id', $order->area_id)->first();
  88. if (empty($setting)) {
  89. Log::error('区域配置出错,无法计算');
  90. return false;
  91. }
  92. // 计算用车小时数
  93. $use_hours = ceil($second / 3600);
  94. // 计算用车价格
  95. $money = 0;
  96. $over_hours = 0;
  97. //是否需要收取超出费用
  98. $is_over_time = (strtotime($order->return_end_bike_time) < time());
  99. if ($is_over_time) {
  100. //超出时间
  101. $over_hours = ceil(Carbon::now()->diffInMinutes(Carbon::parse($order->return_end_bike_time)) / 60);
  102. $hours = ceil(Carbon::now()->diffInMinutes(Carbon::parse($order->start_use_bike_time)) / 60);
  103. // Log::info($hours);
  104. if ($hours > 24) {
  105. //超过1天 不超过8小时
  106. $day = ceil($hours / 24);
  107. $end_hours = $hours % 24;
  108. $money = bcmul($setting->day_rent_capping_money, ($day - 1), 2);
  109. // $money = bcadd($money, $setting->day_rent_money, 2);
  110. if ($end_hours > $setting->day_rent_hours) {
  111. // 超过一天 又超过8小时
  112. // (超时费 + 日封顶租金*天)
  113. $money = bcadd(bcmul(($end_hours - $setting->day_rent_hours), $setting->per_hours_day_rent_timeout_money, 2), $money, 2);
  114. }
  115. } else {
  116. // 不超过1天 但是超过8小时
  117. $money = bcmul($over_hours, $setting->per_hours_day_rent_timeout_money, 2);
  118. // (超时费 + 基础租金) 是否大于日封顶租金
  119. $total_money = bcadd($money, $setting->day_rent_money, 2);
  120. if ($total_money > $setting->day_rent_capping_money) {
  121. // 总租金 大于 封顶租金
  122. //(封顶租金 - 基础租金)
  123. $money = bcsub($setting->day_rent_capping_money, $setting->day_rent_money, 2);
  124. }
  125. }
  126. }
  127. // 日租结算
  128. $order->time_money = $money;
  129. $order->distance_money = 0.00;
  130. $order->preferential_money = 0.00;
  131. $order->over_hours = $over_hours;
  132. $order->end_use_bike_time = Carbon::now();
  133. $order->status = OrderRent::STATUS_CLOSE_RENT_BIKE;
  134. $order->end_use_bike_time = now();
  135. $order->end_use_bike_location = [
  136. 'latitude' => $lastLocation['lat'],
  137. 'longitude' => $lastLocation['lng'],
  138. ];
  139. //计算用车时间 (分)
  140. $order->use_bike_time_length = ceil($second / 60);
  141. //计算骑行距离
  142. $start_use_bike_location = json_decode($order->start_use_bike_location);
  143. $order->use_bike_distance_length = getDistance($start_use_bike_location->latitude, $start_use_bike_location->longitude, $order->end_use_bike_location['latitude'], $order->end_use_bike_location['longitude']);
  144. $order->use_bike_distance_length = round($order->use_bike_distance_length / 1000, 2);
  145. $order->dispatch_money = 0;
  146. $areaSetting = AreaSetting::where('area_id', $order->area_id)->first();
  147. if ($areaSetting->is_whole_area_huanche === AreaSetting::WHOLE_AREA_HUANCHE_NO) {
  148. // 是否全区域可还车
  149. // 判断车是否在停车的区域
  150. $is_huanche = $BikeHandler->byLocationCheckIsInStopParking($lastLocation['lat'], $lastLocation['lng'], $order->area_id);
  151. // Log::info($is_huanche);
  152. if (!$is_huanche['status']) {
  153. // 不在还车点
  154. $dispatch_money = $BikeHandler->byDistanceGetDistanceMoney($is_huanche['distance'], $setting);
  155. // Log::info($dispatch_money);
  156. $order->dispatch_money = $dispatch_money;
  157. $bike->is_in_parking = Bike::IN_PARKING_NO;
  158. }
  159. }
  160. $order->preferential_money = 0;
  161. $order->end_use_bike_location = json_encode($order->end_use_bike_location);
  162. // 订单价格(调度费 + 超时费 + 租用费 )
  163. $order->total_money = bcadd(bcadd($money, $order->dispatch_money, 2), $setting->day_rent_money, 2);
  164. // 订单支付价格 (超时二次支付的价格)
  165. $order->pay_money = $order->total_money;
  166. // 订单总收入
  167. $order->order_total_money = $order->total_money;
  168. $order->is_admin_settle_order = OrderRent::ADMIN_SETTLE_ORDER_ADMIN;
  169. $order->pay_type = OrderRent::PAY_STATUS_NO;
  170. $order->save();
  171. $bike->save();
  172. DB::commit();
  173. // 关车
  174. (new BaseBikeControl($bike->box_no))::closeLock();
  175. return true;
  176. } catch (\Exception $e) {
  177. DB::rollBack();
  178. Log::error($e->getMessage());
  179. return false;
  180. }
  181. }
  182. }