<?php  if ( ! defined('BASEPATH')) exit('No direct script access allowed');
/**
 * zsb
 *
 * 业务类库
 *
 * @package         zsb
 * @author          EllisLab Dev Team
 * @copyright       Copyright (c) 2006 - 2014, HOUSE365, Inc.
 * @link            http://nj.sell.house.com
 * @since           Version 1.0
 * @filesource
 */

// ------------------------------------------------------------------------

/**
 * 经纪人类
 *
 * 经纪人类文件功能描述
 *
 * @package         zsb
 * @subpackage      Models
 * @category        Models
 * @author          fisher
 */
class Broker_base_model extends MY_Model {
    /**
    * 经纪人UID
    *
    * @access private
    * @var integer
    */
    private $_uid = 0;
    
    /**
    * 缓存KEY
    *
    * @access private
    * @var string
    */
    private $_mem_key;
    
    /**
    * Constructor
    */
    public function __construct()
    {

        parent::__construct(1,1,2);

		$this->_mem_key = GB_CITY.'_broker_base_model';
    }
    
    /**
    * set_uid
    *
    * 设置经纪人UID
    *
    * @access	public
    * @param	int     $uid 经纪人UID
    * @return	void
    */
    public function set_uid($uid)
    {  
		
        $this->_uid = intval($uid);
    }

    /**
    * get_uid
    *
    * 获取经纪人UID
    *
    * @access	public
    * @param	void
    * @return	int     $uid 经纪人UID
    */
    public function get_uid()
    {   
        return $this->_uid;
    }

    /**
    * check_uid
    *
    * 检查权限，目前做类内部使用，检查uid是否已设置
    *
    * @access	protected
    * @param	void
    * @return	int     $uid 经纪人UID
    */
    protected function check_uid()
    {   
        return $this->_uid > 0 ? true : false;
    }

    /**
    * get_broker_info
    *
    * 设置经纪人信息
    *
    * @access	public
    * @param	void
    * @return	array   经纪人基础信息
    */
    public function get_broker_info($cacheFlag)
    {   
        if($this->check_uid())
        {	
            $mem_key = $this->_mem_key . '_get_broker_info_by_uid_'.$this->_uid;
			
			$cache = $this->mc->delete($mem_key);
            $cache = $this->mc->get($mem_key);
			
            if(isset($cache['is_ok']) && $cache['is_ok'] == 1 && $cacheFlag)
            {
                $arr_data = $cache['data'];
            }
            else
            {
                //查询字段
                //$this->dbback->select("uid,username,truename,agentcode,telno,expiredate,postlimit,groupid,is_warn,valid_flag,smallphoto,package_id,festa,is_pay,master,real_limit,nofee_num,isreal,favtag_num,regdate,password,qq,email,idno,selfdesc,skill,starlevel,usetime");
                if(GB_CITY == 'nj'){
                    $this->dbback->select("uid,username,truename,agentcode,telno,expiredate,postlimit,groupid,is_warn,valid_flag,smallphoto,package_id,festa,is_pay,master,nofee_num,favtag_num,regdate,password,qq,email,idno,selfdesc,skill,starlevel,usetime,real_status,alipay_acount");
                }else
                {
                    $this->dbback->select("uid,username,truename,agentcode,telno,expiredate,postlimit,groupid,is_warn,valid_flag,smallphoto,package_id,festa,is_pay,master,nofee_num,favtag_num,regdate,password,qq,email,idno,selfdesc,skill,starlevel,usetime,alipay_acount,real_status");
                }
                //查询条件
                $this->dbback->where("uid = '".$this->_uid."'");
                //查询
                $arr_data = $this->dbback->get('broker')->row_array();
				//获取当前经纪人客户经理信息
				if(!empty($arr_data['master']))
				{
					$this->dbback->select("truename,telno");
					//查询条件
					$this->dbback->where("authname = '".$arr_data['master']."'");
					//查询
					$master_data = $this->dbback->get('home_auth')->row_array();
					$arr_data['master_name'] = $master_data['truename'];
					$arr_data['master_telno'] = $master_data['telno'];
				}
                $arr_data['agent_url'] = str_replace('(*)', $arr_data['uid'], GB_BROKER_SHOP);
                $this->mc->add($mem_key, array('is_ok'=>1,'data'=>$arr_data), 600);
            }
            return $arr_data;
        }
    }
    
    /**
     * get_broker_detail
     * 获取经纪人详细信息
     * @param void
     * @return array
     */
    public function get_broker_detail($cacheFlag = true)
    {	
        $arr_data = array();
        $arr_data = $this->get_broker_info($cacheFlag);

        return $arr_data;
    }

