<?php
load_m('new_base_model');
class vr_model extends new_base_model
{
	public function __construct()
	{

        parent::__construct();

    }


    /*
     * vr 申请校验
     */
	public function check_vr_new($param = array()){
        $this->load->model('zsb_api_model');
        $res = $this->zsb_api_model->getRequrstData('vr_check', $param);
        return $res;
    }

    /*
     * VR 申请提交
     */
    public function vr_applay($param = array()){
        $this->load->model('zsb_api_model');
        $res = $this->zsb_api_model->getRequrstData('vr_add', $param);
        return $res;
    }

    /*
     * VR申请取消
     */
    public function cancle_vr($param = array()){
        $this->load->model('zsb_api_model');
        $res = $this->zsb_api_model->getRequrstData('vr_cancel', $param);
        return $res;
    }







	/*
	 * code 1是自由绑定，无须付费，2：扣除房豆，并返还房豆 3，扣除房豆
	 *
	 */
	public function check_vr($param = array(),$return_array = false){

		$this->checkParam($param,array('rowid','buildingnum','unitnum','roomnum'));
		$house_id = $param['rowid'];
		$buildingnum = $param['buildingnum'];
		$unitnum = $param['unitnum'];
		$roomnum = $param['roomnum'];
		$this->load->model('broker_model');
		$house = $this->checkHouse($house_id);
		if(!$house){
			return $this->newException('房源异常',0,array(),$return_array);
		}

		if($this->package_id == 199){
			return $this->newException('免费账号不能购买增值服务，请先购买套餐',0,array(),$return_array);
		}

		$infotype = $house['infotype'];
		if(!in_array($infotype,array(1,2,7))){
			return $this->newException('VR拍摄只支持住宅、别墅、公寓类型',0,array(),$return_array);
		}

		$district = $house['district'];
		if(in_array($district,array('高淳区','溧水区'))){
			return $this->newException('该区属房源暂不支持vr拍摄',0,array(),$return_array);
		}
		if(!empty($param['order_date'])){
			$this->check_vr_count($param['order_date']);
		}
		$blockid = $house['blockid'];

		//独家代理
		$param['blockid'] = $blockid;
		$res = $this->vr_applay_sole($param);
		if(!$res['code']){
			return $this->newException($res['msg'],0,array(),$return_array);
		}

		$this->check_vr_status($house_id);

		$bean = $this->getVrConf('vr_applay_bean');

		//对特定的绑定不允许别人绑定
		if($blockid=='281305' && strtoupper($buildingnum)=='A' && $unitnum=='0' && $room_num='1002'){

			if($this->uid != '859349'){
				return $this->newException('该套房源无VR效果绑定服务，快去申请其他房源吧!',0,array(),$return_array);
			}

		}
		if($blockid=='480' && strtoupper($buildingnum)=='A' && $unitnum=='0' && $room_num='606'){

			if($this->uid != '910896'){
				return $this->newException('该套房源无VR效果绑定服务，快去申请其他房源吧!',0,array(),$return_array);
			}

		}

		//自己，无需花钱
		$vr_applay_record = $this->getVrRecord($this->uid,$blockid,$buildingnum,$unitnum,$roomnum);
		if($vr_applay_record){
			return $this->newException('该房源已有绑定记录，是否直接绑定？',1,array(),$return_array);
		}
		$vr_applay = $this->getVrModel($blockid,$buildingnum,$unitnum,$roomnum);
		//花钱支付
		if($vr_applay){
			if($vr_applay['user_id'] == $this->uid){
				return $this->newException('该房源已有绑定记录，是否直接绑定？',1,array(),$return_array);
			}
			$bean = $vr_applay['pay_bean'];
			return $this->newException('该房源已有对应房源VR，是否支付'.$bean.'房豆进行绑定？',2,$vr_applay,$return_array);
		}

		$waring = "<span style='color: red'>您好，请确认房源信息，如因您的失误导致服务未达成！房豆将不返还哦！</span><br/>";
		return $this->newException($waring.'预约申请将扣除'.$bean.'房豆。拍摄成功后他人申请绑定该VR时，您将可获得对应豆房奖励，是否确认支付?',3,array('pay_bean'=>$bean),$return_array);
	}

