Browse Source

🔧 Fixed bugs

- Fixed Order Status manually change does not following logic;
- Fixed User get node() will report null sign to int;
- Fixed query where default "null " is not work as expect;
BrettonYe 2 năm trước cách đây
mục cha
commit
454051c386

+ 1 - 1
app/Console/Commands/AutoClearLogs.php

@@ -50,7 +50,7 @@ class AutoClearLogs extends Command
 
             NodeOnlineIp::where('created_at', '<=', strtotime(config('tasks.clean.node_online_ips')))->delete(); // 清除用户连接IP
 
-            UserDailyDataFlow::where('node_id', '<>')
+            UserDailyDataFlow::where('node_id', '<>', null)
                 ->where('created_at', '<=', date('Y-m-d H:i:s', strtotime(config('tasks.clean.user_daily_logs_nodes'))))
                 ->orWhere('created_at', '<=', date('Y-m-d H:i:s', strtotime(config('tasks.clean.user_daily_logs_total'))))
                 ->delete(); // 清除用户各节点 / 节点总计的每天流量数据日志

+ 1 - 1
app/Http/Controllers/Admin/UserController.php

@@ -67,7 +67,7 @@ class UserController extends Controller
 
         // 不活跃用户
         $request->whenFilled('paying', function () use ($query) {
-            $payingUser = Order::whereStatus(2)->where('goods_id', '<>')->whereIsExpire(0)->where('amount', '>', 0)->pluck('user_id')->unique();
+            $payingUser = Order::whereStatus(2)->where('goods_id', '<>', null)->whereIsExpire(0)->where('amount', '>', 0)->pluck('user_id')->unique();
             $query->whereIn('id', $payingUser);
         });
 

+ 1 - 1
app/Http/Controllers/AdminController.php

@@ -35,7 +35,7 @@ class AdminController extends Controller
             'todayRegister' => User::whereDate('created_at', date('Y-m-d'))->count(), // 今日注册用户
             'enableUserCount' => User::whereEnable(1)->count(), // 有效用户数
             'activeUserCount' => User::where('t', '>=', $past)->count(), // 活跃用户数,
-            'payingUserCount' => Order::whereStatus(2)->where('goods_id', '<>')->whereIsExpire(0)->where('amount', '>', 0)->pluck('user_id')->unique()->count(), // 付费用户数
+            'payingUserCount' => Order::whereStatus(2)->where('goods_id', '<>', null)->whereIsExpire(0)->where('amount', '>', 0)->pluck('user_id')->unique()->count(), // 付费用户数
             'inactiveUserCount' => User::whereEnable(1)->whereBetween('t', [1, $past])->count(), // 不活跃用户数
             'onlineUserCount' => User::where('t', '>=', strtotime('-10 minutes'))->count(), // 10分钟内在线用户数
             'expireWarningUserCount' => User::whereBetween('expired_at', [date('Y-m-d'), date('Y-m-d', strtotime(sysConfig('expire_days').' days'))])->count(), // 临近过期用户数

+ 2 - 2
app/Models/User.php

@@ -223,9 +223,9 @@ class User extends Authenticatable
         return $query->where('status', '<>', -1)->whereEnable(0);
     }
 
