<?php
/*********************************************************      
 程序说明：         
    功能说明：HLR Redis同步
              
    调用关系：调用: header.inc    
              被调用:        
 
    变量说明：

    作者：Lai
      
修改注释：      
NO.1      
姓名：    Lai      
时间：  2025 01 09
修改说明：
*********************************************************/
require("../../inc/header.inc");
$sysTypeNo=$_REQUEST['sysTypeNo'];
$dataTypeNo=$_REQUEST['dataTypeNo'];
$userNum = 0;

$redis = new redis();
$redis->connect('127.0.0.1', 6379);
$redis->auth("helloearth");
$hkey_arr = $redis->keys("*");
$hkeyArrVal = array_values($hkey_arr);
// 打印 $hkeyArrVal
foreach ($hkeyArrVal as $value)
{
    if (strstr($value, "hlruser:"))
    {
        $hlruser[$userNum]=$value;
        $userNum++;
    }
}
//print_r($hlruser)
global $OMC_server;
$objConn = mysqli_connect($OMC_server[0]['host'],$OMC_server[0]['user'],$OMC_server[0]['password'], "OBJ_".$sysTypeNo);
$dbConn = mysqli_connect($OMC_server[0]['host'],$OMC_server[0]['user'],$OMC_server[0]['password'], "HLR_DB");

$sql="TRUNCATE HLR_DB.subscriberData";
mysqli_query($dbConn,$sql);
$tmpsql="TRUNCATE OBJ_330.tmpSubsData_0";
mysqli_query($objConn,$tmpsql);

// 初始化进度条
echo '<div id="progressContainer" style="width: 320px; border: 1px solid #000; background-color: #0000FF; height: 20px; position: relative;">';
echo '<div id="progressBar" style="width: 0%; height: 100%; background-color: #FFFFFF;"></div>';
echo '</div>';
echo '<div id="progressText">0%</div>';

    set_time_limit(0);
    ini_set('max_execution_time', 0);
for ($k = 0; $k < $userNum; $k++) {
    $hlr_detail = $redis->hGetAll($hlruser[$k]);
    $imsi = explode(":", $hlruser[$k])[1];
    // 反转字节顺序
    $reversedCamel = strrev($hlr_detail['camel']);
    // 将反转后的字节序列转换为十六进制字符串
    $hexValue = bin2hex($reversedCamel);
    $nam = bin2hex($hlr_detail['nam']);
    $usedFlag = bin2hex($hlr_detail['enable']);

    $updateSql = "INSERT INTO HLR_DB.subscriberData (imsi, inter_msisdn, camel_flag, nam, used_flag) VALUES ('$imsi', '$hlr_detail[msisdn]', '$hexValue', '$nam', '$usedFlag')";
    mysqli_query($dbConn, $updateSql);
    
    // 检查错误
    $errStr = mysqli_error($dbConn);
    if ($errStr != null) {
        //echo "updateSql=$updateSql<br>";
        //echo mysqli_error($dbConn);
    }

    // 更新进度条
    $progress = round(($k + 1) / $userNum * 100);
    echo "<script>
        document.getElementById('progressBar').style.width = '$progress%';
        document.getElementById('progressText').innerText = '$progress%';
    </script>";
    flush(); // 刷新输出缓冲区
}

// 最后更新进度条到 100%
echo "<script>
    document.getElementById('progressBar').style.width = '100%';
    document.getElementById('progressText').innerText = '100%';
</script>";
usleep(100000); // 可选：添加延迟以便观察进度条变化
echo "<script>window.location.href = './subscriberListDown.php?sysTypeNo=$sysTypeNo&dataTypeNo=$dataTypeNo&sysNo=$sysNo&sysId=$sysId';</script>";  
$redis->close();
?>
