Bläddra i källkod

用户渠道变更 同步更新注册统计

Tree 4 dagar sedan
förälder
incheckning
aaed1354fb

+ 71 - 13
app/Http/Controllers/Api/AgentClickController.php

@@ -135,19 +135,67 @@ class AgentClickController extends Controller
             ->header('Content-Type', 'image/gif')
             ->header('Content-Length', strlen($gif));
     }
-    static function changePlatformData($orgChannel,$newChannel)
+    /**
+     * 用户归属渠道变更时,同步分渠道日统计(RecordPlatformData),并尽力同步 LogOnLineCountSum(若表含 Channel 列)。
+     *
+     * @param  int|string      $orgChannel  原渠道
+     * @param  int|string      $newChannel  新渠道
+     * @param  string|null     $dateId      统计日期 Ymd(应用注册日);null 则当前服务器日,兼容旧调用
+     */
+    static function changePlatformData($orgChannel, $newChannel, $dateId = null)
     {
+        $org = (int) $orgChannel;
+        $nw = (int) $newChannel;
+        if ($org === $nw) {
+            return false;
+        }
+
+        $dateId = $dateId ?: date('Ymd');
+
         DB::connection('write')->table(TableName::QPRecordDB() . 'RecordPlatformData')
-            ->where('DateID', date('Ymd'))
-            ->where('Channel', $orgChannel)
-            ->update(['RegPeple'=>DB::raw('RegPeple-1'),'ActivePeple'=>DB::raw('ActivePeple-1')]);
+            ->where('DateID', $dateId)
+            ->where('Channel', $org)
+            ->update(['RegPeple' => DB::raw('RegPeple-1')]);
 
         DB::connection('write')->table(TableName::QPRecordDB() . 'RecordPlatformData')
-            ->where('DateID', date('Ymd'))
-            ->where('Channel', $newChannel)
-            ->update(['RegPeple'=>DB::raw('RegPeple+1'),'ActivePeple'=>DB::raw('ActivePeple+1')]);
+            ->where('DateID', $dateId)
+            ->where('Channel', $nw)
+            ->update(['RegPeple' => DB::raw('RegPeple+1')]);
 
+        //self::syncLogOnLineCountSumRegisterByChannel($org, $nw, $dateId);
+        return true;
+    }
 
