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

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

/**
 * House_match_base_model CLASS
 *
 * 房源匹配信息管理类。
 *
 * @package         zsb
 * @subpackage      Models
 * @category        Models
 * @author          xz     
 */
class House_match_base_model extends MY_Model {
    
    /**
     * 求购、求租用户编号
     *
     * @access private
     * @var integer
     */
    private $_want_uid    = 0;
    
    /**
     * 求购、求租信息编号
     *
     * @access private
     * @var integer
     */
    private $_want_id    = 0;

    /**
     * 匹配房源用户编号
     *
     * @access private
     * @var integer
     */
    private $_match_uid    = 0;
    
    /**
     * 匹配房源编号
     *
     * @access private
     * @var integer
     */
    private $_match_id    = 0;

    /**
     * 用户操作内容所在的城市
     *
     * @access private
     * @var string
     */
    private $_city = NULL;
    
    /**
     * 匹配房源信息表
     *
     * @access private
     * @var string
     */
    private $_tbl_match = NULL;
    
    /**
     * 求购、求租信息表名称数组
     *
     * @access private
     * @var string
     */
    protected $_tbl_arr = array( 'buy_match' , 'rentwanted_match' );
    
    /**
     * 查询的字段
     *
     * @access private
     * @var string
     */
    private $_search_fields = array();
    
    /**
     * Constructor
     */
    public function __construct() 
    {
        parent::__construct(1,1,1);
    }


    /**
     * 设置需求用户编号
     *
     * @access	public
     * @param	int $want_uid 需求用户编号
     * @return	void
     */
    public function set_want_uid($want_uid)
    {
        $this->_want_uid = intval( $want_uid );
    }
    
    
    /**
     * 获取需求用户编号
     *
     * @access	public
     * @return	void
     */
    public function get_want_uid()
    {
        return $this->_want_uid;
    }
    
    
    /**
     * 设置需求信息编号
     *
     * @access	public
     * @param	int $want_id 需求信息编号
     * @return	void
     */
    public function set_want_id($want_id)
    {
        $this->_want_id = intval( $want_id );
    }
   
    
    /**
     * 获取需求信息编号
     *
     * @access	public
     * @return	void
     */
    public function get_want_id()
    {
        return $this->_want_id;
    }
    
    
    /**
     * 设置匹配用户编号
     *
     * @access	public
     * @param	int $match_uid 匹配用户编号
     * @return	void
     */
    public function set_match_uid( $match_uid )
    {
        $this->_match_uid = intval( $match_uid );
    }
    
    
    /**
     * 获取设置匹配用户编号
     *
     * @access	public
     * @return	void
     */
    public function get_match_uid()
    {
        return $this->_match_uid;
    }
    
    
    /**
     * 设置匹配房源编号
     *
     * @access	public
     * @param	int $match_id 匹配房源编号
     * @return	void
     */
    public function set_match_id($match_id)
    {
        $this->_match_id = intval( $match_id );
    }
    
    
    /**
     * 获取匹配房源编号
     *
     * @access	public
     * @return	void
     */
    public function get_match_id()
    {
        return $this->_match_id;
    }
    
    
    /**
     * 设置求购、求组匹配表名称
     *
     * @access	public
     * @param	string $tbl_name 表名称
     * @return	void
     */
    public function set_tbl_match( $tbl_name )
    {
        $this->_tbl_match = strip_tags( $tbl_name );
    }
    
    
    /**
     * 获取求购、求组匹配表名称
     *
     * @access	public
     * @param	void
     * @return	string 求购、求组匹配表名称
     */
    public function get_tbl_match( )
    {
        return $this->_tbl_match;
    }
    
    
    /**
     * 设置查询字段数组
     *
     * @access	public
     * @param	array $serach_fields 需要查询的数组
     * @return	void
     */
    public function set_search_fields( $serach_fields )
    {
        return $this->_search_fields = $serach_fields;
    }
    
   
    /**
     * 获取设置的查询字段数组
     *
     * @access	public
     * @return	array 查询字段数组
     */
    public function get_search_fields( )
    {
        return $this->_search_fields;
    }
    
    
    
