<?php
//--------------------- 更新会员表 ---------------------

//会员表结构开始处理
function upStartChangeMemberTbStru(){
	global $empire,$dbtbpre,$ecms_config;
	$empire->query("alter table {$dbtbpre}enewsmember 
		change userid userid int(10) unsigned NOT NULL auto_increment,
		change username username char(20) NOT NULL default '',
		change password password char(32) NOT NULL default '',
		change rnd rnd char(20) NOT NULL default '',
		change email email char(50) NOT NULL default '',
		add `oldregistertime` datetime NOT NULL default '0000-00-00 00:00:00',
		change groupid groupid smallint(5) unsigned NOT NULL default '0',
		change userfen userfen mediumint(8) unsigned NOT NULL default '0',
		change userdate userdate int(10) unsigned NOT NULL default '0',
		change zgroupid zgroupid smallint(5) unsigned NOT NULL default '0',
		change salt salt char(8) NOT NULL default '',
		add `userkey` char(12) NOT NULL default '',
		add INDEX(groupid);");

	$empire->query("update {$dbtbpre}enewsmember set oldregistertime=registertime");

	$empire->query("alter table {$dbtbpre}enewsmember 
		change registertime registertime int(10) unsigned NOT NULL default '0';");

	//副表
	$empire->query("alter table {$dbtbpre}enewsmemberadd 
		change userid userid int(10) unsigned NOT NULL default '0',
		add `lasttime` int(10) unsigned NOT NULL default '0',
		add `lastip` varchar(20) NOT NULL default '',
		add `loginnum` int(10) unsigned NOT NULL default '0';");

	//建公共表
	$empire->query(SetCreateTable("CREATE TABLE `{$dbtbpre}enewsmemberpub` (
  `userid` mediumint(8) unsigned NOT NULL auto_increment,
  `todayinfodate` char(10) NOT NULL default '',
  `todayaddinfo` mediumint(8) unsigned NOT NULL default '0',
  `todaydate` char(10) NOT NULL default '',
  `todaydown` mediumint(8) unsigned NOT NULL default '0',
  `authstr` char(30) NOT NULL default '',
  PRIMARY KEY  (`userid`)
) TYPE=MyISAM;",$ecms_config['db']['dbchar']));

	//自定义字段处理
	$fnum=$empire->gettotal("select count(*) as total from {$dbtbpre}enewsmemberf where f='call' limit 1");
	if($fnum)
	{
		$empire->query("alter table {$dbtbpre}enewsmemberadd 
		change `call` `mycall` varchar(30) NOT NULL default '';");

		$empire->query("update {$dbtbpre}enewsmemberf set f='mycall',fhtml=REPLACE(fhtml,'call','mycall') where f='call' limit 1");

		$empire->query("update {$dbtbpre}enewsmemberform set ftemp=REPLACE(ftemp,'call','mycall'),enter=REPLACE(enter,'call','mycall'),mustenter=REPLACE(mustenter,'call','mycall'),viewenter=REPLACE(viewenter,'call','mycall'),searchvar=REPLACE(searchvar,'call','mycall'),canaddf=REPLACE(canaddf,'call','mycall'),caneditf=REPLACE(caneditf,'call','mycall')");
	}

}

//会员表结构结束处理
function upEndChangeMemberTbStru(){
	global $empire,$dbtbpre,$ecms_config;
	$empire->query("alter table {$dbtbpre}enewsmember 
		drop COLUMN oldregistertime;");

	$empire->query("alter table {$dbtbpre}enewsmemberadd 
		drop COLUMN todayinfodate,
		drop COLUMN todayaddinfo,
		drop COLUMN todaydate,
		drop COLUMN todaydown,
		drop COLUMN authstr;");
}

//会员转换
function upChangeMemberTbData($add){
	global $empire,$public_r,$class_r,$fun_r,$dbtbpre,$etable_r,$ecms_config,$domemberline,$domemberretime;
	$domemberline=(int)$domemberline;
	$domemberretime=(int)$domemberretime;
	$start=(int)$add['start'];
	//表结构开始处理
	if($start==0)
	{
		upStartChangeMemberTbStru();
	}
	$b=0;
	$sql=$empire->query("select * from {$dbtbpre}enewsmember where userid>$start order by userid limit ".$domemberline);
	while($r=$empire->fetch($sql))
	{
		$b=1;
		$new_start=$r[userid];
		//副表
		$fr=$empire->fetch1("select * from {$dbtbpre}enewsmemberadd where userid='$r[userid]' limit 1");
		//更新主表
		$userkey=make_password(12);
		$registertime=to_time($r['oldregistertime']);
		//密码
		$uppassword='';
		$newsalt=make_password(6);
		$newpassword=md5($r['password'].$newsalt);
		$uppassword=",password='$newpassword',salt='$newsalt'";
		$empire->query("update {$dbtbpre}enewsmember set registertime='$registertime',userkey='$userkey'".$uppassword." where userid='$r[userid]'");
		//更新公共表
		$empire->query("replace into {$dbtbpre}enewsmemberpub(userid,todayinfodate,todayaddinfo,todaydate,todaydown,authstr) values('$r[userid]','$fr[todayinfodate]','$fr[todayaddinfo]','$fr[todaydate]','$fr[todaydown]','$fr[authstr]');");
	}
	if(empty($b))
	{
		//表结构结束处理
		upEndChangeMemberTbStru();
		echo"<link rel=\"stylesheet\" href=\"../data/images/css.css\" type=\"text/css\"><meta http-equiv=\"refresh\" content=\"".$domemberretime.";url=index.php?ecms=ChangeShopTbData\">会员数据转换完毕!正进入商城订单数据转换......";
		db_close();
		$empire=null;
		exit();
	}
	echo"<link rel=\"stylesheet\" href=\"../data/images/css.css\" type=\"text/css\"><meta http-equiv=\"refresh\" content=\"".$domemberretime.";url=index.php?ecms=ChangeMemberTbData&start=$new_start\">转换一组会员数据完毕，正在进入下一组......(ID:<font color=red><b>".$new_start."</b></font>)";
	exit();
}

?>