<?
class Credit 
{
	/*
	减分项	
	m_1  虚假低价	  -200分
	m_2  严重虚假低价 -500分
	m_3  房源已售	  -100分
	m_4  虚假房源	  -2000分
	m_5  房源基本信息不符  -50分
	m_6  经纪人点评信息不符 -100分

	*/

	/*
	加分项	
	p_1  端口开通	  +200分
	p_2  身份认证	  +3000分
	p_3  名片认证	  +2000分
	p_4_1  南京房产经纪人证书	  +20000分
	p_4_2  全国房产经纪人证书	  +20000分
	p_5  参与在线评估	  +1000分
	p_6  发布三星房源	  +100分
	p_7  更新三星房源	  +10分
	p_8  上传买卖成交合同	  +5000分
	p_9  上传租赁成交合同	  +3000分
	p_10  参与购房大课堂活动	  +1000分
	p_11  参与看房车活动	  +1000分
	p_12  参与365社区行活动	  +500分
	p_13  回答365知道最佳答案	  +50分
	p_14  每月自动增加	  +200分

	*/

	var $uid;
	var $row;
	var $conf;
	static  $minus=array("m_1"  =>array('title'=>'虚假低价', 'credit'=>-200),    
						 "m_2"  =>array('title'=>'严重虚假低价', 'credit'=>-500),
						 "m_3"  =>array('title'=>'房源已售', 'credit'=>-100),
						 "m_4"  =>array('title'=>'虚假房源', 'credit'=>-2000),
						 "m_5"  =>array('title'=>'房源基本信息不符', 'credit'=>-50),
						 "m_6"  =>array('title'=>'经纪人点评信息不符', 'credit'=>-100),
                         "m_7"  =>array('title'=>'申请小区专家成功', 'credit'=>-500),
                         "m_8"  =>array('title'=>'橱窗显示房源', 'credit'=>-2),
                         "m_9"  =>array('title'=>'恶意发帖', 'credit'=>-500),
                         "m_10"  =>array('title'=>'冻结房源', 'credit'=>-100)
			   );


	static $plus=array(
                        "p_1"  => array('title'=>'端口开通', 'credit'=>200),
						"p_2"  => array('title'=>'身份认证', 'credit'=>300),
						"p_3"  => array('title'=>'名片认证', 'credit'=>200),
						"p_4_1"  => array('title'=>'南京房产经纪人证书', 'credit'=>2000),
						"p_4_2"  => array('title'=>'全国房产经纪人证书', 'credit'=>2000),
						"p_5"  => array('title'=>'参与在线评估', 'credit'=>1000),
						//"p_6"  => array('title'=>'发布三星房源', 'credit'=>100),
						//"p_7"  => array('title'=>'更新三星房源', 'credit'=>10),
						"p_8_1"  => array('title'=>'上传买卖成交合同-满意', 'credit'=>500),
						"p_8_2"  => array('title'=>'上传买卖成交合同-中评', 'credit'=>300),
						"p_8_3"  => array('title'=>'上传买卖成交合同-不满意', 'credit'=>0),
						"p_9_1"  => array('title'=>'上传租赁成交合同-满意', 'credit'=>300),
						"p_9_2"  => array('title'=>'上传租赁成交合同-中评', 'credit'=>200),
						"p_9_3"  => array('title'=>'上传租赁成交合同-不满意','credit'=>0),
						"p_10"  => array('title'=>'参与购房大课堂活动', 'credit'=>1000),
						"p_11"  => array('title'=>'参与看房车活动', 'credit'=>1000),
						"p_12"  => array('title'=>'参与365社区行活动', 'credit'=>500),
						"p_13"  => array('title'=>'365知道最佳答案', 'credit'=>50),
						"p_14"  => array('title'=>'每月自动增加', 'credit'=>200),
						"p_15"  => array('title'=>'参加网络营销培训', 'credit'=>500),
                        "p_16"  => array('title'=>'登陆', 'credit'=>15),
                        "p_17"  => array('title'=>'绑定论坛id', 'credit'=>25),
                        "p_18"  => array('title'=>'店铺装修', 'credit'=>50),
                        "p_19"  => array('title'=>'续约', 'credit'=>25),
                        "p_20"  => array('title'=>'发布多图房源', 'credit'=>100),
                        "p_21"  => array('title'=>'临时加分项', 'credit'=>55000)//可变
			   );


