#include <stdlib.h>
#include <string.h>

#include "mysql.h"

#include "subsDataBackup.h"

/******************************************************/
/******            Heartbeat Parameter           ******/
/******************************************************/
DWORD PROC_HEARTBEAT_OID[12]={1,3,6,1,4,1,1373,2,4,10,7};
int PROC_HEARTBEAT_OIDLEN=11;
int sendHeartbeat();

/* by simon at 23/9/25 */
subsData_Backup subsDataBackup[MaxSysConfNum];

/**************subsdata backup functions declare**********************/
void backupTimer();
void sendCmd(int proc);
void scanCmd(int proc);
void backupFile(int proc);
void debug_log(const char *fmt,...);

/***********subsdata backup global var declare And init*********************/
//static	FILE *subsDataBackupLog;
static	MYSQL *dbConn;

int subsDataBackupFlag=0;
int automatic_scratch_card_time=0;
int automatic_scratch_card_flag=0;
int cmdId=0;
int	oldDayForLog = 0; //1-31
int	oldDayForBackup = 0; //1-31
unsigned long  waitTime;
int waitFlag;
unsigned long  inTime;
int inFlag;
int lastFinishNum=0;
int finishFlag;
unsigned long finishTime;

void subsDataBackupInit()
{
	int i=0;
	char tmpSql[1024];
	MYSQL_RES *tmpResult = NULL;
	MYSQL_ROW tmpRow = NULL;
	
	debug_init(1);
	iptrans_init();
	snmp_init(4957);
	//heartbeat_init(0);
	debug_log("subsDataBackup is starting......");
	dbConn = mysql_conn(HOSTNAME,PUB_DB);
	if(dbConn == NULL){
		debug_log("[ERR][subsDataBackupInit]connect OMC_PUB");
		exitLog("subsDataBackup:subsDataBackupInit, Connect OMC_PUB error");
		exit(1);
	}

	sprintf(tmpSql,
			"SELECT pubVarValue FROM OMC_PUB.omcPubVarConf WHERE pubVarName='automatic_scratch_card_time'");
	tmpResult = (MYSQL_RES *) mysql_getres(dbConn, tmpSql);

	if (tmpResult == NULL || mysql_num_rows(tmpResult) == 0){		
		automatic_scratch_card_time  = 0;
		mysql_free_result(tmpResult);
		debug_log("The automatic_scratch_card_time value is NULL");
	}else {
		tmpRow = mysql_fetch_row(tmpResult);
		automatic_scratch_card_time = atoi(tmpRow[0]);
		mysql_free_result(tmpResult);
	}
	if (automatic_scratch_card_time  < 0 || automatic_scratch_card_time  > 23){
		debug_log("The automatic_scratch_card_time value is %d not in range",automatic_scratch_card_time);
		automatic_scratch_card_time  = 0;
	}
	debug_log("automatic_scratch_card_time=%d",automatic_scratch_card_time);

	sprintf(tmpSql,
			"SELECT pubVarValue FROM OMC_PUB.omcPubVarConf WHERE pubVarName='automatic_scratch_card_flag'");
	tmpResult = (MYSQL_RES *) mysql_getres(dbConn, tmpSql);

	if (tmpResult == NULL || mysql_num_rows(tmpResult) == 0){
		automatic_scratch_card_flag = 0;
		mysql_free_result(tmpResult);
		debug_log("The automatic_scratch_card_flag value is NULL");
	}else {
		tmpRow = mysql_fetch_row(tmpResult);
		automatic_scratch_card_flag = atoi(tmpRow[0]);
		mysql_free_result(tmpResult);
	}
	if (automatic_scratch_card_flag  < 0 || automatic_scratch_card_flag  > 1){
		debug_log("The automatic_scratch_card_time value %d is not in range",automatic_scratch_card_flag);
		automatic_scratch_card_flag = 0;
	}
	debug_log("automatic_scratch_card_flag=%d",automatic_scratch_card_flag);

	sprintf(tmpSql, "SELECT subsDataBackup.sysTypeNo,subsDataBackup.dataTypeNo,subsDataTable,tmpDataTable"\
				" FROM subsDataBackup,sysSubsConf "\
				" WHERE sysSubsConf.sysTypeNo = subsDataBackup.sysTypeNo AND sysSubsConf.dataTypeNo = subsDataBackup.dataTypeNo AND sysSubsConf.isEnable > '0' "\
				" AND subsDataBackup.flag > '0' ORDER BY subsDataBackup.sysTypeNo AND subsDataBackup.dataTypeNo");
	tmpResult = (MYSQL_RES *) mysql_getres(dbConn, tmpSql);

	if (tmpResult == NULL && mysql_num_rows(tmpResult) == 0) {
		subsDataBackupFlag=0;
		mysql_free_result(tmpResult);
		debug_log("No subsdata backup"); 
		return ;
	}
	debug_log("backup server list");
	while((tmpRow=mysql_fetch_row(tmpResult)) > 0){
		subsDataBackup[i].backupStat=nWaitBackup;
		subsDataBackup[i].sysTypeNo=atoi(tmpRow[0]);
		subsDataBackup[i].dataTypeNo=atoi(tmpRow[1]);
		strcpy(subsDataBackup[i].subsDataTable,tmpRow[2]);
		strcpy(subsDataBackup[i].tmpDataTable,tmpRow[3]);
		debug_log("%d,sysTypeNo=%d,dataTypeNo=%d,subsDataTable=%s,tmpDataTable=%s",i,subsDataBackup[i].sysTypeNo,subsDataBackup[i].dataTypeNo,subsDataBackup[i].subsDataTable,subsDataBackup[i].tmpDataTable);
		i++;
	}
	mysql_free_result(tmpResult);

	return;
}

