<?php
//小区专家类
class apply_brokercp{
	protected $uid;
	protected $companycode;
	protected $username;
	protected $truename;
	protected $agentname;
	protected $telno;
	protected $headimg;
	protected $groupid;
	protected $groupname;

	protected $wfen = 0; //最低积分
	protected $room = 5; //优质房源数
	protected $block_num = 4;   //专家数量
	protected $freeze_num = 3;  //可以被冻结的次数	

	function apply_brokercp($info){
		global $db,$dbback,$sys;
		$this->uid = $info[uid];
		$this->username = $info[username];
		$this->truename = $info[truename];
		$this->groupid = $info[groupid];
		$this->groupname = $info[groupname];
		$this->companycode = $info[companycode];
		$this->telno = $info[telno];
		$sql = "select agentshortname from agency where agentcode = '".$info[companycode]."'";
		$this->agentname = $dbback->GetOne($sql);
		$sql = "select smallphoto from broker where uid=$info[uid]";
		$this->headimg = $dbback->GetOne($sql);

		//读取配置文件的变量
		$this->wfen = $sys['wfen'];
		$this->room = $sys['room'];
		$this->block_num = $sys['block_num'];
		$this->freeze_num = $sys['freeze_num'];
	}

	function get_block_num() {
		return $this->block_num;
	}

	function get_wfen(){
		return $this->wfen;
	}

	function get_fen(){
		global $db;
		$sql = "select credit from broker where uid=".$this->uid;
		return (int)$db->GetOne($sql);
	}

	//验证是否申请中
	function is_apping(){
		global $db,$dbback;
		$sql = "select block_apply.id,block_apply.apptime,blockname,reason,b_introduction,b_bus,b_metro,b_other,bi_seri from block_apply left join block_apply_data on block_apply.uid=block_apply_data.uid and block_apply.apptime=block_apply_data.apptime where block_apply.uid=".$this->uid." and agentcode='".$this->companycode."' and esta=0";
		return $dbback->GetRow($sql);
		
	}

	//验证是否专家并返回信息
	function get_expert(){
		global $db,$dbback;
		$sql = "select block_apply.blockid,block_apply.apptime,block_apply.endtime,block_apply_data.reason from block_apply left join block_apply_data on block_apply.uid=block_apply_data.uid where block_apply.uid=".$this->uid." and block_apply.agentcode='".$this->companycode."' and esta = 1 and endtime>'".time()."'";


		$rs = $dbback->GetRow($sql);
		//return $sql;
		return $rs;
	}

	//获得线上小区详细
	function get_blockinfo($blockid){
		global $db,$dbback;
		$sql = "select blockname,b_introduction,b_bus,b_metro,b_other,app from block where id=".$blockid;
		return $dbback->GetRow($sql);
	}

	//是否有处罚
	function get_festa(){
		global $db,$dbback;
		$sql = "select festa from broker where groupid=3 and uid=".$this->uid." and agentcode=".$this->companycode;
		return $dbback->GetOne($sql);
	}
	
	//检查小区是否可申请
	public function get_people($blockid){
		global $db,$dbback;
		$sql = "select count(id) as num from block_apply where blockid=".$blockid." and esta=1 and endtime>".time();
		return $dbback->GetOne($sql);
	}

	//符合条件的小区新
	function get_block(){
		global $db,$dbback,$web_auth;


			if($this->wfen != 0){
			
				if($this->get_fen < $this->wfen){
					return array();
				}
			}
			
			if($this->expert_freeze()>$this->freeze_num){
				return array();
			}

		$sql = "select blockid,count(id) as num from sell where uid=".$this->uid." and agentcode='B000' and esta='1' and isstar='3' and blockid!=0 group by blockid order by num desc";
		
		$rs = $dbback->GetAssoc($sql);
		$arr_block = array();
		if(!is_array($rs)) return $arr_block;

		foreach($rs as $key=>$value){
			$sql = "select blockname from block where id=".$key . ' and esta = 2';
			$blockname = $dbback->GetOne($sql);
			if (!$blockname) {continue;}
			$blocknum = $this->get_people($key);
			if($value >= $this->room && $blocknum < $this->block_num) {
				$arr_block[$key] = array('blockname' => $blockname, 'num' => $blocknum);
			}
		}
		
		return $arr_block;
	}

	//强制指定默认的小区专家
	function get_init_expert($blockid)
	{
		global $db;
		$strSql = 'SELECT COUNT(*) FROM `block_apply` WHERE blockid = ' . $blockid . ' AND esta = ' . "'11'";
		$intInitExpertNum = $db->GetOne($strSql);
		//我爱我家在指定的小区上显示小区专家
		$this->block_num = $intInitExpertNum != 0 ? 4 - $intInitExpertNum : 4;
		return;
	}

	//已申请满小区专家
	function get_block_full(){
		global $db,$dbback;

 		$sql = "select credit from broker where uid=".$this->uid;
        if($dbback->GetOne($sql) < $this->get_wfen()){
             return array();
        }
		$sql = "select blockid,count(id) as num from sell where uid=".$this->uid." and agentcode='B000' and esta='1' and isstar='3' and blockid in (select id from block where app in (1,2) and esta =2 ) group by blockid order by num desc";

		$rs = $dbback->GetAssoc($sql);
		$arr_block = array();
		if ($rs) {
			foreach($rs as $key=>$value){
				$sql = "select blockname from block where id=".$key;
				$this->get_init_expert($key);
				$blocknum = $this->get_people($key);
				if($value >= $this->room && $blocknum>=$this->block_num) {
					$arr_block[$key]['blockname'] = $dbback->GetOne($sql);
					$sql = "select endtime from block_apply where blockid='".$key."' and endtime>'".time()."' order by endtime asc ";
					$arr_block[$key]['endtime'] = $db->GetOne($sql);
					$arr_block[$key]['num'] = $blocknum;
				}
				//echo $value.','.$this->get_people($key)."<br />";
			}
		}

		return $arr_block;
	}

