<?php

class User_model extends CI_Model {

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

    function check_login($security_code) {
        $sql = "
                SELECT TOP 1 WU_ID
                FROM   Web_UserInfo
                WHERE  SUBSTRING(sys.fn_VarBinToHexStr(hashbytes('MD5', CONVERT(varchar(100),WU_LMI_SN)+'#yan!@'+CONVERT(varchar(100), GETDATE(), 23))),3,32) = ?                  
                    AND WU_LimitSign = 0

                ";
        $query = $this->HT->query($sql, array($security_code));
        //print_r($this->HT->queries);
        if ($query->result()) {
            $row = $query->row();
            return $row->WU_ID;
        } else {
            return FALSE;
        }
    }

    //供应商平台登录用户信息
    function get_user_info($wu_id) {
        $sql = "
                SELECT TOP 1 WU_ID,
                       WU_UserName,
                       WU_UserPassword,
                       WU_LastLoginDate,
                       WU_LastLoginIP,
                       WU_LimitSign,
                       LMI_SN,
                       LMI_VEI_SN,
                       LMI_listmail
                FROM   Web_UserInfo
                       INNER JOIN LinkManInfo lmi
                            ON  lmi.LMI_SN = WU_LMI_SN
                WHERE  WU_ID = ?

                ";
        $query = $this->HT->query($sql, array($wu_id));
        if ($query->result()) {
            $row = $query->row();
            return $row;
        } else {
            return FALSE;
        }
    }
	
	//获取用户权限
	function user_security($lmi_sn,$securitycode){
		$sql="
				SELECT DISTINCT
					   (
						   SELECT TOP 1 CCP_SecurityCode
						   FROM   CCP_Security
						   WHERE  CCP_SID = CCP_SctSN
					   ) CCP_SecurityCode
				FROM                CCP_UGSecurity
				WHERE  (CCP_UGId = ? AND CCP_BindType = 0)
					   OR  (
							   CCP_UGId IN (SELECT CCP_SctSN
											FROM   CCP_UGSecurity
											WHERE  CCP_UGId = ?
												   AND CCP_BindType = 2
												   AND Deleteflag = 0)
							   AND Deleteflag = 0
						   )
		";
        $query = $this->HT->query($sql, array($lmi_sn,$lmi_sn));
        if ($query->result()) {
          foreach($query->result() as $item){
			  if (trim($item->CCP_SecurityCode)==$securitycode){
				  return true;
			  }
		  }
		}
        return FALSE;
	}

	//获取导游SN
	public function get_guide_sn($gri_sn,$vei_sn,$cii_sn){
		$sql="
			SELECT TOP 1 EOI_ObjSN
			FROM   Eva_ObjectInfo
			WHERE  EOI_GRI_SN = ?
				   AND EOI_VEI_SN = ?
				   AND EOI_CII_SN = ?
				   AND EOI_Type = 3
		";
        $query = $this->HT->query($sql,array($gri_sn,$vei_sn,$cii_sn));
        //print_r($this->HT->queries);
        if ($query->result()) {
            $row = $query->row();
            return $row->EOI_ObjSN;
        } else {
            return FALSE;
        }
	}
	
    //导游列表
    function get_guide_list($vei_sn) {
        $sql = "   
            SELECT TGI_VEI_SN,
                   TGI_SN,
                   TGI2_Name,
                   (
                       SELECT TGI2_Name
                       FROM   TouristGuideInfo2
                       WHERE  TGI2_LGC = 1
                              AND TGI2_TGI_SN = TGI_SN
                   )             TGI2_Name2,
                   TGI_Mobile
            FROM                 TouristGuideInfo,
                   TouristGuideInfo2
            WHERE  TGI2_TGI_SN = TGI_SN
                   AND TGI2_LGC = 2
                   AND LEN(TGI2_Name) > 0
                   AND TGI2_Name LIKE '24%'
                   AND TGI_VEI_SN = ?
            ORDER BY
                   TGI2_Name     ASC        
          ";
        $query = $this->HT->query($sql, array($vei_sn));
        //print_r($this->HT->queries);
        return $query->result();
    }

}