-    public function nodes(?int $userLevel = null, int $userGroupId = 0): Node|Builder|BelongsToMany
+    public function nodes(?int $userLevel = null, ?int $userGroupId = null): Node|Builder|BelongsToMany
     {
-        if ($userGroupId === 0 && $this->user_group_id) { // 使用默认的用户分组
+        if ($userGroupId === null && $this->user_group_id) { // 使用默认的用户分组
             $query = $this->userGroup->nodes();
         } elseif ($userGroupId) { // 使用给的用户分组
             $query = UserGroup::findOrFail($userGroupId)->nodes();

+ 9 - 3
app/Observers/OrderObserver.php

@@ -43,15 +43,21 @@ class OrderObserver
                     $this->returnCoupon($order, $order->coupon);
                 }
 
-                if ($order->goods && $order->goods->type === 2 && $order->getOriginal('status') === 2 && Order::userActivePlan($order->user_id)->where('id', '<>', $order->id)->doesntExist()) { // 下一个套餐
+                if ($order->goods && $order->goods->type === 2 && $order->getOriginal('status') === 2 && Order::userPrepay($order->user_id)->exists()) { // 下一个套餐
                     $this->activatePrepaidPlan($order->user_id);
+                } else {
+                    (new OrderService($order))->refreshAccountExpiration();
                 }
             } elseif ($changes['status'] === 1) { // 待确认支付 通知管理
                 Notification::send(User::find(1), new PaymentConfirm($order));
             } elseif ($changes['status'] === 2 && $order->getOriginal('status') !== 3) { // 本地订单-在线订单 支付成功互联
                 (new OrderService($order))->receivedPayment();
-            } elseif ($changes['status'] === 3 && Order::userActivePlan($order->user_id)->doesntExist()) {
-                $this->activatePrepaidPlan($order->user_id);
+            } elseif ($changes['status'] === 3) {
+                if (Order::userActivePlan($order->user_id)->doesntExist()) {
+                    $this->activatePrepaidPlan($order->user_id);
+                } else {
+                    (new OrderService($order))->refreshAccountExpiration();
+                }
             }
         }
     }

+ 29 - 17
app/Services/OrderService.php

@@ -18,7 +18,7 @@ class OrderService
 
     public static Payment|null $payment;
 
-    public function __construct(private readonly Order $order)
+    public function __construct(private Order $order)
     { // 获取需要的信息
         self::$user = $order->user;
         self::$goods = $order->goods;
@@ -42,7 +42,7 @@ class OrderService
                     break;
                 case 2: // 套餐
                     if (Order::userActivePlan(self::$user->id)->where('id', '<>', $this->order->id)->exists()) {// 判断套餐是否直接激活
-                        $ret = $this->setPrepaidPlan();
+                        $ret = $this->order->prepay();
                     } else {
                         $ret = $this->activatePlan();
                     }
@@ -77,21 +77,6 @@ class OrderService
         return false;
     }
 
-    private function setPrepaidPlan(): bool
-    { // 设置预支付套餐, 刷新账号有效时间用于流量重置判断
-        $this->order->prepay();
-
-        return self::$user->update(['expired_at' => $this->getFinallyExpiredTime()]);
-    }
-
-    public function getFinallyExpiredTime(): string
-    { // 推算最新的到期时间
-        $orders = self::$user->orders()->whereIn('status', [2, 3])->whereIsExpire(0)->isPlan()->get();
-        $current = $orders->where('status', '==', 2)->first();
-
-        return ($current->expired_at ?? now())->addDays($orders->except($current->id ?? 0)->sum('goods.days'))->format('Y-m-d');
-    }
-
     public function activatePlan(): bool
     { // 激活套餐
         $this->order->update(['expired_at' => date('Y-m-d H:i:s', strtotime(self::$goods->days.' days'))]);
@@ -136,6 +121,14 @@ class OrderService
         ];
     }
 
+    private function getFinallyExpiredTime(): string
+    { // 推算最新的到期时间
+        $orders = self::$user->orders()->whereIn('status', [2, 3])->whereIsExpire(0)->isPlan()->get();
+        $current = $orders->where('status', '==', 2)->first();
+
+        return ($current->expired_at ?? now())->addDays($orders->except($current->id ?? 0)->sum('goods.days'))->format('Y-m-d');
+    }
+
     private function setCommissionExpense(User $user)
     { // 佣金计算
         $referralType = sysConfig('referral_type');
@@ -162,6 +155,25 @@ class OrderService
         }
     }
 
+    public function refreshAccountExpiration(): bool
+    { // 刷新账号有效时间
+        $data = ['expired_at' => $this->getFinallyExpiredTime()];
+
+        if ($data['expired_at'] === now()->format('Y-m-d')) {
+            $data = array_merge([
+                'u' => 0,
+                'd' => 0,
+                'transfer_enable' => 0,
+                'enable' => 0,
+                'level' => 0,
+                'reset_time' => null,
+                'ban_time' => null,
+            ], $data);
+        }
+
+        return self::$user->update($data);
+    }
+
     public function activatePrepaidPlan(): bool
     { // 激活预支付套餐
         $this->order->complete();