	function Credit($uid)
	{
		global $db;

	    $this->uid=$uid;
		$this->row=$db->GetRow("select * from broker_credit where uid=$uid");
	    $this->conf=self::$plus+self::$minus;

		if(!$this->row){
		  $db->Execute("INSERT INTO broker_credit (`uid`) VALUES ('$uid'); ");
		}

	}


	function update_credit()    
	{
		global $db;
		$this->row=$db->GetRow("select * from broker_credit where uid=".$this->uid);
		$credit=$this->get_result();
		$db->Execute("update broker set credit=$credit where uid=".$this->uid);
		
		return $credit;

	}


	function get_result()    
	{
		$plus=$this->get_plus();
		$minus=$this->get_minus();

		$total=$plus+$minus;

		//return   $total;
		return   $total>0? $total:0;
	}

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


	function get_minus()    
	{
		foreach(self::$minus as $key=>$r)
		{
          $total+=$this->row[$key]*$r[credit];
		}

		return   $total;
	}



	function get_plus()    
	{
		foreach(self::$plus as $key=>$r)
		{
          $total+=$this->row[$key]*$r[credit];
		}

		return   $total;
	}

	function get_count($kind)    
	{
		if($kind=="p_8") return $this->row["p_8_1"]+$this->row["p_8_2"]+$this->row["p_8_3"];
		if($kind=="p_9") return $this->row["p_9_1"]+$this->row["p_9_2"]+$this->row["p_9_3"];
		return $this->row[$kind];
	}

	function get_itemcredit($kind)    
	{
		if($kind=="p_8") return $this->row["p_8_1"]*self::$plus["p_8_1"]["credit"]+$this->row["p_8_2"]*self::$plus["p_8_2"]["credit"]+$this->row["p_8_3"]*self::$plus["p_8_3"]["credit"];
		if($kind=="p_9") return $this->row["p_9_1"]*self::$plus["p_9_1"]["credit"]+$this->row["p_9_2"]*self::$plus["p_9_2"]["credit"]+$this->row["p_9_3"]*self::$plus["p_9_3"]["credit"];
		if($kind{0}=="p")  return $this->row[$kind]*self::$plus[$kind]["credit"];
		if($kind{0}=="m")  return $this->row[$kind]*self::$minus[$kind]["credit"];
	}

	function update($kind)    
	{  
		global $db;
		
		if($kind=="p_1") {
          
		  $q="update broker_credit set p_1=1 where uid='".$this->uid."' ";
		
		} elseif($kind=="p_2") {
				  
				  $q="update broker_credit set p_2=(select count(*) from broker where uid='".$this->uid."' and p_2_pic!='') where uid='".$this->uid."'";
		} elseif($kind=="p_3") {
				  
				  $q="update broker_credit set p_3=(select count(*) from broker where uid='".$this->uid."' and p_3_pic!='') where uid='".$this->uid."'";
		} elseif($kind=="p_4_1") {
				  
				  $q="update broker_credit set p_4_1=(select count(*) from broker where uid='".$this->uid."' and p_4_1_pic!='') where uid='".$this->uid."'";
		} elseif($kind=="p_4_2") {
				  
				  $q="update broker_credit set p_4_2=(select count(*) from broker where uid='".$this->uid."' and p_4_2_pic!='') where uid='".$this->uid."'";
		} elseif($kind=="p_6") {
				  
				  $q="update broker_credit set p_6=(select count(*) from broker_house where uid='".$this->uid."' and is3star=1) where uid='".$this->uid."'";

		} elseif($kind=="p_8") {
          
		  $q="update broker_credit 
		         set p_8_1=(select count(*) from broker_contract where uid='".$this->uid."' and valid_flag=1 and tbl='sell' and level='1'),
				     p_8_2=(select count(*) from broker_contract where uid='".$this->uid."' and valid_flag=1 and tbl='sell' and level='2'),
					 p_8_3=(select count(*) from broker_contract where uid='".$this->uid."' and valid_flag=1 and tbl='sell' and level='3') 
					 where uid='".$this->uid."'";

		} elseif($kind=="p_9") {
          
		  $q="update broker_credit 
		         set p_9_1=(select count(*) from broker_contract where uid='".$this->uid."' and valid_flag=1 and tbl='rent' and level='1'),
				     p_9_2=(select count(*) from broker_contract where uid='".$this->uid."' and valid_flag=1 and tbl='rent' and level='2'),
					 p_9_3=(select count(*) from broker_contract where uid='".$this->uid."' and valid_flag=1 and tbl='rent' and level='3') 
					 where uid='".$this->uid."'";

		} elseif(array_key_exists ($kind, self::$minus)) {
			
			$q="update broker_credit set `$kind`=(select count(*) from broker_minus where uid='".$this->uid."' and kind='$kind') where uid='".$this->uid."' ";

		} elseif(array_key_exists ($kind, self::$plus)) {
			
			$q="update broker_credit set `$kind`=(select count(*) from broker_plus where uid='".$this->uid."' and kind='$kind') where uid='".$this->uid."' ";

		} else {}
		
		//echo $q; exit;dsd();
        $rs = $db->Execute($q);
		return $rs->Affected_Rows();
	}

