1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
<?php
set_time_limit(600); //暂时设置本脚本执行时间x秒,0不限制
ini_set('memory_limit', '256M'); // 将内存限制设置为256MB
 
include_once '/Common/CommFunc.php';
include_once '/Common/Logging.php';
include_once "/db/DBOper.php";
include_once "/serverrep/report.php";
 
header("Content-type: text/html; charset=utf-8");
 
\Logging\CreateLogging("rep.ImportAccountLoginpay.php");
 
$Channel = $_GET["appID"];
$opt = $_GET["opt"];
if (!$Channel) {
    echo "no appID";
    exit();
}
logweb("开始导入:" . $Channel . " opt:" . $opt);
if (array_key_exists("reset", $_GET)) {
    logweb("重置重新导出:" . $Channel);
    $AccountFirstLoginOK = \DBOper\Remove("AccountFirstLogin", array("Channel" => $Channel));
    $AccountFirstPayOK = \DBOper\Remove("AccountFirstPay", array("Channel" => $Channel));
    $AccountDayActiveOK = \DBOper\Remove("AccountDayActive", array("Channel" => $Channel));
    $DailyReportOK = \DBOper\Remove("DailyReport", array("Channel" => $Channel));
    $AccountFirstLoginReportOK = \DBOper\Remove("AccountFirstLoginReport", array("Channel" => $Channel));
    $AccountFirstPayReportOK = \DBOper\Remove("AccountFirstPayReport", array("Channel" => $Channel));
    $eventYMDOK = \DBOper\Remove("ServerEvent", array("Key" => "FirstKeepReportYMD_" . $Channel));
    logweb("AccountFirstLoginOK:" . $AccountFirstLoginOK);
    logweb("AccountFirstPayOK:" . $AccountFirstPayOK);
    logweb("AccountDayActiveOK:" . $AccountDayActiveOK);
    logweb("DailyReportOK:" . $DailyReportOK);
    logweb("AccountFirstLoginReportOK:" . $AccountFirstLoginReportOK);
    logweb("AccountFirstPayReportOK:" . $AccountFirstPayReportOK);
    logweb("eventYMDOK:" . $eventYMDOK);
}
if (!$opt || $opt == "first") {
    logweb("firstfirstfirst:" . $Channel);
    impAccountFirstLogin();
    impAccountFirstPay();
}
if (!$opt || $opt == "day") {
    logweb("daydaydaydayday:" . $Channel);
    impAccountDayActive("rar");
    // impAccountDayActiveTestData();
}
// 重导日活、重刷报表;前提是先试用 /Server/eventdata/ImportBakDailyActive.py 运行导出中心流向备档日活数据结果保存到json文件
if ($opt == "reDayActive") {
    $AccountDayActiveOK = \DBOper\Remove("AccountDayActive", array("Channel" => $Channel));
    logweb("AccountDayActiveOK:" . $AccountDayActiveOK);
    impAccountDayActive("json");
}
if (!$opt || $opt == "rep" || $opt == "rerep" || $opt == "reDayActive") {
    logweb("开始导出每日报表:" . $Channel);
    if ($opt == "rerep" || $opt == "reDayActive") {
        $DailyReportOK = \DBOper\Remove("DailyReport", array("Channel" => $Channel));
        $AccountFirstLoginReportOK = \DBOper\Remove("AccountFirstLoginReport", array("Channel" => $Channel));
        $AccountFirstPayReportOK = \DBOper\Remove("AccountFirstPayReport", array("Channel" => $Channel));
        $eventYMDOK = \DBOper\Remove("ServerEvent", array("Key" => "FirstKeepReportYMD_" . $Channel));
        logweb("DailyReportOK:" . $DailyReportOK);
        logweb("AccountFirstLoginReportOK:" . $AccountFirstLoginReportOK);
        logweb("AccountFirstPayReportOK:" . $AccountFirstPayReportOK);
        logweb("eventYMDOK:" . $eventYMDOK);
    }
    \Report\CheckAndExportDailyReport($Channel);
    logweb("开始导出留存报表:" . $Channel);
    \Report\CheckAndExportFirstKeepReport($Channel);
}
logweb("处理完毕!");
exit();
 
