<?php
require(dirname(__FILE__) .'/check.php');
$skin->CheckIframe();


/* **** 版权所有 ****

 程序开发：网钛科技
 网钛CMS官网：http://otcms.com
 网钛IDC官网：http://idccms.com
 ＱＱ：877873666
 微信：QQ877873666
 邮箱：877873666@qq.com

 **** 版权所有 **** */


//用户检测
$MB->Open('','login',2);


$MB->IsAdminRight('alertBack');


if ($mudi == 'dbInfo'){
	dbInfo();
	die();
}



$beforeURL=GetUrl::CurrDir(1);
$dataTypeCN = '在线升级';


switch ($mudi){
	case 'changUpdateUrl':
		// 更改升级网址
		ChangUpdateUrl();
		break;

	case 'clearUpdateData':
		// 清空升级库
		ClearUpdateData();
		break;

	case 'del':
		// 删除单条升级记录
		del();
		break;

	case 'updateField':
		// 版本字段修复
		updateField();
		break;

	default:
		die('err');
}

$MB->Close();
$DB->Close();





// 更改升级网址
function ChangUpdateUrl(){
	global $DB,$beforeURL,$updateUrlVerStr,$sysAdminArr;

	$updateUrl = OT::GetStr('updateUrl');

	$record = array();
	$record['SA_updateUrl']		= $updateUrl;
	$judRes = $DB->UpdateParam('sysAdmin',$record,'SA_ID=1');
		if ($judRes){
			$alertRes = '成功';

			$Cache = new Cache();
			$result = $Cache->Php('sysAdmin');	// 更新缓存文件
		}else{
			$alertRes = '失败';
		}

	echo('// 更改'. $alertRes);
}



// 清空升级库
function ClearUpdateData(){
	global $DB,$beforeURL,$updateUrlVerStr,$sysAdminArr;

	$fileexe = $DB->query('select FD_ID,FD_fileName,FD_contentMd5 from '. OT_dbPref .'fileData where FD_mode=1');
		while ($row = $fileexe->fetch()){
			File::Del(UpdateAdminDir . $row['FD_ID'] .'_'. $row['FD_contentMd5'] .'.OTtpl');
		}
	$fileexe = null;

	$DB->query('delete from '. OT_dbPref .'fileData');
	$DB->query('delete from '. OT_dbPref .'fileVer');

	File::MoreDel(UpdateAdminDir,'OTtpl');

	echo('alert("清空升级库成功");');
}



// 删除
function del(){
	global $DB,$beforeURL,$updateUrlVerStr,$sysAdminArr;

	$dataID	= OT::GetInt2( OT::ParaArr('id') );
	$theme	= OT::GetStr('theme');

	if ($dataID <= 0){
		JS::AlertEnd('指定ID错误');
	}

	$fileexe = $DB->query('select FD_ID,FD_fileName,FD_contentMd5 from '. OT_dbPref .'fileData where FD_verID='. $dataID .' and FD_mode=1');
		while ($row = $fileexe->fetch()){
			File::Del(UpdateAdminDir . $row['FD_ID'] .'_'. $row['FD_contentMd5'] .'.OTtpl');
		}
	$fileexe = null;

	$DB->query('delete from '. OT_dbPref .'fileData where FD_verID='. $dataID);
	$DB->query('delete from '. OT_dbPref .'fileVer where FV_ID='. $dataID);


	JS::ParentHiddenEnd('data'. $dataID);
}



// 升级库信息
function dbInfo(){
	global $DB;

	$infoStr = '$id("updateDbInfo").innerHTML=\'<span style="color:red;">(升级记录'. $DB->GetOne('select count(FV_ID) from '. OT_dbPref .'fileVer') .'条)</span>\';';

	echo($infoStr);
}



