<?php
//--------------------- 更新商城表 ---------------------

//商城表结构开始处理
function upStartChangeShopTbStru(){
	global $empire,$dbtbpre,$ecms_config;
	$empire->query("alter table {$dbtbpre}enewsshopdd 
	change ddid ddid int(10) unsigned NOT NULL auto_increment,
	change userid userid mediumint(8) unsigned NOT NULL default '0',
	change `call` `mycall` varchar(30) NOT NULL default '',
	add `fpname` varchar(50) NOT NULL default '',
	add `userip` varchar(20) NOT NULL default '',
	add `signbuild` varchar(100) NOT NULL default '',
	add `besttime` varchar(120) NOT NULL default '',
	add `pretotal` float(11,2) NOT NULL default '0.00',
	add `ddtruetime` int(10) unsigned NOT NULL default '0',
	add `havecutnum` tinyint(1) NOT NULL default '0';");

	//建订单副表
	$empire->query(SetCreateTable("CREATE TABLE `{$dbtbpre}enewsshopdd_add` (
  `ddid` int(10) unsigned NOT NULL default '0',
  `buycar` mediumtext NOT NULL,
  `bz` text NOT NULL,
  `retext` text NOT NULL,
  PRIMARY KEY  (`ddid`)
) TYPE=MyISAM;",$ecms_config['db']['dbchar']));

}

//商城表结构结束处理
function upEndChangeShopTbStru(){
	global $empire,$dbtbpre,$ecms_config;
	$empire->query("alter table {$dbtbpre}enewsshopdd 
		drop COLUMN bz,
		drop COLUMN g_truename,
		drop COLUMN g_oicq,
		drop COLUMN g_msn,
		drop COLUMN g_email,
		drop COLUMN g_call,
		drop COLUMN g_phone,
		drop COLUMN g_address,
		drop COLUMN g_zip,
		drop COLUMN buycar;");
}

//订单转换
function upChangeShopTbData($add){
	global $empire,$public_r,$class_r,$fun_r,$dbtbpre,$etable_r,$ecms_config,$doshopline,$doshopretime;
	$doshopline=(int)$doshopline;
	$doshopretime=(int)$doshopretime;
	$start=(int)$add['start'];
	//表结构开始处理
	if($start==0)
	{
		upStartChangeShopTbStru();
	}
	$b=0;
	$sql=$empire->query("select * from {$dbtbpre}enewsshopdd where ddid>$start order by ddid limit ".$doshopline);
	while($r=$empire->fetch($sql))
	{
		$b=1;
		$new_start=$r[ddid];
		//更新主表
		$ddtruetime=to_time($r['ddtime']);
		$empire->query("update {$dbtbpre}enewsshopdd set ddtruetime='$ddtruetime' where ddid='$r[ddid]'");
		//更新副表
		$newbuycar=upChangeShopReturnBuycar($r['buycar']);
		$empire->query("replace into {$dbtbpre}enewsshopdd_add(ddid,buycar,bz,retext) values('$r[ddid]','".addslashes($newbuycar)."','".addslashes($r[bz])."','');");
	}
	if(empty($b))
	{
		//表结构结束处理
		upEndChangeShopTbStru();
		echo"<link rel=\"stylesheet\" href=\"../data/images/css.css\" type=\"text/css\"><meta http-equiv=\"refresh\" content=\"".$doshopretime.";url=index.php?ecms=ChangeTable\">所有数据表全部数据转换完毕!正进入数据表结构升级......";
		db_close();
		$empire=null;
		exit();
	}
	echo"<link rel=\"stylesheet\" href=\"../data/images/css.css\" type=\"text/css\"><meta http-equiv=\"refresh\" content=\"".$doshopretime.";url=index.php?ecms=ChangeShopTbData&start=$new_start\">转换一组商城订单数据完毕，正在进入下一组......(ID:<font color=red><b>".$new_start."</b></font>)";
	exit();
}

//返回新购物车
function upChangeShopReturnBuycar($buycar){
	if(empty($buycar))
	{
		return '';
	}
	$buycar_lr=explode('!',$buycar);
	$count=count($buycar_lr);
	for($i=0;$i<$count-1;$i++)
	{
		$buycarr=explode('|',$buycar_lr[$i]);
		$ids='|'.$buycarr[1].'|';
		$newbuycar.=$ids.'|'.substr($buycar_lr[$i],strlen($ids)).'!';
	}
	return $newbuycar;
}

?>