<?php

class Operator_model extends CI_Model
{

    function __construct()
    {
        parent::__construct();
        $this->HT = $this->load->database('HT', TRUE);
    }

    //输入用户名和密码，检查是否可以正确
    function check_login($user_code, $password)
    {
        $sql = "SELECT TOP 1 1 \n"
                . "FROM   OperatorInfo oi \n"
                . "WHERE  oi.OPI_Code = N? \n"
                . "       AND CONVERT(VARCHAR(100), oi.OPI_Password) = N? ";
        $query = $this->HT->query($sql, array($user_code, $password));
        //print_r($this->HT->queries);
        if ($query->num_rows() > 0)
        {
            return TRUE;
        }
        else
        {
            return FALSE;
        }
    }
	
	function get_password($user_code)
	{
        $sql = "SELECT TOP 1 oi.OPI_Password \n"
			 . "FROM   OperatorInfo oi \n"
             . "WHERE  oi.OPI_Code = N?";
        $query = $this->HT->query($sql, array($user_code));
        //print_r($this->HT->queries);
        if ($query->num_rows() > 0)
        {
            $row = $query->row_array();
            return $row;
        }
        else
        {
            return FALSE;
        }  
	}

    //获取登录用户详细信息
    function get_user($user_code)
    {
		if(empty($user_code))
		{
			return false;
		}
	
        $sql = "SELECT TOP 1 oi.OPI_SN, \n"
             . "       oi.OPI_Name, \n"
             . "       oi.OPI_Email, \n"
             . "       oi.OPI_FirstName, \n"
             . "       oi.OPI_Code \n"
             . "FROM   OperatorInfo oi \n"
             . "WHERE  oi.OPI_Code = N?";
        $query = $this->HT->query($sql, array($user_code));
        //print_r($this->HT->queries);
        if ($query->num_rows() > 0)
        {
            $row = $query->row_array();
            return $row;
        }
        else
        {
            return FALSE;
        }             
    }

    //获取作者平台的作者信息
    public function get_author_user($a_email){
        $sql = "SELECT TOP 1 
                            a_id as OPI_SN,
                            a_email as OPI_Email,
                            a_name as OPI_Name,
                            a_name as OPI_FirstName,
                            a_id as OPI_Code
                FROM        infoAuthors 
                WHERE       a_email=? AND a_active=1";
        $query = $this->HT->query($sql, array($a_email));
        if ($query->num_rows() > 0)
        {
            $row = $query->row_array();
            return $row;
        }
        else
        {
            return FALSE;
        }      
    }
	
	//获取作者信息
    function get_author($user_code=array())
    {
		$user_code_str='';
		foreach($user_code as $user_item)
		{
			$user_code_str.="'".$user_item."',";
		}
		$user_code_str.="'0'";
		
        $sql = "SELECT oi.OPI_SN, \n"
             . "       oi.OPI_Name, \n"
             . "       oi.OPI_Email, \n"
             . "       oi.OPI_FirstName, \n"
             . "       oi.OPI_Code \n"
             . "FROM   OperatorInfo oi \n"
             . "WHERE  oi.OPI_Code IN ( $user_code_str )";
        $query = $this->HT->query($sql);
        //print_r($this->HT->queries);
		return $query->result();           
    }
	
	//HT中所有账号
	function all()
	{
        $sql = "SELECT oi.OPI_SN, \n"
             . "       oi.OPI_Name, \n"
             . "       oi.OPI_Email, \n"
             . "       oi.OPI_FirstName, \n"
             . "       oi.OPI_Code \n"
             . "FROM   OperatorInfo oi \n"
             . "WHERE  oi.deleteFlag=0 \n"
             . "ORDER BY OPI_Code ASC ";
        $query = $this->HT->query($sql);
        //print_r($this->HT->queries);
		return $query->result();      
	}

    //获取当前站点的作者
    public function get_site_user($site_code){
        $sql = "SELECT  oi.OPI_SN,
                        oi.OPI_Name,
                        oi.OPI_Email,
                        oi.OPI_FirstName,
                        oi.OPI_Code
                 FROM   OperatorInfo oi INNER JOIN infoaccessownsite i ON oi.OPI_Code=i.iao_author
                 WHERE  i.iao_site=?";
        $query = $this->HT->query($sql,$site_code);
        return $query->result();
    }

