<?php
$conf_basedir	= "../../";	//相对主目录的位置
$conf_where		= "admin";	//选择调用相应模块
$directory		= basename(dirname(__FILE__));	//定义当前所在的目录，引入模块公用函数
include_once($conf_basedir."base.inc.php");
include_once WEB_MODULE . '/growth/base.inc.php';
//error_reporting(7);
ini_set("display_errors",true);
checkAuth('19');	//判断登录和权限

$act = array("head", "certificate", "account", "real","id_card","business_card");

if(is_array($stat) && !empty($stat))
{
 
	$brokercheck = new BrokerHouseCheck();
	foreach($stat as $key=>$value)
	{
              if($config_GrowthCredit==1){
                $brokerGroup = new BrokerGroup();
                $brokerInfo = $brokerGroup->getBrokerGroupInfo($uid[$key]);        
                $arrBroker = array(
                    'id' => $brokerInfo['uid'],
                    'groupid' => $brokerInfo['groupid'],
                ); 
                
                $gbc = new GrowthBrokerCredit($arrBroker); 
                $gbe = new GrowthBrokerExp($arrBroker);    
              }
         
		$value = intval($value);
		if($actdo == 'real')	//real审核
		{
			//$brokercheck->checkreal($value,$reason[$key],$id[$key],$indate[$key]);
           
		}
		else if($actdo == 'head')	//头像审核
		{
			$brokercheck->checkhead($value,$reason[$key],$uid[$key],$key,$pic[$key],$indate[$key]);
                        if($config_GrowthCredit==1){
                             $gbc->increase('avatarCert');                           
                             $gbe->increase('avatarCert');//增长代码
                        }
                       
		}
                else if($actdo == 'certificate')	//证书审核
		{
			$brokercheck->checkcertificate($value,$reason[$key],$uid[$key],$key,$pic[$key],$type[$key],$indate[$key]);
                        if($config_GrowthCredit==1){
                             $gbc->increase('brokerCert');                           
                             $gbe->increase('brokerCert');//增长代码      
                        }
                                         
                }
                else if($actdo == 'account')	//账号审核
		{
			//$brokercheck->checkaccount($value,$reason[$key],$id[$key],$indate[$key]);
		}elseif ($actdo == 'id_card') {//身份证审核 xue 2014-2-17       
                        $brokercheck->checkIdCard($value,$reason[$key],$uid[$key],$key,$pic[$key],$type[$key],$indate[$key]);
                        if($config_GrowthCredit==1){
                             $gbc->increase('idCert');                 
                             $gbe->increase('iDCert');//增长代码    
                        }
                       
                        
                }elseif ($actdo == 'business_card') {//名片审核 xue 2014-2-17
                        $brokercheck->checkBusinessCard($value,$reason[$key],$uid[$key],$key,$pic[$key],$type[$key],$indate[$key]);
                        if($config_GrowthCredit==1){
                            $gbc->increase('bCardCert');                           
                            $gbe->increase('bCardCert');//增长代码    
                        }
                        
                        
                }
	}
    //设置管理员审核经纪人数据操作数量记录
	//$num = count($stat);
	//setCheckBrokerOpernum($actdo, array($actdo=>$num));
	
}

sleep(1);
echo "<script>window.location.href='./".$actdo."_manage.php'</script>";
exit;

//经纪人审核
class BrokerHouseCheck
{
	private $systime = '';
        private $db;
        private $dbback;
        private $mc;
        private $conf_cityflag;
        public function __construct() {
            global $db,$dbback,$mc,$conf_cityflag;           
            $this->db = $db;
            $this->dbback = $dbback;
            $this->mc = $mc;
            $this->conf_cityflag = $conf_cityflag;
            $this->systime = time();
        }