/*************************************************
Function:        // sendHeartbeat
Description:     // Send process heartbeat to beatProc
Calls:           // snmp_send
Called By:       // on_Timer
Table Accessed:  // 
Table Updated:   // 
Input:           // 
Output:          // 
Return:          // 1:OK; 0:Fail
Others:          // 
*************************************************/
int sendHeartbeat()
{
	snmp_pdu pdu;
	snmp_addr addr;
	char proc_name[16];
	
	memset(proc_name,0,16);
	sprintf(proc_name,"subsDataBackup");
	
	//pdu
	memset(&pdu,0,sizeof(snmp_pdu));
	pdu.pdu_type=7;
	pdu.request_id=0;
	sprintf(pdu.community,"public");
	pdu.var_num=1;
	pdu.error_status=0;
	pdu.error_index=0;
	
	//Addr
	addr.local_port = 4957;
	//addr.remote_ip = inet_addr("127.0.0.1");
	addr.remote_ip = GetLocalIP();
	addr.broadcast = 0;
	addr.remote_port = 4957;
	
	//Varlist
	memcpy(pdu.var[0].oid,PROC_HEARTBEAT_OID,PROC_HEARTBEAT_OIDLEN*sizeof(DWORD));

	pdu.var[0].oidlen=PROC_HEARTBEAT_OIDLEN;
	pdu.var[0].vartype=0x04;
	pdu.var[0].msglen=16;
	memcpy(pdu.var[0].msg,proc_name,16);
	
	if(snmp_send(&pdu, &addr) <= 0)
	{
		debug_log("Send heartbeat fail");
		return 0;
	}
	return 1;
}
unsigned long heartbeat_timeout=0;
void subsDataBackupTimer()
{
	long			l_time;
	struct tm		*t;
	
	
	l_time=time(NULL);
	t=localtime(&l_time);

	if(time(NULL) - heartbeat_timeout > 5)
	{
		heartbeat_timeout=time(NULL);
		sendHeartbeat();
	}

	if(t->tm_mday != oldDayForLog){
		//init logfile,create a new log file to restore log
		oldDayForLog = t->tm_mday;
	}

	if(automatic_scratch_card_flag==1&&t->tm_mday != oldDayForBackup&&t->tm_hour==automatic_scratch_card_time){
		debug_log("start to backup process, last backup date is %d",oldDayForBackup);
		subsDataBackupFlag=1;
		oldDayForBackup = t->tm_mday;
	}
	
	//printf("subsDataBackupFlag=%d\n",subsDataBackupFlag);

	if(subsDataBackupFlag==1){
		backupTimer();
	}
	return ;
}
static int start_proc_index=0;
void backupTimer()
{
	int i;
	int proc;
	char tmpSql[1024];
	MYSQL_RES *tmpResult = NULL;
	MYSQL_ROW tmpRow = NULL;

	char tmpLoadoutSql[400];
	char tmpLoadoutFile[100];

	for(proc=start_proc_index;proc<MaxSysConfNum;proc++){
		switch(subsDataBackup[proc].backupStat){
			case nNotBackup:
				break;
			case nWaitBackup:
				/*
				sprintf(tmpSql, "SELECT * FROM %s ",CommTable);

				tmpResult = (MYSQL_RES *) mysql_getres(dbConn, tmpSql);

				if (tmpResult != NULL && (mysql_num_rows(tmpResult) > 0)) {
					mysql_free_result(tmpResult);
					omcLog(subsDataBackupLog,"sysTypeNo=%d,dataTypeNo=%d;The database of subsComm is not NULL",subsDataBackup[proc].sysTypeNo,subsDataBackup[proc].dataTypeNo);
					return ;
				}

				mysql_free_result(tmpResult);
				*/
				
				//check mysql connection
				if(mysql_ping(dbConn))
				{
					debug_log("[ERR][backupTimer]mysql gone away,reconnect");
					mysql_close(dbConn);
					dbConn = mysql_conn(HOSTNAME,PUB_DB);
					if(dbConn == NULL)
					{
						debug_log("[ERR][backupTimer]reconnect mysql OMC_PUB");
					}
				}
				
			
				sprintf(tmpSql,	"SELECT sysNo,subSysNo FROM %s WHERE sysTypeNo =\'%d\' ORDER BY baseStat DESC",IpTable, subsDataBackup[proc].sysTypeNo);
			
				tmpResult = (MYSQL_RES *) mysql_getres(dbConn, tmpSql);

				if (tmpResult == NULL || mysql_num_rows(tmpResult) == 0) {

					mysql_free_result(tmpResult);
					subsDataBackup[proc].backupStat=nOutBackup;
					debug_log("%d,system-%d,dataTypeNo=%d,can not get the IP",proc,subsDataBackup[proc].sysTypeNo,subsDataBackup[proc].dataTypeNo);
				}else{
					tmpRow = mysql_fetch_row(tmpResult);
					subsDataBackup[proc].sysNo = atoi(tmpRow[0]);
					subsDataBackup[proc].subSysNo = atoi(tmpRow[1]);
					mysql_free_result(tmpResult);
					subsDataBackup[proc].backupStat=nInBackup;
					
					sprintf(tmpLoadoutFile,"%s/%s_tmp.csv",subsComm_backupPath,subsDataBackup[proc].subsDataTable);
					unlink(tmpLoadoutFile);
					sprintf(tmpLoadoutSql,"SELECT * INTO OUTFILE '%s' "\
					" FIELDS TERMINATED BY ',' FROM %s",tmpLoadoutFile,subsDataBackup[proc].subsDataTable);
					if(mysql_getnores(dbConn,tmpLoadoutSql) == 0)
					{
						debug_log("%d,system-%d,dataTypeNo=%d,execute sql success=%s",proc,subsDataBackup[proc].sysTypeNo,subsDataBackup[proc].dataTypeNo,tmpLoadoutSql);
					}
					else
					{
						debug_log("%d,system-%d,dataTypeNo=%d,execute sql fail=%s",proc,subsDataBackup[proc].sysTypeNo,subsDataBackup[proc].dataTypeNo,tmpLoadoutSql);
					}
					
					sprintf(tmpSql,	"DELETE FROM %s",subsDataBackup[proc].subsDataTable);
					if(mysql_getnores(dbConn, tmpSql) == 0)
					{
						debug_log("%d,system-%d,dataTypeNo=%d,execute sql success=%s",proc,subsDataBackup[proc].sysTypeNo,subsDataBackup[proc].dataTypeNo,tmpSql);
					}
					else
					{
						debug_log("%d,system-%d,dataTypeNo=%d,execute sql fail=%s",proc,subsDataBackup[proc].sysTypeNo,subsDataBackup[proc].dataTypeNo,tmpSql);
					}
					sprintf(tmpSql,	"DELETE FROM %s",subsDataBackup[proc].tmpDataTable);
					if(mysql_getnores(dbConn, tmpSql) == 0)
					{
						debug_log("%d,system-%d,dataTypeNo=%d,execute sql success=%s",proc,subsDataBackup[proc].sysTypeNo,subsDataBackup[proc].dataTypeNo,tmpSql);
					}
					else
					{
						debug_log("%d,system-%d,dataTypeNo=%d,execute sql fail=%s",proc,subsDataBackup[proc].sysTypeNo,subsDataBackup[proc].dataTypeNo,tmpSql);
					}
					sendCmd(proc);
					return ;
				}
				break;
			case  nInBackup:
				if(finishFlag!=1)
					scanCmd(proc);
				else{
					//if(time(NULL)-finishTime>MAX_GETALL_TIMEOUT)
						backupFile(proc);
				}
				return ;
				break;
			case nOutBackup:
				debug_log("%d,system-%d,dataTypeNo=%d,affair finish, start next",start_proc_index,subsDataBackup[start_proc_index].sysTypeNo,subsDataBackup[start_proc_index].dataTypeNo);
				start_proc_index++;
				break;
			default:
				break;
		}
	}
	if(proc==MaxSysConfNum){
		subsDataBackupFlag=0;
		start_proc_index=0;
		for(i=0;i<MaxSysConfNum;i++){
			if(subsDataBackup[i].backupStat==nOutBackup)
				subsDataBackup[i].backupStat=nWaitBackup;
		}
	}
	return ;
}