	/*
	 * vr 独家代理限制
	 * 规则：个人房源限制，则不允许所有人申请，经纪人限制:只能自己绑定，公司代理：同公司下可绑定
	 */
	public function vr_applay_sole($param = array()){
		$blockid = $param['blockid'];
		$buildingnum = $param['buildingnum'];
		$unitnum = $param['unitnum'];
		$roomnum = $param['roomnum'];
		//个人独家代理，不允许经纪人绑定
		$count1 = $this->dbback->from('vr_applay_sole')->where(array('block_id'=>$blockid,'building'=>$buildingnum,'unit'=>$unitnum,'room_num'=>$roomnum,'type'=>2,'status'=>1))->count_all_results();
		if($count1){
			return array('code'=>0,'msg'=>'此套房源已提供独家代理，快去申请其他房源吧!');
		}
		//经纪人独家代理
		$date = date('Y-m-d');
		$result = $this->dbback->from('vr_applay_sole')->where(array('block_id'=>$blockid,'building'=>$buildingnum,'unit'=>$unitnum,'room_num'=>$roomnum,'type'=>1,'status >'=>0,'end_time >='=>$date))->get()->result_array();
		$uids = array();
		$agentcodes = array();
		foreach ($result as $key=>$value){
			$uid = $value['user_id'];
			$agentcode = $value['agentcode'];
			$status = $value['status'];
			if($status == 1){
				array_push($uids,$uid);
			}else{
				array_push($agentcodes,$agentcode);
			}
		}

		//公司代理
		$getAllAgentCode = $this->getAllAgentCode();
		$res = array_intersect($agentcodes,$getAllAgentCode);
		if(!in_array($this->uid,$uids) && $uids || !$res && $agentcodes){
			return array('code'=>0,'msg'=>'此套房源已提供独家代理，快去申请其他房源吧!');
		}
		return array('code'=>1,'msg'=>'可以申请');
	}


	/*
	 * 绑定VR
	 */
	public function vr_bind($param = array()){

		$this->checkParam($param,array('rowid','buildingnum','unitnum','roomnum'));
		$param['buildingnum'] = $this->str_iconv($param['buildingnum']);
		$param['unitnum'] = $this->str_iconv($param['unitnum']);
		$param['roomnum'] = $this->str_iconv($param['roomnum']);

		$re = $this->check_vr($param,true);
		$code = $re['code'];
		$redata = $re['data'];
		$param['pay_bean'] = 0;
		//此时要检测是否是
		if(in_array($code,array(2,3))){
			$this->load->model('bean_model');
			$bean = $this->bean_model->getBeanNum();
			$pay_bean = $this->getVrConf('vr_applay_bean');
			if($code == 2){
				$pay_bean = $redata['pay_bean'];
			}
			if($bean < $pay_bean){
				$this->newException('剩余房豆不足，是否立即充值房豆?',0,array('url'=>'http://nj.zsb.house365.com/payment/recharge/'));
			}
			$param['pay_bean'] = $pay_bean;
		}
		$rs = $this->vr_bind_appay($param,$code);
		$this->newException($rs['msg'],1);
	}


