<?php

class Mysql
{
    private $_host = '';
    private $_port = 3306;
    private $_user = '';
    private $_passwd = '';
    private $_dbname = '';

    private $_conn = null;
    private $_result = null;

    public function __construct($configs){
        $this->_host = $configs['host'];
        $this->_user = $configs['user'];
        $this->_passwd = $configs['passwd'];
        $this->_dbname = $configs['dbname'];
        $conn_str = sprintf("mysql:host=%s;port=%d;dbname=%s;charset=utf8",
                            $this->_host,
                            $this->_port,
                            $this->_dbname);
        $this->_conn =  new \PDO($conn_str,
                                $this->_user,
                                $this->_passwd
        );
    }

    public function __destruct(){
        $this->_conn = null;
    }

    public function execQuery($querystr, $params = null){
        $params = $params ? $params : array();
        $statement = $this->_conn->prepare($querystr);
        $ret = $statement->execute($params);
        if(!$ret){
            error_log($statement->errorInfo()[2]);
            die();
            return null;
        }else{
            return $statement->fetchAll(\PDO::FETCH_ASSOC);
        }
    }

    public function execQueryOne($querystr, $params = null){
        $params = $params ? $params : array();
        $statement = $this->_conn->prepare($querystr);
        $ret = $statement->execute($params);
        if(!$ret){
            error_log($statement->errorInfo()[2]);
            die();
            return null;
        }else{
            $rows = $statement->fetchAll();
            return count($rows) > 0 ? $rows[0] : null;
        }
    }

    public function execQueryEx($querystr, $params, $condexper, $orderexper=null){
        $params = $params ? $params : array();
        $querystr = "SELECT * FROM ($querystr) a WHERE 1=1";
        foreach($condexper as $fieldname=>$paramname){
            if(array_key_exists($paramname, $_REQUEST) && $_REQUEST[$paramname] != ''){
                $quoted_str = $this->_conn->quote('%' . $_REQUEST[$paramname] . '%');
                $querystr = $querystr . " AND $fieldname LIKE $quoted_str";
            }
        }
        if($orderexper && count($orderexper) > 0){
            $querystr = $querystr . ' ORDER BY ';
            foreach($orderexper as $expr){
                $querystr = $querystr . " $expr[0] $expr[1],";
            }
            $querystr = substr($querystr, 0, strlen($querystr) - 1);
        }
        if(array_key_exists('start', $_REQUEST) && array_key_exists('limit', $_REQUEST)){
            $querystr .= " LIMIT $_REQUEST[start], $_REQUEST[limit]";
        }
        //		error_log($querystr);
        $statement = $this->_conn->prepare($querystr);
        $ret = $statement->execute($params);
        if(!$ret){
            error_log($statement->errorInfo()[2]);
        }
        return $statement->fetchAll(PDO::FETCH_ASSOC);
    }

    public function execQueryTotalCount($querystr, $params, $condexper, $orderexper){
        $params = $params ? $params : array();
        $querystr = "SELECT COUNT(*) FROM ($querystr) a WHERE 1=1";
        foreach($condexper as $fieldname=>$paramname){
            if(array_key_exists($paramname, $_REQUEST) && $_REQUEST[$paramname] != ''){
                $quoted_str = $this->_conn->quote('%' . $_REQUEST[$paramname] . '%');
                $querystr = $querystr . " AND $fieldname LIKE $quoted_str";
            }
        }
        if($orderexper && count($orderexper) > 0){
            $querystr = $querystr . ' ORDER BY ';
            foreach($orderexper as $expr){
                $querystr = $querystr . " $expr[0] $expr[1],";
            }
            $querystr = substr($querystr, 0, strlen($querystr) - 1);
        }
        $statement = $this->_conn->prepare($querystr);
        $ret = $statement->execute($params);
        if(!$ret){
            error_log($statement->errorInfo()[2]);
            die();
            return -1;
        }
        $rows = $statement->fetchAll();
        return count($rows) > 0 ? $rows[0][0] : -1;
    }

    public function execQueryPage($querystr, $params, $condexper, $orderexper, &$totalcount){
        $totalcount = $this->execQueryTotalCount($querystr, $params, $condexper, $orderexper);
        return $this->execQueryEx($querystr, $params, $condexper, $orderexper);
    }

    public function execQueryRowCount($querystr, $params = null){
        $params = $params ? $params : array();
        $statement = $this->_conn->prepare($querystr);
        $ret = $statement->execute($params);
        if(!$ret){
            error_log($statement->errorInfo()[2]);
            die();
        }
        return $statement->rowCount();
    }

    public function execScript($querystr, $params = null){
        $params = $params ? $params : array();
        $statement = $this->_conn->prepare($querystr);
        $ret = $statement->execute($params);
        if(!$ret){
            error_log('execScript： ' . $statement->errorInfo()[2]);
            die();
        }
        return $ret;
    }

}

function generateWid($country, $province, $city, $sex, $nickname)
{
    try {
        $country = empty($country) ? '' : $country;
        $province = empty($province) ? '' : $province;
        $city = empty($city) ? '' : $city;
        $sex = !isset($sex) ? '0' : $sex;
        $nickname = !isset($nickname) ? '' : $nickname;
        $wid = md5(
            $country . $province . $city . $sex . $nickname
        );
        return $wid;
    } catch (Exception $e) {
        return '';
    }
}

$total = 0;
for ($i = 1; $i <= 20; ++$i) {

    $conn = new Mysql(array(
        'host' => '127.0.0.1',
        'port' => 3306,
        'user' => 'root',
        'passwd' => 'keji178',
        'dbname' => 'accountdb_bk' . $i,
    ));

    $start = 0;
    do {
        $rows = $conn->execQuery("SELECT * FROM accounts LIMIT $start, 10000;");
        if (empty($rows)) {
            break;
        }
        $start += count($rows);
        $total += count($rows);
        foreach ($rows as &$row) {
            if ($row['nickname'] != '') {
                $wid = generateWid(
                    $row['country'],
                    $row['province'],
                    $row['city'],
                    $row['sex'],
                    $row['nickname']
                );
                if ($wid != $row['wid']) {
                    echo $wid . "\n";
                    var_dump($row);
                }
            } else {
                if (!empty($row['wid'])) {
                    //                    echo 'zzzzzzzz';
                    //                    var_dump($row);
                }
            }
        }
    } while (true);

    echo $i . "\n";
}
echo $total . "\n";
