<?php
class netssysdbInfo extends netsDb {
	private $sysErrorClassID='0000';
	private $sysErrorFuncID=0;
	private $sysErrorID=0;
	
	public function netssysdbInfo($pConnection){
		$this->netsDb($pConnection);
	}
	public function Check(&$pTableDescriptAR,$pExceptionAR1,$pExceptionAR2){
		$this->sysErrorFuncID=$this->sysErrorClassID.'02';

		if(count($pExceptionAR1)==0){
			$pExceptionAR1=array();
		}
		parent::Check($pTableDescriptAR,$pExceptionAR1,$pExceptionAR2);
		//
		$tmpSTR='sditName';
		if(strlen($pTableDescriptAR[0]['Columns'][$tmpSTR]['Value'])<1 && !in_array($tmpSTR,$pExceptionAR2)){
			$this->sysLastError='提交内容错误:1.名称不能为空';
			return $this->sysErrorFuncID.'001';
		}
		
		return 0;
	}
	
	public function onBackup_Getalltable(&$pDescriptions,&$pTables){
		$this->sysErrorFuncID=$this->sysErrorClassID.'02';
		$tmpCONTENT='';
		//-- ------ ### ------ --
		$tmpSQL=' SHOW TABLE STATUS FROM {Nets.DBNAME}; ';
		$tmpSTR=str_replace('{Nets.DBNAME}',$this->sysMySQLDBName,$tmpSQL);
		$tmpData0=$this->Search($tmpSTR);
		$Coun0=0;
		foreach ($tmpData0 as $Row0){
			
			if ($Coun0<7) {
			
			
			
			if ($Coun0>0) {
				$tmpCONTENT.=$this->sysSPLIT2;
			}
			$tmpCONTENT.=$Row0['Name'];
			
			
			}
			
			$Coun0++;
		}
		//-- ------ #Add ------ --
		$this->TransStart();
		//#Get numCode
		$tmpSTR='sditNumCode';
		$pDescriptions[0]['TableDescription']['SearchColumn']=$tmpSTR;
		$pDescriptions[0]['Columns'][$tmpSTR]['Value']='N'.$this->sysSPLIT11.date('YmdHis');
		$this->numCode($pDescriptions[0]);

		//-- #SET --
		//#Other
		$pDescriptions[0]['Columns']['sditName']['Value']=$pDescriptions[0]['Columns']['sditNumCode']['Value'];
		$pDescriptions[0]['Columns']['sditPath']['Value']=$pDescriptions[0]['Columns']['sditNumCode']['Value'].'/';
		$pDescriptions[0]['Columns']['sditChecked']['Value']=2;
		$pDescriptions[0]['Columns']['sditCreateTime']['Value']=date($this->sysTimeFormat);
		//-- #Add --
		$pDescriptions[0]['TableDescription']['SearchColumn']='sditNumCode,sditID';
		$pDescriptions[1]['TableDescription']['SearchColumn']='sdictIndexID';
		$tmpINT=$this->add($pDescriptions);
		if($tmpINT!=0){
			$this->TransRollback();
			$this->sysLastError='数据库备份记录错误，请客服人员联系。';
			return $tmpINT;
		}

		
		$pTables=$tmpCONTENT;
		$this->TransCommit();
		return 0;
	}
	public function onBackup_Tablestruct($pNumCode,$pTablename){
		$this->sysErrorFuncID=$this->sysErrorClassID.'02';
		$tmpCONTENT='';
		//-- ------ ### ------ --
		$tmpSQL=' SELECT A.sditID,A.sditName,A.sditNumCode,A.sditPath
			FROM {Nets.TableHead}netssysdbInfo AS A
			WHERE 1=1 AND A.sditNumCode LIKE \'{Nets.NumCode}\' ';
		$tmpSTR=str_replace('{Nets.TableHead}',$this->sysTableHead,$tmpSQL);
		$tmpSTR=str_replace('{Nets.NumCode}',$pNumCode,$tmpSTR);
		$tmpData_Db=$this->Search($tmpSTR);
		if (count($tmpData_Db)==0) {
			$this->sysLastError='数据库备份记录错误，请客服人员联系。';
			return $this->sysErrorFuncID.'001';
		}
		$tmpPath=$this->sysFileConfig['sysCoreRoot'].
			$this->sysFileConfig['DBBackupPath'].
			$tmpData_Db[0]['sditPath'].'STRUCT/'.$pTablename.'_struct.php';
		//-- ------ ### ------ --
		$tmpSQL=' describe {Nets.Tablename} ';
		$tmpSTR=str_replace('{Nets.Tablename}',$pTablename,$tmpSQL);
		$tmpData0=$this->Search($tmpSTR);
		//-- ------ #Make content ------ --
		$tmpCONTENT='<?php /*--Nets.START--
-- ----------------------------
-- Table structure for `'.$pTablename.'`
-- ----------------------------
DROP TABLE IF EXISTS `'.$pTablename.'`;
CREATE TABLE `'.$pTablename.'` (
';
		foreach ($tmpData0 as $Row0){
		$tmpField=$Row0['Field'];
		$tmpType=$Row0['Type'];
		$tmpNULL='';
		if ($Row0['Null']=='NO') {
			$tmpNULL=' NOT NULL ';
		}
		$tmpDefault=$Row0['Default'];
		if (str_replace('varchar','',$tmpType)!=$tmpType) {
			$tmpDefault=' default \''.$tmpDefault.'\'';
		}else {
			$tmpDefault=' default '.$tmpDefault.'';
		}
		if ($tmpDefault==' default ') {
			$tmpDefault='';
		}
		$tmpExtra=$Row0['Extra'];
		
		$tmpCONTENT.='
`'.$tmpField.'` '.$tmpType.' '.$tmpNULL.' '.$tmpDefault.' '.$tmpExtra.',';
		}
		foreach ($tmpData0 as $Row0){
			if ($Row0['Key']!='' && $Row0['Extra']!='') {
			$tmpField=$Row0['Field'];
			$tmpCONTENT.='
PRIMARY KEY  (`'.$tmpField.'`)

';
			}
		}
		$tmpCONTENT.='
		) ENGINE=InnoDB DEFAULT CHARSET=utf8;
		
		--Nets.START--*/ ?>';


		//-- ------ #Write ------ --
		$tmpINT=$this->writeFile($tmpPath,$tmpCONTENT);
		return 0;
	}
	public function onBackup_Tabledata($pNumCode,$pTablename,&$pPageinfo){
		$this->sysErrorFuncID=$this->sysErrorClassID.'02';
		$tmpCONTENT='';
		//-- ------ ### ------ --
		$tmpSQL=' SELECT A.sditID,A.sditName,A.sditNumCode,A.sditPath
			FROM {Nets.TableHead}netssysdbInfo AS A
			WHERE 1=1 AND A.sditNumCode LIKE \'{Nets.NumCode}\' ';
		$tmpSTR=str_replace('{Nets.TableHead}',$this->sysTableHead,$tmpSQL);
		$tmpSTR=str_replace('{Nets.NumCode}',$pNumCode,$tmpSTR);
		$tmpData_Db=$this->Search($tmpSTR);
		if (count($tmpData_Db)==0) {
			$this->sysLastError='数据库备份记录错误，请客服人员联系。';
			return $this->sysErrorFuncID.'001';
		}
		//-- ------ ### ------ --
		$tmpSQL=' SELECT * FROM {Nets.Tablename} ';
		$tmpSTR=str_replace('{Nets.Tablename}',$pTablename,$tmpSQL);
		$tmpData0=$this->SearchP($tmpSTR,$pPageinfo);
		$pPageinfo=$this->sysPageInfo;
		
		$tmpPath=$this->sysFileConfig['sysCoreRoot'].
			$this->sysFileConfig['DBBackupPath'].
			$tmpData_Db[0]['sditPath'].'DATA/'.$pTablename.'_data_'.$pPageinfo[0].'.php';
		//-- ------ #Make content ------ --
		$tmpCONTENT='<?php /*--Nets.START--
';
		foreach ($tmpData0 as $Row0){
			$tmpCONTENT.='INSERT INTO `'.$pTablename.'` VALUES (';
			$Coun1=0;
			foreach ($Row0 as $Row1){
				if ($Coun1>0) {
					$tmpCONTENT.=$this->sysSPLIT1;
				}
				$tmpCONTENT.=' \''.$Row1.'\' ';
				$Coun1++;
			}
			$tmpCONTENT.=');
';
		}
		$tmpCONTENT.='

--Nets.START--*/ ?>';

		//-- ------ #Write ------ --
		$tmpINT=$this->writeFile($tmpPath,$tmpCONTENT);
		return 0;
	}
	public function onRestore_Getallstruct($pNumCode,&$pTablesname){
		$this->sysErrorFuncID=$this->sysErrorClassID.'02';
		$tmpCONTENT='';
		//-- ------ #GET ------ --
		$tmpSQL=' SELECT A.sditID,A.sditName,A.sditNumCode,A.sditPath
			FROM {Nets.TableHead}netssysdbInfo AS A
			WHERE 1=1 AND A.sditNumCode LIKE \'{Nets.NumCode}\' ';
		$tmpSTR=str_replace('{Nets.TableHead}',$this->sysTableHead,$tmpSQL);
		$tmpSTR=str_replace('{Nets.NumCode}',$pNumCode,$tmpSTR);
		$tmpData_Db=$this->Search($tmpSTR);
		if (count($tmpData_Db)==0) {
			$this->sysLastError='数据库备份记录不存在，请客服人员联系。';
			return $this->sysErrorFuncID.'001';
		}
		$tmpPath=$this->sysFileConfig['sysCoreRoot'].
			$this->sysFileConfig['DBBackupPath'].
			$tmpData_Db[0]['sditPath'].'STRUCT/';
		//-- ------ ### ------ --
		$tmpFileType=array('php',);
		$tmpFileName=array('File.php',);
		$tmpHandle=opendir($tmpPath);
		if($tmpHandle){
			$Coun0=0;
			while (($tmpFile=readdir($tmpHandle))!=false) {
				$tmpSTR=$this->getFileExname($tmpFile);
				if(in_array($tmpSTR,$tmpFileType) && !in_array($tmpFile,$tmpFileName)){
					if ($Coun0>0) {
						$tmpCONTENT.=$this->sysSPLIT2;
					}
					$tmpCONTENT.=$tmpFile;
					$Coun0++;
				}
			}
			closedir($tmpHandle);
		}else {
			$this->sysLastError='数据库备份记录不存在，请客服人员联系。';
			return $this->sysErrorFuncID.'001';
		}
		
		
		$pTablesname=$tmpCONTENT;
		return 0;
	}
	public function onRestore_Getstruct($pNumCode,$pTablename){
		$this->sysErrorFuncID=$this->sysErrorClassID.'02';
		$tmpCONTENT='';
		//-- ------ #GET ------ --
		$tmpSQL=' SELECT A.sditID,A.sditName,A.sditNumCode,A.sditPath
			FROM {Nets.TableHead}netssysdbInfo AS A
			WHERE 1=1 AND A.sditNumCode LIKE \'{Nets.NumCode}\' ';
		$tmpSTR=str_replace('{Nets.TableHead}',$this->sysTableHead,$tmpSQL);
		$tmpSTR=str_replace('{Nets.NumCode}',$pNumCode,$tmpSTR);
		$tmpData_Db=$this->Search($tmpSTR);
		if (count($tmpData_Db)==0) {
			$this->sysLastError='数据库备份记录不存在，请客服人员联系。';
			return $this->sysErrorFuncID.'001';
		}
		$tmpPath=$this->sysFileConfig['sysCoreRoot'].
			$this->sysFileConfig['DBBackupPath'].
			$tmpData_Db[0]['sditPath'].'STRUCT/';
		$tmpCONTENT=$this->readFile($this->getRealname($tmpPath.$pTablename,0));
		//-- ------ #EXECUTE ------ --
		$tmpCONTENT=str_replace('<?php /*--Nets.START--','',$tmpCONTENT);
		$tmpCONTENT=str_replace('--Nets.START--*/ ?>','',$tmpCONTENT);
		
		
		
		
		$tmpINT=$this->Executes($tmpCONTENT,0,0,0);
		if ($tmpINT!=0) {
			$this->sysLastError='数据库还原时出错，请客服人员联系。';
			return $tmpINT;
		}
		
		
		return 0;
	}
	public function onRestore_Getalldata($pNumCode,&$pTablesname){
		$this->sysErrorFuncID=$this->sysErrorClassID.'02';
		$tmpCONTENT='';
		//-- ------ ### ------ --
		$tmpSQL=' SELECT A.sditID,A.sditName,A.sditNumCode,A.sditPath
			FROM {Nets.TableHead}netssysdbInfo AS A
			WHERE 1=1 AND A.sditNumCode LIKE \'{Nets.NumCode}\' ';
		$tmpSTR=str_replace('{Nets.TableHead}',$this->sysTableHead,$tmpSQL);
		$tmpSTR=str_replace('{Nets.NumCode}',$pNumCode,$tmpSTR);
		$tmpData_Db=$this->Search($tmpSTR);
		if (count($tmpData_Db)==0) {
			$this->sysLastError='数据库备份记录不存在，请客服人员联系。';
			return $this->sysErrorFuncID.'001';
		}
		$tmpPath=$this->sysFileConfig['sysCoreRoot'].
			$this->sysFileConfig['DBBackupPath'].
			$tmpData_Db[0]['sditPath'].'DATA/';
		//-- ------ ### ------ --
		$tmpFileType=array('php',);
		$tmpFileName=array('File.php',);
		$tmpHandle=opendir($tmpPath);
		if($tmpHandle){
			$Coun0=0;
			while (($tmpFile=readdir($tmpHandle))!=false) {
				$tmpSTR=$this->getFileExname($tmpFile);
				if(in_array($tmpSTR,$tmpFileType) && !in_array($tmpFile,$tmpFileName)){
					if ($Coun0>0) {
						$tmpCONTENT.=$this->sysSPLIT2;
					}
					$tmpCONTENT.=$tmpFile;
					$Coun0++;
				}
			}
			closedir($tmpHandle);
		}else {
			$this->sysLastError='数据库备份记录不存在，请客服人员联系。';
			return $this->sysErrorFuncID.'001';
		}
		
		
		$pTablesname=$tmpCONTENT;
		return 0;
	}
	public function onRestore_Getdata($pNumCode,$pTablename){
		$this->sysErrorFuncID=$this->sysErrorClassID.'02';
		$tmpCONTENT='';
		//-- ------ #GET ------ --
		$tmpSQL=' SELECT A.sditID,A.sditName,A.sditNumCode,A.sditPath
			FROM {Nets.TableHead}netssysdbInfo AS A
			WHERE 1=1 AND A.sditNumCode LIKE \'{Nets.NumCode}\' ';
		$tmpSTR=str_replace('{Nets.TableHead}',$this->sysTableHead,$tmpSQL);
		$tmpSTR=str_replace('{Nets.NumCode}',$pNumCode,$tmpSTR);
		$tmpData_Db=$this->Search($tmpSTR);
		if (count($tmpData_Db)==0) {
			$this->sysLastError='数据库备份记录不存在，请客服人员联系。';
			return $this->sysErrorFuncID.'001';
		}
		$tmpPath=$this->sysFileConfig['sysCoreRoot'].
			$this->sysFileConfig['DBBackupPath'].
			$tmpData_Db[0]['sditPath'].'DATA/';
		$tmpCONTENT=$this->readFile($this->getRealname($tmpPath.$pTablename,0));
		//-- ------ #EXECUTE ------ --
		$tmpCONTENT=str_replace('<?php /*--Nets.START--','',$tmpCONTENT);
		$tmpCONTENT=str_replace('--Nets.START--*/ ?>','',$tmpCONTENT);
		
		$tmpINT=$this->Executes($tmpCONTENT,0,0,0);
		if ($tmpINT!=0) {
			$this->sysLastError='数据库还原时出错，请客服人员联系。';
			return $tmpINT;
		}
		
		
		return 0;
	}
	
	
}
?>