OrderHandler.php 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216
  1. <?php
  2. namespace App\Handlers;
  3. use App\Models\AreaSetting;
  4. use App\Models\Bike;
  5. use App\Models\CardRidingUseLog;
  6. use App\Models\CardRidingUserBags;
  7. use App\Models\LocationsLog;
  8. use App\Models\Order;
  9. use App\Models\OrderBikeOperate;
  10. use App\Utils\Admin;
  11. use Carbon\Carbon;
  12. use Illuminate\Support\Facades\DB;
  13. use Illuminate\Support\Facades\Log;
  14. class OrderHandler
  15. {
  16. /**
  17. * 关锁
  18. * */
  19. public static function closeOrder($order_id){
  20. $order = Order::query()->find($order_id);
  21. if(empty($order)){
  22. Log::error('找不到订单信息');
  23. return false;
  24. }
  25. if($order->status !== Order::STATUS_PAUSE_BIKE && $order->status !== Order::STATUS_RIDE_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. try {
  35. $lastLocation = LocationsLog::getNewestLocationByBikeNo($bike->bike_no);
  36. DB::beginTransaction();
  37. // 更新车信息
  38. $bike->is_riding = Bike::RIDING_NO;
  39. $bike->is_lock = Bike::LOCK_YES;
  40. $bike->last_use_bike_end_time = date('Y-m-d H:i:s');
  41. $bike->save();
  42. // 记录关锁日志信息
  43. $orderOperateBike = new OrderBikeOperate();
  44. $orderOperateBike->type = OrderBikeOperate::TYPE_CLONE_BIKE;
  45. $orderOperateBike->name = OrderBikeOperate::$typeMaps[OrderBikeOperate::TYPE_CLONE_BIKE].',后台关锁';
  46. $orderOperateBike->bike_id = $bike->id;
  47. $orderOperateBike->latitude = $lastLocation['lat'];
  48. $orderOperateBike->longitude = $lastLocation['lng'];
  49. $orderOperateBike->order_id = $order_id;
  50. $orderOperateBike->user_id = Admin::user()->id;
  51. $orderOperateBike->is_admin = OrderBikeOperate::IS_ADMIN_YES;
  52. $orderOperateBike->save();
  53. // 删除redis订单
  54. (new BikeStatusInfoSyncHandler())->toBikeWaitRideStatus($bike->bike_no,$lastLocation['lng'],$lastLocation['lat'],$bike->put_status);
  55. // 30秒内关车,关闭订单
  56. $second = Carbon::now()->diffInSeconds(Carbon::parse($order->start_use_bike_time));
  57. // 判断是否经常这样操作(未做)
  58. if ($second <= 30) {
  59. // 关闭订单
  60. $order->status = Order::STATUS_CLOSE_ORDER;
  61. $order->end_use_bike_time = now();
  62. $order->end_use_bike_location = [
  63. 'latitude' => $lastLocation['lat'],
  64. 'longitude' => $lastLocation['lng'],
  65. ];
  66. $order->is_admin_settle_order = Order::ADMIN_SETTLE_ORDER_ADMIN;
  67. $order->end_use_bike_location = json_encode( $order->end_use_bike_location);
  68. $order->save();
  69. DB::commit();
  70. return true;
  71. }
  72. // 计算订单数据
  73. $setting = AreaSetting::query()->where('area_id',$order->area_id)->first();
  74. // 是否处于临时停车的状态
  75. if ($order->status === Order::STATUS_PAUSE_BIKE) {
  76. $order->pause_bike_time_length += Carbon::now()->diffInMinutes(Carbon::parse($order->pause_bike_time));
  77. }
  78. $order->status = Order::STATUS_CLOSE_BIKE;
  79. $order->end_use_bike_time = now();
  80. $order->end_use_bike_location = [
  81. 'latitude' => $lastLocation['lat'],
  82. 'longitude' => $lastLocation['lng'],
  83. ];
  84. //计算用车时间
  85. $order->use_bike_time_length = ceil($second / 60);
  86. //计算骑行距离
  87. $start_use_bike_location = json_decode($order->start_use_bike_location);
  88. $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']);
  89. $order->use_bike_distance_length = round($order->use_bike_distance_length / 1000,2);
  90. // 计算价格
  91. $time_money = $setting->per_money * ceil($order->use_bike_time_length / $setting->per_minute);
  92. if ($time_money < 0) {
  93. $time_money = 0;
  94. }
  95. // 判断车是否在停车的区域
  96. $BikeHandler = new BikeHandler();
  97. $is_huanche = $BikeHandler->byLocationCheckIsInStopParking($lastLocation['lat'], $lastLocation['lng'], $order->area_id);
  98. // Log::info($is_huanche);
  99. $order->dispatch_money = 0;
  100. if (!$is_huanche['status']) {
  101. // 不在还车点
  102. $dispatch_money = $BikeHandler->byDistanceGetDistanceMoney($is_huanche['distance'], $setting);
  103. // Log::info($dispatch_money);
  104. $order->dispatch_money = $dispatch_money;
  105. $bike->is_in_parking = Bike::IN_PARKING_NO;
  106. }
  107. $order->time_money = $time_money;
  108. $order->pause_money = 0;
  109. $order->preferential_money = 0;
  110. $order->end_use_bike_location = json_encode( $order->end_use_bike_location);
  111. $order->total_money = bcadd($time_money, $order->dispatch_money, 2);
  112. $order->pay_money = $order->total_money;
  113. $order->is_admin_settle_order = Order::ADMIN_SETTLE_ORDER_ADMIN;
  114. $order->pay_type = Order::PAY_STATUS_NO;
  115. if ($order->time_money > 0){
  116. // 所有金额计算完毕 如果需要支付时才消耗骑行卡
  117. $cardRiding = CardRidingUserBags::isExist($order->user_id,$order->start_use_bike_time);
  118. // Log::info($cardRiding);
  119. if(!empty($cardRiding)){
  120. // 判断骑行卡类型
  121. $cardLogs = CardRidingUseLog::isUseThisCard($order->id,$cardRiding->id);
  122. if(empty($cardLogs)){
  123. if($cardRiding->is_limit_times == CardRidingUserBags::LIMIT_TIMES_YES){
  124. // 限次卡
  125. $discount_time_money = bcsub($order->time_money,$cardRiding->deduction_money,2);
  126. // Log::info($discount_time_money);
  127. if($discount_time_money < 0){
  128. $order->preferential_money = $order->time_money;
  129. }else{
  130. $order->preferential_money = $cardRiding->deduction_money;
  131. }
  132. // Log::info($order->preferential_money);
  133. // Log::info($cardRiding->deduction_money);
  134. $order->total_money = bcsub($order->total_money,$order->preferential_money,2);
  135. $order->pay_money = $order->total_money;
  136. // 更新骑行卡
  137. $cardRiding->can_ridding_times -= 1;
  138. if($cardRiding->can_ridding_times <= 0){
  139. // 如果没次数了 直接使其失效
  140. $cardRiding->status = CardRidingUserBags::STATUS_NO;
  141. }
  142. $cardRiding->save();
  143. }else{
  144. // 不限次卡
  145. //查询该用户今日订单次数
  146. $orderNum = Order::getTodayOrderNum($order->user_id);
  147. if($orderNum < $cardRiding->day_can_ridding_times){
  148. $discount_time_money = bcsub($order->time_money,$cardRiding->deduction_money,2);
  149. if($discount_time_money < 0){
  150. $order->preferential_money = $order->time_money;
  151. }else{
  152. $order->preferential_money = $cardRiding->deduction_money;
  153. }
  154. $order->total_money = bcsub($order->total_money,$order->preferential_money,2);
  155. $order->pay_money = $order->total_money;
  156. }
  157. }
  158. $order->preferential_type = Order::PREFERENTIAL_CARD_RIDING; //优惠方式
  159. CardRidingUseLog::logs($cardRiding->id,$order->id,$order->preferential_money);
  160. }
  161. }
  162. }
  163. if ($order->total_money === 0) {
  164. $order->pay_type = Order::PAY_STATUS_OK;
  165. $order->status = Order::STATUS_CLOSE_ORDER;
  166. }
  167. $order->save();
  168. $bike->save();
  169. DB::commit();
  170. // 关车
  171. $closeLockBool = BikeControl::closeLock($bike->box_no);
  172. if(!$closeLockBool){
  173. Log::error('关锁失败');
  174. return false;
  175. }
  176. return true;
  177. }catch (\Exception $e){
  178. DB::rollBack();
  179. Log::error($e->getMessage());
  180. return false;
  181. }
  182. }
  183. }