<?php
//竞投虚假房源举报
class Complain_model extends MY_Model{
    /**
     * 类初始化
     */
	
	private $_mem_key = 'nj_complain_model';

    public function __construct() 
    {
        parent::__construct(0,1,3);		

    }
    
    
    //房源是否在推广中
    public function is_promot_house($house_id){
		$mem_key = $this->_mem_key."_is_promot_house_".$house_id;
		$cache = $this->mc->get($mem_key);        
        if(isset($cache['is_ok']) && $cache['is_ok'] == 1 )
        {
            $return = $cache['data'];
        }
        else
        {
			$now_time = date('Y-m-d H:i:s');
			//查询字段
			$this->dbback->select("id");
			//查询条件
			$this->dbback->where("house_id = '".$house_id."' and '".$now_time."' >tg_start_time  and  '".$now_time ."'< tg_end_time ");
			//查询
			$arr_data = $this->dbback->get('growth_bid_house_record_finish')->row_array();
			$return = count($arr_data)>0 ? TRUE : FALSE;
			$this->mc->add($mem_key , array('is_ok'=>1 , 'data'=>$return) ,  600);
		}
        return $return;
    }   

    

    
    //查找所有分公司
    public function all_company_code($company_code){
		$mem_key = $this->_mem_key."_all_company_code_".$company_code;
		$cache = $this->mc->get($mem_key);        
        if(isset($cache['is_ok']) && $cache['is_ok'] == 1 )
        {
            $all_son_company = $cache['data'];
        }
        else
        {
			//查询字段
			$this->dbback->select("parentcode");
			//查询条件
			$this->dbback->where("agentcode = '".$company_code."'");
			//查询
			$arr_data = $this->dbback->get('agency')->row_array();  		
			if($arr_data['parentcode']){			
			   $all_son_company = $this->son_company($arr_data['parentcode']);
			}else{
			   $all_son_company =  array($company_code);
			}
            $this->mc->add($mem_key , array('is_ok'=>1 , 'data'=>$all_son_company) ,  3600);
		}
        return $all_son_company;
    }
    
    
     /*
     * 获取分公司code
     *  prohibit_company
     * return array
     */
    public function son_company($pcompany_code){
		$mem_key = $this->_mem_key."_son_company_".$pcompany_code;
		$cache = $this->mc->get($mem_key);        
        if(isset($cache['is_ok']) && $cache['is_ok'] == 1 )
        {
            $temp_agent = $cache['data'];
        }
        else
        {

			$this->dbback->select('agentcode');
			$cond_where = "parentcode = '".$pcompany_code."'";
			$this->dbback->where($cond_where);		
			
			$arr_data = $this->dbback->get('agency')->result_array();    
			//echo $this->dbback->last_query();echo "@@@";
			if(is_array($arr_data)&&!empty($arr_data)){
				foreach ($arr_data as $value){
				   $temp_agent[] =  $value['agentcode'];
				}
			}
			$this->mc->add($mem_key , array('is_ok'=>1 , 'data'=>$temp_agent) ,  3600);
		}
        return $temp_agent;        
    }

	  /**
     * 经纪人是否在黑名单中(加缓存)
     */    
    public function isJoinBidBlackList($broker){
		$mem_key = $this->_mem_key."_is_join_bid_black_list_".$broker;
		$cache = $this->mc->get($mem_key);        
        if(isset($cache['is_ok']) && $cache['is_ok'] == 1 )
        {
            $return = $cache['data'];
        }
        else
        {

			$complainbidblacklist = new ComplainBidBlackList();        
			$nowTime = date('Y-m-d H:i:s');
			$conf = "'".$nowTime."'> add_time and end_time>'".$nowTime."'";
			$complainNum = $complainbidblacklist->findNumberBy($conf);
			$return = $complainNum===0 ? FALSE : TRUE; 
            $this->mc->add($mem_key , array('is_ok'=>1 , 'data'=>$return) ,  300);
		}
        return $return;
    }
    
    /**
     * 经纪人加入黑名单的最新记录(加缓存)
     */
    public function getLastBidBlackList($broker){       
		$mem_key = $this->_mem_key."_get_last_bid_black_list_".$broker;
		$cache = $this->mc->get($mem_key);        
        if(isset($cache['is_ok']) && $cache['is_ok'] == 1 )
        {
            $return = $cache['data'];
        }
        else
        {
			$nowTime = date('Y-m-d H:i:s');
			$tbl = "complain_bid_blacklist";
			$cond_where = "broker_id = ".$broker." and '".$nowTime."'> add_time and end_time>'".$nowTime."'";
			$this->dbback->select('broker_id,add_time,end_time');
			$this->dbback->where($cond_where);
			$this->dbback->order_by('id', 'desc');
			$this->dbback->limit(1);
			$arr_data = $this->dbback->get($tbl)->row_array(); 
			if(is_array($arr_data)&&!empty($arr_data)){
				$return = array('isJoin'=>TRUE,'end_time'=>$arr_data['end_time']);
			}else{
				$return = array('isJoin'=>FALSE,'end_time'=>'');
			}
            $this->mc->add($mem_key , array('is_ok'=>1 , 'data'=>$return) ,  300);
		}
        return $return;        
    }
    
    
    
    /**
     * 获取一个月举报成功的记录
     * getSuccessComplain
     * @return array
     */
    public function getSuccessComplain(){
        $mem_key = $this->_mem_key."_get_success_complain_list_lastest";
       // $this->mc->delete($mem_key);
		$cache = $this->mc->get($mem_key);        
        if(isset($cache['is_ok']) && $cache['is_ok'] == 1 )
        {
            $arr_data = $cache['data'];
        }
        else
        {        
            $startTime = date('Y-m-d',strtotime('-1 month'))." 23:59:59";
            $endTime = date('Y-m-d')." 23:59:59";          
            $cond_where = "status = 2 AND check_time>'".$startTime."' AND check_time<'".$endTime."'";
            $this->dbback->select("is_bid,broker_id,check_time,username,truename,report_status");
            $this->dbback->where($cond_where);	
            $this->dbback->order_by('check_time', 'desc');
            $this->dbback->join('broker', 'broker.uid = complain_record.broker_id');
			$this->dbback->limit(20);
            $arr_data = $this->dbback->get('complain_record')->result_array();
            $this->mc->add($mem_key , array('is_ok'=>1 , 'data'=>$arr_data) ,  3600);
        }
        return $arr_data;
        
    }



	 /**
     * 获取经纪人被举报几次
     * getBrokerComplainNum
	 * @param  int $uid  
     * @return int
     */
	 public function getBrokerComplainNum($uid){
		$mem_key = $this->_mem_key."_get_broker_complain_num_".$uid;
		//$this->mc->delete($mem_key);
		$cache = $this->mc->get($mem_key);    		
        if(isset($cache['is_ok']) && $cache['is_ok'] == 1 )
        {
            $num = $cache['data'];
        }
        else
        {
			$complainRecord = new ComplainRecord();  
			$cond_where = "complain_broker = $uid and status =2";
		    $num = $complainRecord->findCountByConf($cond_where,"house_id");		
			$this->mc->add($mem_key , array('is_ok'=>1 , 'data'=>$num) ,  600);
		}
		return $num;		
	 }
    
    
    
    
    
    
    
    
    
  
    
    
    
    
    
    
    
    
    
    
    
    
}

