<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

/**
 * User_Statistic_Model Class
 *
 * 用户操作日志、用户登录日志、用户注册方式、用户行为记录等数据
 *
 * @package         center
 * @subpackage      model
 * @category        model
 * @author          liuhu
 */
class Want_House_Model extends MY_Model
{

    /**
    * 用户ID
    *
    * @access private
    * @var integer
    */
    private  $_uid    = 0;

    /**
    * 用户操作内容所在的城市
    *
    * @access private
    * @var string
    */
    private $_city  = 'nj';

    /**
    * 房源表
    *
    * @access private
    * @var string
    */
    private  $_tbl_house    = NULL;

    /**
    * 匹配房源信息表
    *
    * @access private
    * @var string
    */
    private $_tbl_match  = NULL;


    /**
    * 房源查询字段
    *
    * @access private
    * @var array
    */
    private $_house_fields = array();


    /**
    * Constructor
    */
    public function __construct()
    {
        parent::__construct();

    }


    /**
    * set_tbl_house
    *
    * 初始化房源表表名参数
    *
    * @access	public
    * @param	string $tbl 房源表名称
    * @return	void
    */
    public function set_tbl_house($tbl)
    {
        $this->_tbl_house = strip_tags($tbl);
    }


    /**
    * set_tbl_match
    *
    * 初始化匹配表名参数
    *
    * @access	public
    * @param	string $tbl 匹配表名称
    * @return	void
    */
    public function set_tbl_match($tbl)
    {
        $this->_tbl_match = strip_tags($tbl);
    }



    /**
    * set_tbl_match
    *
    * 初始化需要查询的房源信息字段数组
    *
    * @access	public
    * @param	array $fields 字段数组
    * @return	void
    */
    public function set_house_fields($fields)
    {
        $this->_house_fields = $fields;
    }


    /**
    * get_matched_by_id
    *
    * 获取求购、求租已匹配房源信息
    *
    * @access	public
    * @param	int     $want_id 求租、求购编号
    * @param	int     $offset 偏移数
    * @param	int     $pagesize 每次取的条数
    * @param	string  $order 升序降序
    * @return	array   求租求购记录
    */
    function get_matched_by_id ($want_id , $offset = 0 , $pagesize = 0 , $order = 'DESC')
    {
        //匹配房源房源数组
        $MatchedHouseInfo = array();

        $want_id = intval($want_id);
        $tbl_house = $this->_tbl_house;
        $tbl_match = $this->_tbl_match;

        //判断求租、求购信息编号是否为空
        if($want_id == 0)
        {
            return $MatchedHouseInfo;
        }

        //判断求购、求租表 和房源表是否为空
        if( $tbl_house == NULL  || $tbl_match == NULL )
        {
            return $MatchedHouseInfo;
        }

        //需要查询的房源字段信息
        $house_fields_arr = $this->_house_fields;
        $fields_num = count ( $house_fields_arr );

        if( $fields_num >= 1 )
        {
            foreach ( $house_fields_arr as $key => $value )
            {
                $house_fields[$key] = $tbl_house.'.'.$value;
            }
        }
        $house_fields[] = $tbl_match.'.id as match_id';
        $house_fields[] = $tbl_match.'.isread as isread';

        //查询条件
        $cond_where = "$tbl_match.want_id = '".$want_id."' AND $tbl_match.esta = 0 and $tbl_house.esta=1 ";

        //设置查询字段
        $this->db_esfhousebak->select( $house_fields );

        //设置查询条件
        $this->db_esfhousebak->where($cond_where);

        //设置查询的表
        $this->db_esfhousebak->from($tbl_match);

        //链接表
        $this->db_esfhousebak->join($tbl_house , $tbl_match.".match_id=".$tbl_house.".id" ,'left');

        //排序条件
        $this->db_esfhousebak->order_by( $tbl_match.'.creattime',$order);

        //LIMIT条件
        $this->db_esfhousebak->limit( $pagesize , $offset );

        //查询数组
        $MatchedHouseInfo = $this->db_esfhousebak->get()->result_array();
		//if($want_id==103023){
		//echo $this->db_esfhousebak->last_query();
		//}


        return $MatchedHouseInfo;
    }


    /**
    * get_matched_num_by_id
    *
    * 获取求购、求租已匹配房源个数
    *
    * @access	public
    * @param	int   $want_id 求租、求购编号
    * @return	int   求购求租匹配房源个数
    */
    function get_matched_num_by_id ($want_id)
    {
        $want_id = intval($want_id);

        $tbl_house = $this->_tbl_house;
        $tbl_match = $this->_tbl_match;

        //经纪人自己的房源数组
        $MatchedNum = 0;
        $cond_where = "$tbl_match.want_id = '".$want_id."' AND $tbl_match.esta = 0 and $tbl_house.esta=1";

        $this->db_esfhousebak->where($cond_where);
        $this->db_esfhousebak->from($tbl_match);
        $this->db_esfhousebak->join($tbl_house,"$tbl_match.match_id = $tbl_house.id ");
        $MatchedNum = $this->db_esfhousebak->count_all_results();
		//if($want_id==103023){
		//echo $this->db_esfhousebak->last_query();
		//}

        return $MatchedNum;
    }
}