Browse Source

fix join笛卡尔集问题

laowu 1 day ago
parent
commit
6e3cd01810
1 changed files with 41 additions and 20 deletions
  1. 41 20
      app/Models/AccountsInfo.php

+ 41 - 20
app/Models/AccountsInfo.php

@@ -644,24 +644,37 @@ class AccountsInfo extends Model
     }
     //关联提现mail
     public function sameWithDrawEmailByUserID($UserID){
-        return DB::connection('read')->table('QPAccountsDB.dbo.AccountWithDrawInfo as di')
-                 ->join('QPAccountsDB.dbo.AccountWithDrawInfo as dif', 'di.EmailAddress', 'dif.EmailAddress')
-                 ->where('di.UserID', $UserID)
-                 ->whereNotNull('di.EmailAddress')
-                 ->where('di.EmailAddress', '<>', '')
-                 ->selectRaw('count(1) count,di.EmailAddress')
-                 ->groupBy('di.EmailAddress')
-                 ->first();
+        // 先获取用户的邮箱
+        $user = DB::connection('read')->table('QPAccountsDB.dbo.AccountWithDrawInfo')
+            ->where('UserID', $UserID)
+            ->whereNotNull('EmailAddress')
+            ->where('EmailAddress', '<>', '')
+            ->first(['EmailAddress']);
+
+        $result = json_decode('{"count":null,"EmailAddress":null}');
+        if ($user) {
+            // 再查询相同邮箱的用户数
+            $result = DB::connection('read')->table('QPAccountsDB.dbo.AccountWithDrawInfo')
+                ->where('EmailAddress', $user->EmailAddress)
+                ->selectRaw('COUNT(*) as count, ? as EmailAddress', [$user->EmailAddress])
+                ->first();
+        }
+        return $result;
 
     }
     // 关联登录IP数量
     public function sameLoginIPCount($UserID)
     {
-        return DB::connection('read')->table('QPRecordDB.dbo.RecordUserLogonStatistics as a')
-            ->join('QPRecordDB.dbo.RecordUserLogonStatistics as b', 'a.LogonIP', 'b.LogonIP')
-            ->where('a.UserID', $UserID)
-            ->selectRaw('count(distinct(b.UserID)) countIP')
+        $ips = DB::connection('read')->table('QPRecordDB.dbo.RecordUserLogonStatistics')
+            ->where('UserID', $UserID)
+            ->distinct()
+            ->pluck('LogonIP');
+
+        $count = DB::connection('read')->table('QPRecordDB.dbo.RecordUserLogonStatistics')
+            ->whereIn('LogonIP', $ips)
+            ->selectRaw('count(distinct UserID) countIP')
             ->first()->countIP;
+        return $count;
     }
 
 
@@ -688,14 +701,22 @@ class AccountsInfo extends Model
     // 关联银行卡
     public function sameBankNo($UserID)
     {
-        return DB::connection('read')->table('QPAccountsDB.dbo.AccountWithDrawInfo as di')
-            ->join('QPAccountsDB.dbo.AccountWithDrawInfo as dif', 'di.BankNo', 'dif.BankNo')
-            ->where('di.UserID', $UserID)
-            ->whereNotNull('di.BankNo')
-            ->where('di.BankNo', '<>', '')
-            ->selectRaw('count(1) count,di.BankNo')
-            ->groupBy('di.BankNo')
-            ->first();
+        // 步骤1: 获取当前用户的BankNo
+        $user = DB::connection('read')->table('QPAccountsDB.dbo.AccountWithDrawInfo')
+            ->where('UserID', $UserID)
+            ->whereNotNull('BankNo')
+            ->where('BankNo', '<>', '')
+            ->first(['BankNo']);
+
+        // 步骤2: 查询相同BankNo的数量
+        $result = json_decode('{"count":null,"BankNo":null}');
+        if ($user) {
+            $result = DB::connection('read')->table('QPAccountsDB.dbo.AccountWithDrawInfo')
+                ->where('BankNo', $user->BankNo)
+                ->selectRaw('count(*) as count, ? as BankNo', [$user->BankNo])
+                ->first();
+        }
+        return $result;
     }
 
     // 用户彩金