	function get_minuslist($kind)    
	{
		global $db;

		$q="select * from broker_minus where uid='".$this->uid."' and kind='$kind' order by id desc";
		$rs= $db->Execute($q);
		$i=1;
		$ss="<table align=center class=tableoutline width=700 border=0 cellpadding=2 cellspacing=1>
		<tr bgcolor=#eeeeee align=center><td >&nbsp;</td><td >房源表</td><td >房源编号</td><td >投诉人电话</td><td >操作</td></tr>";

		while ($row=$rs->FetchRow())
		{
			if($row[tbl]=="sell") $row[tbl]="二手房";
			if($row[tbl]=="rent") $row[tbl]="出租房";

			//$act= "<a href='minus.php?action=modify&id=$row[id]' target='_blank'>修改</a>&nbsp;";
			$act="<a href='minus.php?action=delete_minus&kind=$kind&id=$row[id]' onclick=\"delete_minus('$kind',$row[id]);return false;\">删除</a>";

			$ss.="<tr bgcolor=#eeeeee align=center><td >$i</td><td >$row[tbl]</td><td >$row[rowid]</td><td >$row[telno]</td><td >$act</td></tr>";
			$i++;
		} 

		$ss.="</table>";
		$rs->Close();
		return $ss;
	}



	function get_pluslist($kind)    
	{
		global $db;

		$q="select * from broker_plus where uid='".$this->uid."' and kind='$kind' order by id desc";
		$rs= $db->Execute($q);
		$i=1;
		$ss="<table align=center class=tableoutline width=700 border=0 cellpadding=2 cellspacing=1>
		<tr bgcolor=#eeeeee align=center><td >&nbsp;</td><td >日期</td><td >地点名称</td><td >操作</td></tr>";

		while ($row=$rs->FetchRow())
		{

			//$act= "<a href='plus.php?action=modify&id=$row[id]' target='_blank'>修改</a>&nbsp;";
			$act="<a href='plus.php?action=delete_plus&kind=$kind&id=$row[id]' onclick=\"delete_plus('$kind',$row[id]);return false;\">删除</a>";

			$ss.="<tr bgcolor=#eeeeee align=center><td >$i</td><td >$row[indate]</td><td >$row[title]</td><td >$act</td></tr>";
			$i++;
		} 

		$ss.="</table>";
		$rs->Close();
		return $ss;
	}



	function get_contractlist($tbl)    
	{
		global $db;
		
		$arr[level]=array ('1'=>'满意', '2'=>'中评','3'=>'不满意');
		
		if($tbl=="sell") $kind="p_8";
		if($tbl=="rent") $kind="p_9";

		
		$q="select * from broker_contract where uid='".$this->uid."' and tbl='$tbl' and valid_flag=1 order by id desc";
		$rs= $db->Execute($q);
		$i=1;
		$ss="<table align=center class=tableoutline width=700 border=0 cellpadding=2 cellspacing=1>
		<tr bgcolor=#eeeeee align=center><td >&nbsp;</td><td >日期</td><td >类型</td><td >房源编号</td><td >评价</td><td >审核</td></tr>";

		while ($row=$rs->FetchRow())
		{

			//$act= "<a href='contract.php?action=modify&id=$row[id]&uid=".$this->uid."' target='_blank'>修改</a>&nbsp;";
			//$act.="<a href='contract.php?action=delete_contract&kind=$kind&id=$row[id]' onclick=\"delete_contract('$kind',$row[id],'$tbl');return false;\">删除</a>";

			$ss.="<tr bgcolor=#eeeeee align=center><td >$i</td><td >$row[indate]</td><td >$row[tbl]</td><td >$row[rowid]</td><td >".$arr[level][$row[level]]."</td><td >有效</td></tr>";
			$i++;
		} 

		$ss.="</table>";
		$rs->Close();
		return $ss;
	}

}

?>