	/*
	* code 1是自由绑定，无须付费，2：扣除房豆，并返还房豆 3，扣除房豆
	*/
	private function vr_bind_appay($param = array(),$code){
		$this->checkParam($param,array('rowid','buildingnum','unitnum','roomnum'));
		$house_id = $param['rowid'];
		$buildingnum = $param['buildingnum'];
		$unitnum = $param['unitnum'];
		$roomnum = $param['roomnum'];
		$house = $this->checkHouse($house_id);
		$vr_applay_data = $this->vr_applay_data($param);
		$block_id = $house['blockid'];
		//无须扣除房豆，直接绑定
		if($code == 1){
			$vrmodel = $this->getVrRecord($this->uid,$block_id,$buildingnum,$unitnum,$roomnum);
			if(!$vrmodel){
				$vrmodel = $this->getVrModel($block_id,$buildingnum,$unitnum,$roomnum);
			}
			$snyc_house_id = $vrmodel['house_id'];//相似绑定的房源id
			$vr_model_id = $vrmodel['vr_model_id'];
			$vr_applay_data['shot_status'] = 4;
			$vr_applay_data['vr_model_id'] = $vr_model_id;
			$vr_applay_data['from_applay_first_id'] = $vrmodel['id'];
			$vr_applay_data['order_time'] = 5;
			$insert_id = $this->add_vr_applay($vr_applay_data,$snyc_house_id);
			if(!$insert_id){
				$this->newException('申请失败');
			}
			$this->snyc_vr_applay($vr_applay_data,$insert_id,$snyc_house_id);

			return array('code'=>1,'msg'=>'您的房源VR已绑定成功');
		}
		$transaction_id = $vr_applay_data['transaction_id'];

		$this->load->model('bean_model');
		$pay_bean = $this->getVrConf('vr_applay_bean');
		$pay_bean = empty($param['pay_bean'])?$pay_bean:$param['pay_bean'];
		$description = '申请VR扣除房豆'.$pay_bean.'(房源编号:'.$house_id.')';
		//扣除房豆，处于待审核状态
		if($code == 3){

			$vr_applay_data['pay_bean'] = $pay_bean;
			$vr_applay_data['pay_state'] = 1;
			$insert_id = $this->add_vr_applay($vr_applay_data);
			if(!$insert_id){
				$this->newException('申请失败，请重新尝试');
			}
			$re = $this->bean_model->payBean($this->uid,'vrApplay',$transaction_id,$pay_bean,$description);
			if(!$re['code']){

				$msg = empty($re['msg'])?'房豆扣除失败':$re['msg'];
				$vrupdate['pay_state'] = 0;
				$vrupdate['shot_status'] = 5;
				$vrupdate['remark'] = $msg;
				$this->db->where(array('id'=>$insert_id))->update('vr_applay',$vrupdate);
				$from = empty($re['from'])?'0':$re['from'];
				if($from){
					$this->newException($re['msg'],2);
				}else{
					$this->newException($re['msg']);
				}
			}
			return array('code'=>2,'msg'=>'恭喜您申请成功，请保持手机通畅，客服会尽快和你确定具体时间和地点，近期由于申请量巨大，我们最迟会在预约时间前1天和您联系。');

		}



		//扣除房豆，并奖励第一人
		if($code == 2){
			$vrmodel = $this->getVrModel($block_id,$buildingnum,$unitnum,$roomnum);
			$snyc_house_id = $vrmodel['house_id'];//相似绑定的房源id
			$vr_model_id = $vrmodel['vr_model_id'];
			$vr_applay_data['shot_status'] = 4;
			$vr_applay_data['order_time'] = 5;
			$vr_applay_data['vr_model_id'] = $vr_model_id;
			$vr_applay_data['pay_bean'] = $pay_bean;
			$first_broker_uid = $vrmodel['user_id'];
			$from_applay_first_id = $vrmodel['id'];
			$vr_applay_data['from_applay_first_id'] = $from_applay_first_id;
			$applay_type = $vrmodel['applay_type'];//1 经纪人，2个人

			$is_return = empty($vrmodel['is_return'])?0:$vrmodel['is_return'];

			//已经获取奖励的人数
			$return_first_bean = 0;
			if($is_return && $applay_type==1){
				$count = $this->db->from('vr_applay')->where(array('from_applay_first_id'=>$from_applay_first_id,'return_first_bean >'=>0,'pay_state'=>1))->count_all_results();
				$vr_applay_data['pay_state'] = 1;

				$vr_applay_data['from_applay_first_id'] = $from_applay_first_id;
				if($count <= 0){
					$return_first_bean = 10;
				}else if($count >=1 && $count <=2){
					$return_first_bean = 30;
				}else if($count == 3){
					$return_first_bean = 60;
				}else{
					$return_first_bean = 10;
				}
			}

			$vr_applay_data['return_first_bean'] = $return_first_bean;

			$insert_id = $this->add_vr_applay($vr_applay_data,$snyc_house_id);
			if(!$insert_id){
				$this->newException('申请失败了，请重新尝试');
			}
			$re = $this->bean_model->payBean($this->uid,'vrApplay',$transaction_id,$pay_bean,$description);
			if(!$re['code']){
				$msg = empty($re['msg'])?'房豆扣除失败':$re['msg'];
				$vrupdate['pay_state'] = 0;
				$vrupdate['shot_status'] = 5;
				$vrupdate['remark'] = $msg;
				$this->db->where(array('id'=>$insert_id))->update('vr_applay',$vrupdate);
				$from = empty($re['from'])?'0':$re['from'];
				if($from){
					$this->newException($re['msg'],2);
				}else{
					$this->newException($re['msg']);
				}
			}
			$this->snyc_vr_applay($vr_applay_data,$insert_id,$snyc_house_id);

			if($applay_type == 1 && $is_return){
				//返回房豆
				$description2 = 'VR奖励返还房豆'.$return_first_bean.'个';
				$transaction_id_new = $this->orderNo($first_broker_uid,'VR');
				$this->bean_model->increaseBean($first_broker_uid,'vrApplayReward',$transaction_id_new,$return_first_bean,$description2,'赠送');
			}
			return array('code'=>1,'msg'=>'您的房源VR已绑定成功');
		}

		$this->newException('申请已提交，出现异常');

	}




