123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216 |
- <?php
- namespace App\Handlers;
- use App\Models\AreaSetting;
- use App\Models\Bike;
- use App\Models\CardRidingUseLog;
- use App\Models\CardRidingUserBags;
- use App\Models\LocationsLog;
- use App\Models\Order;
- use App\Models\OrderBikeOperate;
- use App\Utils\Admin;
- use Carbon\Carbon;
- use Illuminate\Support\Facades\DB;
- use Illuminate\Support\Facades\Log;
- class OrderHandler
- {
- /**
- * 关锁
- * */
- public static function closeOrder($order_id){
- $order = Order::query()->find($order_id);
- if(empty($order)){
- Log::error('找不到订单信息');
- return false;
- }
- if($order->status !== Order::STATUS_PAUSE_BIKE && $order->status !== Order::STATUS_RIDE_BIKE){
- Log::error('订单状态不为骑行中或暂时停车,无法改为待结算');
- return false;
- }
- $bike = Bike::query()->find($order->bike_id);
- if(empty($bike)){
- Log::error('找不到订单车辆信息');
- return false;
- }
- try {
- $lastLocation = LocationsLog::getNewestLocationByBikeNo($bike->bike_no);
- DB::beginTransaction();
- // 更新车信息
- $bike->is_riding = Bike::RIDING_NO;
- $bike->is_lock = Bike::LOCK_YES;
- $bike->last_use_bike_end_time = date('Y-m-d H:i:s');
- $bike->save();
- // 记录关锁日志信息
- $orderOperateBike = new OrderBikeOperate();
- $orderOperateBike->type = OrderBikeOperate::TYPE_CLONE_BIKE;
- $orderOperateBike->name = OrderBikeOperate::$typeMaps[OrderBikeOperate::TYPE_CLONE_BIKE].',后台关锁';
- $orderOperateBike->bike_id = $bike->id;
- $orderOperateBike->latitude = $lastLocation['lat'];
- $orderOperateBike->longitude = $lastLocation['lng'];
- $orderOperateBike->order_id = $order_id;
- $orderOperateBike->user_id = Admin::user()->id;
- $orderOperateBike->is_admin = OrderBikeOperate::IS_ADMIN_YES;
- $orderOperateBike->save();
- // 删除redis订单
- (new BikeStatusInfoSyncHandler())->toBikeWaitRideStatus($bike->bike_no,$lastLocation['lng'],$lastLocation['lat'],$bike->put_status);
- // 30秒内关车,关闭订单
- $second = Carbon::now()->diffInSeconds(Carbon::parse($order->start_use_bike_time));
- // 判断是否经常这样操作(未做)
- if ($second <= 30) {
- // 关闭订单
- $order->status = Order::STATUS_CLOSE_ORDER;
- $order->end_use_bike_time = now();
- $order->end_use_bike_location = [
- 'latitude' => $lastLocation['lat'],
- 'longitude' => $lastLocation['lng'],
- ];
- $order->is_admin_settle_order = Order::ADMIN_SETTLE_ORDER_ADMIN;
- $order->end_use_bike_location = json_encode( $order->end_use_bike_location);
- $order->save();
- DB::commit();
- return true;
- }
- // 计算订单数据
- $setting = AreaSetting::query()->where('area_id',$order->area_id)->first();
- // 是否处于临时停车的状态
- if ($order->status === Order::STATUS_PAUSE_BIKE) {
- $order->pause_bike_time_length += Carbon::now()->diffInMinutes(Carbon::parse($order->pause_bike_time));
- }
- $order->status = Order::STATUS_CLOSE_BIKE;
- $order->end_use_bike_time = now();
- $order->end_use_bike_location = [
- 'latitude' => $lastLocation['lat'],
- 'longitude' => $lastLocation['lng'],
- ];
- //计算用车时间
- $order->use_bike_time_length = ceil($second / 60);
- //计算骑行距离
- $start_use_bike_location = json_decode($order->start_use_bike_location);
- $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']);
- $order->use_bike_distance_length = round($order->use_bike_distance_length / 1000,2);
- // 计算价格
- $time_money = $setting->per_money * ceil($order->use_bike_time_length / $setting->per_minute);
- if ($time_money < 0) {
- $time_money = 0;
- }
- // 判断车是否在停车的区域
- $BikeHandler = new BikeHandler();
- $is_huanche = $BikeHandler->byLocationCheckIsInStopParking($lastLocation['lat'], $lastLocation['lng'], $order->area_id);
- // Log::info($is_huanche);
- $order->dispatch_money = 0;
- if (!$is_huanche['status']) {
- // 不在还车点
- $dispatch_money = $BikeHandler->byDistanceGetDistanceMoney($is_huanche['distance'], $setting);
- // Log::info($dispatch_money);
- $order->dispatch_money = $dispatch_money;
- $bike->is_in_parking = Bike::IN_PARKING_NO;
- }
- $order->time_money = $time_money;
- $order->pause_money = 0;
- $order->preferential_money = 0;
- $order->end_use_bike_location = json_encode( $order->end_use_bike_location);
- $order->total_money = bcadd($time_money, $order->dispatch_money, 2);
- $order->pay_money = $order->total_money;
- $order->is_admin_settle_order = Order::ADMIN_SETTLE_ORDER_ADMIN;
- $order->pay_type = Order::PAY_STATUS_NO;
- if ($order->time_money > 0){
- // 所有金额计算完毕 如果需要支付时才消耗骑行卡
- $cardRiding = CardRidingUserBags::isExist($order->user_id,$order->start_use_bike_time);
- // Log::info($cardRiding);
- if(!empty($cardRiding)){
- // 判断骑行卡类型
- $cardLogs = CardRidingUseLog::isUseThisCard($order->id,$cardRiding->id);
- if(empty($cardLogs)){
- if($cardRiding->is_limit_times == CardRidingUserBags::LIMIT_TIMES_YES){
- // 限次卡
- $discount_time_money = bcsub($order->time_money,$cardRiding->deduction_money,2);
- // Log::info($discount_time_money);
- if($discount_time_money < 0){
- $order->preferential_money = $order->time_money;
- }else{
- $order->preferential_money = $cardRiding->deduction_money;
- }
- // Log::info($order->preferential_money);
- // Log::info($cardRiding->deduction_money);
- $order->total_money = bcsub($order->total_money,$order->preferential_money,2);
- $order->pay_money = $order->total_money;
- // 更新骑行卡
- $cardRiding->can_ridding_times -= 1;
- if($cardRiding->can_ridding_times <= 0){
- // 如果没次数了 直接使其失效
- $cardRiding->status = CardRidingUserBags::STATUS_NO;
- }
- $cardRiding->save();
- }else{
- // 不限次卡
- //查询该用户今日订单次数
- $orderNum = Order::getTodayOrderNum($order->user_id);
- if($orderNum < $cardRiding->day_can_ridding_times){
- $discount_time_money = bcsub($order->time_money,$cardRiding->deduction_money,2);
- if($discount_time_money < 0){
- $order->preferential_money = $order->time_money;
- }else{
- $order->preferential_money = $cardRiding->deduction_money;
- }
- $order->total_money = bcsub($order->total_money,$order->preferential_money,2);
- $order->pay_money = $order->total_money;
- }
- }
- $order->preferential_type = Order::PREFERENTIAL_CARD_RIDING; //优惠方式
- CardRidingUseLog::logs($cardRiding->id,$order->id,$order->preferential_money);
- }
- }
- }
- if ($order->total_money === 0) {
- $order->pay_type = Order::PAY_STATUS_OK;
- $order->status = Order::STATUS_CLOSE_ORDER;
- }
- $order->save();
- $bike->save();
- DB::commit();
- // 关车
- $closeLockBool = BikeControl::closeLock($bike->box_no);
- if(!$closeLockBool){
- Log::error('关锁失败');
- return false;
- }
- return true;
- }catch (\Exception $e){
- DB::rollBack();
- Log::error($e->getMessage());
- return false;
- }
- }
- }
|