function impAccountFirstLogin()
{
    global $Channel;
    logweb("开始导入首登账号");
    \DBOper\Find("AccountFirstLogin", array("Channel" => $Channel), $findFirstLoginDataList, array("AccountID" => 1));
    $firstLoginAccountIDList = array();
    foreach ($findFirstLoginDataList as $findData) {
        $firstLoginAccountIDList[$findData["AccountID"]] = 1;
    }
    logweb("已存在首登账号数:" . count($firstLoginAccountIDList));
 
    $limit = 10000;
    $skip = 0;
    $succCountTotal = 0;
 
    $gameRolesCount = \DBOper\Count("GameRoles", array("Channel" => $Channel));
    logweb("GameRoles 总条数:" . $gameRolesCount);
 
    while ($gameRolesCount > 0) {
        $ret = \DBOper\Find("GameRoles", array("Channel" => $Channel), $rolesDataList, null, null, $limit, $skip);
        logweb("查询GameRoles:  limit:" . $limit . "    skip:" . $skip . "    ret:" . $ret . " findCount:" . count($rolesDataList));
 
        $batchInsFirstLogin = array();
        foreach ($rolesDataList as $findData) {
            $AccountID = $findData["AccountID"];
            if (isset($firstLoginAccountIDList[$AccountID])) {
                continue;
            }
            $firstLoginAccountIDList[$AccountID] = 1;
 
            $firstLoginInfo = null;
            $firstLoginTime = "";
            foreach ($findData as $field => $value) {
                $ServerID = \CommFunc\GetServerIDBySid($field);
                if ($ServerID <= 0) {
                    continue;
                }
                $roleInfo = $value;
                $roleInfo["ServerID"] = $ServerID;
                $CreateRoleTime = $roleInfo["CreateRoleTime"];
                // $PlayerID = $roleInfo["PlayerID"];
                // $PlayerName = $roleInfo["PlayerName"];
                // $Job = $roleInfo["Job"];
                // $IP = $roleInfo["IP"];
                // $LoginTime = $roleInfo["LoginTime"];
                // $LogoffTime = $roleInfo["LogoffTime"];
 
                // 首次创角
                if ($CreateRoleTime < $firstLoginTime || $firstLoginTime == "") {
                    $firstLoginTime = $CreateRoleTime;
                    $firstLoginInfo = $roleInfo;
                }
            }
 
            if (isset($firstLoginInfo)) {
                $CreateRoleTime = $firstLoginInfo["CreateRoleTime"];
                $CreateYMD = substr($CreateRoleTime, 0, 10);
 
                if (!array_key_exists($CreateYMD, $batchInsFirstLogin)) {
                    $batchInsFirstLogin[$CreateYMD] = array();
                }
                $insArray = $batchInsFirstLogin[$CreateYMD];
                array_push($insArray, array(
                    "Channel" => $Channel,
                    "AccountID" => $AccountID,
                    "CreateYMD" => $CreateYMD,
                    "CreateTime" => $CreateRoleTime,
                    "ServerID" => $firstLoginInfo["ServerID"],
                    "PlayerID" => $firstLoginInfo["PlayerID"],
                    "PlayerName" => $firstLoginInfo["PlayerName"],
                    "Job" => $firstLoginInfo["Job"],
                    "IP" => $firstLoginInfo["IP"],
                ));
                $batchInsFirstLogin[$CreateYMD] = $insArray;
            }
        }
 
        logweb("    firstLoginAccountIDList:" . count($firstLoginAccountIDList));
        $gameRolesCount -= $limit;
        $skip += $limit;
 
        if (count($batchInsFirstLogin)) {
            ksort($batchInsFirstLogin);
            $succCount = 0;
            foreach ($batchInsFirstLogin as $CreateYMD => $insArray) {
                if (DBOper\BatchInsert("AccountFirstLogin", $insArray)) {
                    // logweb("批量导入首登账号数:  CreateYMD:" . $CreateYMD . " count:" . count($insArray));
                    $succCount += count($insArray);
                } else {
                    logweb("###批量导入首登账号数异常:  CreateYMD:" . $CreateYMD . " count:" . count($insArray));
                }
            }
            $succCountTotal += $succCount;
            logweb("新增导入首登账号数: " . $succCount);
        }
    }
 
    logweb("本次新增导入首登账号总数: " . $succCountTotal);
    \Logging\LogInfo("---------------------------------------------------------------");
    echo "<hr/>";
}
 