    /**
    * encrypt_broker_pwd
    *
    * 加密经纪人密码
    *
    * @access	protected
    * @param	string  $password 经纪人密码
    * @return	array   经纪人基础信息
    */
    protected function encrypt_broker_pwd($password, $key = '')
    {   
        return md5($key.$password);
    }

    /**
    * log_broker_login
    *
    * 检查经纪人登陆
    *
    * @access	public
    * @param	array   $data 日志数据
    * @param	string  $tbl 日志表名
    * @return	array   经纪人基础信息
    */
    public function log_broker_login($data, $tbl = 'log_login')
    {   
        if(is_array($data) && !empty($data) && $tbl != '')
        {
            $this->db->insert($tbl, $data);
        }
    }
    
    /**
     * get_info_by_username
     * 
     * 根据经纪人的用户名获取信息
     * @access public
     * @param string $username 帐号
     * @return array 经纪人信息
     */
    public function get_info_by_username($username)
    {
        //查询字段
        $this->dbback->select("uid,username,truename,agentcode,telno,expiredate,postlimit,groupid,valid_flag,smallphoto,package_id");
        //查询条件
        $this->dbback->where("username = '".$username."'");
        //查询
        $arr_data = $this->dbback->get('broker')->row_array();

        return $arr_data;
    }

    /**
     * get_info_by_username
     *
     * 根据经纪人的用户名获取信息
     * @access public
     * @param string $username 帐号
     * @return array 经纪人信息
     */
    public function get_enter_num_by_uid($uid)
    {

        $logintime =time()-3600;
        //查询字段
        $this->dbback->select("id");
        //查询条件
        $this->dbback->where("uid = '".$uid."' and logintime>=$logintime");
        //查询
        $arr_data = $this->dbback->get('log_login')->row_array();
        $num      =count($arr_data);
        return $num;
    }
    
        /*
     * insert_info_sms
     * 短息信息插入数据库
     * @access public
     * @param int $uid
     * @param string $telno
     * @param int $validcode
     * @param string $tbl
     */
    public function insert_info_sms($data)
    {
        if(is_array($data)&&!empty($data))
        {
           $result = $this->db->insert('info_sms', $data);
            return $result;
        }
    }
    
    /**
     * get_broker_shop
     * 经纪人店铺信息
     * @access public
     * @param int $uid
     * @return array
     */
    public function get_broker_shop($uid){
        $this->dbback->select("skin_title, announce, skin_color");
         //查询条件
         $this->dbback->where("uid = '".$uid."'");
         //查询
         $arr_data = $this->dbback->get('broker')->row_array();
         return $arr_data;
    }
    
    /**
     * set_broker_info
     * 经纪人信息设置
     * @access public
     * @param int $uid 用户id
     * @param array $data设置数据
     * @retrun void
     */
    public function set_broker_info($uid,$data){
        $tbl = "broker";

        $data_string = array();
        if(GB_CITY == 'nj' && (!empty($data['telno']) || !empty($data['idno'])))
        {
            $this->dbback->select("*");
            $this->dbback->where("uid = '".$uid."'");
            $data_string = $this->dbback->get('broker')->row_array();
        }

        $cond_where = array('uid'=>$uid);
        $this->db->where($cond_where);
        $this->db->update($tbl, $data);
        $rows = $this->db->affected_rows();

        if(GB_CITY == 'nj' && (!empty($data['telno']) || !empty($data['idno'])))
        {
            $url = 'http://esfadmin.house365.com/module/broker_manage/curl_sync_crm_broker.php';
            $ch = curl_init();
            $t_url = "http://".$_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI'];
            curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
            curl_setopt($ch, CURLOPT_POSTFIELDS, $data_string);
            curl_setopt($ch, CURLOPT_URL, $url);
            curl_setopt($ch, CURLOPT_TIMEOUT, 1);
            curl_setopt($ch, CURLOPT_REFERER, $t_url);
            curl_setopt($ch,CURLOPT_RETURNTRANSFER, 1);

            $returnVal = curl_exec($ch);
            curl_close($ch);
        }

        return $rows;
    }

    /**
     * 获取经纪人数据(实时)
     * @return mixed
     */
    public function get_broker_info_new() {
        if(GB_CITY == 'nj'){
            $this->dbback->select("uid,username,truename,agentcode");
        }else
        {
            $fieldStr = "uid,username,truename,agentcode";
            if ( GB_CITY == 'hf' ) {
                $fieldStr .= ",license_number,license_number_url,institution_record_no,institution_record_no_url";
            }
            $this->dbback->select( $fieldStr );
        }
        //查询条件
        $this->dbback->where("uid = '".$this->_uid."'");
        //查询
        $arr_data = $this->dbback->get('broker')->row_array();
        return $arr_data;
    }
}
/* End of file broker_model.php */
/* Location: ./models/broker_model.php */