	public function add_vr_applay($vr_applay_data,$snyc_house_id = ''){
		$house_id = $vr_applay_data['house_id'];
		$vr_model_id = $vr_applay_data['vr_model_id'];
		$ok = $this->db->insert('vr_applay', $vr_applay_data);
		$insert_id = 0;
		if($ok){
			$insert_id = $this->db->insert_id();
		}
		if(!$insert_id){
			return false;
		}
		return $insert_id;
	}


	/*
	 * 同步 VR封面，室内图，户型图，模型id
	 *
	 * $issnyc 是否需要同步
	 */
	private function snyc_vr_applay($vr_applay_data,$insert_id,$snyc_house_id = ''){
		$house_id = $vr_applay_data['house_id'];
		$vr_model_id = $vr_applay_data['vr_model_id'];
		if(!$insert_id){
			return false;
		}
		if(!$snyc_house_id){
			return $insert_id;
		}

		//同步开始
		$snyc_house_vr_image = $this->dbback->from('vr_image')->where(array('tbl'=>1,'houseid'=>$snyc_house_id))->get()->row_array();

		$vr_pic = '';//VR封面
		$vr_house_layout = ''; //VR户型图
		$vr_room_images = ''; //VR室内图

		if($snyc_house_vr_image){

			$vr_pic = $snyc_house_vr_image['filename'];
			$vr_house_layout = $snyc_house_vr_image['house_type_images'];
			$vr_room_images = $snyc_house_vr_image['rom_images'];

			$vri = $this->dbback->from('vr_image')->where(array('tbl'=>1,'houseid'=>$house_id))->get()->row_array();
			$vr_imgs_data['tbl'] = 1;
			$vr_imgs_data['filename'] = $vr_pic;
			$vr_imgs_data['house_type_images'] = $vr_house_layout;
			$vr_imgs_data['rom_images'] = $vr_room_images;
			$vr_imgs_data['houseid'] = $house_id;

			if($vri){
				$this->db->where(array('tbl'=>1,'houseid'=>$house_id))->update('vr_image', $vr_imgs_data);
			}else{
				$this->db->insert('vr_image', $vr_imgs_data);
			}
		}

		//同步户型图，室内图，VR模型等
		$this->load->model('sell_model');
		//如果有户型图，户型图同步
		if($vr_house_layout){
			$house_typeims[0] = $vr_house_layout;
			$this->sell_model->romupdate($house_id,$house_typeims,0);
		}
		$rom_images = array();
		if(!empty($vr_room_images)){
			$rom_images = json_decode($vr_room_images);
		}
		//室内图
		if($rom_images){
			$this->sell_model->romupdate($house_id,$rom_images,2);
		}

		$ups['vr_model_id'] = $vr_model_id;
		$ups['isvr'] = 1;
		if($vr_pic){
			$ups['pic1'] = $vr_pic;
		}
		$sellok = $this->db->where(array('id'=>$house_id))->update('sell',$ups);
		if(!$sellok){
			return false;
		}
		return $insert_id;
	}