function impAccountFirstPay()
{
    global $Channel;
    logweb("开始导入首充账号");
    \DBOper\Find("AccountFirstPay", array("Channel" => $Channel), $findFirstPayDataList, array("AccountID" => 1));
    $firstPayAccountIDList = array();
    foreach ($findFirstPayDataList as $findData) {
        $firstPayAccountIDList[$findData["AccountID"]] = 1;
    }
    logweb("已存在首充账号数:" . count($firstPayAccountIDList));
 
    $payOrderCount = \DBOper\Count("PayOrder", array("Channel" => $Channel));
    logweb("PayOrder 总条数:" . $payOrderCount);
 
    $limit = 10000;
    $skip = 0;
    $succCountTotal = 0;
 
    while ($payOrderCount > 0) {
        $ret = \DBOper\Find("PayOrder", array("Channel" => $Channel), $payDataList, null, array("PayTime" => 1), $limit, $skip);
        logweb("查询PayOrder:  limit:" . $limit . "    skip:" . $skip . "    ret:" . $ret . " findCount:" . count($payDataList));
 
        $batchInsFirstPay = array();
        foreach ($payDataList as $payOrderInfo) {
            $AccountID = $payOrderInfo["AccountID"];
            if (isset($firstPayAccountIDList[$AccountID])) {
                continue;
            }
            $firstPayAccountIDList[$AccountID] = 1;
 
            $PayYMD = substr($payOrderInfo["PayTime"], 0, 10);
 
            if (!array_key_exists($PayYMD, $batchInsFirstPay)) {
                $batchInsFirstPay[$PayYMD] = array();
            }
            $insArray = $batchInsFirstPay[$PayYMD];
 
            array_push($insArray, array(
                "Channel" => $Channel,
                "AccountID" => $AccountID,
                "OrderID" => $payOrderInfo["OrderID"],
                "OrderIDSDK" => $payOrderInfo["OrderIDSDK"],
                "ServerID" => $payOrderInfo["ServerID"],
                "OrderInfo" => $payOrderInfo["OrderInfo"],
                "OrderAmount" => $payOrderInfo["OrderAmount"], // 中心的这个字段是实际支付金额
                "OriginalAmount" => $payOrderInfo["OriginalAmount"],
                "PayTime" => $payOrderInfo["PayTime"],
                "PayYMD" => $PayYMD,
                "Extras" => $payOrderInfo["Extras"],
            ));
 
            $batchInsFirstPay[$PayYMD] = $insArray;
        }
 
        logweb("    firstPayAccountIDList:" . count($firstPayAccountIDList));
        $payOrderCount -= $limit;
        $skip += $limit;
 
        if (count($batchInsFirstPay)) {
            ksort($batchInsFirstPay);
            $succCount = 0;
            foreach ($batchInsFirstPay as $PayTime => $insArray) {
                if (DBOper\BatchInsert("AccountFirstPay", $insArray)) {
                    // logweb("批量导入首充账号数:  PayTime:" . $PayTime . " count:" . count($insArray));
                    $succCount += count($insArray);
                } else {
                    logweb("###批量导入首充账号数异常:  PayTime:" . $PayTime . " count:" . count($insArray));
                }
            }
            $succCountTotal += $succCount;
            logweb("新增导入首充账号数: " . $succCount);
        }
    }
 
    logweb("本次新增导入首充账号总数: " . $succCountTotal);
    \Logging\LogInfo("---------------------------------------------------------------");
    echo "<hr/>";
}
 