        public function BrokerHousePicCheck()
	{
		$this->systime = time();
	}
       //头像审核
	public function checkhead($valid_flag,$reason,$uid,$id,$pic,$indate)
	{
            $kind = $this->conf_cityflag == 'nj' ? 'p_1' : 'p_2_2';    //xue 2014-2-17   
            $sql = "update broker_certificate set valid_flag='".$valid_flag."',reason='".$reason."' where id = '".$id."'";
            $this->db->Execute($sql);
            $this->broke_log($uid,$pic,$valid_flag,$kind,$id);
	    //如果通过审核，按照比例同步到QA库
            if($valid_flag == 1){
               $this->addBroker2Qa($uid,$pic,$valid_flag,$kind,$indate);
	    }
            //修改broker xue 2014-2-17
            $this->updateBrokerPic($id,$pic,$valid_flag);
	}
        
        //身份证审核 xue 2014-2-17
        public function checkIdCard($valid_flag,$reason,$uid,$id,$pic,$type,$indate)
	{   $kind = 'p_2';
            $sql = "update broker_certificate set valid_flag='".$valid_flag."',reason='".$reason."' where id = '".$id."'";
            $this->db->Execute($sql);
            $this->broke_log($uid,$pic,$valid_flag,$kind,$id);
	    //如果通过审核，按照比例同步到QA库
	    if($valid_flag == 1){
                 $this->addBroker2Qa($uid,$pic,$valid_flag,$kind,$indate);
	    }
            //修改broker xue 2014-2-17
            $this->updateBrokerPic($id,$pic,$valid_flag);
	}
        //证件审核
        public function checkcertificate($valid_flag,$reason,$uid,$id,$pic,$type,$indate){
            $sql = "update broker_certificate set valid_flag='".$valid_flag."',reason='".$reason."' where id = '".$id."'";       
            $this->db->Execute($sql);
            $this->broke_log($uid,$pic,$valid_flag,$type,$id);
	    //如果通过审核，按照比例同步到QA库
	    if($valid_flag == 1){
	        $this->addBroker2Qa($uid,$pic,$valid_flag,$type,$indate);
	    }
            //修改broker xue 2014-2-17
            $this->updateBrokerPic($id,$valid_flag);
	}        
        
        //名片审核 xue 2014-2-17
        public function checkBusinessCard($valid_flag,$reason,$uid,$id,$pic,$type,$indate)
	{   $kind = 'p_3';
            $sql = "update broker_certificate set valid_flag='".$valid_flag."',reason='".$reason."' where id = '".$id."'";
            $this->db->Execute($sql);
            $this->broke_log($uid,$pic,$valid_flag,$kind,$id);
	    //如果通过审核，按照比例同步到QA库
	    if($valid_flag == 1){
                 $this->addBroker2Qa($uid,$pic,$valid_flag,$kind,$indate);
	    }
            //修改broker xue 2014-2-17
            $this->updateBrokerPic($id,$valid_flag);
	}




	//记录日志
	public function broke_log($uid,$pic,$valid_flag,$type,$id){
		global $web_auth;		
		$uid = intval($uid);
		$binfo = $this->getBrokerInfo($uid);
                $arr['certificate_id'] = $id;
                $arr['uid'] = $uid;
		$arr['agentcode'] = $binfo['username'];
		$arr['type'] = $type;
		$arr['account'] = $_SESSION[$web_auth]["username"];
		$arr['dateline'] = time();
		$arr['pic'] = $pic;
                $arr['valid_flag'] = $valid_flag;
		$q = insert_sql("admin_broker_check_log",$arr);
                $this->db->Execute($q);
	}


       //获得经纪人详细信息
	private function getBrokerInfo($uid)
	{
		global $dbback,$mc;

		$return = array();

		$memkey = "getBrokerInfo".$uid;
		$cache = $mc->get($memkey);
		if($cache['isok'] == 1)
		{
			$return = $cache['data'];
		}
		else
		{
			$sql = "select b.username, a.agentshortname, b.master from broker b left join agency a on b.agentcode = a.agentcode where b.uid = '".$uid."'";
			$return = $dbback->GetRow($sql);
			$mc->add($memkey, array('isok'=>1,'data'=>$return), false, 3600);
		}
		return $return;
	}