    /**
     * 添加匹配信息
     *
     * @access	public
     * @return	int 是否添加成功，大于0添加成功，等于0添加失败。
     */
    public function add_match_info( $data_info )
    {   
        $result = 0;
        if( is_array($data_info) && !empty($data_info) )
        {
            $this->db->insert( $this->get_tbl_match() , $data_info );
            $result=  ( $this->db->affected_rows() == 1 ) ? $this->db->insert_id() : 0;
        }
        
        return $result;
    }
    
    
    /**
     * 根据条件删除匹配信息
     *
     * @access	private
     * @param string  $cond_where 删除条件
     * @return boolean true-删除成功，false-删除失败。
     */
    protected function delete_matchinfo_by_cond( $cond_where )
    {   
        $result = FALSE;
        
        if( $cond_where != '' )
        {   
            $this->db->where( $cond_where );
            $this->db->delete( $this->get_tbl_match());
            $result =  ( $this->db->affected_rows() == 1 ) ? TURE : FALSE;
        }
        
        return $result;
    }
    
    
    /**
     * 根据需求ID删除匹配信息
     *
     * @access	public
     * @return boolean TRUE-删除成功，FALSE-删除失败
     */
    public function delete_matchinfo_by_wantid( )
    {   
        $result = FALSE;
        
        $want_id = $this->get_want_id();
        
        if( $wantid > 0 )
        {   
            $cond_where = "want_id = '.$want_id.' ";
            $result = $this->delete_matchinfo_by_cond($cond_where);
        }
        
        return $result;
    }
    
    
    /**
     * 根据匹配房源的ID删除匹配信息
     *
     * @access	public
     * @return boolean TRUE-删除成功，FALSE-删除失败
     */
    public function delete_matchinfo_by_matchid( )
    {   
        $result = FALSE;
        
        $match_id = $this->get_match_id_id();
        
        if( $match_id > 0 )
        {   
            $cond_where = "match_id = '.$match_id.' ";
            $result = $this->delete_matchinfo_by_cond($cond_where);
        }
        
        return $result;
    }
    
    
    /**
     * 根据需求人编号删除匹配信息
     *
     * @access	public
     * @return boolean TRUE-删除成功，FALSE-删除失败
     */
    public function delete_matchinfo_by_wantuid( )
    {   
        $result = FALSE;
        
        $want_id = $this->get_want_uid();
        
        if( $want_id > 0 )
        {   
            $cond_where = "want_uid = '.$want_uid.' ";
            $result = $this->delete_matchinfo_by_cond($cond_where);
        }
        
        return $result;
    }
    
    
    /**
     * 根据匹配人编号删除匹配信息
     *
     * @access	public
     * @param int $infofrom  账号类型 1个人、5 经纪人
     * @return boolean TRUE-删除成功，FALSE-删除失败
     */
    public function delete_matchinfo_by_match_uid( $infofrom = '' )
    {   
        $result = FALSE;
        
        $match_uid = $this->get_match_uid();
        
        if( $match_uid > 0 )
        {   
            $cond_where = "match_uid = '.$match_uid.'";
            
            if( $infofrom != '')
            {
                $cond_where .= " AND infofrom = '.$infofrom.' ";
            }
            
            $result = $this->delete_matchinfo_by_cond($cond_where);
        }
        
        return $result;
    }
    
    
    /**
     * 根据匹配人编号删除匹配信息
     *
     * @access	protected
     * @param array $update_arr  需要更新的字段键值对数组
     * @param string $cond_where 更新数据的查询条件
     * @return boolean TRUE-更新成功，FALSE-更新失败
     */
    protected function update_matchinfo_by_cond( $update_arr , $cond_where )
    {   
        $result = FALSE;
        
        if(is_array($update_arr) && !empty($update_arr) && $cond_where != '')
        {
           $this->db->where( $cond_where ); 
           $this->db->update( $this->get_tbl_match() , $update_arr);
           $result=  ( $this->db->affected_rows() == 1 ) ? TRUE : FALSE;
        }
        
        return  $result ;
    }
    
    
    /**
     * 根据主键id更新匹配信息
     *
     * @access	protected
     * @param int    $id 主键id 
     * @param array  $update_arr  需要更新的字段键值对数组
     * @return boolean TRUE-更新成功，FALSE-更新失败
     */
    public function update_matchinfo_by_id( $id , $update_arr )
    {   
        $result = FALSE;
        
        if( $id > 0 )
        {   
            $cond_where = "id = '.$id.' ";
            $result = $this->update_matchinfo_by_cond( $update_arr, $cond_where);
        }
        
        return $result;
    }
    
    
    /**
     * 根据条件获得总数
     *
     * @access	protected
     * @param string  $cond_where  根据条件
     * @return int  符合条件的记录个数
     */
    protected function get_count_by_cond( $cond_where )
    {
        $count_num = 0;
        
        //查询条件
        if( $cond_where != '')
        {
            $this->dbback->where( $cond_where );
            $count_num = $this->dbback->count_all_results($this->get_tbl_match());
        }
        
        return  intval( $count_num );
    }
    
    
    /**
     * 根据需求编号获取已匹配信息总数
     *
     * @access   public
     * @param    string  $infofrom  匹配房源的来源
     * @param    int  starttime  从给定的时间戳时间点开始统计
     * @return   int  符合条件的记录个数
     */
    public function get_count_by_wantid( $infofrom = '' , $starttime = 0 )
    {
        $count_num = 0;

        $want_id = $this->get_want_id();

        if( $want_id > 0 )
        {   
            $cond_where = "want_id = '".$want_id."'";
            
            if( $infofrom != '')
            {
                $cond_where .= " AND infofrom = '".$infofrom."' ";
            }
            
            if( $starttime == 0)
            {
                $start_time = time() - 86400 * 30;
            }
            
            $cond_where .= " AND creattime >= '".$start_time."' ";
            
            $count_num = $this->get_count_by_cond($cond_where);
            //echo $this->dbback->last_query();
        }

        return  intval( $count_num );
    }
    
    
    /**
     * 根据匹配人账号获取已匹配信息总数
     *
     * @access   public
     * @param    string  $infofrom  匹配房源的来源
     * @param    int  starttime  从给定的时间戳时间点开始统计
     * @return   int  符合条件的记录个数
     */
    public function get_count_by_matchuid( $infofrom = '' , $starttime = 0 )
    {
        $count_num = 0;

        $match_uid = $this->get_match_uid();

        if( $match_uid > 0 )
        {   
            $cond_where = "match_uid = '".$match_uid."'";
            
            if( $infofrom != '')
            {
                $cond_where .= " AND infofrom = '".$infofrom."' ";
            }
            
            if( $starttime == 0)
            {
                $start_time = time() - 86400 * 30;
            }
            
            $cond_where .= " AND creattime >= '".$start_time."' ";
            
            
            $count_num = $this->get_count_by_cond($cond_where);
        }

        return  intval( $count_num );
    }
    

