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

/**
 * Tool_Model Class
 *
 * 工具类
 *
 * @package         center
 * @subpackage      model
 * @category        model
 * @author          liuhu
 */
class Tool_Model extends MY_Model
{
    /**
    * Constructor
    */
    public function __construct()
    {
        parent::__construct();
    }



    //刷新数据
    public function update_buy_data($offset,$limit)
    {
        $buyinfo = array();

        //查询数据
        $this->db_esfhousebak->limit($limit,$offset);
        $this->db_esfhousebak->from('buy');
        $buyinfo  =  $this->db_esfhousebak->get()->result_array();

        if(is_array($buyinfo) && !empty($buyinfo))
        {
            foreach($buyinfo as $key => $value)
            {
                $buildyear_arr = explode('-',$value['buildyear']);

                $buy_info_new['buildyear1'] = $buildyear_arr[0];
                $buy_info_new['buildyear2'] = $buildyear_arr[1];

                $price_arr = explode('-',$value['price']);

                $buy_info_new['price1'] = $price_arr[0];
                $buy_info_new['price2'] = $price_arr[1];

                $buildarea_arr = explode('-',$value['buildarea']);

                $buy_info_new['buildarea1'] = $buildarea_arr[0];
                $buy_info_new['buildarea2'] = $buildarea_arr[1];

                $buy_info_new['allow_match'] = 0;
                $buy_info_new['expiretime'] = $value['creattime'] + 86400 * 180 ;

                $this->db_esfhouse->where('id', $value['id']);
                if(!empty($buy_info_new['price1'])){
                $this->db_esfhouse->update('buy', $buy_info_new);
                }
                echo $this->db_esfhouse->last_query();
                

            }

            $result = true;
        }
        else
        {
            $result = false;
        }

        return $result;
    }


        //刷新数据
    public function update_rentwant_data($offset,$limit)
    {
        $buyinfo = array();

        //查询数据
        $this->db_esfhousebak->limit($limit,$offset);
        $this->db_esfhousebak->from('rentwanted');
        $buyinfo  =  $this->db_esfhousebak->get()->result_array();

        if(is_array($buyinfo) && !empty($buyinfo))
        {
            foreach($buyinfo as $key => $value)
            {
                $buildyear_arr = explode('-',$value['buildyear']);

                $buy_info_new['buildyear1'] = $buildyear_arr[0];
                $buy_info_new['buildyear2'] = $buildyear_arr[1];

                $buy_info_new['allow_match'] = 0;
                $buy_info_new['expiretime'] = $value['creattime'] + 86400 * 180 ;

                $price_arr = explode('-',$value['price']);

                $buy_info_new['price1'] = $price_arr[0];
                $buy_info_new['price2'] = $price_arr[1];

                $buildarea_arr = explode('-',$value['buildarea']);

                $buy_info_new['buildarea1'] = $buildarea_arr[0];
                $buy_info_new['buildarea2'] = $buildarea_arr[1];

                $this->db_esfhouse->where('id', $value['id']);
                if(!empty($buy_info_new['price1'])){
                $this->db_esfhouse->update('rentwanted', $buy_info_new);
                }
                echo $this->db_esfhouse->last_query();
                

            }

            $result = true;
        }
        else
        {
            $result = false;
        }

        return $result;
    }

    //参与匹配的中介人数
    public function get_match_broker_num_by_day( $tbl , $count_date )
    {
        if($tbl == 'buy')
        {
            $tbl_match = 'buy_match';
        }
        else if($tbl == 'rentwanted')
        {
            $tbl_match = 'rentwanted_match';
        }

        $start_time = strtotime($count_date);
        $end_time = $start_time + 86400 ;
        $where_cond = "infofrom = 5 AND creattime > '".$start_time."' AND creattime < $end_time ";

        $this->db_esfhousebak->select('match_uid');
        $this->db_esfhousebak->distinct();
        $this->db_esfhousebak->where( $where_cond );
        $match_brokerinfo  =  $this->db_esfhousebak->get($tbl_match)->result_array();

        //echo $this->db_esfhousebak->last_query();

        return intval(count($match_brokerinfo));
    }


    //匹配中介房源数
    public function get_match_num_by_day( $tbl , $count_date )
    {
        if($tbl == 'buy')
        {
            $tbl_match = 'buy_match';
        }
        else if($tbl == 'rentwanted')
        {
            $tbl_match = 'rentwanted_match';
        }

        $start_time = strtotime($count_date);
        $end_time = $start_time + 86400 ;
        $where_cond = "infofrom = 5 AND creattime > '".$start_time."' AND creattime < $end_time ";

        $this->db_esfhousebak->from($tbl_match);
        $this->db_esfhousebak->where( $where_cond );

        $total_num  =  $this->db_esfhousebak->count_all_results();
        //echo $this->db_esfhousebak->last_query();

        return intval($total_num);
    }


    //求购求租房源数
    public function get_want_num_by_day( $tbl , $count_date )
    {
        $start_time = strtotime($count_date);
        $end_time = $start_time + 86400 ;
        $where_cond = "esta = 1 AND creattime > '".$start_time."' AND creattime < $end_time ";
        $this->db_esfhousebak->from($tbl);
        $this->db_esfhousebak->where( $where_cond );
        $total_num  =  $this->db_esfhousebak->count_all_results();
        //echo $this->db_esfhousebak->last_query();

        return intval($total_num);
    }
}