function impAccountDayActive($bakWay = "")
{
    global $Channel;
    $startDate = $_GET["startDate"];
    if (!$startDate) {
        $startDate = "2024-05-01";
    }
    logweb("开始从中心导入备档日活数据! startDate:" . $startDate);
 
    $allDateAccIDInfo = array(); // 所有记录
    // 查询备份流向
    if ($bakWay == "rar") {
        $pack_data = array();
        $pack_data["spID"] = $Channel;
        $pack_data["eventType"] = "QueryAccountLoginOut";
        $pack_data["queryCenterbak"] = 1; # 设置查询中心的
        $pack_data["allServer"] = 1; # 设置检索所有服务器流向
        $pack_data["startDate"] = $startDate;
        $pack_data["endDate"] = "";
        $centerToolUrl = 'http://' . $_SERVER['HTTP_HOST'] . "/Server/eventdata/toolcenter.php";
        logweb("centerToolUrl:" . $centerToolUrl);
 
        $retStr = \CommFunc\DoPost($centerToolUrl, $pack_data, false, 300);
        $ret = json_decode($retStr, true);
        if (isset($ret)) {
            $allDateAccIDInfo = $ret[0];
        } else {
            logweb("query center backup error: " . $retStr);
            return;
        }
    } elseif ($bakWay == "json") {
        if (!\CommFunc\GetConfig("ServerInfo", "PlayerCenterRoot", $PlayerCenterRoot)) {
            return "";
        }
        $jsonFile = $PlayerCenterRoot . "/Server/eventdata/BakDailyActive.json";
        logweb("jsonFile:" . $jsonFile);
        if (!file_exists($jsonFile)) {
            logweb("###jsonFile is not exists:" . $jsonFile);
            return;
        }
        $allDateAccIDInfo = json_decode(file_get_contents($jsonFile), true);
    }
    if (!isset($allDateAccIDInfo)) {
        logweb("###center bak AccountDayActive error.");
        return;
    }
    ksort($allDateAccIDInfo);
    foreach ($allDateAccIDInfo as $dateStr => $accIDInfo) {
        logweb("中心流向备档日活数量:" . " dateStr:" . $dateStr . " count:" . count($accIDInfo));
    }
 
    logweb("开始从子服导入日活数据! startDate:" . $startDate);
    $pack_data = array();
    $pack_data["spID"] = $Channel;
    $pack_data["eventType"] = "QueryAccountLoginOut";
    // $pack_data["queryAllData"] = "on"; // 设置可查询备份数据,子服不查备份流向,防止同时多服访问多次
    $pack_data["startDate"] = $startDate;
    $pack_data["endDate"] = "";
    $sendServers = array();
    $serverPageInfos = \CommFunc\GetGameServerPageInfo($Channel);
    // 多服查询的
    foreach ($serverPageInfos as $mainServerID => $serverInfo) {
        $sendServers[] = array($mainServerID, \CommFunc\GetQueryEventToolUrl($serverInfo['Page']), $pack_data);
    }
    \Logging\LogInfo("待发送的服务器信息: " . count($sendServers) . " " . print_r($sendServers, true));
 
    $retList = array();
    $retMulti = \CommFunc\DoPostMulti($sendServers);
    if ($retMulti && count($retMulti) == count($sendServers)) {
        for ($i = 0; $i < count($sendServers); $i++) {
            $ret = json_decode($retMulti[$i], true);
            if (!isset($ret)) {
                $retList[$sendServers[$i][0]] = $retMulti[$i];
            } else {
                $retList[$sendServers[$i][0]] = $ret;
            }
        }
    }
 
    $retCount = count($retList);
    \Logging\LogInfo("返回查询结果条数: " .  $retCount);
    // \Logging\LogInfo("retList: " .  print_r($retList, true));
 
    // 先合并中心备份 及 各服务器数据
    foreach ($retList as $serverName => $ret) {
        logweb("-----");
        if (is_array($ret) && $ret["OK"] == 1) {
            ksort($ret["loginoutDateAccIDInfo"]);
            foreach ($ret["loginoutDateAccIDInfo"] as $dateStr => $accIDInfo) {
                logweb("服务器返回日活数量 serverName:" . $serverName . " dateStr:" . $dateStr . " count:" . count($accIDInfo));
                if (!isset($allDateAccIDInfo[$dateStr])) {
                    $allDateAccIDInfo[$dateStr] = array();
                }
                $allAccIDInfo = $allDateAccIDInfo[$dateStr];
                foreach ($accIDInfo as $accID => $loginoutInfo) {
                    $allAccIDInfo[$accID] = $loginoutInfo; // 各子服一定是最新的,直接替换备份流向数据
                }
                $allDateAccIDInfo[$dateStr] = $allAccIDInfo;
            }
        } else {
            logweb("###服务器返回日活数据异常! serverName:" . $serverName . " ret:" . $ret);
        }
    }
 
    ksort($allDateAccIDInfo);
    foreach ($allDateAccIDInfo as $dateStr => $accIDInfo) {
        logweb("日活游戏账号数量:" . " dateStr:" . $dateStr . " count:" . count($accIDInfo));
        $accountIDArray = array();
        $dateInsInfo = array();
        foreach ($accIDInfo as $accID => $loginoutInfo) {
            // logweb("    accID:" . $accID. json_encode($loginoutInfo));
            $accIDParts = explode("@", $accID);
            $AccountID = implode("@", array_slice($accIDParts, 0, count($accIDParts) - 2));
            $ServerID = \CommFunc\GetServerIDBySid($accIDParts[count($accIDParts) - 1]);
            $LoginTime = $loginoutInfo[0];
            $LogoffTime = $loginoutInfo[1];
            $IP = $loginoutInfo[2];
            $ActiveYMD = "";
            if ($LoginTime != "") {
                $ActiveYMD = substr($LoginTime, 0, 10);
            } else {
                $ActiveYMD = substr($LogoffTime, 0, 10);
            }
 
            if ($accountIDArray[$AccountID]) {
                logweb("###今日日活账号已存在:AccountID:" . $$AccountID . " accID:" . $accID);
                continue;
            }
            $accountIDArray[$AccountID] = 1;
            array_push($dateInsInfo, array(
                "Channel" => $Channel,
                "AccountID" => $AccountID,
                "ActiveYMD" => $ActiveYMD,
                "LoginTime" => $LoginTime,
                "LogoffTime" => $LogoffTime,
                "ServerID" => $ServerID,
                "IP" => $IP,
            ));
        }
 
        $actCount = DBOper\Count("AccountDayActive", array("Channel" => $Channel, "ActiveYMD" => $dateStr));
        if ($actCount == count($dateInsInfo)) {
            logweb("日活数据数相同,不重新插入: " . $dateStr . " count:" . $actCount);
            continue;
        }
        // 数据量不同,则删除后重新插入
        if ($actCount > 0) {
            if (!DBOper\Remove("AccountDayActive", array("Channel" => $Channel, "ActiveYMD" => $dateStr), false)) {
                logweb("###日活数据数不同,移除时失败!: " . $dateStr . " delCount:" . $actCount . " insCount:" . count($dateInsInfo));
                continue;
            }
        }
        if (DBOper\BatchInsert("AccountDayActive", $dateInsInfo)) {
            logweb("批量导入日活数:" . $dateStr . " count:" . count($dateInsInfo));
        } else {
            logweb("###批量导入日活数失败:" . $dateStr . " count:" . count($dateInsInfo));
        }
    }
 
    \Logging\LogInfo("---------------------------------------------------------------");
    echo "<hr/>";
}
 
