<?php
$conf_basedir	= "../../";	//相对主目录的位置
$conf_where		= "admin";	//选择调用相应模块
$directory		= basename(dirname(__FILE__));	//定义当前所在的目录，引入模块公用函数

include_once($conf_basedir."base.inc.php");
$title = '虚假房源举报记录';

$complainRecord = new ComplainRecord();

$settingType = array('0'=>'全部','1'=>'待审核','2'=>'已冻结');//状态
$settingSearch = array(0=>'请选择','houseid'=>'房源编号','brokername'=>'经纪人帐号');//查询条件
$settingOrderBy = array('ctime'=>'按时间排序');
$settingAscDesc = array('desc'=>'倒序排列','asc'=>'正序排列',);
$settingDispRows = array(20=>'20',30=>'30',40=>'40');


//查询条件，关键字
if($search_type&&$keyword) {
   
    if ($search_type == 'houseid') {
        $conf_sql['house_id'] = $keyword;
    } elseif($search_type == 'brokername') {
        $uid = getUidbyUsername($mc, $dbback, $keyword);        
        $house_arr = getBrokerHouse($mc, $dbback,'sell', $uid);     
        if(is_array($house_arr)&&!empty($house_arr)){
            foreach ($house_arr as $value){
                $house_str .= $value['id'].",";
            }
        }
        $house_str = rtrim($house_str,',');
        $search_where = " house_id in($house_str) ";
    } 
}
//状态
if($status){   
    $conf_sql['status'] = $status; 
}





//排序
$asc_desc_order = $asc_desc ? " add_time ".$asc_desc : " add_time ".'desc';

//条数uid 
$page_now = intval($page_now)==0?1:intval($page_now);
$disp_rows = intval($disp_rows)==0?20:intval($disp_rows);
$startRow = ($page_now-1)*$disp_rows;

//条件组装
if(CommonBase::isFullArray($conf_sql)){
    $conf_num = count($conf_sql);
    foreach ($conf_sql as $key=>$value){
        $sql_where .= $key."='".$value."'"; 
        $sql_where .= $conf_num != 1 ? ' and ' : ''; 
        $conf_num--;        
    }    
}
if($search_where){
    if($sql_where){
        $sql_where .= " and ".$search_where;    
    }else{
        $sql_where = $search_where; 
    }    
}

//审核时间
if($stime){
    $timeStart = $stime." 00:00:01";
    $timeEnd = $stime." 23:59:59";
    $time_where = "check_time >'".$timeStart."'  and  '".$timeEnd."'>check_time";
     if($sql_where){
        $sql_where .= " and ".$time_where;    
    }else{
        $sql_where = $time_where; 
    }    
}

$recordList = $complainRecord->findByConfGroupHaving($sql_where,$asc_desc_order,$startRow,$disp_rows,'house_id',$having);

$total_num = $complainRecord->countByConfGroupHaving($sql_where,'house_id',$having);
$page_total = ceil($total_num/$disp_rows);
$new_pagenavi = new_pagenavi2($page_now,$page_total,$total_num);



    
include_once(Template("header"));
include_once(Template("complain/complain_record"));
include_once(Template("footer"));



//获取被举报人的房源
function getBrokerHouse($mc, $dbback, $tbl='sell',$broker){
     $sql = SqlCombinHelper::select_sql($tbl,'id'," uid= '".$broker."'");	  
     $house = $mc->get($sql);
	if ($house === false) {
		$house = $dbback->getAll($sql);
		$mc->add($sql, $house, 200);
	}
    return $house;    
    
}

//获取经纪人id
function getUidByUsername($mc, $dbback, $username){
    $sql = SqlCombinHelper::select_sql('broker', 'uid', "username = '".$username."'");	
	$brokerUid = $mc->get($sql);
	if ($brokerUid === false) {
		$brokerUid = $dbback->getOne($sql);
		$mc->add($sql, $brokerUid, 3600);
	}
    return $brokerUid;
}

//根据经纪人id获取username
function getUsernameByUid($uid){
    global $mc,$dbback;
    $sql = SqlCombinHelper::select_sql('broker', 'username', "uid = '".$uid."'");	   
	$username = $mc->get($sql);
	//if ($brokerUid === false) {
		$username = $dbback->getOne($sql);
	//	$mc->add($sql, $brokerUid, 3600);
	//}
    return $username;
    
}


//根据房源查找用户id
function getUidByHouse($mc,$dbback,$tbl='sell',$house){
    $sql = SqlCombinHelper::select_sql('complain_record', 'broker_id', "tbl = '".$tbl."' AND house_id = $house");	   
	$brokerUid = $mc->get($sql);
	if ($brokerUid === false) {
		$brokerUid = $dbback->getOne($sql);
		$mc->add($sql, $brokerUid, 3600);
	}
    return $brokerUid;
    
}


//根据房源查找信息处理
function getInfoByHouse($house){
    $complainRecord = new ComplainRecord();
    $recordList = $complainRecord->findByHouseId($house);
    //投诉次数
    $complainNum = count($recordList);
    //待审核数量
    $tempId = "";
    $tempTime = "";
    $toCheckNum=0;
    if(is_array($recordList)&&!empty($recordList)){
        foreach ($recordList as $value){
           if($value['status']==1){
               $toCheckNum++;
           }
           $status[] = $value['status']; 
           //获取最小的时间（根据ID，ID越小时间越早）
           if($tempTime==""&&$tempId==""){
               $tempId = $value['id'];
               $tempTime = $value['add_time'];               
           }else{
               if($value['id']<$tempId){
                   $tempId = $value['id'];
                   $tempTime = $value['add_time'];                    
               }               
           }       
        }        
       $status = getStatusByArr($status);//状态
    }
    $data_arr = array('totalNum'=>$complainNum,'toCheckNum'=>$toCheckNum,'time'=>$tempTime,
        'status'=>$status);
    return $data_arr;
}




//获取显示状态
function getStatus($status){
    switch ($status){
        case '1': $return = '待审核';
            break;
        case '2': $return = "冻结";
            break;
        case '3': $return = "审核通过";
            break;       
        default : $return = "";
    }
    return $return;
}

//根据状态数组找出对应状态
function getStatusByArr($status){    
    if(in_array(2, $status)){
        $status = array(2,'冻结'); //冻结
    }elseif(!in_array(2, $status)&&in_array(1, $status)){
        $status = array(1,'待审核');//待审核
    }elseif(!in_array(2, $status)&&!in_array(1, $status)){
        $status = array(3,'审核通过');//审核通过
    }
    return $status;
}