    /**
     * 根据编号获取匹配信息详情
     *
     * @access   public
     * @param    int  $id  信息编号
     * @return   int  匹配信息详情
     */
    public function get_info_by_id( $id )
    {
        $data_info = array();
        
        $tbl_match = $this->get_tbl_match();

        if( $id > 0 )
        {   
            $cond_where = "id = '.$id.'";
            $this->dbback->where( $cond_where );
            
            //需要查询的房源需求信息字段
            $select_fields = $this->get_search_fields();

            if( isset( $select_fields ) && !empty( $select_fields ) )
            {   
                //查询字段
                $select_fields_str = implode( ',' , $select_fields ) ;
                $this->dbback->select( $select_fields );
            }
        
            $data_info = $this->dbback->get( $tbl_match )->row_array();
        }

        return  $data_info;
    }
    
    
    /**
     * 根据编号获取匹配信息详情
     *
     * @access   protected
     * @param	string  $cond_where 查询条件
     * @param	int     $offset 偏移数,默认值为0
     * @param	int     $limit 每次取的条数，默认值为10
     * @param	string  $order_key 排序字段，默认值
     * @param	string  $order_by  升序、降序，默认降序排序
     * @return   int  匹配信息详情
     */
    protected function get_list_by_cond( $cond_where , $offset = 0 , $limit = 10 
            , $order_key = 'creattime' , $order_by = 'DESC' )
    {
        //房源需求信息表
        $tbl_match = $this->get_tbl_match();
        
        //需要查询的房源需求信息字段
        $select_fields = $this->get_search_fields();
        
        if( isset( $select_fields ) && !empty( $select_fields ) )
        {   
            //查询字段
            $select_fields_str = implode( ',' , $select_fields ) ;
            $this->dbback->select( $select_fields );
        }
        
        //查询条件
        if( $cond_where != '')
        {
            $this->dbback->where( $cond_where );
        }

        //排序条件
        $this->dbback->order_by( $order_key , $order_by );

        //查询
        $arr_data = $this->dbback->get( $tbl_match , $limit , $offset )->result_array();

        return  $arr_data;
    }
    
    
    /**
     * 根据需求编号获取匹配记录
     *
     * @access   public
     * @param	int  $infofrom 匹配信息来源 1 个人、5中介
     * @param	int     $offset 偏移数,默认值为0
     * @param	int     $limit 每次取的条数，默认值为10
     * @param	string  $order_key 排序字段，默认值
     * @param	string  $order_by  升序、降序，默认降序排序
     * @return   int  匹配信息详情
     */
    public function get_list_by_wantid( $infofrom = '' , $offset = 0 
                , $limit = 10 , $order_key = 'creattime' , $order_by = 'DESC' )
    {   
        $arr_data = array();
        
        $want_id = $this->get_want_id();
        
        if( $want_id > 0 )
        {  
            $cond_where = "want_id = '".$want_id."' ";
            
            if($infofrom != '')
            {
                $cond_where .= " AND infofrom = '".$infofrom."'";
            }
            
            $arr_data = $this->get_list_by_cond( $cond_where , $offset , 
                    $limit , $order_key , $order_by );
        }
        
        return $arr_data;
    }
    
    
    /**
     * 根据根据匹配人编号获取匹配记录
     *
     * @access   public
     * @param	int  $infofrom 匹配信息来源 1 个人、5中介
     * @param	int     $offset 偏移数,默认值为0
     * @param	int     $limit 每次取的条数，默认值为10
     * @param	string  $order_key 排序字段，默认值
     * @param	string  $order_by  升序、降序，默认降序排序
     * @param	int  $starttime  开始统计时间戳时间点
     * @return   int  匹配信息详情
     */
    public function get_list_by_matchuid( $infofrom = '' , $offset = 0 , $limit = 10 
                    , $order_key = 'creattime' , $order_by = 'DESC' , $starttime = 0 )
    {   
        $arr_data = array();
        
        $match_uid = $this->get_match_uid();
        
        if( $match_uid > 0 )
        {  
            $cond_where = "match_uid = '".$match_uid."' ";
            
            if($infofrom != '')
            {
                $cond_where .= " AND infofrom = '".$infofrom."'";
            }
            
            if( $starttime == 0)
            {
                $start_time = time() - 86400 * 30;
            }
            
            $cond_where .= " AND creattime >= '".$start_time."' ";

            
            $arr_data = $this->get_list_by_cond( $cond_where , $offset , $limit 
                    , $order_key , $order_by );
        }
        
        return $arr_data;
    }
    
    
    /**
     * 获取已参与人数
     *
     * @access  public
     * @param   int  $infofrom 匹配信息来源 1 个人、5中介
     * @param   string  $counttype 是否去重
     * @return  int  匹配个数
     */
    public function get_matchednum( $infofrom = 5 , $counttype = 'distinct'  )
    {   
        $matched_num = 0;
        
        //房源需求信息表
        $tbl_match = $this->get_tbl_match();
        
        if( $tbl_match != '')
        {   
            if( $counttype == 'distinct')
            {
                $sql = "SELECT DISTINCT(match_uid) FROM  $tbl_match ";
            }
            else
            {
                $sql = "SELECT match_uid  FROM  $tbl_match ";
            }
            
            $cond_where = " WHERE infofrom = '".$infofrom."'";
            $sql .= $cond_where;
            
            $matched_num  = $this->dbback->query( $sql )->num_rows();
        }
        
        return  $matched_num;
    }
}
/* End of file house_match_base_model.php */
/* Location: ./app/models/house_match_base_model.php */