	/*
	 * 组成vr申请data
	 */
	private function vr_applay_data($param = array()){

		$house_id = $param['rowid'];
		$buildingnum = $param['buildingnum'];
		$unitnum = $param['unitnum'];
		$roomnum = $param['roomnum'];
		$houseinfo = $this->checkHouse($house_id);

		$uid = $houseinfo['uid'];
		$transaction_id = $this->orderNo($uid,'VR');
		$vr_applay_data['broker_id'] = $houseinfo['uid'];
		$vr_applay_data['transaction_id'] = $transaction_id;
		$vr_applay_data['block_id'] = $houseinfo['blockid'];
		$vr_applay_data['broker_telno'] = $houseinfo['telno'];
		$vr_applay_data['truename'] = $this->userSession['truename'];
		$vr_applay_data['username'] = $this->userSession['username'];
		$agentcode = $this->userSession['agentcode'];
		$agency = $this->dbback->where(array('agentcode'=>$agentcode))->get('agency')->row_array();
		$vr_applay_data['agentshortname'] = $agency['agentshortname'];
		$vr_applay_data['house_id'] = $house_id;
		$vr_applay_data['house_type'] = 1;
		$vr_applay_data['village_name'] = $houseinfo['blockshowname'];
		$vr_applay_data['area'] = $houseinfo['district'];
		$vr_applay_data['plate'] = $houseinfo['streetname'];
		$vr_applay_data['building'] = $buildingnum;
		$vr_applay_data['unit'] = $unitnum;
		$vr_applay_data['room_num'] = $roomnum;

		$real_status = $this->userSession['real_status'];
		if($real_status > 0){
			$vr_applay_data['applay_type'] = 1;
		}else{
			$vr_applay_data['applay_type'] = 2;
		}
		$vr_applay_data['save_time'] = date('Y-m-d H:i:s');

		if(!empty($param['agent_telno'])){
			$vr_applay_data['broker_telno'] = $param['agent_telno'];
		}

		if(!empty($param['order_date'])){
			$vr_applay_data['order_date'] = $param['order_date'];
		}
		$vr_applay_data['order_time'] = 0;
		if(!empty($param['order_time'])){
			$vr_applay_data['order_time'] = $param['order_time'];
		}
		if(!empty($param['blockshownameall'])){
			$blockshownameall = $param['blockshownameall'];
			$blockshownameall = iconv('utf-8','gbk',$blockshownameall);
			$vr_applay_data['remark'] = $blockshownameall;
		}
		return $vr_applay_data;
	}