function updateField(){
	global $DB,$dbName,$collDbName;

	// 获取采集数据库对象
	// $collDB = AreaApp::GetCollDB();

	$ver	= OT::GetStr('ver');
	$backURL= OT::GetStr('backURL');
	$newStr = '';

	switch ($ver){
		case 'themeMd5':
			// 修复文章缺失标题MD5
			$succNum = $failNum = 0;
			$mtexe = $DB->query('select IF_ID,IF_theme from '. OT_dbPref .'info where IF_themeMd5 = "" or IF_themeMd5 is null');
			while ($row = $mtexe->fetch()){
				$res = $DB->UpdateParam('info', array('IF_themeMd5'=>md5($row["IF_theme"])), 'IF_ID='. $row["IF_ID"]);
				if ($res){
					$succNum ++;
				}else{
					$failNum ++;
				}
			}
			unset($mtexe);
			$newStr .= '[修复文章缺失标题MD5，成功 '. $succNum .' 条，失败 '. $failNum .' 条.]';
			break;

		case 'index_info':
			// 修复文章表(OT_info)索引
			$indexArr = array();
			$mtexe = $DB->query('SHOW INDEX FROM '. OT_dbPref .'info WHERE Seq_in_index=1');
			while ($row = $mtexe->fetch()){
				$indexArr[] = $row['Key_name'];
			}
			if (in_array('IF_type1ID',$indexArr)){
				$newStr .= 'IF_type1ID 索引已存在';
			}else{
				$newStr .= 'IF_type1ID 索引正在创建...';
				$DB->query('alter table '. OT_dbPref .'info add index IF_type1ID(IF_type1ID)');
			}
			if (in_array('IF_type2ID',$indexArr)){
				$newStr .= '\nIF_type2ID 索引已存在';
			}else{
				$newStr .= '\nIF_type2ID 索引正在创建...';
				$DB->query('alter table '. OT_dbPref .'info add index IF_type2ID(IF_type2ID)');
			}
			if (in_array('IF_isAudit',$indexArr)){
				$newStr .= '\nIF_isAudit 索引已存在';
			}else{
				$newStr .= '\nIF_isAudit 索引正在创建...';
				$DB->query('alter table '. OT_dbPref .'info add index IF_isAudit(IF_isAudit)');
			}
			if (in_array('IF_state',$indexArr)){
				$newStr .= '\nIF_state 索引已存在';
			}else{
				$newStr .= '\nIF_state 索引正在创建...';
				$DB->query('alter table '. OT_dbPref .'info add index IF_state(IF_state)');
			}
			if (in_array('IF_wapState',$indexArr)){
				$newStr .= '\nIF_wapState 索引已存在';
			}else{
				$newStr .= '\nIF_wapState 索引正在创建...';
				$DB->query('alter table '. OT_dbPref .'info add index IF_wapState(IF_wapState)');
			}
			if (in_array('IF_isTop',$indexArr)){
				$newStr .= '\nIF_isTop 索引已存在';
			}else{
				$newStr .= '\nIF_isTop 索引正在创建...';
				$DB->query('alter table '. OT_dbPref .'info add index IF_isTop(IF_isTop)');
			}
			if (in_array('IF_collID',$indexArr)){
				$newStr .= '\nIF_collID 索引已存在';
			}else{
				$newStr .= '\nIF_collID 索引正在创建...';
				$DB->query('alter table '. OT_dbPref .'info add index IF_collID(IF_collID)');
			}
			if (in_array('IF_theme',$indexArr)){
				$newStr .= '\nIF_theme 索引已存在';
			}else{
				$newStr .= '\nIF_theme 索引正在创建...';
				$DB->query('alter table '. OT_dbPref .'info add index IF_theme(IF_theme)');
			}
			if (in_array('IF_themeMd5',$indexArr)){
				$newStr .= '\nIF_themeMd5 索引已存在';
			}else{
				$newStr .= '\nIF_themeMd5 索引正在创建...';
				$DB->query('alter table '. OT_dbPref .'info add index IF_themeMd5(IF_themeMd5)');
			}
			if (in_array('IF_revTime',$indexArr)){
				$newStr .= '\nIF_revTime 索引已存在';
			}else{
				$newStr .= '\nIF_revTime 索引正在创建...';
				$DB->query('alter table '. OT_dbPref .'info add index IF_revTime(IF_revTime)');
			}
			if (in_array('IF_htmlTime',$indexArr)){
				$newStr .= '\nIF_htmlTime 索引已存在';
			}else{
				$newStr .= '\nIF_htmlTime 索引正在创建...';
				$DB->query('alter table '. OT_dbPref .'info add index IF_htmlTime(IF_htmlTime)');
			}
			if (in_array('IF_htmlWapTime',$indexArr)){
				$newStr .= '\nIF_htmlWapTime 索引已存在';
			}else{
				$newStr .= '\nIF_htmlWapTime 索引正在创建...';
				$DB->query('alter table '. OT_dbPref .'info add index IF_htmlWapTime(IF_htmlWapTime)');
			}
			if (in_array('IF_markNewsTime',$indexArr)){
				$newStr .= '\nIF_markNewsTime 索引已存在';
			}else{
				$newStr .= '\nIF_markNewsTime 索引正在创建...';
				$DB->query('alter table '. OT_dbPref .'info add index IF_markNewsTime(IF_markNewsTime)');
			}
			if (in_array('IF_prevNextTime',$indexArr)){
				$newStr .= '\nIF_prevNextTime 索引已存在';
			}else{
				$newStr .= '\nIF_prevNextTime 索引正在创建...';
				$DB->query('alter table '. OT_dbPref .'info add index IF_prevNextTime(IF_prevNextTime)');
			}
			break;

		case 'index_infoType':
			// 修复栏目表(OT_infoType)索引
			$indexArr = array();
			$mtexe = $DB->query('SHOW INDEX FROM '. OT_dbPref .'infoType WHERE Seq_in_index=1');
			while ($row = $mtexe->fetch()){
				$indexArr[] = $row['Key_name'];
			}
			if (in_array('IT_isUser',$indexArr)){
				$newStr .= 'IT_isUser 索引已存在';
			}else{
				$newStr .= 'IT_isUser 索引正在创建...';
				$DB->query('alter table '. OT_dbPref .'infoType add index IT_isUser(IT_isUser)');
			}
			if (in_array('IT_isAdminUser',$indexArr)){
				$newStr .= '\nIT_isAdminUser 索引已存在';
			}else{
				$newStr .= '\nIT_isAdminUser 索引正在创建...';
				$DB->query('alter table '. OT_dbPref .'infoType add index IT_isAdminUser(IT_isAdminUser)');
			}
			break;

		case 'index_users':
			// 修复会员表(OT_users)索引
			$indexArr = array();
			$mtexe = $DB->query('SHOW INDEX FROM '. OT_dbPref .'users WHERE Seq_in_index=1');
			while ($row = $mtexe->fetch()){
				$indexArr[] = $row['Key_name'];
			}
			if (in_array('UE_username',$indexArr)){
				$newStr .= 'UE_username 索引已存在';
			}else{
				$newStr .= 'UE_username 索引正在创建...';
				$DB->query('alter table '. OT_dbPref .'users add index UE_username(UE_username)');
			}
			if (in_array('UE_time',$indexArr)){
				$newStr .= '\nUE_time 索引已存在';
			}else{
				$newStr .= '\nUE_time 索引正在创建...';
				$DB->query('alter table '. OT_dbPref .'users add index UE_time(UE_time)');
			}
			if (in_array('UE_recomId',$indexArr)){
				$newStr .= '\nUE_recomId 索引已存在';
			}else{
				$newStr .= '\nUE_recomId 索引正在创建...';
				$DB->query('alter table '. OT_dbPref .'users add index UE_recomId(UE_recomId)');
			}
			if (in_array('UE_recomLevel',$indexArr)){
				$newStr .= '\nUE_recomLevel 索引已存在';
			}else{
				$newStr .= '\nUE_recomLevel 索引正在创建...';
				$DB->query('alter table '. OT_dbPref .'users add index UE_recomLevel(UE_recomLevel)');
			}
			if (in_array('UE_recomTime',$indexArr)){
				$newStr .= '\nUE_recomTime 索引已存在';
			}else{
				$newStr .= '\nUE_recomTime 索引正在创建...';
				$DB->query('alter table '. OT_dbPref .'users add index UE_recomTime(UE_recomTime)');
			}
			if (in_array('UE_state',$indexArr)){
				$newStr .= '\nUE_state 索引已存在';
			}else{
				$newStr .= '\nUE_state 索引正在创建...';
				$DB->query('alter table '. OT_dbPref .'users add index UE_state(UE_state)');
			}
			if (in_array('UE_mail',$indexArr)){
				$newStr .= '\nUE_mail 索引已存在';
			}else{
				$newStr .= '\nUE_mail 索引正在创建...';
				$DB->query('alter table '. OT_dbPref .'users add index UE_mail(UE_mail)');
			}
			if (in_array('UE_phone',$indexArr)){
				$newStr .= '\nUE_phone 索引已存在';
			}else{
				$newStr .= '\nUE_phone 索引正在创建...';
				$DB->query('alter table '. OT_dbPref .'users add index UE_phone(UE_phone)');
			}
			if (in_array('UE_groupID',$indexArr)){
				$newStr .= '\nUE_groupID 索引已存在';
			}else{
				$newStr .= '\nUE_groupID 索引正在创建...';
				$DB->query('alter table '. OT_dbPref .'users add index UE_groupID(UE_groupID)');
			}
			if (in_array('UE_weixinID',$indexArr)){
				$newStr .= '\nUE_weixinID 索引已存在';
			}else{
				$newStr .= '\nUE_weixinID 索引正在创建...';
				$DB->query('alter table '. OT_dbPref .'users add index UE_weixinID(UE_weixinID)');
			}
			break;

		case 'index_moneyPay':
			// 修复在线支付表(OT_moneyPay)索引
			if (! AppMoneyPay::Jud()){
				JS::AlertBackEnd('该表需要已购买【财务基础包】插件才能用。');
			}
			$indexArr = array();
			$mtexe = $DB->query('SHOW INDEX FROM '. OT_dbPref .'moneyPay WHERE Seq_in_index=1');
			while ($row = $mtexe->fetch()){
				$indexArr[] = $row['Key_name'];
			}
			if (in_array('MP_time',$indexArr)){
				$newStr .= 'MP_time 索引已存在';
			}else{
				$newStr .= 'MP_time 索引正在创建...';
				$DB->query('alter table '. OT_dbPref .'moneyPay add index MP_time(MP_time)');
			}
			if (in_array('MP_userID',$indexArr)){
				$newStr .= '\nMP_userID 索引已存在';
			}else{
				$newStr .= '\nMP_userID 索引正在创建...';
				$DB->query('alter table '. OT_dbPref .'moneyPay add index MP_userID(MP_userID)');
			}
			if (in_array('MP_state',$indexArr)){
				$newStr .= '\nMP_state 索引已存在';
			}else{
				$newStr .= '\nMP_state 索引正在创建...';
				$DB->query('alter table '. OT_dbPref .'moneyPay add index MP_state(MP_state)');
			}
			if (in_array('MP_orderID',$indexArr)){
				$newStr .= '\nMP_orderID 索引已存在';
			}else{
				$newStr .= '\nMP_orderID 索引正在创建...';
				$DB->query('alter table '. OT_dbPref .'moneyPay add index MP_orderID(MP_orderID)');
			}
			if (in_array('MP_type',$indexArr)){
				$newStr .= '\nMP_type 索引已存在';
			}else{
				$newStr .= '\nMP_type 索引正在创建...';
				$DB->query('alter table '. OT_dbPref .'moneyPay add index MP_type(MP_type)');
			}
			if (in_array('MP_type2',$indexArr)){
				$newStr .= '\nMP_type2 索引已存在';
			}else{
				$newStr .= '\nMP_type2 索引正在创建...';
				$DB->query('alter table '. OT_dbPref .'moneyPay add index MP_type2(MP_type2)');
			}
			break;

		case 'defValue':
			/*
			$DB->query('ALTER TABLE '. OT_dbPref .'ad ALTER COLUMN AD_startTime SET DEFAULT "2008-08-08 08:08:08"');
			$DB->query('ALTER TABLE '. OT_dbPref .'ad ALTER COLUMN AD_endTime SET DEFAULT "2008-08-08 08:08:08"');
			$DB->query('ALTER TABLE '. OT_dbPref .'appSys ALTER COLUMN AS_gainTime SET DEFAULT "2008-08-08 08:08:08"');
			$DB->query('ALTER TABLE '. OT_dbPref .'autoRunSys ALTER COLUMN ARS_htmlHomeTime SET DEFAULT "2008-08-08 08:08:08"');
			$DB->query('ALTER TABLE '. OT_dbPref .'autoRunSys ALTER COLUMN ARS_htmlHomeWapTime SET DEFAULT "2008-08-08 08:08:08"');
			$DB->query('ALTER TABLE '. OT_dbPref .'autoRunSys ALTER COLUMN ARS_htmlListTime SET DEFAULT "2008-08-08 08:08:08"');
			$DB->query('ALTER TABLE '. OT_dbPref .'autoRunSys ALTER COLUMN ARS_htmlListWapTime SET DEFAULT "2008-08-08 08:08:08"');
			$DB->query('ALTER TABLE '. OT_dbPref .'autoRunSys ALTER COLUMN ARS_announTime SET DEFAULT "2008-08-08 08:08:08"');
			$DB->query('ALTER TABLE '. OT_dbPref .'autoRunSys ALTER COLUMN ARS_announWapTime SET DEFAULT "2008-08-08 08:08:08"');
			$DB->query('ALTER TABLE '. OT_dbPref .'autoRunSys ALTER COLUMN ARS_newTime SET DEFAULT "2008-08-08 08:08:08"');
			$DB->query('ALTER TABLE '. OT_dbPref .'autoRunSys ALTER COLUMN ARS_newWapTime SET DEFAULT "2008-08-08 08:08:08"');
			$DB->query('ALTER TABLE '. OT_dbPref .'autoRunSys ALTER COLUMN ARS_htmlShowTime SET DEFAULT "2008-08-08 08:08:08"');
			$DB->query('ALTER TABLE '. OT_dbPref .'autoRunSys ALTER COLUMN ARS_htmlShowWapTime SET DEFAULT "2008-08-08 08:08:08"');
			$DB->query('ALTER TABLE '. OT_dbPref .'autoRunSys ALTER COLUMN ARS_htmlShowStartTime SET DEFAULT "2008-08-08 08:08:08"');
			$DB->query('ALTER TABLE '. OT_dbPref .'autoRunSys ALTER COLUMN ARS_collTime SET DEFAULT "2008-08-08 08:08:08"');
			$DB->query('ALTER TABLE '. OT_dbPref .'autoRunSys ALTER COLUMN ARS_timeRunTime SET DEFAULT "2008-08-08 08:08:08"');
			$DB->query('ALTER TABLE '. OT_dbPref .'autoRunSys ALTER COLUMN ARS_softBakTime SET DEFAULT "2008-08-08 08:08:08"');
			$DB->query('ALTER TABLE '. OT_dbPref .'autoRunSys ALTER COLUMN ARS_dbBakTime SET DEFAULT "2008-08-08 08:08:08"');
			$DB->query('ALTER TABLE '. OT_dbPref .'autoRunSys ALTER COLUMN ARS_dayRunTime SET DEFAULT "2008-08-08 08:08:08"');
			$DB->query('ALTER TABLE '. OT_dbPref .'autoRunSys ALTER COLUMN ARS_apiItemTime SET DEFAULT "2008-08-08 08:08:08"');
			$DB->query('ALTER TABLE '. OT_dbPref .'autoRunSys ALTER COLUMN ARS_bigItemTime SET DEFAULT "2008-08-08 08:08:08"');
			$DB->query('ALTER TABLE '. OT_dbPref .'autoRunSys ALTER COLUMN ARS_prevNextTime SET DEFAULT "2008-08-08 08:08:08"');
			$DB->query('ALTER TABLE '. OT_dbPref .'autoRunSys ALTER COLUMN ARS_markNewsTime SET DEFAULT "2008-08-08 08:08:08"');
			$DB->query('ALTER TABLE '. OT_dbPref .'info ALTER COLUMN IF_htmlTime SET DEFAULT "2008-08-08 08:08:08"');
			$DB->query('ALTER TABLE '. OT_dbPref .'info ALTER COLUMN IF_htmlWapTime SET DEFAULT "2008-08-08 08:08:08"');
			$DB->query('ALTER TABLE '. OT_dbPref .'info ALTER COLUMN IF_resTime SET DEFAULT "2008-08-08 08:08:08"');
			$DB->query('ALTER TABLE '. OT_dbPref .'info ALTER COLUMN IF_apiTime SET DEFAULT "2008-08-08 08:08:08"');
			$DB->query('ALTER TABLE '. OT_dbPref .'info ALTER COLUMN IF_markNewsTime SET DEFAULT "2008-08-08 08:08:08"');
			$DB->query('ALTER TABLE '. OT_dbPref .'info ALTER COLUMN IF_prevNextTime SET DEFAULT "2008-08-08 08:08:08"');
			$DB->query('ALTER TABLE '. OT_dbPref .'infoMessage ALTER COLUMN IM_replyTime SET DEFAULT "2008-08-08 08:08:08"');
			$DB->query('ALTER TABLE '. OT_dbPref .'infoSys ALTER COLUMN IS_tabCheckTime SET DEFAULT "2008-08-08 08:08:08"');
			$DB->query('ALTER TABLE '. OT_dbPref .'infoType ALTER COLUMN IT_htmlTime SET DEFAULT "2008-08-08 08:08:08"');
			$DB->query('ALTER TABLE '. OT_dbPref .'infoType ALTER COLUMN IT_htmlWapTime SET DEFAULT "2008-08-08 08:08:08"');
			$DB->query('ALTER TABLE '. OT_dbPref .'infoWeb ALTER COLUMN IW_htmlTime SET DEFAULT "2008-08-08 08:08:08"');
			$DB->query('ALTER TABLE '. OT_dbPref .'infoWeb ALTER COLUMN IW_htmlWapTime SET DEFAULT "2008-08-08 08:08:08"');
			$DB->query('ALTER TABLE '. OT_dbPref .'makeDiy ALTER COLUMN MD_htmlTime SET DEFAULT "2008-08-08 08:08:08"');
			$DB->query('ALTER TABLE '. OT_dbPref .'makeDiy ALTER COLUMN MD_htmlWapTime SET DEFAULT "2008-08-08 08:08:08"');
			$DB->query('ALTER TABLE '. OT_dbPref .'message ALTER COLUMN MA_replyTime SET DEFAULT "2008-08-08 08:08:08"');
			$DB->query('ALTER TABLE '. OT_dbPref .'paySoft ALTER COLUMN PS_lastTime SET DEFAULT "2008-08-08 08:08:08"');
			$DB->query('ALTER TABLE '. OT_dbPref .'qrcode ALTER COLUMN QC_loginTime SET DEFAULT "2008-08-08 08:08:08"');
			$DB->query('ALTER TABLE '. OT_dbPref .'sysAdmin ALTER COLUMN SA_softLastTime SET DEFAULT "2008-08-08 08:08:08"');
			$DB->query('ALTER TABLE '. OT_dbPref .'sysAdmin ALTER COLUMN SA_softVerLastTime SET DEFAULT "2008-08-08 08:08:08"');
			$DB->query('ALTER TABLE '. OT_dbPref .'system ALTER COLUMN SYS_htmlCacheTime SET DEFAULT "2008-08-08 08:08:08"');
			$DB->query('ALTER TABLE '. OT_dbPref .'upFile ALTER COLUMN UF_chkTime SET DEFAULT "2008-08-08 08:08:08"');
			$DB->query('ALTER TABLE '. OT_dbPref .'users ALTER COLUMN UE_loginTime SET DEFAULT "2008-08-08 08:08:08"');
			$DB->query('ALTER TABLE '. OT_dbPref .'users ALTER COLUMN UE_qiandaoTime SET DEFAULT "2008-08-08 08:08:08"');
			$DB->query('ALTER TABLE '. OT_dbPref .'users ALTER COLUMN UE_groupTime SET DEFAULT "2008-08-08 08:08:08"');
			$DB->query('ALTER TABLE '. OT_dbPref .'users ALTER COLUMN UE_weixinTime SET DEFAULT "2008-08-08 08:08:08"');
			$DB->query('ALTER TABLE '. OT_dbPref .'users ALTER COLUMN UE_workTime SET DEFAULT "2008-08-08 08:08:08"');
			$DB->query('ALTER TABLE '. OT_dbPref .'users ALTER COLUMN UE_recomTime SET DEFAULT "2008-08-08 08:08:08"');

			$DB->query('ALTER TABLE '. OT_dbPref .'collItem ALTER COLUMN CI_runTime SET DEFAULT "2008-08-08 08:08:08"');
			$DB->query('ALTER TABLE '. OT_dbPref .'collItem ALTER COLUMN CI_importTime SET DEFAULT "2008-08-08 08:08:08"');
			$DB->query('ALTER TABLE '. OT_dbPref .'collItem ALTER COLUMN CI_exportTime SET DEFAULT "2008-08-08 08:08:08"');
			$DB->query('ALTER TABLE '. OT_dbPref .'collResult ALTER COLUMN CR_collTime SET DEFAULT "2008-08-08 08:08:08"');
			*/

			$itemArr = array();
			$itemArr[] = array('tab' => 'ad',			'field' => 'AD_startTime',			'type' => 'datetime');
			$itemArr[] = array('tab' => 'ad',			'field' => 'AD_endTime',			'type' => 'datetime');
			$itemArr[] = array('tab' => 'appSys',		'field' => 'AS_gainTime',			'type' => 'datetime');
			$itemArr[] = array('tab' => 'autoRunSys',	'field' => 'ARS_dayDate',			'type' => 'date');
			$itemArr[] = array('tab' => 'autoRunSys',	'field' => 'ARS_htmlHomeTime',		'type' => 'datetime');
			$itemArr[] = array('tab' => 'autoRunSys',	'field' => 'ARS_htmlHomeWapTime',	'type' => 'datetime');
			$itemArr[] = array('tab' => 'autoRunSys',	'field' => 'ARS_htmlListTime',		'type' => 'datetime');
			$itemArr[] = array('tab' => 'autoRunSys',	'field' => 'ARS_htmlListWapTime',	'type' => 'datetime');
			$itemArr[] = array('tab' => 'autoRunSys',	'field' => 'ARS_announTime',		'type' => 'datetime');
			$itemArr[] = array('tab' => 'autoRunSys',	'field' => 'ARS_announWapTime',		'type' => 'datetime');
			$itemArr[] = array('tab' => 'autoRunSys',	'field' => 'ARS_newTime',			'type' => 'datetime');
			$itemArr[] = array('tab' => 'autoRunSys',	'field' => 'ARS_newWapTime',		'type' => 'datetime');
			$itemArr[] = array('tab' => 'autoRunSys',	'field' => 'ARS_htmlShowTime',		'type' => 'datetime');
			$itemArr[] = array('tab' => 'autoRunSys',	'field' => 'ARS_htmlShowWapTime',	'type' => 'datetime');
			$itemArr[] = array('tab' => 'autoRunSys',	'field' => 'ARS_htmlShowStartTime',	'type' => 'datetime');
			$itemArr[] = array('tab' => 'autoRunSys',	'field' => 'ARS_collTime',			'type' => 'datetime');
			$itemArr[] = array('tab' => 'autoRunSys',	'field' => 'ARS_timeRunTime',		'type' => 'datetime');
			$itemArr[] = array('tab' => 'autoRunSys',	'field' => 'ARS_softBakTime',		'type' => 'datetime');
			$itemArr[] = array('tab' => 'autoRunSys',	'field' => 'ARS_dbBakTime',			'type' => 'datetime');
			$itemArr[] = array('tab' => 'autoRunSys',	'field' => 'ARS_dayRunTime',		'type' => 'datetime');
			$itemArr[] = array('tab' => 'autoRunSys',	'field' => 'ARS_apiItemTime',		'type' => 'datetime');
			$itemArr[] = array('tab' => 'autoRunSys',	'field' => 'ARS_bigItemTime',		'type' => 'datetime');
			$itemArr[] = array('tab' => 'autoRunSys',	'field' => 'ARS_prevNextTime',		'type' => 'datetime');
			$itemArr[] = array('tab' => 'autoRunSys',	'field' => 'ARS_markNewsTime',		'type' => 'datetime');
			$itemArr[] = array('tab' => 'info',			'field' => 'IF_htmlTime',			'type' => 'datetime');
			$itemArr[] = array('tab' => 'info',			'field' => 'IF_htmlWapTime',		'type' => 'datetime');
			$itemArr[] = array('tab' => 'info',			'field' => 'IF_resTime',			'type' => 'datetime');
			$itemArr[] = array('tab' => 'info',			'field' => 'IF_apiTime',			'type' => 'datetime');
			$itemArr[] = array('tab' => 'info',			'field' => 'IF_markNewsTime',		'type' => 'datetime');
			$itemArr[] = array('tab' => 'info',			'field' => 'IF_prevNextTime',		'type' => 'datetime');
			$itemArr[] = array('tab' => 'infoMessage',	'field' => 'IM_replyTime',			'type' => 'datetime');
			$itemArr[] = array('tab' => 'infoMove',		'field' => 'IM_startDate',			'type' => 'date');
			$itemArr[] = array('tab' => 'infoMove',		'field' => 'IM_endDate',			'type' => 'date');
			$itemArr[] = array('tab' => 'infoSys',		'field' => 'IS_tabCheckTime',		'type' => 'datetime');
			$itemArr[] = array('tab' => 'infoSys',		'field' => 'IS_readNumDayDate',		'type' => 'date');
			$itemArr[] = array('tab' => 'infoSys',		'field' => 'IS_readNumWeekDate',	'type' => 'date');
			$itemArr[] = array('tab' => 'infoSys',		'field' => 'IS_readNumMonthDate',	'type' => 'date');
			$itemArr[] = array('tab' => 'infoSys',		'field' => 'IS_readNumYearDate',	'type' => 'date');
			$itemArr[] = array('tab' => 'infoType',		'field' => 'IT_htmlTime',			'type' => 'datetime');
			$itemArr[] = array('tab' => 'infoType',		'field' => 'IT_htmlWapTime',		'type' => 'datetime');
			$itemArr[] = array('tab' => 'infoWeb',		'field' => 'IW_htmlTime',			'type' => 'datetime');
			$itemArr[] = array('tab' => 'infoWeb',		'field' => 'IW_htmlWapTime',		'type' => 'datetime');
			$itemArr[] = array('tab' => 'makeDiy',		'field' => 'MD_htmlTime',			'type' => 'datetime');
			$itemArr[] = array('tab' => 'makeDiy',		'field' => 'MD_htmlWapTime',		'type' => 'datetime');
			$itemArr[] = array('tab' => 'message',		'field' => 'MA_replyTime',			'type' => 'datetime');
			$itemArr[] = array('tab' => 'paySoft',		'field' => 'PS_lastTime',			'type' => 'datetime');
			$itemArr[] = array('tab' => 'paySoft',		'field' => 'PS_buyDate',			'type' => 'date');
			$itemArr[] = array('tab' => 'paySoft',		'field' => 'PS_useDate',			'type' => 'date');
			$itemArr[] = array('tab' => 'paySoft',		'field' => 'PS_updateDate',			'type' => 'date');
			$itemArr[] = array('tab' => 'qrcode',		'field' => 'QC_loginTime',			'type' => 'datetime');
			$itemArr[] = array('tab' => 'sysAdmin',		'field' => 'SA_softLastTime',		'type' => 'datetime');
			$itemArr[] = array('tab' => 'sysAdmin',		'field' => 'SA_softVerLastTime',	'type' => 'datetime');
			$itemArr[] = array('tab' => 'sysAdmin',		'field' => 'SA_softStart',			'type' => 'date');
			$itemArr[] = array('tab' => 'sysAdmin',		'field' => 'SA_softEnd',			'type' => 'date');
			$itemArr[] = array('tab' => 'sysAdmin',		'field' => 'SA_backupCallTime',		'type' => 'date');
			$itemArr[] = array('tab' => 'system',		'field' => 'SYS_htmlCacheTime',		'type' => 'datetime');
			$itemArr[] = array('tab' => 'upFile',		'field' => 'UF_chkTime',			'type' => 'datetime');
			$itemArr[] = array('tab' => 'users',		'field' => 'UE_loginTime',			'type' => 'datetime');
			$itemArr[] = array('tab' => 'users',		'field' => 'UE_qiandaoTime',		'type' => 'datetime');
			$itemArr[] = array('tab' => 'users',		'field' => 'UE_groupTime',			'type' => 'datetime');
			$itemArr[] = array('tab' => 'users',		'field' => 'UE_weixinTime',			'type' => 'datetime');
			$itemArr[] = array('tab' => 'users',		'field' => 'UE_workTime',			'type' => 'datetime');
			$itemArr[] = array('tab' => 'users',		'field' => 'UE_recomTime',			'type' => 'datetime');

			$itemArr[] = array('tab' => 'collItem',		'field' => 'CI_runTime',			'type' => 'datetime');
			$itemArr[] = array('tab' => 'collItem',		'field' => 'CI_importTime',			'type' => 'datetime');
			$itemArr[] = array('tab' => 'collItem',		'field' => 'CI_exportTime',			'type' => 'datetime');
			$itemArr[] = array('tab' => 'collResult',	'field' => 'CR_collTime',			'type' => 'datetime');

			foreach ($itemArr as $val){
				$retStr = SetFieldVal($val['tab'], $val['field'], $val['type']);
				// $newStr .= '\n'. $retStr;
			}
			break;

		case 'content_utf8':
			// 内容字段编码改为utf8
			$fieldArr = GetFieldArr();
			foreach ($fieldArr as $val){
				$charset = $DB->GetOne('SELECT CHARACTER_SET_NAME FROM information_schema.`COLUMNS` WHERE TABLE_SCHEMA="'. $dbName .'" AND TABLE_NAME="'. OT_dbPref . $val['tab'] .'" AND COLUMN_NAME="'. $val['field'] .'"');
				if ($charset != 'utf8'){
					$newStr .= '\n表：'. $val['tab'] .'，字段：'. $val['field'] .' 编码utf8正在更改中...';
					$DB->query('ALTER TABLE `'. OT_dbPref . $val['tab'] .'` MODIFY COLUMN `'. $val['field'] .'` '. $val['type'] .' CHARACTER SET utf8 COLLATE utf8_general_ci');
				}else{
					$newStr .= '\n表：'. $val['tab'] .'，字段：'. $val['field'] .' 已经是编码utf8';
				}
			}
			break;

		case 'content_utf8mb4':
			// 内容字段编码改为utf8mb4
			$fieldArr = GetFieldArr();
			foreach ($fieldArr as $val){
				$charset = $DB->GetOne('SELECT CHARACTER_SET_NAME FROM information_schema.`COLUMNS` WHERE TABLE_SCHEMA="'. $dbName .'" AND TABLE_NAME="'. OT_dbPref . $val['tab'] .'" AND COLUMN_NAME="'. $val['field'] .'"');
				if ($charset != 'utf8mb4'){
					$newStr .= '\n表('. $charset .')：'. $val['tab'] .'，字段：'. $val['field'] .' 编码utf8mb4正在更改中...';
					$DB->query('ALTER TABLE `'. OT_dbPref . $val['tab'] .'` MODIFY COLUMN `'. $val['field'] .'` '. $val['type'] .' CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci');
				}else{
					$newStr .= '\n表：'. $val['tab'] .'，字段：'. $val['field'] .' 已经是编码utf8mb4';
				}
			}
			break;

		default :
			JS::AlertBackEnd('参数不对。');	// （'. $ver .'）
			break;
	}

	if ($ver == 'defValue'){
		die('
		<div style="padding:10px 0;">运行完成，点击<a href="'. $backURL .'" style="color:red;">【返回】</a></div>
		<script language="javascript" type="text/javascript">
		WindowHeight(0);
		</script>
		');
	}else{
		JS::AlertHrefEnd('运行完成，如果还是有问题，请先备份下数据库，然后压缩下数据库，再进行该操作。\n\n'. $newStr, $backURL);
	}
}

// 编码改为utf8/utf8mb4字段数组
function GetFieldArr(){
	global $DB;
	$fieldArr = array();
	$fieldArr[] = array('tab' => 'infoWeb',	'field' => 'IW_content',	'type' => 'longtext');
	$fieldArr[] = array('tab' => 'infoWeb',	'field' => 'IW_contentWap',	'type' => 'longtext');
	$fieldArr[] = array('tab' => 'info',	'field' => 'IF_theme',		'type' => 'varchar(255)');
	$fieldArr[] = array('tab' => 'info',	'field' => 'IF_contentKey',	'type' => 'text');
	$fieldArr[] = array('tab' => 'info',	'field' => 'IF_content',	'type' => 'longtext');
	$fieldArr[] = array('tab' => 'info',	'field' => 'IF_encContent',	'type' => 'longtext');
	$fieldArr[] = array('tab' => 'dbErr',	'field' => 'DE_note',		'type' => 'longtext');
	$fieldArr[] = array('tab' => 'dbErr',	'field' => 'DE_content',	'type' => 'longtext');

	$IS_tabNum = $DB->GetOne('select IS_tabNum from '. OT_dbPref .'infoSys');
	for ($i=1; $i<=$IS_tabNum; $i++){
		$fieldArr[] = array('tab' => 'infoContent'. $i, 'field' => 'IC_content',	'type' => 'longtext');
	}
	return $fieldArr;
}

// 修复字段默认值
function SetFieldVal($tab, $field, $type='datetime', $def=''){
	global $DB,$dbName;
	$crow = $DB->GetRow("select COLUMN_NAME from information_schema.COLUMNS where TABLE_SCHEMA='". $dbName ."' and TABLE_NAME='". OT_dbPref . $tab ."' and COLUMN_NAME='". $field ."'");
	if (! $crow){
		$retStr = '表：'. $tab .'，字段：'. $field .'，不存在';
		echo('<div style="color:red;">'. $retStr .'</div>');
		return $retStr;
	}else{
		$retStr = '表：'. $tab .'，字段：'. $field .'，处理中...';
		echo('<div style="color:blue;">'. $retStr .'</div>');
	}

	if (empty($def)){
		switch ($type){
			case 'datetime':	$def = '2008-08-08 08:08:08';	break;
			case 'date':		$def = '2008-08-08';	break;
			default :			die('SetFieldVal 类型：'. $type);
		}
		
	}
	global $DB;

	$DB->query('ALTER TABLE '. OT_dbPref . $tab .' ALTER COLUMN '. $field .' SET DEFAULT "'. $def .'"');

	// $val = $DB->GetOne('select '. $field .' from '. OT_dbPref . $tab .' limit 1');
	if ($type == 'datetime' || $type == 'date'){
		if ($type == 'date'){ $timeVal = '0000-00-00'; }else{ $timeVal = '0000-00-00 00:00:00'; }
		// if (empty($val) || $val == $timeVal){
			// echo('<div style="color:blue;">SQL：执行中...</div>');
			$DB->query('update '. OT_dbPref . $tab .' set '. $field .'='. $DB->ForTime($def) .' where '. $field .' is null or '. $field .'="'. $timeVal .'" or '. $field .'="0"');

			$tmpArr = explode('_',$field);
			$idField = $tmpArr[0] .'_ID';
			$cexe = $DB->query('select '. $idField .','. $field .' from '. OT_dbPref . $tab .' where '. $field .'<="1990-01-01" order by '. $field .' ASC');
			while ($crow = $cexe->fetch()){
				$DB->query('update '. OT_dbPref . $tab .' set '. $field .'='. $DB->ForTime($def) .' where '. $idField .'='. $crow[$idField]);
			}
			$cexe = null;
		// }
	}
	return $retStr;
}
?>