void sendCmd(int proc)
{
	char tmpSql[2048];
	MYSQL_RES *tmpResult = NULL;
	MYSQL_ROW tmpRow = NULL;
	char tmpLoadoutFile[100];

	if (cmdId > 50000) {
		//sprintf(tmpSql, "DROP TABLE %s", CommTable);
		sprintf(tmpSql, "TRUNCATE TABLE %s", CommTable);
		mysql_getnores(dbConn, tmpSql);
		/*
		sprintf(tmpSql, "CREATE TABLE %s"
				"(commId smallint(5) unsigned NOT NULL auto_increment,"
				"commType tinyint(1) DEFAULT '0' NOT NULL,"
				"dataTypeNo tinyint(4) DEFAULT '0' NOT NULL,"
				"sysTypeNo smallint(5) unsigned DEFAULT '0' NOT NULL,"
				"sysNo smallint(5) unsigned DEFAULT '0' NOT NULL,"
				"subSysNo smallint(5) unsigned DEFAULT '0' NOT NULL,"
				"indexValueList varchar(255) NOT NULL,"
				"fieldNameList text NOT NULL,"
				"paramType tinyint(4) DEFAULT '0' NOT NULL,"
				"commState tinyint(3) unsigned DEFAULT '0' NOT NULL,"
				"totalNum smallint(5) unsigned DEFAULT '0' NOT NULL,"
				"finishNum smallint(5) unsigned DEFAULT '0' NOT NULL,"
				"sendTime datetime DEFAULT '0000-00-00 00:00:00' NOT NULL,"
				"bgnTime datetime DEFAULT '0000-00-00 00:00:00' NOT NULL,"
				"endTime datetime DEFAULT '0000-00-00 00:00:00' NOT NULL,"
				"remark varchar(200) binary NOT NULL,"
				"PRIMARY KEY (commId),"
				"KEY sysTypeNo (sysTypeNo),"
				"KEY sysNo (sysNo),"
				"KEY subSysNo (subSysNo),"
				"KEY bufferKey (indexValueList));", CommTable);
		mysql_getnores(dbConn, tmpSql);
		*/
	}

	//insert to command table
	sprintf(tmpSql, "INSERT INTO %s	"
			"(commType,dataTypeNo,sysTypeNo,sysNo,subSysNo,indexValueList,"
			"fieldNameList,paramType,commState,sendTime,remark)"
			"VALUES"
			"(\'5\',\'%d\',\'%d\',\'%d\',\'%d\',\'\',\'ALL\',"
			"\'0\',\'1\',CURRENT_TIMESTAMP,\'by subsDatabackup\')",
			CommTable,
			subsDataBackup[proc].dataTypeNo,
			subsDataBackup[proc].sysTypeNo,
			subsDataBackup[proc].sysNo,
			subsDataBackup[proc].subSysNo);

	if(mysql_getnores(dbConn, tmpSql) == 0)
	{
		debug_log("%d,system-%d,dataTypeNo=%d,execute sql success=%s",proc,subsDataBackup[proc].sysTypeNo,subsDataBackup[proc].dataTypeNo,tmpSql);
	}
	else
	{
		debug_log("%d,system-%d,dataTypeNo=%d,execute sql fail=%s",proc,subsDataBackup[proc].sysTypeNo,subsDataBackup[proc].dataTypeNo,tmpSql);
	}

	//get commId
	sprintf(tmpSql, "SELECT commId FROM %s "
			" WHERE sysTypeNo=\'%d\' "
			" AND dataTypeNo=\'%d\' "
			" AND indexValueList=\'\'",
			CommTable,
			subsDataBackup[proc].sysTypeNo,
			subsDataBackup[proc].dataTypeNo);

	tmpResult = (MYSQL_RES *) mysql_getres(dbConn, tmpSql);

	if (tmpResult == NULL || mysql_num_rows(tmpResult) == 0) {
		debug_log("%d,system-%d,dataTypeNo=%d,get commId fail",proc,subsDataBackup[proc].sysTypeNo,subsDataBackup[proc].dataTypeNo);
		mysql_free_result(tmpResult);

		sprintf(tmpLoadoutFile,"%s/%s_tmp.csv",subsComm_backupPath,	subsDataBackup[proc].subsDataTable);
		sprintf(tmpSql, "LOAD DATA INFILE '%s' REPLACE INTO TABLE %s "\
			" FIELDS TERMINATED BY ','",tmpLoadoutFile,subsDataBackup[proc].subsDataTable);
		if(mysql_getnores(dbConn, tmpSql) == 0)
		{
			debug_log("%d,system-%d,dataTypeNo=%d,execute sql success=%s",proc,subsDataBackup[proc].sysTypeNo,subsDataBackup[proc].dataTypeNo,tmpSql);
		}
		else
		{
			debug_log("%d,system-%d,dataTypeNo=%d,execute sql fail=%s",proc,subsDataBackup[proc].sysTypeNo,subsDataBackup[proc].dataTypeNo,tmpSql);
		}
		unlink(tmpLoadoutFile);

		subsDataBackup[proc].backupStat=nOutBackup;
		return ;		
	}
	tmpRow = mysql_fetch_row(tmpResult);
	cmdId = atoi(tmpRow[0]);
	mysql_free_result(tmpResult);

	debug_log("%d,system-%d,dataTypeNo=%d,get commId=%d",proc,subsDataBackup[proc].sysTypeNo,subsDataBackup[proc].dataTypeNo,cmdId);
	return ;
}

