<?
/**
* @说明	经纪人降权处理类 BrokerPunish
* @ xz 2012-9-24
**/


class BrokerPunish{
	var $limitTimes = 8 ;	//违规上线次数
	var $waringTimes = 7 ;	//违规警告上线
	var $minisCredit = 1000;
	var $uid ;	//经纪人编号
	var $db;	//数据库连接
	var $dbback; //备份数据库连接，用来查询数据
	var $tbl = "log_BrokerPunish";
	var $festa = array("normal"=>array("festa"=>0,"title"=>"正常"),"punish"=>array("festa"=>1,"title"=>"降权"),"nopunish"=>array("festa"=>2,"title"=>"已达标未降权"));


	//构造函数，初始化经纪人对象信息
	function BrokerPunish($uid){
		global $db,$dbback;
		$this->uid = intval($uid);
		$this->db = $db;
		$this->dbback = $dbback;
	}


	/**
	*@explain 获取经纪人基本信息 getBrokerInfo()
	*@param 无
	*@return array 经纪人信息
	*@ xz 2012-9-24
	**/
	function getBrokerInfo (){

		$sql = " SELECT uid,username,truename,telno,festa,agentcode FROM broker WHERE uid = ".$this->uid;
		$brokerinfo = $this->dbback->GetRow($sql);

		return $brokerinfo;
	}


	/**
	*@explain 获取经纪人门店简称
	*@param 无
	*@return array 经纪人门店简称
	*@ xz 2012-9-24
	**/
	function getBrokeragentshortname ($agentcode){

		$sql = " SELECT agentshortname FROM agency WHERE agentcode = '".$agentcode."' ";
		$agentshortname = $this->dbback->GetOne($sql);

		return $agentshortname;
	}


	/**
	* getBrokerCredit 获取经纪人积分
	*
	* @param bool $doview	//为TRUE时，返回最小为0
	* @return integer
	*/
	function getBrokerCredit($doview = false)
	{
		$brokercredit = $this->db->GetOne("SELECT credit FROM broker WHERE uid = '".$this->uid."'");
		$brokercredit = intval($brokercredit);

		$brokercredit = $doview === true && $brokercredit < 0 ? 0 : $brokercredit;

		return $brokercredit;
	}


	/**
	*@explain 获取经纪人惩罚状态 getBrokerStatus ()
	*@param		无
	*@return string 经纪人状态
	*@author xz 2012-9-24
	**/
	function getBrokerStatus (){

		$sql = " SELECT festa FROM broker WHERE uid = ".$this->uid;
		$status = $this->dbback->GetOne($sql);

		foreach($this->festa as $key=>$value){

			if( $status == $value['festa'] ){
				return $value['title'];
			}
		}
	}


	/**
	*@explain 设置经纪人惩罚状态 setBrokerStatus ()
	*@param	$festa 经纪人处罚状态
	*@return 无
	*@author xz 2012-9-24
	**/
	function setBrokerStatus ($festa){

		$arr = array();
		$arr['festa'] = intval($festa);
		$sql =  update_sql("broker",$arr," WHERE uid = ".$this->uid." ");
		if($this->db->Execute($sql)){
			return true;
		}

		return false;
	}


	/**
	*@explain 获取经纪人历史降权次数 getPunishTimes()
	*@param 无
	*@return int 降权次数
	*@ xz 2012-9-24
	**/
	function getPunishTimes (){

		$sql = " SELECT COUNT(*) FROM $this->tbl WHERE uid = ".$this->uid;
		$num = $this->dbback->GetOne($sql);

		return intval($num);
	}


	/**
	*@explain 获取当月内自上次降权后违规房源的数量
	*@param	无
	*@return 违规次数
	*@ xz 2012-9-24
	**/
	function getIrregularTimes (){

		$startime = mktime(0,0,0,date( "m" ,time() ),1,date( "Y", time() ) );    //本月一号时间戳
		$now_moth = date( "Y-m", time());
		$max_day  = date("t",strtotime($now_moth));
		$endtime  = $startime + ( $max_day * 86400 );

		//最近被降权的时间
		$lastTime = $this->getLastPunishTime();
		$startime = $startime >= $lastTime ? $startime : $lastTime;

		$sql = " SELECT COUNT(DISTINCT rowid) FROM house_freeze  WHERE uid = ".$this->uid." AND ".
				" freezetime >= $startime AND freezetime <= $endtime AND esta IN (1,2,4) ";
		$num = $this->dbback->GetOne($sql);//本月该经纪人被投诉的冻结房源数量，

		return intval($num);
	}


