<?php
/**
 * 生成待分配使用用户id
 */
require __DIR__ . '/../../vendor/autoload.php';

use think\App;
use app\model\UserIdAssignModel;
use app\model\UserModel;

$app = App::getInstance();
$app->initialize();

// 可使用的用户id范围
$userIdRange = [10000, 99999];

$offset = $userIdRange[0];
$limit = $userIdRange[1];

for ($i = $offset; $i < $limit; $i += 5000)
{
    $tempRange = range($i, $i + 5000);
    echo $i . " " . ($i + 5000) . "\n";

    // 打乱数组
    shuffle($tempRange);
    $availableUids = [];
    if (!empty($tempRange))
    {
        foreach ($tempRange as $key => $val)
        {
            $isFilter = false;
            if (preg_match('/(1{4}|2{4}|3{4}|4{4}|5{4}|6{4}|7{4}|8{4}|9{4}|0{4})/', $val, $matches))
            {
                // 带4A以上的
                $isFilter = true;
            }
            elseif (preg_match('/(12345|98765)/', $val, $matches))
            {
                // 5连顺的
                $isFilter = true;
            }

            // id已经注册过
            if (UserModel::where([['user_id', '=', $val]])->find())
            {
                $isFilter = true;
            }

            if (!$isFilter)
            {
                $availableUids[] = ['user_id' => $val];
            }

            if ($key % 1000 === 0)
            {
                if (!empty($availableUids))
                {
                    UserIdAssignModel::insertAll($availableUids);
                    echo "添加" . count($availableUids) . "\n";
                    $availableUids = [];
                }
            }
        }

        if ($availableUids)
        {
            UserIdAssignModel::insertAll($availableUids);
            echo "添加" . count($availableUids) . "\n";
        }
        unset($availableUids);
    }
}