<?
 global $rowcontrol;
 $sj=date("Y-m-d H:i:s");
 $uip=$_SERVER["REMOTE_ADDR"];
 //开始执行购买
 $probh=preg_split("/xcf/",$bharr);
 $pronum=preg_split("/xcf/",$numarr);
 $tcidm=preg_split("/xcf/",$tcidarr);
 $buyform=preg_split("/yj99yjcode/",$buyformarr);
 for($i=0;$i<=count($probh);$i++){
 if($probh[$i]!=""){
  $sql="select * from yjcode_pro where bh='".$probh[$i]."' and zt=0 and ifxj=0 and kcnum>=".$pronum[$i];mysql_query("SET NAMES 'GBK'");$res=mysql_query($sql);
  if($row=mysql_fetch_array($res)){
  /////////////////////////////////开始逐一购买
  $orderbh=time().$i.rnd_num(100);
  $money=returnyhmoney($row[yhxs],$row[money2],$row[money3],$sj,$row[yhsj1],$row[yhsj2],$row[id]);

  $tcv="";
  $tcfhxs=0;
  $tcid=0;
  if(!empty($tcidm[$i])){
  $tcid=$tcidm[$i];
  $sqlt="select * from yjcode_taocan where id=".$tcid;mysql_query("SET NAMES 'GBK'");$rest=mysql_query($sqlt);
  if($rowt=mysql_fetch_array($rest)){
   $tcfhxs=$rowt[fhxs];
   $money=$rowt[money1];
   $tit=$rowt[tit];
   if($rowt[admin]==2){$tit=$tit." ".$rowt[tit2];}
   $tcv=$tit." 价格:".$rowt[money1];}
  }

  $allmoney=$money*$pronum[$i];

  if(!empty($row[ifuserdj])){
   $sqlu2="select * from yjcode_user where id=".$buyuserid;mysql_query("SET NAMES 'GBK'");$resu2=mysql_query($sqlu2);$rowu2=mysql_fetch_array($resu2);
   if(!empty($rowu2[userdj])){$s=" and name1='".$rowu2[userdj]."'";}else{$s="";}
   $sqlu3="select * from yjcode_userdj where zt=0".$s." order by xh asc limit 1";mysql_query("SET NAMES 'GBK'");$resu3=mysql_query($sqlu3);if($rowu3=mysql_fetch_array($resu3)){
   $allmoney=$allmoney*($rowu3[zhekou]/10);
   $money=$money*($rowu3[zhekou]/10);
   } 
  }
  
  $sqlu="select id,money1,email from yjcode_user where id=".$buyuserid;mysql_query("SET NAMES 'GBK'");$resu=mysql_query($sqlu);if(!$rowu=mysql_fetch_array($resu)){exit;}
  if($rowu[money1]<$allmoney){exit;}
  $kc=$row[kcnum]-$pronum[$i];updatetable("yjcode_pro","kcnum=".$kc." where id=".$row[id]);
  intotable("yjcode_order","probh,sj,uip,selluserid,userid,money1,orderbh,num,tit,ddzt,tcv,buyform,tcid,fhxs","'".$row[bh]."','".$sj."','".$uip."',".$row[userid].",".$buyuserid.",".$money.",'".$orderbh."',".$pronum[$i].",'".$row[tit]."','wait','".$tcv."','".$buyform[$i]."',".$tcid.",".$row[fhxs]."");
  PointUpdateM($buyuserid,$allmoney*(-1));
  PointIntoM($buyuserid,"购买商品,数量".$pronum[$i],$allmoney*(-1));
  updatetable("yjcode_pro","xsnum=xsnum+".$pronum[$i]." where id=".$row[id]);
  deletetable("yjcode_car where userid=".$buyuserid." and probh='".$probh[$i]."'");
  
  if(empty($tcid) || $tcfhxs==0){
  //无套餐或套餐跟随商品自动发货商品B
  if($row[fhxs]==2 || $row[fhxs]==3 || $row[fhxs]==4){
  updatetable("yjcode_order","fhsj='".$sj."',ddzt='db' where ddzt='wait' and orderbh='".$orderbh."'");
  $oksj=date("Y-m-d H:i:s",strtotime("+".$rowcontrol[dbsj]." day"));
  $c_tit="卖家已经发货，款项进入担保阶段，等待买家确认收货";
  intotable("yjcode_db","money1,sj,selluserid,userid,dboksj,probh,tit,orderbh","".$allmoney.",'".$sj."',".$row[userid].",".$buyuserid.",'".$oksj."','".$row[bh]."','".$c_tit."','".$orderbh."'");
   //卡密B
   if(4==$row[fhxs]){
	$sqla="select * from yjcode_kc where probh='".$row[bh]."' and ifok=0 and userid=".$row[userid]." order by id asc limit ".$pronum[$i];mysql_query("SET NAMES 'GBK'");
	$resa=mysql_query($sqla);while($rowa=mysql_fetch_array($resa)){
	$txt=$txt."卡号：".$rowa[ka]." 密码：".$rowa[mi]."<br>";
    updatetable("yjcode_kc","ifok=1,sj='".$sj."',uip='".$uip."' where id=".$rowa[id]);
	} 
   }
   //卡密E
  }
  //无套餐或套餐跟随商品自动发货商品E
  }else{
  //有套餐自动发货商品B
  updatetable("yjcode_taocan","kcnum=kcnum-".$pronum[$i]." where id=".$tcid);
  if(1!=$tcfhxs){
  updatetable("yjcode_order","fhsj='".$sj."',ddzt='db' where ddzt='wait' and orderbh='".$orderbh."'");
  $oksj=date("Y-m-d H:i:s",strtotime("+".$rowcontrol[dbsj]." day"));
  $c_tit="卖家已经发货，款项进入担保阶段，等待买家确认收货";
  intotable("yjcode_db","money1,sj,selluserid,userid,dboksj,probh,tit,orderbh","".$allmoney.",'".$sj."',".$row[userid].",".$buyuserid.",'".$oksj."','".$row[bh]."','".$c_tit."','".$orderbh."'");
   //卡密B
   if(4==$tcfhxs){
	$sqla="select * from yjcode_taocan_kc where probh='".$row[bh]."' and tcid=".$tcid." and ifok=0 and userid=".$row[userid]." order by id asc limit ".$pronum[$i];mysql_query("SET NAMES 'GBK'");
	$resa=mysql_query($sqla);while($rowa=mysql_fetch_array($resa)){
	$txt=$txt."卡号：".$rowa[ka]." 密码：".$rowa[mi]."<br>";
    updatetable("yjcode_taocan_kc","ifok=1,sj='".$sj."',uip='".$uip."' where id=".$rowa[id]);
	} 
   }
   //卡密E
  }
  //有套餐自动发货商品E
  }
  updatetable("yjcode_order","txt='".$txt."' where orderbh='".$orderbh."'");
  
  //写入邮件B
  $sqlm="select id,email,ifemail from yjcode_user where id=".$row[userid];mysql_query("SET NAMES 'GBK'");$resm=mysql_query($sqlm);if(!$rowm=mysql_fetch_array($resm)){exit;}
  if(1==$rowm[ifemail] && !empty($rowm[email])){
   $t="亲，有新订单啦！请尽快登录网站发货，".weburl;
   $sqls="select * from yjcode_smsmail where admin=1 and tyid=1 and fa='".$rowm[email]."'";mysql_query("SET NAMES 'GBK'");$ress=mysql_query($sqls);
   if(!$rows=mysql_fetch_array($ress)){
   intotable("yjcode_smsmail","admin,fa,tyid,userid,txt,tit","1,'".$rowm[email]."',1,".$buyuserid.",'".$t."','您的订单信息'");
   }
  }
  //写入邮件E
  
  //写入短信B
  $sqlm="select id,mot,ifmot from yjcode_user where id=".$row[userid];mysql_query("SET NAMES 'GBK'");$resm=mysql_query($sqlm);if(!$rowm=mysql_fetch_array($resm)){exit;}
  if(1==$rowm[ifmot] && !empty($rowm[mot])){
   $t="亲，有新订单啦！请尽快登录网站发货，购买商品为：\${tit}";
   $sqls="select * from yjcode_smsmail where admin=2 and tyid=1 and fa='".$rowm[mot]."'";mysql_query("SET NAMES 'GBK'");$ress=mysql_query($sqls);
   if(!$rows=mysql_fetch_array($ress)){
   $dt=sprintf("%.2f",$allmoney);
   intotable("yjcode_smsmail","admin,fa,tyid,userid,txt,tit","2,'".$rowm[mot]."',1,".$buyuserid.",'".$t."','".$dt."'");
   }
  }
  //写入短信E
  
  //快照B
  $uv=preg_split("/\//",$_SERVER['PHP_SELF']);
  $lj="";
  $ip=str_replace(".","",$uip);
  for($ui=1;$ui<count($uv)-1;$ui++){$lj=$lj."../";}
  @createDir($lj."tem/kz/".dateYMDN($sj)."/");
  @html_template("product/view".$row[id].".html",$lj."tem/kz/".dateYMDN($sj)."/".$ip."_".$orderbh.".html");
  //快照E
  //////////////////////////////////结束逐一购买
  }
 }	 
 }
 //结束执行购买
?>