<?php
/**
 * 正码与特码关联规律统计分析脚本
 *
 * 分析维度：
 * 1. 正码平均值与特码差值分布
 * 2. 特码是否在正码范围内
 * 3. 特码与最近正码的距离分布
 * 4. 和值尾数关系
 * 5. 区间覆盖分析
 * 6. 波色/生肖关联
 */

// 波色映射表 (从fa_num表提取)
$colorMap = [
    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 => '绿'
];

// 生肖映射表
$animalMap = [
    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 => '马'
];

/**
 * 获取数字所在的区间 (1-10, 11-20, 21-30, 31-40, 41-49)
 */
function getZone($num) {
    if ($num >= 1 && $num <= 10) return 1;
    if ($num >= 11 && $num <= 20) return 2;
    if ($num >= 21 && $num <= 30) return 3;
    if ($num >= 31 && $num <= 40) return 4;
    if ($num >= 41 && $num <= 49) return 5;
    return 0;
}

/**
 * 计算特码到最近正码的距离
 */
function getMinDistance($sortedNums, $num7) {
    $minDist = 49;
    foreach ($sortedNums as $num) {
        $dist = abs($num7 - $num);
        if ($dist < $minDist) {
            $minDist = $dist;
        }
    }
    return $minDist;
}

// 解析SQL文件中的数据
$sqlFile = 'C:\Users\91611\Desktop\fa_history.sql';
$content = file_get_contents($sqlFile);

// 提取INSERT语句中的数据
$pattern = '/INSERT INTO `fa_history` VALUES \((\d+), (\d+), (\d+), (\d+), (\d+), (\d+), (\d+), (\d+), \'([^\']+)\'\);/';
preg_match_all($pattern, $content, $matches);

$data = [];
for ($i = 0; $i < count($matches[0]); $i++) {
    $data[] = [
        'expect' => (int)$matches[1][$i],
        'num1' => (int)$matches[2][$i],
        'num2' => (int)$matches[3][$i],
        'num3' => (int)$matches[4][$i],
        'num4' => (int)$matches[5][$i],
        'num5' => (int)$matches[6][$i],
        'num6' => (int)$matches[7][$i],
        'num7' => (int)$matches[8][$i],
        'openTime' => $matches[9][$i]
    ];
}

$total = count($data);
echo "==================== 正码与特码关联规律统计分析 ====================\n";
echo "数据总量: {$total} 期 (从 {$data[0]['expect']} 到 {$data[$total-1]['expect']})\n\n";

// ==================== 1. 正码平均值与特码差值分布 ====================
echo "==================== 1. 正码平均值与特码差值分布 ====================\n";

$diffCounts = [];
$inRange5 = 0;

foreach ($data as $row) {
    $avg = ($row['num1'] + $row['num2'] + $row['num3'] + $row['num4'] + $row['num5'] + $row['num6']) / 6;
    $diff = round($row['num7'] - $avg); // 四舍五入

    $diffKey = $diff;
    if (!isset($diffCounts[$diffKey])) {
        $diffCounts[$diffKey] = 0;
    }
    $diffCounts[$diffKey]++;

    if ($diff >= -5 && $diff <= 5) {
        $inRange5++;
    }
}

// 按差值排序
ksort($diffCounts);

echo "差值分布统计:\n";
foreach ($diffCounts as $diff => $count) {
    $pct = round($count / $total * 100, 2);
    echo "  差值 {$diff}: {$count} 次 ({$pct}%)\n";
}

echo "\n差值在 [-5, +5] 范围内的概率: " . round($inRange5 / $total * 100, 2) . "% ($inRange5/$total)\n";

// 找出高频差值区间 (>5%)
echo "\n高频差值区间 (>5%):\n";
foreach ($diffCounts as $diff => $count) {
    $pct = round($count / $total * 100, 2);
    if ($pct > 5) {
        echo "  差值 {$diff}: {$pct}%\n";
    }
}

// ==================== 2. 特码是否在正码范围内 ====================
echo "\n==================== 2. 特码是否在正码范围内 ====================\n";

$inRange = 0;
$outRange = 0;

foreach ($data as $row) {
    $nums = [$row['num1'], $row['num2'], $row['num3'], $row['num4'], $row['num5'], $row['num6']];
    $min = min($nums);
    $max = max($nums);

    if ($row['num7'] >= $min && $row['num7'] <= $max) {
        $inRange++;
    } else {
        $outRange++;
    }
}