	private function addBroker2Qa($uid,$pic,$valid_flag,$type,$indate){
		global $web_auth;
		$randnum = rand(1, 50);

		if($randnum == 1){
			$ss['uid'] = $uid;
			$ss['type'] = $type;
			$ss['picurl'] = $pic;
			$ss['valid_flag'] = $valid_flag;
			$ss['dateline'] = $this->systime;
			$ss['indate'] = $indate;
			$ss['admin'] = $_SESSION[$web_auth]["username"];
			
			$q = insert_sql("admin_broker_qa", $ss);
			$this->db->Execute($q);
		}
	}
        
        //审核后修改broker表中数据 xue 2014-2-17
        private function updateBrokerPic($id,$valid_flag){
            
            $r = $this->dbback->GetRow("SELECT * FROM broker_certificate WHERE id= '".intval($id)."' ");
            if ($valid_flag == 1) {                 
                if($r[kind]=='p_2_3') $r[kind] = 'p_2';
                if($r[kind]== 'p_2'||$r[kind]=='p_3'||$r[kind]=='p_4_1'||$r[kind]=='p_4_2'){                   
                    $q = "update broker set `" . $r[kind] . "_pic`='$r[pic1]' where uid =$r[uid] ";
                    $this->db->Execute($q);
                    //身份证标识字段
		    if($r['kind'] == "p_2"){
			$this->db->Execute("UPDATE broker SET identity = 1 WHERE uid =$r[uid]");
		     }
		     //更新职业认证
		     if($r['kind'] == "p_4_1" || $r['kind'] == "p_4_2"){
                         $this->db->Execute("UPDATE broker SET license = 1 WHERE uid = $r[uid]");
		     }
                }elseif ( $kind == 'p_1'||$kind =='p_2_2') {
                    $q="UPDATE broker SET smallphoto = '$r[pic1]' WHERE uid = $r[uid] ";
		    $this->db->Execute($q);
                }   
                //加积分的放这儿
                
                /**
                //经纪人名片认证积分操作
	        include_once(CORE_LIB."/class_BrokerCredit.php");
		$I = new BrokerCredit($r[uid]);
		switch ($kind){
		    case "p_2_2":
			$I->dealCredit('plus_smallphoto');
			break;
		    case "p_2_3":
			$I->dealCredit('plus_identity');
		        break;
		    case "p_3":
			 $I->dealCredit('plus_card');
			 break;
		    case "p_4_1":
		         $I->dealCredit('plus_locCertificate');
			 break;
		    case "p_4_2":
		         $I->dealCredit('plus_pubCertificate');
			 break;
		}
                **/
            } else {
               if($r[kind]=='p_2_3') $r[kind] = 'p_2';
               if($r[kind]== 'p_2'||$r[kind]=='p_3'||$r[kind]=='p_4_1'||$r[kind]=='p_4_2'){                   
                    $q = "update broker set `" . $r[kind] . "_pic`='' where uid =$r[uid] ";
                    $this->db->Execute($q);
                    //身份证标识字段
		    if($r['kind'] == "p_2"){
			$this->db->Execute("UPDATE broker SET identity = 0 WHERE uid =$r[uid]");
		     }
		     //更新职业认证
		     if($r['kind'] == "p_4_1" || $r['kind'] == "p_4_2"){
                         $this->db->Execute("UPDATE broker SET license = 0 WHERE uid = $r[uid]");
		     }
                }elseif ( $kind == 'p_1'||$kind =='p_2_2') {
                    $q="UPDATE broker SET smallphoto = '$r[pic1]' WHERE uid = $r[uid] ";
		    $this->db->Execute($q);
                }  
                //删积分的接口放这儿
                
                
            }
    
        }

}
?>