<?php
// 确保这些常量已经定义
defined('DB_SERVER') ? null : define('DB_SERVER', 'localhost');
defined('DB_USER') ? null : define('DB_USER', 'root');
defined('DB_PASS') ? null : define('DB_PASS', 'wch123.com');
defined('DB_NAME') ? null : define('DB_NAME', 'sdk');

// 连接数据库
$connection = new mysqli(DB_SERVER, DB_USER, DB_PASS, DB_NAME);

// 检查连接
if ($connection->connect_error) {
    die("连接失败: " . $connection->connect_error);
}

$sql = "SELECT account FROM accounts ";
$accountRes = $connection->query($sql);

$accounts = [];
while ($row = $accountRes->fetch_assoc()) {
    $accounts[] = $row['account'];
}


// 查询数据库
$query = "SELECT ANY_VALUE(uid) as uid, ANY_VALUE(create_time) as create_time FROM game_enter_server group by uid ";
$result = $connection->query($query);

if ($result) {
    while ($row = $result->fetch_assoc()) {

        if (!in_array($row['uid'], $accounts)) {

            $value1 = $row['uid'];
            $value2 = '1';
            $value3 = '1';
            $value4 = $row['create_time'];

            $sql =  "INSERT INTO accounts (`account`, `status`,`channel_id`,`created_at`) VALUES (?, ?, ?, ?)";
            $stmt = $connection->prepare($sql);
            $stmt->bind_param("ssss", $value1, $value2, $value3, $value4);

            // 执行语句
            if ($stmt->execute()) {
                echo "{$row['uid']} 新记录插入成功";
            } else {
                echo "插入记录时出错: " . $stmt->error;
            }
        } else {
            echo "{$row['uid']} 已经存在";
        }
    }
} else {
    die("查询失败: " . $connection->error);
}

// 关闭数据库连接
$connection->close();