echo "特码在正码范围内 [min(num1-6), max(num1-6)]:\n";
echo "  是: {$inRange} 次 (" . round($inRange / $total * 100, 2) . "%)\n";
echo "  否: {$outRange} 次 (" . round($outRange / $total * 100, 2) . "%)\n";

// ==================== 3. 特码与最近正码的距离分布 ====================
echo "\n==================== 3. 特码与最近正码的距离分布 ====================\n";

$distCounts = [];
$equalCount = 0; // 特码等于某正码

foreach ($data as $row) {
    $nums = [$row['num1'], $row['num2'], $row['num3'], $row['num4'], $row['num5'], $row['num6']];
    sort($nums);

    $minDist = getMinDistance($nums, $row['num7']);

    if (!isset($distCounts[$minDist])) {
        $distCounts[$minDist] = 0;
    }
    $distCounts[$minDist]++;

    if ($minDist == 0) {
        $equalCount++;
    }
}

ksort($distCounts);

echo "距离分布统计:\n";
foreach ($distCounts as $dist => $count) {
    $pct = round($count / $total * 100, 2);
    echo "  距离 {$dist}: {$count} 次 ({$pct}%)\n";
}

echo "\n特码等于某正码 (距离=0) 的概率: " . round($equalCount / $total * 100, 2) . "% ($equalCount/$total)\n";

// 距离<=5的概率
$distLE5 = 0;
for ($i = 0; $i <= 5; $i++) {
    if (isset($distCounts[$i])) {
        $distLE5 += $distCounts[$i];
    }
}
echo "距离 <= 5 的概率: " . round($distLE5 / $total * 100, 2) . "% ($distLE5/$total)\n";

// ==================== 4. 和值尾数关系 ====================
echo "\n==================== 4. 和值尾数关系 ====================\n";

$sameTail = 0;
$tailDiffCounts = [];

foreach ($data as $row) {
    $sum = $row['num1'] + $row['num2'] + $row['num3'] + $row['num4'] + $row['num5'] + $row['num6'];
    $sumTail = $sum % 10;
    $num7Tail = $row['num7'] % 10;

    $tailDiff = abs($sumTail - $num7Tail);

    if (!isset($tailDiffCounts[$tailDiff])) {
        $tailDiffCounts[$tailDiff] = 0;
    }
    $tailDiffCounts[$tailDiff]++;

    if ($sumTail == $num7Tail) {
        $sameTail++;
    }
}

ksort($tailDiffCounts);

echo "和值尾数与特码尾数同尾概率: " . round($sameTail / $total * 100, 2) . "% ($sameTail/$total)\n";
echo "\n尾数差值分布:\n";
foreach ($tailDiffCounts as $diff => $count) {
    $pct = round($count / $total * 100, 2);
    echo "  尾数差 {$diff}: {$count} 次 ({$pct}%)\n";
}

// 尾数差 <= 3 的概率
$tailDiffLE3 = 0;
for ($i = 0; $i <= 3; $i++) {
    if (isset($tailDiffCounts[$i])) {
        $tailDiffLE3 += $tailDiffCounts[$i];
    }
}
echo "\n尾数差 <= 3 的概率: " . round($tailDiffLE3 / $total * 100, 2) . "% ($tailDiffLE3/$total)\n";

// ==================== 5. 区间覆盖分析 ====================
echo "\n==================== 5. 区间覆盖分析 ====================\n";

$zoneCoveredCounts = [0, 0, 0, 0, 0, 0]; // 覆盖0-5个区间
$zone7Covered = 0; // 特码所在区间被正码覆盖

foreach ($data as $row) {
    $nums = [$row['num1'], $row['num2'], $row['num3'], $row['num4'], $row['num5'], $row['num6']];
    $zones = [];
    foreach ($nums as $num) {
        $zone = getZone($num);
        $zones[$zone] = true;
    }

    $coverCount = count($zones);
    $zoneCoveredCounts[$coverCount]++;

    $zone7 = getZone($row['num7']);
    if (isset($zones[$zone7])) {
        $zone7Covered++;
    }
}

echo "正码覆盖区间数量分布:\n";
for ($i = 0; $i <= 5; $i++) {
    $count = $zoneCoveredCounts[$i];
    $pct = round($count / $total * 100, 2);
    echo "  覆盖 {$i} 个区间: {$count} 次 ({$pct}%)\n";
}

echo "\n特码所在区间被正码覆盖的概率: " . round($zone7Covered / $total * 100, 2) . "% ($zone7Covered/$total)\n";

// ==================== 6. 波色/生肖关联 ====================
echo "\n==================== 6. 波色/生肖关联 ====================\n";

$color7InNums = 0; // 特码波色在正码中出现过
$animal7InNums = 0; // 特码生肖在正码中出现过