// 插入山寨日活数据
function impAccountDayActiveTestData()
{
    global $Channel;
    DBOper\Remove("AccountDayActive", array(), false);
    \DBOper\Find("AccountFirstLogin", array("Channel" => $Channel), $findFirstLoginDataList, array("CreateYMD" => 1, "AccountID" => 1));
    $firstAccountIDInfo = array();
    foreach ($findFirstLoginDataList as $findData) {
        $CreateYMD = $findData["CreateYMD"];
        $AccountID = $findData["AccountID"];
        if (!isset($firstAccountIDInfo[$CreateYMD])) {
            $firstAccountIDInfo[$CreateYMD] = array();
        }
        $accountIDList = $firstAccountIDInfo[$CreateYMD];
        array_push($accountIDList, $AccountID);
        $firstAccountIDInfo[$CreateYMD] = $accountIDList;
    }
 
    $keepPer = 100;
    $keepPerSet = array(100, 50, 35, 30, 25, 15, 10);
    ksort($firstAccountIDInfo);
    foreach ($firstAccountIDInfo as $CreateYMD => $accountIDList) {
        logweb("CreateYMD:" . $CreateYMD . " count:" . count($accountIDList));
        if ($CreateYMD < "2024-05-23") {
            continue;
        }
        $dateInsInfo = array();
        $accountCount = count($accountIDList);
        $onePerCount = $accountCount * 0.1;
        for ($i = 0; $i < 30; $i++) {
            // $randNum = mt_rand(1, 100);
            if (count($keepPerSet) > $i) {
                $keepPer = $keepPerSet[$i];
            } else {
                $keepPer = mt_rand(-1, 5);
            }
            if ($keepPer <= 0) {
                continue;
            }
 
            $keepCount = $accountCount;
            if ($keepPer < 100) {
                $keepCount = intval($accountCount * $keepPer / 100.0);
                $keepCount = mt_rand($keepCount - $onePerCount, $keepCount + $onePerCount);
            }
            if ($keepCount <= 0) {
                continue;
            }
 
            $ActiveYMD = date("Y-m-d", strtotime("+" . $i . " day", strtotime($CreateYMD)));
            logweb("CreateYMD:" . $CreateYMD . " i:" . $i . " ActiveYMD:" . $ActiveYMD . " keepPer:" . $keepPer . " keepCount:" . $keepCount);
 
            for ($k = 0; $k < $keepCount; $k++) {
                $AccountID = $accountIDList[$k];
                array_push($dateInsInfo, array(
                    "Channel" => $Channel,
                    "AccountID" => $AccountID,
                    "ActiveYMD" => $ActiveYMD,
                    "LoginTime" => $ActiveYMD . " 00:00:00",
                    "LogoffTime" => $ActiveYMD . " 23:59:59",
                    "ServerID" => 999,
                    "IP" => "127.0.0.1",
                ));
            }
        }
 
        if (DBOper\BatchInsert("AccountDayActive", $dateInsInfo)) {
            logweb("批量山寨日活数, CreateYMD:" . $CreateYMD . " count:" . count($dateInsInfo));
        } else {
            logweb("###批量山寨日活数失败, CreateYMD:" . $CreateYMD . " count:" . count($dateInsInfo));
        }
    }
}
 
function logweb($msg, $showWeb = True)
{
    \Logging\LogInfo($msg);
    if ($showWeb) {
        echo date('Y-m-d H:i:s'), $msg, "<br/>";
    }
}