	/**
	*@explain 从经纪人信息表中获取当月内自上次降权后违规房源的数量 getIrregularTimes_broker()
	*@param	无
	*@return 违规次数
	*@ xz 2012-9-24
	**/
	function getIrregularTimes_broker (){

		$sql = " SELECT irregularTimes FROM broker WHERE uid = ".$this->uid;
		$num = $this->db->GetOne($sql);

		return intval($num);
	}


	/**
	*@explain 获取最新一次被降权的时间
	*@param	无
	*@return 违规次数
	*@ xz 2012-9-24
	**/
	function getLastPunishTime(){
		$sql = " SELECT starttime FROM $this->tbl WHERE uid = ".$this->uid." ORDER BY starttime DESC LIMIT 1";
		$time = $this->dbback->GetOne($sql);
		$time = !empty($time) ? $time : 0;

		return $time;
	}


	/**
	*@explain 获取经纪人降权日志
	*@param	$limit 日志条数，为0 表示不限制
	*@return
	*@ xz 2012-9-24
	**/
	function getPunishLogs($limit = 0){

		if($limit >0 ){
			$cond = " LIMIT $limit ";
		}
		$sql = " SELECT * FROM $this->tbl WHERE uid = ".$this->uid.$cond;
		$punishlog = $this->dbback->GetAll($sql);

		return $punishlog;
	}


	/**
	*@explain 添加经纪人降权日志
	*@param	$day 降权天数
	*@return
	*@ xz 2012-9-24
	**/
	function addPunishLogs($day,$operuser,$reason){

		$arr = array();
		$arr[uid] = $this->uid;
		$arr[starttime] = time();
		$arr[endtime] = time() + ($day*86400);
		$arr[operuser] = $operuser;
		$arr[esta] = 1;
		$arr[reason] = strip_tags($reason);
		$sql = insert_sql($this->tbl,$arr);
		$this->db->Execute($sql);

	}


	/**
	*@explain 更新经纪人降权日志
	*@param	operuser 操作人
	*@return
	*@ xz 2012-9-24
	**/
	function updatePunishLogs($operuser,$reason){

		$arr = array();
		$arr[operuser] = $operuser;
		$arr[reason] = $reason;
		$arr[manual_uptime] = time();
		$arr[esta] = 2;
		$sql = update_sql($this->tbl,$arr,"WHERE uid = ".$this->uid." AND esta = 1 ORDER BY id DESC LIMIT 1");
		if($this->db->Execute($sql)){
			return true;
		}

		return false;
	}


	/**
	*@explain 经纪人处罚处理
	*@param	$credit	处罚积分
	*@param	$operuser	操作人
	*@param	$reason	操作原因
	*@return 无
	*@auth xz 2012-9-24
	**/
	function doPunish($credit,$day,$operuser,$subject,$msg,$reason){

		//记录惩罚日志
		$this->addPunishLogs($day,$operuser,$reason);
		//更新经纪人的状态
		$this->setBrokerStatus ($this->festa['punish']['festa']);
		//重置经纪人违规次数
		$this->resetIrregularTimes();
		//更新经纪人降权次数
		$this->updatePunishTimes();
		//扣除积分
		$this->minusBrokerCredit($kind = 'minus_operate',$credit,$operuser,$reason);
		//发送站内信
		$subject	= "<font style='color:red;'>您的账号已被降权</font>";
		$msg		= "您当前已经累计违规房源超过".$this->limitTimes."条，账号已被降权".$day."天，扣除".$credit."积分，如有疑问，请联系您的客户经理。";
		$this->send_systemmsg($subject,$msg);

		return true;
	}


	/**
	*@explain 恢复经纪人降权状态
	*@param $operuser 操作人
	*@return Boolean
	*@ xz 2012-9-24
	**/
	function cancelPunish($operuser,$reason){

		$flg1 = false;
		$flg2 = false;
		//更新经纪人的状态
		$flg1 = $this->setBrokerStatus($this->festa['normal']['festa']);
		//更新日志信息
		$flg2 = $this->updatePunishLogs($operuser,$reason);

		return ($flg1&&$flg2);
	}


	/**
	*@explain  更改经纪人惩罚日志状态为已失效 setLogEsta()
	*@param 无
	*@return Boolean
	*@ xz 2012-9-24
	**/
	function setLogEsta(){

		$flg = false;
		//更改惩罚日志状态
		$sql = " UPDATE $this->tbl SET esta = 2  WHERE esta = 1 AND uid = ".$this->uid;
		if($this->db->Execute($sql)){
			$flg = true;
		}

		return $flg;
	}