//scan command progress
void scanCmd(int proc)
{
	int cmdState = 0;
	char tmpSql[1024];
	char ErrorMsg[256];
	MYSQL_RES *tmpResult = NULL;
	MYSQL_ROW tmpRow = NULL;
	
	
	char tmpLoadoutFile[100];
	
	long	l_time;
	struct tm	*t;
	l_time=time(NULL);
	t=localtime(&l_time);

	if (cmdId <= 0){
		debug_log("%d,system-%d,dataTypeNo=%d,the commId is error when scanCmd",proc,subsDataBackup[proc].sysTypeNo,subsDataBackup[proc].dataTypeNo);
		sprintf(tmpLoadoutFile,"%s/%s_tmp.csv",subsComm_backupPath,	subsDataBackup[proc].subsDataTable);
		sprintf(tmpSql, "LOAD DATA INFILE '%s' REPLACE INTO TABLE %s "\
		" FIELDS TERMINATED BY ','",tmpLoadoutFile,subsDataBackup[proc].subsDataTable);
		if(mysql_getnores(dbConn, tmpSql) == 0)
		{
			debug_log("%d,system-%d,dataTypeNo=%d,execute sql success=%s",proc,subsDataBackup[proc].sysTypeNo,subsDataBackup[proc].dataTypeNo,tmpSql);
		}
		else
		{
			debug_log("%d,system-%d,dataTypeNo=%d,execute sql fail=%s",proc,subsDataBackup[proc].sysTypeNo,subsDataBackup[proc].dataTypeNo,tmpSql);
		}
		unlink(tmpLoadoutFile);

		subsDataBackup[proc].backupStat=nOutBackup;
		return ;
	}

	sprintf(tmpSql, "SELECT commState,remark,finishNum "
			" FROM %s WHERE commId=\'%d\'", CommTable, cmdId);
	tmpResult = (MYSQL_RES *) mysql_getres(dbConn, tmpSql);

	if (tmpResult == NULL || mysql_num_rows(tmpResult) == 0) {
		debug_log("%d,system-%d,dataTypeNo=%d,the commId is lost when scanCmd",proc,subsDataBackup[proc].sysTypeNo,subsDataBackup[proc].dataTypeNo);
		subsDataBackup[proc].backupStat=nOutBackup;
		mysql_free_result(tmpResult);

		sprintf(tmpLoadoutFile,"%s/%s_tmp.csv",subsComm_backupPath,	subsDataBackup[proc].subsDataTable);
		sprintf(tmpSql, "LOAD DATA INFILE '%s' REPLACE INTO TABLE %s "\
			" FIELDS TERMINATED BY ','",tmpLoadoutFile,subsDataBackup[proc].subsDataTable);
		if(mysql_getnores(dbConn, tmpSql) == 0)
		{
			debug_log("%d,system-%d,dataTypeNo=%d,execute sql success=%s",proc,subsDataBackup[proc].sysTypeNo,subsDataBackup[proc].dataTypeNo,tmpSql);
		}
		else
		{
			debug_log("%d,system-%d,dataTypeNo=%d,execute sql fail=%s",proc,subsDataBackup[proc].sysTypeNo,subsDataBackup[proc].dataTypeNo,tmpSql);
		}
		unlink(tmpLoadoutFile);

		return ;
	}
	tmpRow = mysql_fetch_row(tmpResult);
	cmdState = atoi(tmpRow[0]);
	strcpy(ErrorMsg, tmpRow[1]);
	mysql_free_result(tmpResult);
		
	switch (cmdState) {
	case nProcFail:
		waitFlag=0;
		inFlag=0;
	
		debug_log("%d,system-%d,dataTypeNo=%d,the get all command execute fail when scanCmd,%s",proc,subsDataBackup[proc].sysTypeNo,subsDataBackup[proc].dataTypeNo,ErrorMsg);
		sprintf(tmpLoadoutFile,"%s/%s_tmp.csv",subsComm_backupPath,	subsDataBackup[proc].subsDataTable);
		sprintf(tmpSql, "LOAD DATA INFILE '%s' REPLACE INTO TABLE %s "\
				" FIELDS TERMINATED BY ','",tmpLoadoutFile,subsDataBackup[proc].subsDataTable);
		if(mysql_getnores(dbConn, tmpSql) == 0)
		{
			debug_log("%d,system-%d,dataTypeNo=%d,execute sql success=%s",proc,subsDataBackup[proc].sysTypeNo,subsDataBackup[proc].dataTypeNo,tmpSql);
		}
		else
		{
			debug_log("%d,system-%d,dataTypeNo=%d,execute sql fail=%s",proc,subsDataBackup[proc].sysTypeNo,subsDataBackup[proc].dataTypeNo,tmpSql);
		}
		unlink(tmpLoadoutFile);

		sprintf(tmpSql, "DELETE FROM %s WHERE commId=\'%d\'", CommTable, cmdId);
		mysql_getnores(dbConn, tmpSql);
		subsDataBackup[proc].backupStat=nOutBackup;
		lastFinishNum=0;
		return ;
		break;
	case nWaitProc:
		inFlag=0;
		if(waitFlag==1){
			if(time(NULL)-waitTime>100){
				debug_log("%d,system-%d,dataTypeNo=%d,wait the get all command execute timeout",proc,subsDataBackup[proc].sysTypeNo,subsDataBackup[proc].dataTypeNo);
				sprintf(tmpLoadoutFile,"%s/%s_tmp.csv",subsComm_backupPath,	subsDataBackup[proc].subsDataTable);
				sprintf(tmpSql, "LOAD DATA INFILE '%s' REPLACE INTO TABLE %s "\
					" FIELDS TERMINATED BY ','",tmpLoadoutFile,subsDataBackup[proc].subsDataTable);
				if(mysql_getnores(dbConn, tmpSql) == 0)
				{
					debug_log("%d,system-%d,dataTypeNo=%d,execute sql success=%s",proc,subsDataBackup[proc].sysTypeNo,subsDataBackup[proc].dataTypeNo,tmpSql);
				}
				else
				{
					debug_log("%d,system-%d,dataTypeNo=%d,execute sql fail=%s",proc,subsDataBackup[proc].sysTypeNo,subsDataBackup[proc].dataTypeNo,tmpSql);
				}
				unlink(tmpLoadoutFile);
				
				sprintf(tmpSql, "DELETE FROM %s WHERE commId=\'%d\'",CommTable, cmdId);
				mysql_getnores(dbConn, tmpSql);
				waitFlag=0;
				lastFinishNum=0;
				subsDataBackup[proc].backupStat=nOutBackup;
				return ;
			}
		}else{
			waitTime=time(NULL);
			waitFlag=1;
		}		
		break;
	case nInProc:
		waitFlag=0;
		if((atoi(tmpRow[2])-lastFinishNum)>0){
			inFlag=0;
			lastFinishNum=atoi(tmpRow[2]);
		}else{
			if(inFlag==1){
				if((time(NULL)-inTime)>MAX_GETALL_TIMEOUT){
					debug_log("%d,system-%d,dataTypeNo=%d,wait the get all command executing timeout,the lastFinishNum is %d",proc,subsDataBackup[proc].sysTypeNo,subsDataBackup[proc].dataTypeNo,lastFinishNum);
					sprintf(tmpLoadoutFile,"%s/%s_tmp.csv",subsComm_backupPath,	subsDataBackup[proc].subsDataTable);
					sprintf(tmpSql, "LOAD DATA INFILE '%s' REPLACE INTO TABLE %s "\
						" FIELDS TERMINATED BY ','",tmpLoadoutFile,subsDataBackup[proc].subsDataTable);
					if(mysql_getnores(dbConn, tmpSql) == 0)
					{
						debug_log("%d,system-%d,dataTypeNo=%d,execute sql success=%s",proc,subsDataBackup[proc].sysTypeNo,subsDataBackup[proc].dataTypeNo,tmpSql);
					}
					else
					{
						debug_log("%d,system-%d,dataTypeNo=%d,execute sql fail=%s",proc,subsDataBackup[proc].sysTypeNo,subsDataBackup[proc].dataTypeNo,tmpSql);
					}
					unlink(tmpLoadoutFile);

					sprintf(tmpSql, "DELETE FROM %s WHERE commId=\'%d\'",CommTable, cmdId);
					mysql_getnores(dbConn, tmpSql);
					subsDataBackup[proc].backupStat=nOutBackup;
					
					inFlag=0;
					lastFinishNum=0;
					return ;
				}
			}else{
				inFlag=1;
				inTime=time(NULL);
			}
		}
		break;		
	case nOutProc:
		waitFlag=0;
		inFlag=0;
		lastFinishNum=0;
		finishFlag=1;
		sprintf(tmpSql, "DELETE FROM %s WHERE commId=\'%d\'",CommTable, cmdId);
		mysql_getnores(dbConn, tmpSql);
		debug_log("%d,system-%d,dataTypeNo=%d,the get all command execute finish",proc,subsDataBackup[proc].sysTypeNo,subsDataBackup[proc].dataTypeNo);
		finishTime=time(NULL);
		break;
	default:
		break;
	}						//end of switch
	return ;
}

