<?php
//学区房
class School_model extends MY_Model{
    private $tbl = '';
    
    public function __construct() {
        parent::__construct(0,1,3);
        $this->tbl = "school";
        $this->load->library('server',array('appName'=>NS_SELL,'strCheckMethod'=>"",'returnMethod'=>"ary",'returnCharSet'=>"gbk"));
		
        
    }
    
    /**
     * 某个学校的周边小区id数组
     */
    public function getBlockIdBySchoolId($schoolId){
		$key = $this->config_cityflag . '_set_promot_school_house' . $schoolId;
        $cache = $this->mc->get($key);
		if($cache['isok'] == 1)
        {
            $data_arr = $cache['data'];
        }
        else
        {

			$where_cond['id'] =  $schoolId;
			$this->dbback->where($where_cond);
			$data_arr = $this->dbback->select('blockid')->get($this->tbl)->row_array();   
			if(!empty($data_arr)){
				$data_arr = explode(',', $data_arr['blockid']); 
			}

		    $this->mc->set($key, array('isok'=>1, 'data'=>$data_arr), 3600);


		}
        
        return $data_arr;
    }
    
    
    /**
     * 配置文件学校所有小区id
     */
    public function getAllSchoolBlock(){
        $key = $this->config_cityflag . '_set_promot_all_school_block';
        $cache = $this->mc->get($key);
		if($cache['isok'] == 1)
        {
            $newblock = $cache['data'];
        }
        else
        {
		   $school = $this->config->item('set_promot_school');
		   if(is_array($school)&&!empty($school)){
			   foreach ($school as $key=>$value){
				   $block[] = $this->getBlockIdBySchoolId($key);              
			   }           
			   $newblock =array();
			   for($i=0;$i<count($block);$i++){
				   $newblock = array_merge($newblock,$block[$i]);
			   }            
			  
		   }

		   $this->mc->set($key, array('isok'=>1, 'data'=>$newblock), 3600);

		}

		return $newblock;
    }
    
    /**
     * 经纪人是否有房源在学区中
     */
    public function isEstHouseInSchoolByUid($brokerId,$blockArr){
        $record = $this->getSchoolHouseCountByUid($brokerId, $blockArr);
        if($record>0){
            return TRUE;
        }  else {
            return FALSE;    
        }
    }
    
    
    /**
     * 获取经纪人的学区房源
     */
    public function getSchoolHouseByUid($brokerId,$blockArr){     
        if(is_array($blockArr)&&!empty($blockArr)){            
            $blockField = "";
            foreach ($blockArr as $value){                
                    $blockField .= $value.","; 
            }      
            $blockField = rtrim($blockField,',');
        }

		$key = $this->config_cityflag . '_set_promot_'.$brokerId.'_school_house'.$blockField;
        $cache = $this->mc->get($key);
		if($cache['isok'] == 1)
        {
            $record = $cache['data'];
        }
        else
        {
			$fields = array('id', 'infotype', 'blockshowname', 'room', 'hall', 'buildarea', 'price', 'creattime', 'contactor', 'telno');
			$cond_where = "uid = ".$brokerId." and infofrom = 5 and esta = 1 And blockid in(".$blockField.")";     
	   
			
			$this->load->model('sell_model');
			$this->sell_model->set_select_fields($fields);
			$record =  $this->sell_model->find_all_by( $cond_where , 0,200);

            $this->mc->set($key, array('isok'=>1, 'data'=>$record), 30);
		}
        
        return $record;
    }
    
    
        /**
     * 获取经纪人的学区房源
     */
    public function getSchoolHouseCountByUid($brokerId,$blockArr){     
      
        if(is_array($blockArr)&&!empty($blockArr)){            
            $blockField = "";
            foreach ($blockArr as $value){                
                    $blockField .= $value.","; 
            }      
            $blockField = rtrim($blockField,',');
        }

        $key = $this->config_cityflag . '_set_promot_'.$brokerId.'_school_house_count'.$blockField;
        $cache = $this->mc->get($key);
		if($cache['isok'] == 1)
        {
            $total = $cache['data'];
        }
        else
        {

			$cond_where = "uid = ".$brokerId." and infofrom = 5 and esta = 1 And blockid in(".$blockField.") ";  
				
			$this->load->model('sell_model');      
			$total =  $this->sell_model->find_count_by($cond_where);  
			$this->mc->set($key, array('isok'=>1, 'data'=>$total), 30);

		}
        return $total;
    }
    
    
    
    /**
     * 获取经纪人的学区房源
     */
    public function getSchoolHouseByUidLuc($brokerId,$blockArr){     
      
        if(is_array($blockArr)&&!empty($blockArr)){
            $i=0;
            $blockField = "";
            foreach ($blockArr as $value){
                if($i==0){
                    $blockField .= " blockid =".$value; 
                }  else {
                    $blockField .= " OR blockid =".$value; 
                }
                 $i++;
            }           
        }
     
    	$strCondition = 'id, infotype, blockshowname, room, hall, buildarea, price, creattime, contactor, telno';
        $sql = "SELECT ".$strCondition." FROM sell WHERE uid = ".$brokerId." and infofrom = 5 and esta = 1 And (".$blockField.")
            ORDER BY updatetime DESC  LUC";     
        //echo $sql;
        $ary = $this->server->query($sql);    
        $total = isset($ary["TOTAL"]) ? $ary["TOTAL"] : 0;
        $record = isset($ary["Record"]) ? $ary["Record"] : "";
        $numrows = intval($total);
        return array('num'=>$numrows,$total);
    }
    
    
    
    
    
    
    
    
}

