<?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
 */

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

/**
 * Rent_demand_model CLASS
 *
 * 求租管理类，提供管理求租购信息的方法。
 *
 * @package         zsb
 * @subpackage      Models
 * @category        Models
 * @author          xz     
 */

//加载父类
load_m("rent_demand_base_model");

class Rent_demand_model extends Rent_demand_base_model{
    
    /**
    * Constructor
    */
    public function __construct() 
    {
        parent::__construct();
    }
    
    
    /**
    * 根据经纪人编号获取经纪人出租房源的所有版块信息
    *
    * @access	public
    * @param	void
    * @param   
    * @return	array  房源需求信息表需要查询的字段数组
    */
    private function _get_streetinfo_by_uid ( $uid )
    {
        //经纪人房源的版块信息[调取房源类，获取经纪人所有房源涉及到的版块信息]
        $street_info = array();
        
        if( $uid > 0  )
        {
            //加载二手房模块
            $this->load->model('rent_model');
            
            //设置查询字段
            $selet_fields = array('streetid');
            $this->rent_model->set_select_fields($selet_fields);
            
            //组装查询条件
            $cond_where = "uid = '".$uid."' AND esta = 1 AND infofrom = '5' ";
            
            $street_info_all = array();
            //获取所有板块信息
            $street_info_all = $this->rent_model->find_all_by( $cond_where , 0 , 0 );
            
            if(is_array($street_info_all) && !empty($street_info_all))
            {   
                $street_info_all = array_unique($street_info_all);
                
                foreach($street_info_all as $key => $value)
                {
                    $street_info[] = $value['streetid'];
                }
            }
        }
         
        return $street_info;
    }
    
    
    /**
    *
    * 根据经纪人编号获取与经纪人房源版块相匹配的求租信息条数
    *
    * @access	public
    * @return	int   符合条件的求租信息条数
    */
    public function get_demand_num_by_uid( $uid )
    {   
        //需求房源信息条数
        $demands_num = 0;
        
        //经纪人房源的版块信息[调取房源类，获取经纪人所有房源涉及到的版块信息]
        $street_info = $this->_get_streetinfo_by_uid( $uid );

        //如果版块信息为空，则直接返回空数组
        if( empty( $street_info ) )
        {
            return  $demands_num;
        }
        
        //默认的查询条件
        $limit_time = time() + 86400;
        $cond_where = "esta = 1 AND allow_match = 1 AND "
                . "( infofrom = '0' OR infofrom = '5' ) AND expiretime > $limit_time";
        
        $str_streetid = implode( ',' , $street_info ) ;
        $cond_where .= " AND ( street IN (".$str_streetid.") "
                . "OR street1 IN (".$str_streetid.") OR street2 IN (".$str_streetid.") )";

        $demands_num = $this->get_count_by_cond( $cond_where );
        
        return $demands_num ;
    }
    
    
    /**
    *
    * 根据经纪人编号获取与经纪人房源版块相匹配的求租信息信息列表
    *
    * @access	public
    * @param	int     $uid    经纪人编号
    * @param	int     $offset 偏移数,默认值为0
    * @param	int     $pagesize 每次取的条数，默认值为10
    * @param	string  $order_key 排序字段，默认值
    * @param	string  $order_by  升序、降序，默认降序排序
    * @return	array   经纪人已匹配的求租求租信息
    */
    public function get_demand_list_by_uid( $uid , $offset = 0 , $limit = 10 
            , $order_key = 'creattime' , $order_by = 'DESC' )
    {   
        //需求房源信息
        $arr_data = array();
        
        //经纪人房源的版块信息[调取房源类，获取经纪人所有房源涉及到的版块信息]
        $street_info = $this->_get_streetinfo_by_uid( $uid );
        
        //默认的查询条件
        $limit_time = time() + 86400;
        $cond_where = "esta = 1 AND allow_match = 1 AND "
                . "( infofrom = '0' OR infofrom = '5' ) AND expiretime > $limit_time";
        
        //判断是否存在板块信息
        if( is_array( $street_info ) && !empty( $street_info ) )
        {
            $str_streetid = implode( ',' , $street_info ) ;
            $cond_where .= " AND ( street IN (".$str_streetid.") OR "
                . "street1 IN (".$str_streetid.") OR street2 IN (".$str_streetid.") )";
        }
        else
        {
           return  $arr_data;
        }
       
        $arr_data = $this->get_list_by_cond( $cond_where , $offset , $limit , $order_key , $order_by );
        
        return $arr_data ;
    }
}
/* End of file rent_demand_model.php */
/* Location: ./app/models/rent_demand_model.php */