    public function get_webhtuser_by_ip($ip,$filed='whu_uid')
    {
        $sql="SELECT $filed FROM webht_user WHERE whu_ip=?";
        $query = $this->HT->query($sql, array($ip));
        $result=$query->result();
        if ($result) {
            if ($filed=='*') {
                $result=$result[0];
            }else{
                $result=$result[0]->$filed;
            }
        }
        return $result;
    }
	
	public function get_webhtuser_by_id($whu_uid){
		$sql="
			SELECT TOP 1 wu.whu_uid,
				   wu.whu_uname,
				   wu.whu_email,
				   wu.whu_ip,
				   wu.whu_mac,
				   wu.whu_status,
				   wu.whu_isadmin,
				   wu.department
			FROM   webht_user wu
			WHERE  wu.whu_uid = ?
		";
		$query = $this->HT->query($sql,array($whu_uid));
			if ($query->num_rows() > 0)
			{
				$row = $query->row();
				return $row;
			}else{
				return false;
			}
	}

    public function get_webhtuser_by_name($whu_uname,$filed='whu_uid',$flag=false)
    {
        $map=" WHERE whu_uname like '$whu_uname%' ";
        if ($flag) {
            $map=$whu_uname;
        }
        $sql="SELECT $filed FROM webht_user $map";
        $query = $this->HT->query($sql);
        $result=$query->result();
        if ($result) {
            if ($filed=='*') {
                $result=$flag?$result:$result[0];
            }else{
                $result=$result[0]->$filed;
            }
        }
		//print_r($this->HT->queries);
        return $result;
    }

    //获取登录用户详细信息
    function get_user_by_name($OPI_Name,$top=1)
    {
        if(empty($OPI_Name))
        {
            return false;
        }
		//echo $OPI_Name;

        $sql = "SELECT TOP $top oi.OPI_SN, \n"
             . "       oi.OPI_Name, \n"
             . "       oi.OPI_Email,OPI_MoveTelephone,OPI_Telephone, \n"
             . "       oi.OPI_FirstName, \n"
             . "       oi.OPI_Code \n"
             . "FROM   OperatorInfo oi \n"
             . "WHERE  oi.OPI_Name like '%$OPI_Name%'";
        $query = $this->HT->query($sql);
        if ($query->num_rows() > 0)
        {
            if ($top==1) {
                $row = $query->row_array();
                return $row;
            }else{
                return $query->result();
            }
        }
        else
        {
            return FALSE;
        }             
    }

    function get_user_by_sn($OPI_SN_STRING)
    {
        if(empty($OPI_SN_STRING))
        {
            return false;
        }
    
        $sql = "SELECT oi.OPI_SN, \n"
             . "       oi.OPI_Name, \n"
             . "       oi.OPI_Email,OPI_MoveTelephone,OPI_Telephone, \n"
             . "       oi.OPI_FirstName, \n"
             . "       oi.OPI_Code \n"
             . "FROM   OperatorInfo oi \n"
             . "WHERE  oi.OPI_SN in ($OPI_SN_STRING)";
        $query = $this->HT->query($sql);
        if ($query->num_rows() > 0)
        {
            return $query->result();
        }
        else
        {
            return FALSE;
        }             
    }

    function get_client_ip($type = 0) {
        $type       =  $type ? 1 : 0;
        static $ip  =   NULL;
        if ($ip !== NULL) return $ip[$type];
        if (isset($_SERVER['HTTP_X_FORWARDED_FOR'])) {
            $arr    =   explode(',', $_SERVER['HTTP_X_FORWARDED_FOR']);
            $pos    =   array_search('unknown',$arr);
            if(false !== $pos) unset($arr[$pos]);
            $ip     =   trim($arr[0]);
        }elseif (isset($_SERVER['HTTP_CLIENT_IP'])) {
            $ip     =   $_SERVER['HTTP_CLIENT_IP'];
        }elseif (isset($_SERVER['REMOTE_ADDR'])) {
            $ip     =   $_SERVER['REMOTE_ADDR'];
        }
        // IP地址合法验证
        $long = sprintf("%u",ip2long($ip));
        $ip   = $long ? array($ip, $long) : array('127.0.0.1', 0);
        return $ip[$type];
    }

}