<?php

/**
 * Class that operate on table 'user'. Database Mysql.
 *
 * @author: http://phpdao.com
 * @date: 2013-04-06 10:57
 */
class UserMySqlExtDAO extends UserMySqlDAO {

    public function validationUser($username, $password) {

        $sql = 'SELECT user.* , usergroup.usergroupname
		FROM user join usergroup
		on user.usergroupid = usergroup.usergroupid
		 WHERE username = "' . $username . '" AND password =  "' . $password . '" and user.conditions=0';
        $sqlQuery = new SqlQuery($sql);
        return $this->getRow($sqlQuery);
    }

    public function queryAllByUserNameAndUserId($username, $userid) {
        $sql = 'SELECT * FROM user where username = "' . $username . '" and userid !=' . $userid . '';
        $sqlQuery = new SqlQuery($sql);
        return $this->getList($sqlQuery);
    }

    public function queryAllWithoutSessionUser($userid) {
        $sql = 'SELECT user.*, usergroup.usergroupname
		 FROM user JOIN usergroup
		 ON user.usergroupid = usergroup.usergroupid
		 where userid !=' . $userid . '';
        $sqlQuery = new SqlQuery($sql);
        return $this->getList($sqlQuery);
    }

    public function queryAllWithoutSessionUserLimited($start, $end, $userid) {
        $sql = 'SELECT user.*, usergroup.usergroupname
		 FROM user JOIN usergroup
		 ON user.usergroupid = usergroup.usergroupid
		  where userid !=' . $userid . ' order by userid desc
		   limit ' . $start . ',' . $end . '';
        $sqlQuery = new SqlQuery($sql);
        return $this->getList($sqlQuery);
    }

    public function queryWithGroupIdAndUserId($userid, $usergroupid) {
        $sql = 'SELECT user.*, usergroup.usergroupname
		 FROM user JOIN usergroup
		 ON user.usergroupid = usergroup.usergroupid
		 where userid !=' . $userid . ' and user.usergroupid =' . $usergroupid . '';
        $sqlQuery = new SqlQuery($sql);
        return $this->getList($sqlQuery);
    }

    public function queryWithGroupId($usergroupid) {
        $sql = 'SELECT user.userid,user.employeename
		 FROM user
		 where user.usergroupid =' . $usergroupid . '';
        $sqlQuery = new SqlQuery($sql);
        return $this->getList($sqlQuery);
    }

    public function queryWithGroupIdAndUserIdLimited($start, $end, $userid, $usergroupid) {
        $sql = 'SELECT user.*, usergroup.usergroupname
		 FROM user JOIN usergroup
		 ON user.usergroupid = usergroup.usergroupid
		  where userid !=' . $userid . '  and user.usergroupid =' . $usergroupid . '
		   order by userid desc
		   limit ' . $start . ',' . $end . '';
        $sqlQuery = new SqlQuery($sql);
        return $this->getList($sqlQuery);
    }

    public function updateConditions($user) {
        $sql = 'UPDATE user SET currentdate = ?, conditions = ? WHERE userid = ?';
        $sqlQuery = new SqlQuery($sql);

        $sqlQuery->set($user->currentdate);
        $sqlQuery->setNumber($user->conditions);

        $sqlQuery->setNumber($user->userid);
        return $this->executeUpdate($sqlQuery);
    }

    public function queryWithLevel($level) {
        $sql = 'SELECT user.*
		FROM user JOIN usergroup
		ON user.usergroupid = usergroup.usergroupid
		where  user.conditions =0 and usergroup.conditions =0
		and usergroup.level =' . $level . '';
        $sqlQuery = new SqlQuery($sql);
        return $this->getList($sqlQuery);
    }

    public function loadDocotr($usergroupid) {
        $sql = 'SELECT * FROM user WHERE usergroupid = ? ';
        $sqlQuery = new SqlQuery($sql);
        $sqlQuery->setNumber($usergroupid);
        return $this->getList($sqlQuery);
    }

    public function loadDocotr2($docid, $usergroupid) {
        $sql = 'SELECT * FROM user WHERE userid = ? AND usergroupid = ? ';
        $sqlQuery = new SqlQuery($sql);
        $sqlQuery->setNumber($docid);
        $sqlQuery->setNumber($usergroupid);

        return $this->getRow($sqlQuery);
    }

    public function queryAllcountuser() {
        $sql = 'SELECT count(*) FROM user';
        $sqlQuery = new SqlQuery($sql);
        return $this->getList($sqlQuery);
    }

}

?>