void backupFile(int proc)
{
	char loadoutSql[400];
	char loadoutFile[100];
	
	char tmpLoadoutFile[100];
	
	long	l_time;
	struct tm	*t;
	l_time=time(NULL);
	t=localtime(&l_time);

	sprintf(loadoutFile,"%s/%s_%d_%d.csv",subsComm_backupPath,subsDataBackup[proc].subsDataTable,t->tm_mon + 1,t->tm_mday-1);
	unlink(loadoutFile);
	sprintf(loadoutSql,"SELECT * INTO OUTFILE '%s' "\
		" FIELDS TERMINATED BY ',' FROM %s",loadoutFile,subsDataBackup[proc].subsDataTable);
	mysql_getnores(dbConn,loadoutSql);
	if(mysql_getnores(dbConn, loadoutSql) == 0)
	{
		debug_log("%d,system-%d,dataTypeNo=%d,execute sql success=%s",proc,subsDataBackup[proc].sysTypeNo,subsDataBackup[proc].dataTypeNo,loadoutSql);
	}
	else
	{
		debug_log("%d,system-%d,dataTypeNo=%d,execute sql fail=%s",proc,subsDataBackup[proc].sysTypeNo,subsDataBackup[proc].dataTypeNo,loadoutSql);
	}
	
	sprintf(tmpLoadoutFile,"%s/%s_tmp.csv",subsComm_backupPath,	subsDataBackup[proc].subsDataTable);
	unlink(tmpLoadoutFile);
	debug_log("%d,system-%d,dataTypeNo=%d,backup %s finish",proc,subsDataBackup[proc].sysTypeNo,subsDataBackup[proc].dataTypeNo,subsDataBackup[proc].subsDataTable);
	subsDataBackup[proc].backupStat=nOutBackup;
	finishFlag=0;
	return ;
}


void debug_log(const char *fmt,...)
{
	char logFile[64];
	FILE *fp;
	time_t l_time;
	va_list ap;
	char buf[8192];
	struct tm *t;
	char timestr[64];
	
	l_time=time(NULL);
	t=localtime(&l_time);
	sprintf(logFile,"../log/subsDataBackup_%04d%02d%02d.log",t->tm_year+1900,t->tm_mon+1,t->tm_mday);
	fp=fopen(logFile,"a");
	if(fp!=NULL)
	{
		va_start(ap,fmt);
		vsprintf(buf,fmt,ap);
		sprintf(timestr,"%02d:%02d:%02d: ",t->tm_hour,t->tm_min,t->tm_sec);
		fputs(timestr,fp);
		fputs(buf,fp);
		fputs("\n",fp);
		fflush(fp);
		va_end(ap);
		fclose(fp);
	}
}

