<?php
class apply{
	protected static $indate	= 7;
	protected static $wfen		= 500;
	protected static $fnum		= 3;


	function apply(){
		//初始化数据
		$this->indate = $sys['indate'];
		$this->wfen = $credit_arr['minus_block'];
		$this->fnum = $sys['freeze_num'];
	}

	//审核
	static function expert_thaw($uid,$apptime,$endtime='',$blockname=''){
		GLOBAL $db,$config_BrokerCredit;
		if(self::expert_freeze($uid)>self::$fnum){
			self::send_systemmsg($uid,"小区专家审核","您有多条房源被冻结，无法通过审核");
			echo "<script>alert('此用户有多条房源被冻结');</script>";
			return;
		}

		//判断要不要走积分验证
		if(false && $config_BrokerCredit == 1 ){
			if(self::user_fen($uid)<self::$wfen){
				self::send_systemmsg($uid,"小区专家审核","您目前的积分不够，无法通过审核");
				echo "<script>alert('此用户目前的积分不够');</script>";
				return;
			}
		}

		$addtime = self::get_addtime($uid,$apptime);
		$endtime = $endtime==''?self::get_endtime():$endtime;
		$sql="update block_apply set esta=1,addtime=".$addtime.",endtime=".$endtime." where uid=".intval($uid)." and apptime='$apptime' and esta=0";
		$db->Execute($sql);
		//if($db->Execute($sql)) self::waster_fen($uid);

		$title = $blockname."小区专家申请成功";
		$content = "您好，您已通过本次申请，成功上任".$blockname."小区专家，到期时间为".date('d',$endtime)."日".date('H',$endtime)."点".date('i',$endtime)."分";
		self::send_systemmsg($uid,$title,$content);
	}

	//驳回
	static function expert_down($uid,$apptime,$esta='-1'){
		GLOBAL $db;
		$sql="update block_apply set esta='-1' where uid=".intval($uid)." and apptime='$apptime' and esta=0";
		$db->Execute($sql);

		//驳回返还积分
		self::return_fen($uid);

		$sql="update block_apply_data set uptime=".time().",flg=1 where uid=".intval($uid)." and apptime='$apptime'";
		$db->Execute($sql);


	}

	//获得审核时间
	static function get_addtime($uid,$apptime){
		GLOBAL $db;
		$sql="select addtime from block_apply where uid=".intval($uid)." and apptime='$apptime' and esta=1";
		$addtime = $db->GetOne($sql);
		return $addtime?$addtime:time();
	}

	//获得到期时间
	static function get_endtime(){
		return time()+3600*24*self::$indate;
	}

	//修改到期时间
	static function update_endtime($uid,$apptime,$endtime){
		GLOBAL $db;
		$sql="update block_apply set endtime=".$endtime." where uid=".intval($uid)." and apptime='$apptime'";
		$db->Execute($sql);
	}

	//解职
	static function expert_reject($uid,$esta=2){
		GLOBAL $db;
		$sql="update block_apply set esta=".$esta." where uid=".$uid." and esta=1";
		$db->Execute($sql);
		$sql="delete from block_apply_data where uid=".$uid;
		$db->Execute($sql);

		$blockname=$db->GetOne("select blockname from block_apply where esta=1 and uid=$uid");
		$title = "小区专家解职提醒";
		$content = "您好，您担任的".$blockname."小区专家被解职，如有疑问请致电400-8859-365。";
		self::send_systemmsg($uid,$title,$content);
	}

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

	//其他解职
	static function freeze_reject($uid,$esta){
		GLOBAL $db;
		$sql = "select id from block_apply where uid=$uid and esta=1";
		if($db->GetOne($sql)){
			self::expert_reject($uid,$esta);
		}
	}

	//查询积分
	static function user_fen($uid){

		include_once(CORE_LIB."/class_brokercredit.php");
		$I = new BrokerCredit($uid);
		$credit = $I->getBrokerCredit();
		return $credit;
	}

	//消费积分
	static function waster_fen($uid){

		include_once(CORE_LIB."/class_brokercredit.php");
		$I = new BrokerCredit($uid);
		$I->dealCredit('minus_block');

	}
	
	//返还积分
	static function return_fen($uid) {
		include_once(CORE_LIB."/class_brokercredit.php");
		$I = new BrokerCredit($uid);
		$I->dealCredit('plus_block');
	}

	//发送消息
	static function send_systemmsg($uid,$subject,$msg){
		global $db;

		$ss=array();
		$ss[kind] = "system";
		$ss[subject] ="【系统信息】".$subject;
		$ss[message] = $msg;
		$ss[operator] = "system";
		$ss[creattime] = time();
		$ss[issystem] = 1;

		$q=insert_sql("agent_announce",$ss);
		$db->Execute($q);
		$newid=$db->Insert_ID();//自增的用法

		$ss=array();
		$ss[announceid]=$newid;
		$ss[agentcode] = 'B000';
		$ss['uid']=$uid;
		$ss["new"] = 1;

		//写入数据库
		$q=insert_sql("agent_announceitem" ,$ss);
		$db->Execute($q);
	}

	static function get_people($blockid){
		global $db;
		$sql = "select count(id) from block_apply where blockid=".$blockid." and esta=1 and endtime>".time();
		return (int)$db->GetOne($sql);
	}
}
?>