OrderHandler.php 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296
  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 App\Utils\RedisKeys;
  12. use Carbon\Carbon;
  13. use Illuminate\Support\Facades\DB;
  14. use Illuminate\Support\Facades\Log;
  15. class OrderHandler
  16. {
  17. /**
  18. * 关锁
  19. * */
  20. public static function closeOrder($order_id)
  21. {
  22. $order = Order::query()->find($order_id);
  23. if (empty($order)) {
  24. Log::error('找不到订单信息');
  25. return false;
  26. }
  27. if ($order->status !== Order::STATUS_PAUSE_BIKE && $order->status !== Order::STATUS_RIDE_BIKE) {
  28. Log::error('订单状态不为骑行中或暂时停车,无法改为待结算');
  29. return false;
  30. }
  31. $bike = Bike::query()->find($order->bike_id);
  32. if (empty($bike)) {
  33. Log::error('找不到订单车辆信息');
  34. return false;
  35. }
  36. try {
  37. $lastLocation = LocationsLog::getNewestLocationByBikeNo($bike->bike_no);
  38. $BikeHandler = new BikeHandler();
  39. // $is_ban_stop_bike = $BikeHandler->byLocationCheckIsInBanStopParking($lastLocation['lat'], $lastLocation['lng'], $order->area_id);
  40. // if ($is_ban_stop_bike) {
  41. // Log::error('车辆在禁停区,无法关闭订单还车');
  42. // return false;
  43. // }
  44. DB::beginTransaction();
  45. // 更新车信息
  46. $bike->is_riding = Bike::RIDING_NO;
  47. $bike->is_lock = Bike::LOCK_YES;
  48. $bike->last_use_bike_end_time = date('Y-m-d H:i:s');
  49. $bike->save();
  50. // 记录关锁日志信息
  51. $orderOperateBike = new OrderBikeOperate();
  52. $orderOperateBike->type = OrderBikeOperate::TYPE_CLONE_BIKE;
  53. $orderOperateBike->name = OrderBikeOperate::$typeMaps[OrderBikeOperate::TYPE_CLONE_BIKE] . ',后台关锁';
  54. $orderOperateBike->bike_id = $bike->id;
  55. $orderOperateBike->latitude = $lastLocation['lat'];
  56. $orderOperateBike->longitude = $lastLocation['lng'];
  57. $orderOperateBike->order_id = $order_id;
  58. $orderOperateBike->user_id = Admin::user()->id;
  59. $orderOperateBike->is_admin = OrderBikeOperate::IS_ADMIN_YES;
  60. $orderOperateBike->save();
  61. // 删除redis订单
  62. (new BikeStatusInfoSyncHandler())->toBikeWaitRideStatus($bike->bike_no, $lastLocation['lng'], $lastLocation['lat'], $bike->put_status);
  63. // 30秒内关车,关闭订单
  64. $second = Carbon::now()->diffInSeconds(Carbon::parse($order->start_use_bike_time));
  65. // 判断是否经常这样操作(未做)
  66. if ($second <= 30) {
  67. // 关闭订单
  68. $order->status = Order::STATUS_CLOSE_ORDER;
  69. $order->end_use_bike_time = now();
  70. $order->end_use_bike_location = [
  71. 'latitude' => $lastLocation['lat'],
  72. 'longitude' => $lastLocation['lng'],
  73. ];
  74. $order->is_admin_settle_order = Order::ADMIN_SETTLE_ORDER_ADMIN;
  75. $order->end_use_bike_location = json_encode($order->end_use_bike_location);
  76. $order->save();
  77. DB::commit();
  78. $closeLockBool = (new BaseBikeControl($bike->box_no))::closeLock();
  79. return true;
  80. }
  81. // 计算订单数据
  82. $setting = AreaSetting::query()->where('area_id', $order->area_id)->first();
  83. // 是否处于临时停车的状态
  84. if ($order->status === Order::STATUS_PAUSE_BIKE) {
  85. $order->pause_bike_time_length += Carbon::now()->diffInMinutes(Carbon::parse($order->pause_bike_time));
  86. }
  87. $order->status = Order::STATUS_CLOSE_BIKE;
  88. $order->end_use_bike_time = now();
  89. $order->end_use_bike_location = [
  90. 'latitude' => $lastLocation['lat'],
  91. 'longitude' => $lastLocation['lng'],
  92. ];
  93. //计算用车时间
  94. $order->use_bike_time_length = ceil($second / 60);
  95. //计算骑行距离
  96. $start_use_bike_location = json_decode($order->start_use_bike_location);
  97. $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']);
  98. $order->use_bike_distance_length = round($order->use_bike_distance_length / 1000, 2);
  99. // 计算价格
  100. $time_money = $setting->per_money * ceil($order->use_bike_time_length / $setting->per_minute);
  101. if ($setting->is_minute_free == AreaSetting::FREE_MINUTE_OK && $setting->many_minute_free >= $order->use_bike_time_length) {
  102. if ($setting->minute_free_times == 0) {
  103. $time_money = 0;
  104. } else {
  105. $key = sprintf(RedisKeys::FREE_MINUTE_USER_ID, $order->user_id);
  106. app()->redis->sadd($key, $order->id);
  107. app()->redis->expireat($key, Carbon::tomorrow()->timestamp);
  108. if (app()->redis->scard($key) <= $setting->minute_free_times) {
  109. $time_money = 0;
  110. }
  111. }
  112. }
  113. if ($time_money < 0) {
  114. $time_money = 0;
  115. }
  116. $order->dispatch_money = 0;
  117. $areaSetting = AreaSetting::where('area_id', $order->area_id)->first();
  118. if ($areaSetting->is_whole_area_huanche === AreaSetting::WHOLE_AREA_HUANCHE_NO) {
  119. // 是否全区域可还车
  120. // 判断车是否在停车的区域
  121. $is_huanche = $BikeHandler->byLocationCheckIsInStopParking($lastLocation['lat'], $lastLocation['lng'], $order->area_id);
  122. // Log::info($is_huanche);
  123. if (!$is_huanche['status']) {
  124. // 不在还车点
  125. $dispatch_money = $BikeHandler->byDistanceGetDistanceMoney($is_huanche['distance'], $setting);
  126. // Log::info($dispatch_money);
  127. $order->dispatch_money = $dispatch_money;
  128. $bike->is_in_parking = Bike::IN_PARKING_NO;
  129. }
  130. }
  131. $order->time_money = $time_money;
  132. $order->pause_money = 0;
  133. $order->preferential_money = 0;
  134. $order->end_use_bike_location = json_encode($order->end_use_bike_location);
  135. $order->order_money = bcadd($time_money, $order->dispatch_money, 2);
  136. $order->total_money = bcadd($time_money, $order->dispatch_money, 2);
  137. $order->pay_money = $order->order_money;
  138. $order->is_admin_settle_order = Order::ADMIN_SETTLE_ORDER_ADMIN;
  139. $order->pay_type = Order::PAY_STATUS_NO;
  140. if ($order->time_money > 0) {
  141. // 所有金额计算完毕 如果需要支付时才消耗骑行卡
  142. $cardRiding = CardRidingUserBags::isExist($order->user_id, $order->start_use_bike_time);
  143. // Log::info($cardRiding);
  144. if (!empty($cardRiding)) {
  145. // 判断骑行卡类型
  146. $cardLogs = CardRidingUseLog::isUseThisCard($order->id, $cardRiding->id);
  147. if (empty($cardLogs)) {
  148. if ($cardRiding->is_limit_times == CardRidingUserBags::LIMIT_TIMES_YES) {
  149. // 限次卡
  150. $discount_time_money = bcsub($order->time_money, $cardRiding->deduction_money, 2);
  151. // Log::info($discount_time_money);
  152. if ($discount_time_money < 0) {
  153. $order->preferential_money = $order->time_money;
  154. } else {
  155. $order->preferential_money = $cardRiding->deduction_money;
  156. }
  157. // Log::info($order->preferential_money);
  158. // Log::info($cardRiding->deduction_money);
  159. $order->order_money = bcsub($order->order_money, $order->preferential_money, 2);
  160. $order->pay_money = $order->order_money;
  161. // 更新骑行卡
  162. $cardRiding->can_ridding_times -= 1;
  163. if ($cardRiding->can_ridding_times <= 0) {
  164. // 如果没次数了 直接使其失效
  165. $cardRiding->status = CardRidingUserBags::STATUS_NO;
  166. }
  167. $cardRiding->save();
  168. } else {
  169. // 不限次卡
  170. //查询该用户今日订单次数
  171. $orderNum = Order::getTodayOrderNum($order->user_id);
  172. if ($orderNum < $cardRiding->day_can_ridding_times) {
  173. $discount_time_money = bcsub($order->time_money, $cardRiding->deduction_money, 2);
  174. if ($discount_time_money < 0) {
  175. $order->preferential_money = $order->time_money;
  176. } else {
  177. $order->preferential_money = $cardRiding->deduction_money;
  178. }
  179. $order->order_money = bcsub($order->order_money, $order->preferential_money, 2);
  180. $order->pay_money = $order->order_money;
  181. }
  182. }
  183. $order->is_riding_card = Order::RIDING_CARD_OK;
  184. //优惠方式
  185. if ($order->preferential_type == 0) {
  186. $order->preferential_type = arr2str([Order::PREFERENTIAL_CARD_RIDING],'-');
  187. } else {
  188. $arr = str2arr($order->preferential_type,'-');
  189. $order->preferential_type = arr2str(array_merge($arr, [Order::PREFERENTIAL_CARD_RIDING]),'-');
  190. }
  191. CardRidingUseLog::logs($cardRiding->id, $order->id, $order->preferential_money);
  192. }
  193. }
  194. }
  195. if ($order->order_money === 0) {
  196. $order->pay_type = Order::PAY_STATUS_OK;
  197. $order->status = Order::STATUS_CLOSE_ORDER;
  198. }
  199. $order->save();
  200. $bike->save();
  201. // 关车
  202. $closeLockBool = (new BaseBikeControl($bike->box_no))::closeLock();
  203. if (!$closeLockBool) {
  204. DB::rollBack();
  205. Log::error('关锁失败');
  206. return false;
  207. }
  208. DB::commit();
  209. return true;
  210. } catch (\Exception $e) {
  211. DB::rollBack();
  212. Log::error($e->getMessage());
  213. return false;
  214. }
  215. }
  216. /**
  217. * 骑行卡回溯 cardRidingHuSu
  218. *
  219. * @param $order_id
  220. * @return void
  221. * @author Fx
  222. *
  223. */
  224. public static function cardRidingHuSu($order_id)
  225. {
  226. $order = Order::find($order_id);
  227. if ($order->is_riding_card == Order::RIDING_CARD_OK) {
  228. $card_use_log = CardRidingUseLog::where('order_id', $order->id)->first();
  229. if (empty($card_use_log)) return;
  230. $card_id = $card_use_log->card_riding_user_bags_id;
  231. $cardRidingUser = CardRidingUserBags::find($card_id);
  232. if (!empty($cardRidingUser) && $cardRidingUser->is_limit_times == CardRidingUserBags::LIMIT_TIMES_YES) {
  233. $cardRidingUser->can_ridding_times += 1;
  234. $cardRidingUser->save();
  235. }
  236. $card_use_log->delete();
  237. // 优惠金额全部为0
  238. $order->card_preferential_money = 0.00;
  239. $order->preferential_money = 0.00;
  240. $order->is_riding_card = Order::RIDING_CARD_NO;
  241. if ($order->preferential_type == 0) {
  242. $order->preferential_type = 0;
  243. } else {
  244. $arr = str2arr($order->preferential_type,'-');
  245. $arr = array_diff($arr, [Order::PREFERENTIAL_CARD_RIDING]);
  246. if (empty($arr)) {
  247. $order->preferential_type = 0;
  248. } else {
  249. $order->preferential_type = arr2str($arr,'-');
  250. }
  251. }
  252. $order->save();
  253. }
  254. }
  255. }