123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296 |
- <?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 App\Utils\RedisKeys;
- 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);
- $BikeHandler = new BikeHandler();
- // $is_ban_stop_bike = $BikeHandler->byLocationCheckIsInBanStopParking($lastLocation['lat'], $lastLocation['lng'], $order->area_id);
- // if ($is_ban_stop_bike) {
- // Log::error('车辆在禁停区,无法关闭订单还车');
- // return false;
- // }
- 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();
- $closeLockBool = (new BaseBikeControl($bike->box_no))::closeLock();
- 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 ($setting->is_minute_free == AreaSetting::FREE_MINUTE_OK && $setting->many_minute_free >= $order->use_bike_time_length) {
- if ($setting->minute_free_times == 0) {
- $time_money = 0;
- } else {
- $key = sprintf(RedisKeys::FREE_MINUTE_USER_ID, $order->user_id);
- app()->redis->sadd($key, $order->id);
- app()->redis->expireat($key, Carbon::tomorrow()->timestamp);
- if (app()->redis->scard($key) <= $setting->minute_free_times) {
- $time_money = 0;
- }
- }
- }
- if ($time_money < 0) {
- $time_money = 0;
- }
- $order->dispatch_money = 0;
- $areaSetting = AreaSetting::where('area_id', $order->area_id)->first();
- if ($areaSetting->is_whole_area_huanche === AreaSetting::WHOLE_AREA_HUANCHE_NO) {
- // 是否全区域可还车
- // 判断车是否在停车的区域
- $is_huanche = $BikeHandler->byLocationCheckIsInStopParking($lastLocation['lat'], $lastLocation['lng'], $order->area_id);
- // Log::info($is_huanche);
- 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->order_money = bcadd($time_money, $order->dispatch_money, 2);
- $order->total_money = bcadd($time_money, $order->dispatch_money, 2);
- $order->pay_money = $order->order_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->order_money = bcsub($order->order_money, $order->preferential_money, 2);
- $order->pay_money = $order->order_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->order_money = bcsub($order->order_money, $order->preferential_money, 2);
- $order->pay_money = $order->order_money;
- }
- }
- $order->is_riding_card = Order::RIDING_CARD_OK;
- //优惠方式
- if ($order->preferential_type == 0) {
- $order->preferential_type = arr2str([Order::PREFERENTIAL_CARD_RIDING],'-');
- } else {
- $arr = str2arr($order->preferential_type,'-');
- $order->preferential_type = arr2str(array_merge($arr, [Order::PREFERENTIAL_CARD_RIDING]),'-');
- }
- CardRidingUseLog::logs($cardRiding->id, $order->id, $order->preferential_money);
- }
- }
- }
- if ($order->order_money === 0) {
- $order->pay_type = Order::PAY_STATUS_OK;
- $order->status = Order::STATUS_CLOSE_ORDER;
- }
- $order->save();
- $bike->save();
- // 关车
- $closeLockBool = (new BaseBikeControl($bike->box_no))::closeLock();
- if (!$closeLockBool) {
- DB::rollBack();
- Log::error('关锁失败');
- return false;
- }
- DB::commit();
- return true;
- } catch (\Exception $e) {
- DB::rollBack();
- Log::error($e->getMessage());
- return false;
- }
- }
- /**
- * 骑行卡回溯 cardRidingHuSu
- *
- * @param $order_id
- * @return void
- * @author Fx
- *
- */
- public static function cardRidingHuSu($order_id)
- {
- $order = Order::find($order_id);
- if ($order->is_riding_card == Order::RIDING_CARD_OK) {
- $card_use_log = CardRidingUseLog::where('order_id', $order->id)->first();
- if (empty($card_use_log)) return;
- $card_id = $card_use_log->card_riding_user_bags_id;
- $cardRidingUser = CardRidingUserBags::find($card_id);
- if (!empty($cardRidingUser) && $cardRidingUser->is_limit_times == CardRidingUserBags::LIMIT_TIMES_YES) {
- $cardRidingUser->can_ridding_times += 1;
- $cardRidingUser->save();
- }
- $card_use_log->delete();
- // 优惠金额全部为0
- $order->card_preferential_money = 0.00;
- $order->preferential_money = 0.00;
- $order->is_riding_card = Order::RIDING_CARD_NO;
- if ($order->preferential_type == 0) {
- $order->preferential_type = 0;
- } else {
- $arr = str2arr($order->preferential_type,'-');
- $arr = array_diff($arr, [Order::PREFERENTIAL_CARD_RIDING]);
- if (empty($arr)) {
- $order->preferential_type = 0;
- } else {
- $order->preferential_type = arr2str($arr,'-');
- }
- }
- $order->save();
- }
- }
- }
|