$colorMatchCounts = []; // 正码中某波色数量与特码波色匹配情况
$color7Counts = ['红' => 0, '蓝' => 0, '绿' => 0]; // 特码波色分布

foreach ($data as $row) {
    $nums = [$row['num1'], $row['num2'], $row['num3'], $row['num4'], $row['num5'], $row['num6']];

    $numColors = [];
    $numAnimals = [];
    $colorCounts = ['红' => 0, '蓝' => 0, '绿' => 0];

    foreach ($nums as $num) {
        $color = $colorMap[$num];
        $animal = $animalMap[$num];
        $numColors[$color] = true;
        $numAnimals[$animal] = true;
        $colorCounts[$color]++;
    }

    $color7 = $colorMap[$row['num7']];
    $animal7 = $animalMap[$row['num7']];

    $color7Counts[$color7]++;

    // 特码波色是否在正码中出现过
    if (isset($numColors[$color7])) {
        $color7InNums++;
    }

    // 特码生肖是否在正码中出现过
    if (isset($numAnimals[$animal7])) {
        $animal7InNums++;
    }

    // 正码中某波色数量与特码波色
    $key = $colorCounts[$color7] . '_' . $color7;
    if (!isset($colorMatchCounts[$key])) {
        $colorMatchCounts[$key] = 0;
    }
    $colorMatchCounts[$key]++;
}

echo "特码波色分布:\n";
foreach ($color7Counts as $color => $count) {
    $pct = round($count / $total * 100, 2);
    echo "  {$color}: {$count} 次 ({$pct}%)\n";
}

echo "\n特码波色在正码中出现的概率: " . round($color7InNums / $total * 100, 2) . "% ($color7InNums/$total)\n";
echo "特码生肖在正码中出现的概率: " . round($animal7InNums / $total * 100, 2) . "% ($animal7InNums/$total)\n";

echo "\n正码中特码同波色数量分布:\n";
ksort($colorMatchCounts);
foreach ($colorMatchCounts as $key => $count) {
    $pct = round($count / $total * 100, 2);
    echo "  {$key}: {$count} 次 ({$pct}%)\n";
}

// ==================== 总结: 高命中率规律 ====================
echo "\n==================== 总结: 可能达到40%命中率以上的规律 ====================\n";

$highHitRules = [];

// 检查各规律
if ($inRange5 / $total >= 0.4) {
    $highHitRules[] = "差值在[-5,+5]: " . round($inRange5 / $total * 100, 2) . "%";
}

if ($distLE5 / $total >= 0.4) {
    $highHitRules[] = "距离<=5: " . round($distLE5 / $total * 100, 2) . "%";
}

if ($zone7Covered / $total >= 0.4) {
    $highHitRules[] = "特码区间被正码覆盖: " . round($zone7Covered / $total * 100, 2) . "%";
}

if ($color7InNums / $total >= 0.4) {
    $highHitRules[] = "特码波色在正码中出现: " . round($color7InNums / $total * 100, 2) . "%";
}

if ($animal7InNums / $total >= 0.4) {
    $highHitRules[] = "特码生肖在正码中出现: " . round($animal7InNums / $total * 100, 2) . "%";
}

if ($tailDiffLE3 / $total >= 0.4) {
    $highHitRules[] = "尾数差<=3: " . round($tailDiffLE3 / $total * 100, 2) . "%";
}

if (count($highHitRules) > 0) {
    foreach ($highHitRules as $rule) {
        echo "- {$rule}\n";
    }
} else {
    echo "没有发现明显超过40%命中率的规律，以下是接近40%的规律:\n";

    $allRules = [
        "差值在[-5,+5]" => $inRange5 / $total,
        "距离<=5" => $distLE5 / $total,
        "距离<=10" => 0,
        "特码区间被正码覆盖" => $zone7Covered / $total,
        "特码波色在正码中出现" => $color7InNums / $total,
        "特码生肖在正码中出现" => $animal7InNums / $total,
        "尾数差<=3" => $tailDiffLE3 / $total,
    ];

    // 计算距离<=10
    $distLE10 = 0;
    for ($i = 0; $i <= 10; $i++) {
        if (isset($distCounts[$i])) {
            $distLE10 += $distCounts[$i];
        }
    }
    $allRules["距离<=10"] = $distLE10 / $total;

    arsort($allRules);
    foreach ($allRules as $rule => $rate) {
        echo "- {$rule}: " . round($rate * 100, 2) . "%\n";
    }
}

echo "\n==================== 分析完成 ====================\n";