	/*
	 * 取消VR
	 */
	public function cancleVr($rowid){

		$applay = $this->dbback->from('vr_applay')->where(array('house_id'=>$rowid,'broker_id'=>$this->uid))->order_by('save_time desc')->get()->row_array();
		if($applay){
			$vrstatus = $applay['shot_status'];
			$id = $applay['id'];
			$pay_bean = $applay['pay_bean'];
			$pay_state = $applay['pay_state'];
			$transaction_id = $applay['transaction_id'];
			//取消后退还房豆数
			$return_bean_discount = $this->getVrConf('vr_applay_return_discount_1');
			$return_bean = floor($pay_bean*$return_bean_discount);
			if($vrstatus == 1 || $vrstatus == 7){
				$data['shot_status'] = 5;
				$ok = $this->db->where(array('id'=>$id))->update('vr_applay',$data);
				$description2 = '取消VR,返回'.$return_bean.'房豆';
				if($ok){
					//返还房豆
					if($return_bean && $pay_state && $transaction_id){
						$this->load->model('bean_model');

						$re = $this->bean_model->increaseBean($this->uid,'vrApplayReturn',$transaction_id,$return_bean,$description2,'退款');
						if($re){
							$this->db->where(array('id'=>$id))->update('vr_applay',array('return_bean'=>$return_bean));
						}
					}
					return array('code'=>1,'msg'=>'VR申请已取消');
				}
				return array('code'=>0,'msg'=>'操作失败');

			}elseif($vrstatus == 2){
				return array('code'=>0,'msg'=>'您好，该房源已处于已预约，取消时申请的房豆不会返还给您哦！确认取消拍摄VR，请联系：18662715825');
			}else{
				return array('code'=>0,'msg'=>'状态已发生变化，刷新后操作');
			}
		}
		return array('code'=>0,'msg'=>'无效数据');
	}


	/*
	 * VR操作日志
	 */
	public function vrOptionLog($rowid,$shot_status,$msg){

		$options['house_id'] = $rowid;
		$options['shot_status'] = $shot_status;
		$options['params'] = $msg;
		$options['operator'] = $this->username;
		$options['create_time'] = time();
		$this->db->insert('vr_applay_option_log', $options);
	}


	/*
	 * 根据楼栋门牌获取模型记录
	 */
	public function getVrModel($blockid,$buildingnum,$unitnum,$roomnum){

		$getAllAgentCode = $this->getAllAgentCode();
		$vr_applay = $this->dbback->from('vr_applay_first')->where(array('block_id'=>$blockid,'building'=>$buildingnum,'unit'=>$unitnum,'room_num'=>$roomnum))->where_in('agentcode',$getAllAgentCode)->order_by('id asc')->get()->row_array();
		if($vr_applay){
			$vr_applay['is_sameCompany'] = 1;
			$vr_applay['is_return'] = $vr_applay['is_reward'];
			$vr_applay['pay_bean'] = $this->getVrConf('vr_applay_bean_company');
		}
		if(!$vr_applay){
			$vr_applay = $this->dbback->from('vr_applay_first')->where(array('block_id'=>$blockid,'building'=>$buildingnum,'unit'=>$unitnum,'room_num'=>$roomnum))->order_by('id asc')->get()->row_array();
			if($vr_applay){
				$vr_applay['is_sameCompany'] = 0;
				$vr_applay['is_return'] = $vr_applay['is_reward'];
				$vr_applay['pay_bean'] = $this->getVrConf('vr_applay_bean');
			}
		}

		return $vr_applay;
	}

	/*
	 * 获取VR申请记录
	 */
	public function getVrRecord($uid,$blockid,$buildingnum,$unitnum,$roomnum){
		$vr_applay_record = $this->dbback->from('vr_applay')->where(array('broker_id'=>$uid,'block_id'=>$blockid,'building'=>$buildingnum,'unit'=>$unitnum,'room_num'=>$roomnum,'vr_model_id !='=>''))->order_by('id desc')->get()->row_array();
		return $vr_applay_record;
	}


