Explorar o código

1.修复偶尔购买套餐流量没加上的问题2.购买前判断是否存在有效期更长的套餐

dapinglee %!s(int64=7) %!d(string=hai) anos
pai
achega
6232c8083b

+ 9 - 3
app/Console/Commands/AutoJob.php

@@ -338,7 +338,7 @@ class AutoJob extends Command
 
                         // 商品为流量或者套餐
                         if ($goods->type <= 2) {
-                            // 如果买的是套餐,则先将之前购买的所有套餐置都无效,并扣掉之前所有套餐的流量
+                            // 如果买的是套餐,则先将之前购买的所有套餐置都无效,并扣掉之前所有套餐的流量,重置用户已用流量为0
                             if ($goods->type == 2) {
                                 $existOrderList = Order::query()
                                     ->with(['goods'])
@@ -350,10 +350,16 @@ class AutoJob extends Command
                                     ->where('is_expire', 0)
                                     ->where('status', 2)
                                     ->get();
-
+                                
                                 foreach ($existOrderList as $vo) {
                                     Order::query()->where('oid', $vo->oid)->update(['is_expire' => 1]);
-                                    User::query()->where('id', $order->user_id)->decrement('transfer_enable', $vo->goods->traffic * 1048576);
+                                    // 先判断,防止手动扣减过流量的用户流量被扣成负数
+                                    if ($order->user->transfer_enable - $vo->goods->traffic * 1048576 <= 0) {
+                                        User::query()->where('id', $order->user_id)->update(['u' => 0, 'd' => 0, 'transfer_enable' => 0]);
+                                    } else {
+                                        User::query()->where('id', $order->user_id)->update(['u' => 0, 'd' => 0]);
+                                        User::query()->where('id', $order->user_id)->decrement('transfer_enable', $vo->goods->traffic * 1048576);
+                                    }
                                 }
                             }
 

+ 9 - 3
app/Http/Controllers/Api/YzyController.php

@@ -172,7 +172,7 @@ class YzyController extends Controller
 
             // 商品为流量或者套餐
             if ($goods->type <= 2) {
-                // 如果买的是套餐,则先将之前购买的所有套餐置都无效,并扣掉之前所有套餐的流量
+                // 如果买的是套餐,则先将之前购买的所有套餐置都无效,并扣掉之前所有套餐的流量,重置用户已用流量为0
                 if ($goods->type == 2) {
                     $existOrderList = Order::query()
                         ->with(['goods'])
@@ -184,10 +184,16 @@ class YzyController extends Controller
                         ->where('is_expire', 0)
                         ->where('status', 2)
                         ->get();
-
+                    
                     foreach ($existOrderList as $vo) {
                         Order::query()->where('oid', $vo->oid)->update(['is_expire' => 1]);
-                        User::query()->where('id', $order->user_id)->decrement('transfer_enable', $vo->goods->traffic * 1048576);
+                        // 先判断,防止手动扣减过流量的用户流量被扣成负数
+                        if ($order->user->transfer_enable - $vo->goods->traffic * 1048576 <= 0) {
+                            User::query()->where('id', $order->user_id)->update(['u' => 0, 'd' => 0, 'transfer_enable' => 0]);
+                        } else {
+                            User::query()->where('id', $order->user_id)->update(['u' => 0, 'd' => 0]);
+                            User::query()->where('id', $order->user_id)->decrement('transfer_enable', $vo->goods->traffic * 1048576);
+                        }
                     }
                 }
 

+ 19 - 0
app/Http/Controllers/PaymentController.php

@@ -79,6 +79,25 @@ class PaymentController extends Controller
             return Response::json(['status' => 'fail', 'data' => '', 'message' => '创建支付单失败:订单总价为0,无需使用在线支付']);
         }
 
+        // 验证账号是否存在有效期更长的套餐
+        if ($goods->type == 2) {
+            $existOrderList = Order::query()
+                    ->with(['goods'])
+                    ->whereHas('goods', function ($q) {
+                        $q->where('type', 2);
+                    })
+                    ->where('user_id', $user['id'])
+                    ->where('is_expire', 0)
+                    ->where('status', 2)
+                    ->get();
+
+            foreach ($existOrderList as $vo) {
+                if ($vo->goods->days > $goods->days) {
+                    return Response::json(['status' => 'fail', 'data' => '', 'message' => '支付失败:您已存在有效期更长的套餐,只能购买流量包']);
+                }
+            }
+        }
+
         DB::beginTransaction();
         try {
             $orderSn = date('ymdHis') . mt_rand(100000, 999999);

+ 39 - 6
app/Http/Controllers/UserController.php

@@ -948,6 +948,25 @@ class UserController extends Controller
                 return Response::json(['status' => 'fail', 'data' => '', 'message' => '支付失败:您的余额不足,请先充值']);
             }
 
+            // 验证账号是否存在有效期更长的套餐
+            if ($goods->type == 2) {
+                $existOrderList = Order::query()
+                        ->with(['goods'])
+                        ->whereHas('goods', function ($q) {
+                            $q->where('type', 2);
+                        })
+                        ->where('user_id', $user['id'])
+                        ->where('is_expire', 0)
+                        ->where('status', 2)
+                        ->get();
+
+                foreach ($existOrderList as $vo) {
+                    if ($vo->goods->days > $goods->days) {
+                        return Response::json(['status' => 'fail', 'data' => '', 'message' => '支付失败:您已存在有效期更长的套餐,只能购买流量包']);
+                    }
+                }
+            }
+
             DB::beginTransaction();
             try {
                 // 生成订单
@@ -981,14 +1000,28 @@ class UserController extends Controller
                     $this->addCouponLog($coupon->id, $goods_id, $order->oid, '余额支付订单使用');
                 }
 
-                // 如果买的是套餐,则先将之前购买的所有套餐置都无效,并扣掉之前所有套餐的流量,并移除之前所有套餐的标签
-                if ($goods->type === 2) {
-                    $existOrderList = Order::query()->with('goods')->whereHas('goods', function ($q) {
-                        $q->where('type', 2);
-                    })->where('user_id', $user->id)->where('oid', '<>', $order->oid)->where('is_expire', 0)->where('status', 2)->get();
+                // 如果买的是套餐,则先将之前购买的所有套餐置都无效,并扣掉之前所有套餐的流量,重置用户已用流量为0
+                if ($goods->type == 2) {
+                    $existOrderList = Order::query()
+                        ->with(['goods'])
+                        ->whereHas('goods', function ($q) {
+                            $q->where('type', 2);
+                        })
+                        ->where('user_id', $order->user_id)
+                        ->where('oid', '<>', $order->oid)
+                        ->where('is_expire', 0)
+                        ->where('status', 2)
+                        ->get();
+                    
                     foreach ($existOrderList as $vo) {
                         Order::query()->where('oid', $vo->oid)->update(['is_expire' => 1]);
-                        User::query()->where('id', $user->id)->decrement('transfer_enable', $vo->goods->traffic * 1048576);
+                        // 先判断,防止手动扣减过流量的用户流量被扣成负数
+                        if ($order->user->transfer_enable - $vo->goods->traffic * 1048576 <= 0) {
+                            User::query()->where('id', $order->user_id)->update(['u' => 0, 'd' => 0, 'transfer_enable' => 0]);
+                        } else {
+                            User::query()->where('id', $order->user_id)->update(['u' => 0, 'd' => 0]);
+                            User::query()->where('id', $order->user_id)->decrement('transfer_enable', $vo->goods->traffic * 1048576);
+                        }
                     }
                 }