<?php
include_once('../common.php');
//发起者ID
$postid = intval($_POST[postid]);
//接受者ID
$receiveid = intval($_POST[receiveid]);
//获得域名
$domain = $_SERVER['SERVER_NAME'];
//语音判断
$audio = 1;
//当前用户ID
//$supeUid = $_SGLOBAL['supe_uid'];
$supeUid = intval($_POST[supe_uid]);
//接受关闭信息
$close = getstr($_POST['myClose']);
//查看接受者是否是主播
$count = $_SGLOBAL['db']->result($_SGLOBAL['db']->query("SELECT COUNT(*) FROM ".tname('major')." m JOIN ".tname('collate')." c ON m.u_id=c.u_id WHERE m.u_id='$receiveid' AND c.collated=1"),0);
//获取不是主播的语音金豆数
$reaNum = $_SGLOBAL['db']->result($_SGLOBAL['db']->query("SELECT credit FROM ".tname('creditrule')." WHERE action='audio'"),0);
$experience = $_SGLOBAL['db']->result($_SGLOBAL['db']->query("SELECT experience FROM ".tname('creditrule')." WHERE action='audio'"),0);
if($close=='TheEnd'){
	//关闭音频操作，删除数据库
	$_SGLOBAL['db']->query("DELETE FROM ".tname('avcall')." WHERE workerid='$postid' AND clientid='$receiveid'");//删除
	$xml_return.= '<close>yes</close>';
	echo $xml_return; 
}else{
	//判断用户是否为发起者
	if($supeUid==$postid) {
		$xml_return = '<issend>yes</issend>';
		//获得发起者金豆数
		$postcredit = $_SGLOBAL['db']->result($_SGLOBAL['db']->query("SELECT credit FROM ".tname('space')." WHERE uid='$postid'"),0);
		//判断是否为主播
		if($count>=1) {
			//主播私聊金豆数
			$fee = $_SGLOBAL['db']->result($_SGLOBAL['db']->query("SELECT m.fee FROM ".tname('major')." m JOIN ".tname('collate')." c ON m.u_id=c.u_id WHERE m.u_id='$receiveid' AND c.collated=1"),0);
			//判断金豆是否充足
			if($postcredit>=$fee) {
				$_SGLOBAL['db']->query("DELETE FROM ".tname('avcall')." WHERE workerid='$postid' AND clientid='$receiveid'");//删除
				$_SGLOBAL['db']->query("INSERT INTO ".tname('avcall')." (workerid,clientid,isAudio) VALUES ('$postid','$receiveid','$audio')");//插入
				//媒体开启
				$xml_return.= '<send>yes</send>';
				echo $xml_return; 
			}else{
				$xml_return.= '<send>no</send>';
				echo $xml_return; 
			}
		}else{
			if($postcredit>=$reaNum) {
				$_SGLOBAL['db']->query("DELETE FROM ".tname('avcall')." WHERE workerid='$postid' AND clientid='$receiveid'");//删除
				$_SGLOBAL['db']->query("INSERT INTO ".tname('avcall')." (workerid, clientid,isAudio) VALUES ('$postid','$receiveid','$audio')");//插入
				//媒体开启
				$xml_return.= '<send>yes</send>';
				echo $xml_return; 
			}else{
				$xml_return.= '<send>no</send>';
				echo $xml_return; 
			}
		}
	//判断用户是否为接受者
	}elseif($supeUid==$receiveid) {
		$xml_return = '<issend>no</issend>';
		//是否存在通讯信息
		$exist = $_SGLOBAL['db']->result($_SGLOBAL['db']->query("SELECT COUNT(*) FROM ".tname('avcall')." WHERE workerid='$postid' AND clientid='$receiveid'"),0);
		if($exist>=1) {
			//获得发起者金豆数
			$postcredit = $_SGLOBAL['db']->result($_SGLOBAL['db']->query("SELECT credit FROM ".tname('space')." WHERE uid='$postid'"),0);
			//判断是否为主播
			if($count>=1) {
				//主播私聊金豆数
				$fee = $_SGLOBAL['db']->result($_SGLOBAL['db']->query("SELECT m.fee FROM ".tname('major')." m JOIN ".tname('collate')." c ON m.u_id=c.u_id WHERE m.u_id='$receiveid' AND c.collated=1"),0);
				//判断发起者金豆是否足够
				if($postcredit>=$fee) {
					//开启媒体
					$xml_return.= '<receive>yes</receive>';
					echo $xml_return; 
					$_SGLOBAL['db']->query("DELETE FROM ".tname('avcall')." WHERE workerid='$postid' AND clientid='$receiveid'");//删除
					//--------------减掉对方豆---------------自身豆加----------------系统豆转换--------------------------
					$expert = $_SGLOBAL['db']->result($_SGLOBAL['db']->query("SELECT credit FROM ".tname('creditrule')." WHERE action='expert'"),0);
					$exprule =floor ($fee * (1- $expert /100));
					$_SGLOBAL['db']->query('UPDATE '.tname('space')." SET credit=credit-$fee, experience=experience+$experience WHERE uid='$postid'");
					$_SGLOBAL['db']->query('UPDATE '.tname('space')." SET credit=credit+$exprule  WHERE uid='$receiveid'");	
				}else{
					$_SGLOBAL['db']->query("DELETE FROM ".tname('avcall')." WHERE workerid='$postid' AND clientid='$receiveid'");//删除
					$xml_return.= '<result>no</result>';
					echo $xml_return; 
				}
			}else{
				if($postcredit>=$reaNum) {
					//开启媒体
					$xml_return.= '<receive>yes</receive>';
					echo $xml_return; 
					$_SGLOBAL['db']->query("DELETE FROM ".tname('avcall')." WHERE workerid='$postid' AND clientid='$receiveid'");//删除
					//--------------减掉对方豆---------------自身豆加-----------------------系统豆转换-------------------
					$sysrule = $_SGLOBAL['db']->result($_SGLOBAL['db']->query("SELECT credit FROM ".tname('creditrule')." WHERE action='system'"),0);
					$_SGLOBAL['db']->query('UPDATE '.tname('space')." SET credit=credit-$reaNum, experience=experience+$experience WHERE uid='$postid'");
					$_SGLOBAL['db']->query('UPDATE '.tname('space')." SET credit=credit+$reaNum-$sysrule WHERE uid='$receiveid'");
				}else{
					$_SGLOBAL['db']->query("DELETE FROM ".tname('avcall')." WHERE workerid='$postid' AND clientid='$receiveid'");//删除
					$xml_return.= '<receive>no</receive>';
					echo $xml_return; 
				}
			}
		}else{
				$xml_return.= '<receive>notfound</receive>';
				echo $xml_return; 
		}
	}else{
			$xml_return = '<send>error</send>';
			echo $xml_return; 
	}
}
?>