	/*
	 * vr_applay_bean 花费房豆数
	 * vr_applay_return_usernum 返还至第几人
	 * vr_applay_return_bean 返还房豆数
	 */
	public function getVrConf($key){
		$val = $this->config->item($key);
		return $val;
	}

	/*
	 * 查询房源
	 */
	public function checkHouse($house_id){
		$tab = 'sell';
		$houseInfo = $this->dbback->from($tab)->where(array('id'=>$house_id,'uid'=>$this->uid))->get()->row_array();
		if(!$houseInfo){
			return false;
		}
		$streetid = $houseInfo['streetid'];
		$street = $this->dbback->from('street')->where(array('id'=>$streetid))->get()->row_array();
		$houseInfo['streetname'] = '';
		if($street){
			$houseInfo['streetname'] = $street['streetname'];
		}
		return $houseInfo;
	}

	/*
	 * 申请中的不可再次申请
	 */
	private function check_vr_status($house_id){
		$applay = $this->dbback->from('vr_applay')->where(array('house_id'=>$house_id))->where_not_in('shot_status',array(5,6,9))->order_by('save_time desc')->get()->row_array();
		if($applay){
			$this->newException('该房源正在处理中，不可再次申请');
		}

		return true;
	}

	/*
	 * 检测不能超过总数
	 */
	private function check_vr_count($order_date){
		$count_manage = $this->dbback->from('vr_count_manage')->where(array('order_date'=>$order_date))->get()->row_array();
		if($count_manage){
			$total_count = $count_manage['count'];
		}else{
			$total_count = 40;
		}
		//检测是否超过40套
		$applay_count = $this->db->from('vr_applay')->where(array('order_date'=>$order_date))->where_not_in('shot_status',array(5,6,9))->count_all_results();
		if($applay_count >=$total_count){
			$this->newException('拍摄日期'.$order_date.'申请已超过'.$total_count.'套,不可申请了');
		}
		return true;
	}


	/*
	 * 非放心看经纪人，永久只能申请一次
	 * 放心看经纪人 ，拍摄中的最多存在3个
	 */
	private function check_vr_broker(){
		$broker = $this->dbback->from('broker')->where(array('uid'=>$this->uid))->get()->row_array();
		if(!$broker){
			$this->newException('经纪人不存在');
		}
		//非放心看经纪人，永久只能申请一套
		if(!$broker['real_status']){
			$applay_count = $this->dbback->from('vr_applay')->where(array('broker_id'=>$this->uid))->where_not_in('shot_status',array(5,6,9))->count_all_results();
			if($applay_count){
				$this->newException('您非放心看经纪人，最多只能申请一套');
			}
		}else{
			$applay_count_total = $this->dbback->from('vr_applay')->where(array('broker_id'=>$this->uid))->where_in('shot_status',array(1,2))->count_all_results();
			if($applay_count_total >= 3){
				$this->newException('审核中，已预约不能超过3套房源');
			}
		}
	}

	/*
	 * 获取所有的公司的门店编号
	 */
	public function getAllAgentCode(){
		$broker = $this->dbback->from('broker')->select('uid,agentcode')->where(array('uid'=>$this->uid))->get()->row_array();
		$agentcode = $broker['agentcode'];
		if(!$agentcode){
			return array();
		}
		$agentcodes = array();
		array_push($agentcodes,$agentcode);
		$agency = $this->dbback->from('agency')->where(array('agentcode'=>$agentcode))->get()->row_array();
		$agenttype = $agency['agenttype'];
		if($agenttype == 1){//分公司
			$parentcode = $agency['parentcode'];
		}elseif($agenttype == 2){//总公司
			$parentcode = $agentcode;
		}
		if($parentcode){
			array_push($agentcodes,$parentcode);
			$agencys = $this->dbback->from('agency')->where(array('parentcode'=>$parentcode))->get()->result_array();
			foreach ($agencys as $k=>$v){
				array_push($agentcodes,$v['agentcode']);
			}
		}
		return $agentcodes;

	}

}