+    /**
+     * 若 QPRecordDB.dbo.LogOnLineCountSum 存在 Channel、RegisterCount 且按日聚合,则在渠道迁移时调整分渠道新增注册数。
+     * 若库表无 Channel 列或结构与假设不符,静默跳过(记录 debug 日志)。
+     */
+    private static function syncLogOnLineCountSumRegisterByChannel(int $orgChannel, int $newChannel, string $dateId): void
+    {
+        if (!preg_match('/^\d{8}$/', $dateId)) {
+            return;
+        }
+        $dateStr = substr($dateId, 0, 4) . '-' . substr($dateId, 4, 2) . '-' . substr($dateId, 6, 2);
+        try {
+            $tbl = TableName::QPRecordDB() . 'LogOnLineCountSum';
+            DB::connection('write')->update(
+                'UPDATE ' . $tbl . ' SET RegisterCount = CASE WHEN RegisterCount > 0 THEN RegisterCount - 1 ELSE 0 END '
+                . 'WHERE CAST(InsertDateTime AS DATE) = CAST(? AS DATE) AND Channel = ?',
+                [$dateStr, $orgChannel]
+            );
+            DB::connection('write')->update(
+                'UPDATE ' . $tbl . ' SET RegisterCount = RegisterCount + 1 '
+                . 'WHERE CAST(InsertDateTime AS DATE) = CAST(? AS DATE) AND Channel = ?',
+                [$dateStr, $newChannel]
+            );
+        } catch (\Throwable $e) {
+            Log::debug('LogOnLineCountSum RegisterCount sync skipped (schema mismatch or no Channel)', [
+                'message' => $e->getMessage(),
+                'org' => $orgChannel,
+                'new' => $newChannel,
+                'dateId' => $dateId,
+            ]);
+        }
     }
     public static function checkClick($UserID)
     {
@@ -162,14 +210,19 @@ class AgentClickController extends Controller
         }
         if(intval($SpreaderID)){
             if($UserID!=$SpreaderID){
-                $orgChannel=AccountsInfo::find($UserID)->Channel;
-                $newChannel=AccountsInfo::find($SpreaderID)->Channel;
+                $userAcc = AccountsInfo::find($UserID);
+                if (!$userAcc) {
+                    return;
+                }
+                $orgChannel = $userAcc->Channel;
+                $newChannel = AccountsInfo::find($SpreaderID)->Channel;
+                $registerDateId = $userAcc->RegisterDate ? date('Ymd', strtotime($userAcc->RegisterDate)) : date('Ymd');
                 $BlockInviteChannel=DB::table('QPPlatformDB.dbo.ChannelPackageName')->where('Channel',$newChannel)->value('BlockInviteChannel');
                 if($BlockInviteChannel==1){
                     AccountsInfo::where('UserID', $UserID)->update(['SpreaderID' => $SpreaderID]);
                 }else {
                     AccountsInfo::where('UserID', $UserID)->update(['SpreaderID' => $SpreaderID, 'Channel' => $newChannel]);
-                    self::changePlatformData($orgChannel, $newChannel);
+                    self::changePlatformData($orgChannel, $newChannel, $registerDateId);
                 }
             }
             return;
@@ -203,8 +256,13 @@ class AgentClickController extends Controller
                     $click->save();
 
 
-                    $orgChannel=AccountsInfo::find($UserID)->Channel;
-                    $newChannel=AccountsInfo::find($agentInfo->UserID)->Channel;
+                    $userAcc = AccountsInfo::find($UserID);
+                    if (!$userAcc) {
+                        return;
+                    }
+                    $orgChannel = $userAcc->Channel;
+                    $newChannel = AccountsInfo::find($agentInfo->UserID)->Channel;
+                    $registerDateId = $userAcc->RegisterDate ? date('Ymd', strtotime($userAcc->RegisterDate)) : date('Ymd');
 
                     $BlockInviteChannel=DB::table('QPPlatformDB.dbo.ChannelPackageName')->where('Channel',$newChannel)->value('BlockInviteChannel');
 
@@ -212,7 +270,7 @@ class AgentClickController extends Controller
                         AccountsInfo::where('UserID', $UserID)->update(['SpreaderID' => $agentInfo->UserID]);
                     }else {
                         AccountsInfo::where('UserID', $UserID)->update(['SpreaderID'=>$agentInfo->UserID,'Channel'=>$newChannel]);
-                        self::changePlatformData($orgChannel,$newChannel);
+                        self::changePlatformData($orgChannel, $newChannel, $registerDateId);
                     }
 
 

+ 1 - 1
app/Http/Controllers/Game/AgentController.php

@@ -63,7 +63,7 @@ class AgentController extends Controller
         AccountsInfo::where('UserID',$UserID)->update(['Channel'=>$Channel]);
         if ($user && $user->Channel != $Channel) {
             Log::info("用戶渠道修改", ['UserID'=>$UserID, 'orgChannel'=>$user->Channel, 'Channel'=>$Channel]);
-            AgentClickController::changePlatformData($user->Channel, $Channel);
+            AgentClickController::changePlatformData($user->Channel, $Channel, $user->RegisterDate ? date('Ymd', strtotime($user->RegisterDate)) : date('Ymd'));
         }
     }
     public function newAgent(Request $request)

+ 5 - 0
app/Services/ApkService.php

@@ -3,6 +3,7 @@
 namespace App\Services;
 
 use App\Facade\TableName;
+use App\Http\Controllers\Api\AgentClickController;
 use App\Http\helper\HttpCurl;
 use App\Http\helper\NumConfig;
 use App\IpLocation;
@@ -356,6 +357,10 @@ class ApkService
                 DB::table(TableName::QPAccountsDB() . 'AccountsInfo')
                     ->where('UserID', $UserID)
                     ->update(['Channel' => $record->UrlSign]);
+                $registerDateId = !empty($user->RegisterDate)
+                    ? date('Ymd', strtotime($user->RegisterDate))
+                    : date('Ymd');
+                AgentClickController::changePlatformData((int) $oldChannel, (int) $record->UrlSign, $registerDateId);
                 Util::WriteLog('bindCookie', $logContext + [
                         'stage' => 'channel_corrected',
                         'old_channel' => $oldChannel,