<?php

Doo::loadCore('db/DooModel');

class Users extends DooModel {

    public $id;
    public $username;
    public $useremail;
    public $userpasswd;
    public $userquestion;
    public $defquestion;
    public $useranswer;
    public $actstate;
    public $salt;
    public $regip;
    public $regdate;
    public $lastloginip;
    public $lastlogintime;
    public $lastip;
    public $uptime;
    public $intime;
    public $bank;
    public $mobile;
    public $keys_auth;
    public $_table = 'zh_users';
    public $_primarykey = 'id';
    public $_fields = array('id', 'username', 'useremail', 'userpasswd', 'userquestion', 'defquestion', 'useranswer', 'actstate', 'salt', 'regip', 'regdate', 'lastloginip', 'lastlogintime', 'lastip', 'uptime', 'intime', 'bank', 'mobile','keys_auth');

    public function __construct() {
        parent::setupModel(__CLASS__);
    }

    /**
     *
     * @param type $param
     */
    public function userAdd($uArray) {
        if (!is_array($uArray))
            return FALSE;
        $this->username = $uArray['username'];
        $this->useremail = $uArray['useremail'];
        $this->userpasswd = $uArray['userpasswd'];
        $this->actstate = 0;
        $this->regip = $uArray['clientip'];
        $this->intime = time();
        $this->salt = $uArray['salt'];
        return $this->insert();
    }

    /**
     *
     * @param type $param
     */
    public function getAll($limit) {
        return $this->find(array('select' => 'id,username,mobile,useremail,lastloginip,lastlogintime', 'desc' => 'regdate', 'limit' => $limit, 'asArray' => TRUE));
    }

    /**
     *
     * @param type $param
     */
    public function getCountz() {
        return $this->count();
    }

    /**
     *
     * @param type $param
     */
    public function updateBank($uid, $money) {
        $this->bank = new DooDbExpression('bank+' . $money);
        return $this->update(array('where' => 'id=' . $uid));
    }

    public function getRowByUid($uid) {
        if (empty($uid))
            return false;
        return $this->find(array('select' => 'bank,username,useremail', 'where' => 'id=' . $uid, 'asArray' => TRUE));
    }

    public function getRowByMobile($mobile) {
        return $this->getOne(array('where' => 'mobile="' . $mobile . '"', 'asArray' => TRUE));
    }

    public function getRowByName($name) {
        return $this->getOne(array('where' => 'username="' . $name . '"', 'asArray' => TRUE));
    }

    public function getRowByid($uid) {
        return $this->getOne(array('select' => 'mobile,useremail', 'where' => 'id=' . $uid, 'asArray' => TRUE));
    }

    public function getidBysearch($search) {
        return $this->getOne(array('select' => 'id' ,'where' => 'username=? or useremail=? or mobile=?', 'param' => array($search,$search,$search), 'asArray' => TRUE));
    }


}

?>