PHP业务逻辑处理数据库耗时长达超1分钟,如何修改才能处理的更快?请高手看看下?
function getodds($played,$user,$number=0,$ltyid=0,$lmoney=0,$PeriodsNumber=''){
//$commission=json_decode($user['commission'],true);
//$sjcommission=json_decode($user['sjcommission'],true);
$typearr=array(1,2,3,4,5,6,7);
$ltyid=in_array($ltyid,$typearr) ? $ltyid:1;
$zjpl=json_decode($user['commission'],true);
$commission=$zjpl[$ltyid];
$sjpl=json_decode($user['sjcommission'],true);
$sjcommission=$sjpl[$ltyid];
$bjresult=0;
$dfg=getodds_hm(strtoupper($number),$played,$user['id'],1,$ltyid,$lmoney);
if($dfg){
$bjresult=(float)$dfg;
}
else{
$dfg=getoddsend($played,$user['id'],1,$ltyid,0,$lmoney,$number,$PeriodsNumber);
if($dfg){
$bjresult=(float)$dfg;
}
}
$list=explode('/',$played['bonusprop']);
$clist=explode('/',$played['cbonusprop']);
if(count($list)==1){
$bonusprop=$list[0];
$cbonusprop=$clist[0];
}
else{
$number=$number;
$nlist=str_split($number);
$c=array_count_values($nlist);
$cfnum=count($nlist)-count($c);
$bonusprop=$list[$cfnum];
$cbonusprop=$clist[$cfnum];
}
$result=(float)($bonusprop-$cbonusprop*$sjcommission[$played['bettypeid']]['Commission']);
if($bjresult>0){
if(in_array($played['bettypeid'],array(2,3,4,5,6,7,17,18,25,26))){
$bjresult=$bjresult*100;
}
elseif(in_array($played['bettypeid'],array(9,10,11,12))){
$bjresult=$bjresult*1000;
}
elseif(in_array($played['bettypeid'],array(20,21,22,23,24))){
$bjresult=$bjresult*10;
}
elseif($played['bettypeid']==13){
$bjresult=$bjresult*10000;
}
elseif($played['bettypeid']==14){
$bjresult=$bjresult*10;
}
elseif($played['bettypeid']==15){
$bjresult=$bjresult*100;
}
elseif($played['bettypeid']==16){
$bjresult=$bjresult*1000;
}
$result=$result-$bjresult;
}
return $result;
}
function getodds_hm($hnumber='',$played,$userid,$isuser=0,$ltyid=0,$lmoney=0){
if($hnumber!=''){
if($played['bettypeid']==14 || $played['bettypeid']==15 || $played['bettypeid']==16){
$hnumber=preg_replace('/\D/','',$hnumber);
$jdfg=str_split($hnumber);
sort($jdfg);
$hnumber=implode('',$jdfg);
}
$where['lxid']=$ltyid;
$where['uid']=$userid;
$where['haoma']=$hnumber;
$where['flag']=1;
$where['playid']=$played['bettypeid'];
if($isuser){
$where['isuser']=1;
$user=M('user');
$agent=$user->find($userid);
$whereddc['user_login']=$agent['parent_user'];
$parent=M('agent')->where($whereddc)->find();
}
else{
$where['isuser']=0;
$user=M('agent');
$agent=$user->find($userid);
$parent=M('agent')->find($agent['parent']);
}
$dfg=M('peilv_biandong')->where($where)->find();
if($dfg){
return $dfg['peilv'];
}
else{
if(!$isuser && !$agent['parent']){
return false;
}
return getodds_hm($hnumber,$played,$parent['id'],0,$ltyid,$lmoney);
}
}
return false;
}
function getoddsend($played,$userid,$isuser=0,$ltyid=0,$agentid=0,$lmoney=0,$hnumber='',$PeriodsNumber=''){
if($isuser){
$user=M('user');
$agent=$user->find($userid);
$whereddc['user_login']=$agent['parent_user'];
$parent=M('agent')->where($whereddc)->find();
}
else{
$user=M('agent');
$agent=$user->find($agentid);
$parent=M('agent')->find($agent['parent']);
}
$userw['playedId']=$played['id'];
$userw['typeid']=$ltyid;
$userw['sftm']=0;
if($PeriodsNumber!=''){
$userw['PeriodsNumber']=$PeriodsNumber;
}
if($hnumber!=''){
if($played['bettypeid']==14 || $played['bettypeid']==15 || $played['bettypeid']==16){
$hnumber=preg_replace('/\D/','',$hnumber);
$jdfg=str_split($hnumber);
sort($jdfg);
$hnumber=implode('',$jdfg);
}
//$userw['BetNumber']=$hnumber;
}
$mem=M('userbet'.$userid)->field("sum(BetAmount) as membetamt")->where($userw)->find();
//echo M('userbet'.$userid)->getLastSql();exit;
$dbfw=($mem['membetamt']) ? $mem['membetamt']+$lmoney:0+$lmoney;
if($dbfw>0){
$where['starxzl'] = array('lt',$dbfw);//小于
$where['endxzl'] = array('egt',$dbfw);//大于等于
}else{
$where['starxzl']=0;
}
$where['uid']=$agentid? $agentid:$userid;
$where['lxid']=$ltyid;
$where['playedId']=$played['bettypeid'];
$where['isuser']=0;
if($isuser){
$where['isuser']=1;
}
$dingpanlist=M('dingpan')->where($where)->order('peilv asc')->find();
if($dingpanlist){
$result=$dingpanlist['peilv'];
return $result;
}
else{
if(!$number && !$agent['parent']){
return false;
}
$whered['uid']=$agentid? $agentid:$userid;
$whered['starxzl'] = array('gt',$dbfw);
$whered['endxzl'] = array('elt',$dbfw);//大于等于
$whered['lxid']=$ltyid;
$whered['playedId']=$played['bettypeid'];
$whered['isuser']=1;
$dingpanlist=M('dingpan')->where($whered)->order('peilv asc')->find();
if(!$dingpanlist){
$isuser=0;
$agentid=$parent['id'];
}
return getoddsend($played,$userid,$isuser,$ltyid,$agentid,$lmoney,$hnumber,$PeriodsNumber);
}
}
function getoddsup($played,$user,$number=0,$ltyid=0){
//$commission=json_decode($user['commission'],true);
//$sjcommission=json_decode($user['sjcommission'],true);
$typearr=array(1,2,3,4,5,6,7);
$ltyid=in_array($ltyid,$typearr) ? $ltyid:1;
$zjpl=json_decode($user['commission'],true);
$commission=$zjpl[$ltyid];
$sjpl=json_decode($user['sjcommission'],true);
$sjcommission=$sjpl[$ltyid];
$list=explode('/',$played['bonusprop']);
$clist=explode('/',$played['cbonusprop']);
if(count($list)==1){
$bonusprop=$list[0];
$cbonusprop=$clist[0];
}
else{
$number=$number;
$nlist=str_split($number);
$c=array_count_values($nlist);
$cfnum=count($nlist)-count($c);
$bonusprop=$list[$cfnum];
$cbonusprop=$clist[$cfnum];
}
return (float)($cbonusprop*$sjcommission[$played['bettypeid']]['Commission']);