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

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

/**
 * House_demands_model CLASS
 *
 * 求购管理类，提供管理求购信息的方法(查看、匹配、删除等操作)。
 *
 * @package         zsb
 * @subpackage      Models
 * @category        Models
 * @author          xz     
 */

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

class House_demands_model extends House_demands_base_model{
  
    /**
    * Constructor
    */
    public function __construct() 
    {
        parent::__construct(0,1,0);
    }
    
    
    /**
    * 根据经纪人编号获取经纪人出售或者出租房源的所有版块信息
    *
    * @access	public
    * @param	void
    * @return	array  房源需求信息表需要查询的字段数组
    */
    private function _get_streetinfo_by_uid ()
    {
        //经纪人房源的版块信息[调取房源类，获取经纪人所有房源涉及到的版块信息]
        $street_info = array();
        
        //房源需求信息表
        $tbl_house = $this->get_tbl_house();
        
        //$this->load->model('sell');
        
        //$this->sell->sell_model->
		
		$street_info = array('80','90','95');
         
        return $street_info;
    }
    
    
    /**
    * get_demands_num_by_uid
    *
    * 根据经纪人编号获取与经纪人房源版块相匹配的求购信息条数
    *
    * @access	public
    * @return	int   符合条件的求购信息条数
    */
    public function get_demands_num_by_uid( )
    {   
        //需求房源信息条数
        $demands_num = 0;
        
        //经纪人房源的版块信息[调取房源类，获取经纪人所有房源涉及到的版块信息]
        $street_info = $this->_get_streetinfo_by_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";
        
        //判断是否存在板块信息
        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
        {
            $cond_where .= ' AND 1 = 0';
        }
       
        $demands_num = $this->get_demands_num( $cond_where );
        
        return $demands_num ;
    }
    
    
    /**
    * get_demands_list_by_uid
    *
    * 根据经纪人编号获取与经纪人房源版块相匹配的求购信息信息列表
    *
    * @access	public
    * @param	int     $offset 偏移数,默认值为0
    * @param	int     $pagesize 每次取的条数，默认值为10
    * @param	string  $order_key 排序字段，默认值
    * @param	string  $order_by  升序、降序，默认降序排序
    * @return	array   经纪人已匹配的求购求租信息
    */
    public function get_demands_list_by_uid( $offset = 0 , $limit = 10 , $order_key = 'creattime' , $order_by = 'DESC' )
    {   
        //需求房源信息
        $arr_data = array();
        
        //经纪人房源的版块信息[调取房源类，获取经纪人所有房源涉及到的版块信息]
        $street_info = $this->_get_streetinfo_by_uid();
        
        //如果版块信息为空，则直接返回空数组
        if( empty( $street_info ) )
        {
            return  $arr_data;
        }
        
        //默认的查询条件
        $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
        {
            $cond_where .= ' AND 1 = 0 ';//板块信息为空
        }
       
        $arr_data = $this->get_demands_list( $cond_where , $offset , $limit , $order_key , $order_by );
        
        return $arr_data ;
    }
    
    
    /**
    * get_demands_num
    *
    * 获取符合条件的求购求组信息条数
    *
    * @access	public
    * @param	string  $cond_where 查询条件
    * @return	int   符合条件的求购信息条数
    */
    public function get_matched_num ( $cond_where = '' )
    {
        $matched_num = 0;
        
        //房源匹配表
        $tbl_match = $this->get_tbl_match();
        
        //如果表名称没有设置则返回0
        if( !in_array( $tbl_match , array( 'buy_match' , 'rentwanted_match' ) ) )
        {
           return $matched_num ;
        }
        
        //查询条件
        if( $cond_where != '')
        {
            $this->dbback->where( $cond_where );
        }
        
        //查询
        $matched_num = $this->dbback->get( $tbl_match )->num_rows();

        return  intval( $matched_num );
    }
    
    
    /**
    * get_matched_num_by_uid
    *
    * 获取经纪人已匹配的求购、求租信息条数
    *
    * @access	public
    * @param	int   $days 多少天内的匹配记录
    * @return	int   符合条件的求购信息条数
    */
    public function get_matched_num_by_uid( $days = 30 )
    {   
        $matched_num = 0;
        
        $uid = $this->get_uid();
        
        //查询我匹配的所有求购信息条数
        $start_time = time() - 86400 * $days;
        $cond_where = "infofrom = 5 AND match_uid = '".$uid."' AND creattime > $start_time";
        
        //获取已匹配信息
        $matched_num = $this->get_matched_num( $cond_where );
        
        return $matched_num;
    }
    
    
    /**
    * get_matched_ids_by_uid
    *
    * 获取经纪人已匹配的求购、求租信息编号数组
    *
    * @access	public
    * @param	int     $days 多少天内的匹配记录
    * @param	string  $order_key 排序字段，默认值
    * @param	string  $order_by  升序、降序，默认降序排序
    * @return	array   经纪人已匹配的求购、求租信息编号数组
    */
    public function get_matched_ids_by_uid( $days = 30 , $order_key = 'creattime' , $order_by = 'DESC' )
    {   
        $arr_ids = array();
        
        $uid = $this->get_uid();
        
        if( $uid <= 0)
        {
           return  $arr_ids;
        }
        
        //查询我匹配的所有求购信息条数
        $start_time = time() - 86400 * $days;
        $cond_where = "infofrom = 5 AND match_uid = '".$uid."' AND creattime > $start_time";
        
        //获取已匹配的编号
        $arr_ids = $this->get_matched_ids( $cond_where , $order_key = 'creattime' , $order_by = 'DESC');
        
        return $arr_ids;
    }
    
    
    /**
    * get_matched_list_by_uid
    *
    * 获取经纪人已匹配的求购、求租信息
    *
    * @access	public
    * @param	int     $days 多少天内的匹配记录
    * @param	int     $offset 偏移数,默认值为0
    * @param	int     $limit 每次取的条数，默认值为10
    * @param	string  $order_key 排序字段，默认值
    * @param	string  $order_by  升序、降序，默认降序排序
    * @return	int   符合条件的求购信息
    */
    public function get_matched_list_by_uid( $days = 30 , $offset = 0 , $limit = 10 , $order_key = 'creattime' , $order_by = 'DESC' )
    {   
        //已匹配需求信息
        $matched_info = array();
        
        //匹配信息表
        $tbl_matched = $this->get_tbl_match();
        
        //表明未设置直接返回空
        if( $tbl_matched == '' )
        {
            return  $arr_data;
        }
        
        //查询我已经匹配的需求编号
        $arr_ids = array();
        $arr_ids = $this->get_matched_ids_by_uid( $days , $order_key , $order_by );
        
        if( is_array($arr_ids) && !empty($arr_ids) )
        {
            $want_ids_str = '';
            foreach( $arr_ids as $key => $value)
            {
                $want_ids_str .= $want_ids_str != '' ? ','.$value['want_id'] : $value['want_id'];
            }
            $cond_where = "id IN(".$want_ids_str.") ";
            
            //已匹配的求购房源信息
            $matched_info = $this->get_matched_list($cond_where, $offset, $limit, $want_ids_str, $order_by);
        }
        
        return $matched_info ;
    } 
}
/* End of file sell_demands_model.php */
/* Location: ./zsb/model/sell_demands_model.php */