	//检查小区是否可编辑
	function get_app($blockid){
		global $db,$dbback;
		if(empty($blockid)) return ;
		$sql = "select app from block where id=$blockid";
		return $dbback->GetOne($sql);
	}

	//补充资料(replace)
    function insert_block_apply_data($blockid,$reason,$b_introduction,$b_bus,$b_metro,$b_other,$apptime,$uptime=''){
		global $db;

		$bi_seri = '';
		$img_arr = array();

		if( isset( $_REQUEST['p_filename'] ) ){

			foreach($_REQUEST['p_filename'] as $k => $v){	
				$token		= explode("=",$_REQUEST['p_filename'][$k]);
				$filename	= substr( $_REQUEST['p_filename'][$k], 0, strpos($_REQUEST['p_filename'][$k], '?'));				
				$img_arr[filename][]=$filename;		
				$img_arr[title][]=$_REQUEST['p_title'][$k];	
				$img_arr[type][]=$_REQUEST['p_sort'][$k];
				$idkind   = substr( $token[0],-3);
				if($idkind=='pid') $db->Execute("delete from house_upload_provisional where id=$token[1]");
			}

			$bi_seri = serialize($img_arr);
		}

		$sql	= " REPLACE INTO block_apply_data (uid,reason,b_introduction,b_bus,b_metro,b_other,bi_seri,blockid,apptime,uptime,flg) VALUES (".$this->uid.",'".qstr($reason)."','".qstr($b_introduction)."','".qstr($b_bus)."','".qstr($b_metro)."','".qstr($b_other)."','".$bi_seri."',".$blockid.",'".$apptime."','".$uptime."',0)";	
		$db->Execute($sql);	
	}



	//申请
	function insert_block_apply($blockid,$blockname,$reason,$b_introduction,$b_bus,$b_metro,$b_other){
		global $db,$credit_arr,$config_BrokerCredit;
		$apptime = time();

		$sql = "SELECT id FROM block_apply WHERE uid = ".$this->uid." AND esta = 0";
		$info = $db->GetOne($sql);

		if(!$info){
			if($config_BrokerCredit == 1 && !empty($credit_arr))
			{
				//先行扣分判断
				include_once(CORE_LIB . "/class_apply.php");
				$credit = apply::user_fen($this->uid);
				if($credit_arr['minus_block']['credit'] <= $credit)
				{
					$sql = "insert into block_apply (agentcode,agentname,uid,username,truename,telno,headimg,blockid,blockname,apptime,esta) values ('".$this->companycode."','".$this->agentname."',".$this->uid.",'".$this->username."','".$this->truename."','".$this->telno."','".$this->headimg."',".$blockid.",'".$blockname."','".$apptime."',0)";
					$rs = $db->Execute($sql);
					if($rs){
						//先行扣分
						apply::waster_fen($this->uid);
						$this->insert_block_apply_data($blockid,$reason,$b_introduction,$b_bus,$b_metro,$b_other,$apptime);
						return 1;
					}
					else
					{
						return 0;
					}
				}
				else
				{
					return 2;
				}
			}else{

				$sql = "insert into block_apply (agentcode,agentname,uid,username,truename,telno,headimg,blockid,blockname,apptime,esta) values ('".$this->companycode."','".$this->agentname."',".$this->uid.",'".$this->username."','".$this->truename."','".$this->telno."','".$this->headimg."',".$blockid.",'".$blockname."','".$apptime."',0)";

				$rs = $db->Execute($sql);
				if($rs){
					$this->insert_block_apply_data($blockid,$reason,$b_introduction,$b_bus,$b_metro,$b_other,$apptime);
					return 1;
				}else{	
					return 0;
				}
			}

		}else{	
			return 3;
		}
	}

	//清除用户申请记录
	function del_apply(){
		global $db;
		$sql = "delete from block_apply where uid=".$this->uid." and esta=0";
		//echo $sql;
		$db->Execute($sql);
		$sql = "delete from block_apply_data where uid=".$this->uid;
		//echo $sql;
		$db->Execute($sql);
		
		//先行扣分
		include_once(CORE_LIB . "/class_apply.php");
		apply::return_fen($this->uid);
	}

	//冻结条数
    function expert_freeze(){
		global $db,$dbback;
		$month = mktime(0,0,0,date("m"),1, date("Y"));
		$nextmonth = mktime(0,0,0,date("m")+1,1, date("Y"));
		$total = $dbback->GetOne("select count(*) from house_freeze where uid=".$this->uid." and freezetime>$month and freezetime<$nextmonth and esta != 1");
		return $total;
	}
	

	//公共接口
	static function get_info($uid){
		global $db,$dbback;
		$sql = "select * from block_apply where uid=".$uid." and esta=1 and endtime>".time();
		$info = $dbback->GetRow($sql);
		if(!empty($info)){
			return $info;
		}
		return false;
	}
}
?>