	/**
	*@explain 重置经纪人违规次数  resetIrregularTimes()
	*@param 无
	*@return Boolean
	*@ xz 2012-9-24
	**/
	function resetIrregularTimes(){

		//更改惩罚日志状态
		$sql = " UPDATE broker SET irregularTimes = 0  WHERE valid_flag = 1 AND uid = ".$this->uid;

		if($this->db->Execute($sql)){
			$flg = true;
		}

		return $flg;
	}


	/**
	*@explain 更新经纪人违规次数  updateIrregularTimes()
	*@param 无
	*@return Boolean
	*@ xz 2012-9-24
	**/
	function updateIrregularTimes(){

		//更改惩罚日志状态
		$sql = " UPDATE broker SET irregularTimes = irregularTimes + 1   WHERE valid_flag = 1 AND uid = ".$this->uid;

		if($this->db->Execute($sql)){

			$irregularTimes = $this->getIrregularTimes_broker();
			echo $irregularTimes;

			if( $irregularTimes == $this->waringTimes){
				//发送站信
				$subject	= "房源违规已达<font style='color:red'>".$this->waringTimes."</font>次";
				$msg		= "您当前已经累计<font style='color:red'>".$this->waringTimes."</font>条违规房源，若累计超过".$this->limitTimes."条违规房源，账号将会被降权，请谨慎操作。";
				$this->send_systemmsg($subject,$msg);
			}elseif( $irregularTimes >= $this->limitTimes){
				//更改经纪人状态为已达标未降权
				$this->setBrokerStatus ($this->festa['nopunish']['festa']);
			}

			$flg = true;
		}

		return $flg;
	}


	/**
	*@explain 更新经纪人降权总次数  updatePunishTimes()
	*@param 无
	*@return Boolean
	*@ xz 2012-9-24
	**/
	function updatePunishTimes(){

		//更改惩罚日志状态
		$sql = " UPDATE broker SET punishTimes = punishTimes + 1   WHERE valid_flag = 1 AND uid = ".$this->uid;

		if($this->db->Execute($sql)){
			$flg = true;
		}

		return $flg;
	}



	/**
	*@explain 同步经纪人违规记录到违规记录表中  addViolationRecords()
	*@param string $tbl		 房源类型
	*@param string $rowid	 房源编号
	*@param string $kind	 违规记录类型 ,记录类型 1-投诉、2-冻结、3-违规记录
	*@param string $authname 操作人登陆名
	*@param string $details	 违规详情描述
	*@return Boolean
	*@ xz 2013-1-7
	**/
	function addViolationRecords($tbl,$rowid,$kind,$authname,$details){
		
		$reslult = false;

		$brokerinfo		= $this->getBrokerInfo();
		$agentshortname = $this->getBrokeragentshortname($brokerinfo['agentcode']);
		$data = array();
		$data['kind']		= intval($kind);
		$data['tbl']		= strip_tags($tbl);
		$data['rowid']		= intval($rowid);
		$data['uid']		= $brokerinfo['uid'];
		$data['username']	= $brokerinfo['username'];
		$data['truename']	= $brokerinfo['truename'];
		$data['agentshortname']	= $agentshortname;
		$data['hand_user']	= $authname;
		$data['details']	= $details;
		$data['dateline']	= time();

		$sql = insert_sql('broker_violation_records',$data);
		if($this->db->Execute($sql)){
			$reslult =  true;
		}else{
			$reslult = false;
		}
		return $reslult;
	}


	/**
	*@explain 发送站内信 send_systemmsg($subject,$msg)
	*@param	$subject 主题 $msg  信息内容
	*@return 无
	*@ xz 2012-9-24
	**/
	function send_systemmsg($subject,$msg){

		$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);
		$this->db->Execute($q);
		$newid = $this->db->_insertid();//自增的用法

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

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

	}


	/**
	*@explain 积分操作 minusBrokerCredit()
	*@param	$kind 积分操作类型
	*@param	$credit 积分
	*@param	$operuser 操作人
	*@param	$reason 操作原因
	*@return
	*@ xz 2012-9-24
	**/
	function minusBrokerCredit($kind = 'minus_operate',$credit,$operuser,$reason){
		GLOBAL $base_dir;
		//系统扣除积分
		include_once($base_dir."housecp/broker/BrokerCredit.class.php");
		$I = new BrokerCredit($this->uid);
		$I->dealCredit($kind, array('op_credit'=>$credit, 'operator'=>$operuser, 'reason'=>$reason));
	}

}
?>