⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 yxapi.pc

📁 unix 下用pro*c tuxedo 开发的东西
💻 PC
字号:
#include "stdio.h"
#include "stdlib.h"
#include "sqlcpr.h"
#include <fcntl.h>
#include <signal.h>
#include "/usr/bkfx/include/myapi.h"
#include "/usr/bkfx/include/Pcs_RD_BK_Q_x/Pcs_RD_BK_Q_YX.h"
#include "/usr/bkfx/include/Pcs_RD_BK_Q_x/yxapi.h"

void ProcessError( char *LOG, char *sErrorMessage1, char *sErrorMessage2 )
{
	FILE* hErrorFile;
	char cDateTime[100],tempstr[100];
	int i,j,len,n;
	long filesize;
	char bakfile[100];
	
	tempstr[0]='\0';
	len=strlen(sErrorMessage1);
	for(i=0;i<=len;i++)
	{
		if(sErrorMessage1[i]=='%')
		{			
			n=i;
			if(len!=i+1)
			{
				for(j=0;j<200&&sErrorMessage1[i+2]!='\0';j++,i++)
				tempstr[j]=sErrorMessage1[i+2];
				tempstr[j]='\0';
			}	
			break;
		}
		if(sErrorMessage1[i]=='\0')
		{
			n=i;
			tempstr[0] = '\0';
			break;
		}
			
	}
	
	printf("\n%s",LOG);
	printf("%s",sErrorMessage1);
	printf("%s",sErrorMessage2);
	printf("%s\n",tempstr);

	sErrorMessage1[n]='\0';
	hErrorFile = fopen( ERROR_LOG_FILE, "a" );
	if( hErrorFile == NULL )
	{
		printf( "Critical! Cannot open ERROR_LOG_FILE for append or cannot create!\n");
		return;
	}
	fseek( hErrorFile, 0 ,SEEK_END );
	filesize = ftell(hErrorFile);
	if (filesize > 5242880/5)
	{
		fclose(hErrorFile );
		sprintf(bakfile, "mv %s %s.bak", ERROR_LOG_FILE, ERROR_LOG_FILE);
		system(bakfile);
		hErrorFile = fopen( ERROR_LOG_FILE, "a" );
		if( hErrorFile == NULL )
		{
			printf( "Critical! Cannot open ERROR_LOG_FILE for append or cannot create!\n");
			return;
		}
	}
	fseek( hErrorFile, 0 ,SEEK_END );
	fputs( "\n",hErrorFile);
	fputs( LOG ,hErrorFile);
	fputs( sErrorMessage1,hErrorFile );
	fputs( sErrorMessage2,hErrorFile );
	fputs( tempstr,hErrorFile );
	fputs( "\n",hErrorFile);
	fclose( hErrorFile );

	sprintf( cDateTime, "date >> %s\n", ERROR_LOG_FILE );
	system( cDateTime );
	system( "date");
}

void PutintoF(char *string )
{
	FILE *fp;
	fp=fopen("checkfile.txt","w");
	fputs(string,fp);
	fclose(fp);
}
void sqlerror()
{
	char tempstring[200];
	char ERR_CODE[10];
	int init_time=0;
	
	ERROR=1;
	EXEC SQL WHENEVER sqlerror continue;
	ProcessError( LOG_ERROR, "PCS_RCV_YX: ",sqlca.sqlerrm.sqlerrmc );
	strncpy(ERR_CODE,sqlca.sqlerrm.sqlerrmc,9);
	ERR_CODE[9]='\0';
	if( strcmp(ERR_CODE,"ORA-03127")==0|| strcmp(ERR_CODE,"ORA-03114")==0|| strcmp(ERR_CODE,"ORA-03123")==0|| strcmp(ERR_CODE,"ORA-01034")==0|| strcmp(ERR_CODE,"ORA-01089")==0||strcmp(ERR_CODE,"ORA-01012")==0||strcmp(ERR_CODE,"ORA-01089") ==0||strcmp( ERR_CODE, "ORA-01033") ==0 )
	{
		sleep(INIT_INTERVAL);
		init_time++;
		if (init_time == 6)
		{
			ProcessError(LOG_NORMAL, "PCS_RCV_YX: Could not connect to the database, so exit now!",END);
			freect();
			exit(0);
		}
		InitPcsRcvYX();
		EXEC SQL ROLLBACK WORK;
		return;
	}
	EXEC SQL ROLLBACK WORK;
	return;
}	

/* init the main function such as DB environment */
int InitPcsRcvYX()
{
	EXEC SQL BEGIN DECLARE SECTION;
	VARCHAR username[20];
	VARCHAR password[20];
	EXEC SQL END DECLARE SECTION;
	
	strcpy(username.arr,"pcsdba");
	username.len=strlen(username.arr);
	strcpy(password.arr,"dba");
	password.len=strlen(password.arr);
	EXEC SQL WHENEVER SQLERROR DO sqlerror();
	EXEC SQL CONNECT :username IDENTIFIED BY :password;
	if( ERROR!=0 )
	{
		ERROR=0;
		return(-1);
	}
	return(0);
}

/*	Read a string from Pstring to Cstring 		*/
/*	m,n define the first point and the last point	*/
int myapi_ReadString(char *Pstring,char *Cstring,int m,int n)
{
	int i,j;
	Cstring[0]='\0';	/*First make the target string as a null string*/
	if( (strlen(Pstring)<n) || (m>n) || (m<0) )
	{	
		return(0);/*Some error when use this function */
	}
	for(i=m,j=0;i<=n;i++,j++)
	{
		Cstring[j]=Pstring[i];
	}
	Cstring[n-m+1]='\0';
	return(1);
}

/*delete the ' ' or '\n' of a string at the begin and end*/

void trim(char *CAline)
{
	int i,j;
	char temp_string[100];
	i=strlen(CAline);
	if( CAline[i]!='\0' )
	exit;
	while(i>0)		/*delete the end ' ' or '\n' */
	{
		if(CAline[i-1]==' '||CAline[i-1]=='\n')
			CAline[i-1]='\0';
		else
			break;
		i--;
	}
	i=0;j=0;
	while( CAline[i]!='\0' )	/*delete the head ' ' or '\n' */
	{
		if(CAline[i]==' '||CAline[i]=='\n')
			i++;
		else
		{
			for (j=0;CAline[i]!='\0';i++,j++)
				CAline[j]=CAline[i];
			break;	
		}	
	}
	CAline[j]='\0';
}

/* change the int varible to the string */

void itoa(long The_int_data, char* The_out_string, int j)

{

	int size;

	int i;

	j=j-1;

	sprintf(The_out_string, "%d", The_int_data);

	size = strlen(The_out_string);

	for(i=size;i<j;i++)

	The_out_string[i]=' ';

	The_out_string[i]='\0';

}


/***Read the data from MQ buffer to struct***/

int YXRcv( char *message, struct stYXMsg *YXMsg )
{
	int flag=0;
	flag+=myapi_ReadString(message,YXMsg->fsxh,0,9);
	flag+=myapi_ReadString(message,YXMsg->sjbh,10,11);
	flag+=myapi_ReadString(message,YXMsg->sjbh,12,13);
	flag+=myapi_ReadString(message,YXMsg->wjbh,14,15);
	flag+=myapi_ReadString(message,YXMsg->yxxl,16,18);
	flag+=myapi_ReadString(message,YXMsg->zh,19,21);
	flag+=myapi_ReadString(message,YXMsg->zzh,22,24);
	if( flag!=7 )
		return(-1);	/*  some myapi_ReadString not correctly returned  */
	message+=25;
	YXMsg->yxnr[0]='\0';
	strcpy(YXMsg->yxnr,message);
	return(0);
}

/***  Convert the data from struct stYXMsg to stYXSJ  ***/

int YXSJConv( struct stYXMsg YXMsg, struct stYXSJ  *YXSJ )
{
	strcpy(YXSJ->yxxl,YXMsg.yxxl);
	strcpy(YXSJ->wjbh,YXMsg.wjbh);
	YXSJ->jsxh=atol(YXMsg.fsxh);
	strcpy(YXSJ->sjbh,YXMsg.sjbh);
	return(0);
}

/***  Insert the yx data into database wait for convert into file  ***/

int InsertZCSJ( struct stYXMsg YXMsg )
{
	int zh,zzh;	/* group number */
	long int jsxh;
	
	zh=atoi(YXMsg.zh);
	zzh=atoi(YXMsg.zzh);
	jsxh=atol(YXMsg.fsxh);
	trim(YXMsg.sjbh);
/*	trim(YXMsg.yxnr);*/
/*	printf("##%s##\n",YXMsg.yxnr);
	printf("jsxh%ld,\n",jsxh);
	printf("zh%d,zhh%d,sjbh%s,\n",zh,zzh,YXMsg.sjbh);
*/	ERROR=0;
	EXEC SQL WHENEVER SQLERROR DO sqlerror();
	EXEC SQL INSERT INTO t_zcjssj VALUES
		( :zh,
		  :zzh,
		  :YXMsg.sjbh,
		  :jsxh,
		  :YXMsg.yxnr);
	if( ERROR!=0 )
	{
		ERROR=0;
		return(-1);
	}
	
	return(0);
}

/***  Insert yx data from stYXSJ to database table t_jsyxsj  ***/

int InsertYXSJ( struct stYXSJ YXSJ )
{
	EXEC SQL WHENEVER SQLERROR DO sqlerror();
	EXEC SQL INSERT INTO t_jsyxsj VALUES
		( :YXSJ );
	if( ERROR!=0 )
	{
		ERROR=0;
		return(-1);
	}
	return(0);
}

/************** insert a record into the t_jsrz  ****************/
int CreateRecLogRec(char datatype, char *fssbh ,long int lg_Key )
{
	int i;
	
	EXEC SQL WHENEVER SQLERROR DO sqlerror();
	EXEC SQL INSERT INTO T_JSRZ VALUES (:lg_Key, :datatype, '1',:fssbh, SYSDATE );
	if( ERROR!=0 )
	{
		ERROR=0;
		return(-1);
	}
	return(1);
}

/***  updatae a record in t_jsrz  ***/

int UpdateJSRZ_rec( long int lg_key )
{
	char temp=WAITFORCONVERT;
	ERROR=0;
	EXEC SQL WHENEVER SQLERROR DO sqlerror();
	EXEC SQL UPDATE T_JSRZ SET SJZT=:temp WHERE JSXH=:lg_key;
	
	if( ERROR!=0 )
	{
		ERROR=0;
		return(-1);
	}
	return(1);
}

/*Get the Time_Interval specified in the file and return it */
int GetTimeInterval(char* cType)
{
	FILE *hTimeInterval;
	char cLine[MAX_LINE_LEN+1], cCaption[100], cValue[21];
	int iInterval, i;
	char* pLine;
	
	 
	hTimeInterval = fopen( CONFIG_FILE_FOR_TIMEINTERVAL, "r" );
	if( hTimeInterval == NULL )
	{
		ProcessError(LOG_ERROR,   "GetTimeInterval: Cannot open file: ", CONFIG_FILE_FOR_TIMEINTERVAL);
		return(DEFAULT_TIMEINTERVAL);  
	}
	
	while( myapi_ReadALine( cLine, hTimeInterval ) )
	{
		if( cLine[0] == '#' ) continue;
		if( cLine[0] == NULL ) continue;
		
		pLine = cLine;
		while( (*pLine == ' ') || (*pLine == '\t') ) pLine++;
		i=0;
		while( (*pLine != ' ') && (*pLine != '\t') && (*pLine != '\0') )
		{
			cCaption[i] = *pLine;
			i++;
			pLine++;
		}
		
		cCaption[i] = '\0';
		if( strcmp( cCaption, cType ) == 0 ) /*Match*/
		{
			while( (*pLine == ' ') || (*pLine == '\t') ) pLine++;
			if( *pLine == '\0' )
			{
				fclose(hTimeInterval);
				return(DEFAULT_TIMEINTERVAL);
			}
			i=0;
			while( (*pLine != ' ') && (*pLine != '\t') && (*pLine != '\0') )
			{
				cValue[i] = *pLine;
				i++;
				pLine++;
			}
			cValue[i] = '\0';
			iInterval = atoi(cValue);
			
			if(iInterval <= 0) 		
			{
				fclose(hTimeInterval);
				return(DEFAULT_TIMEINTERVAL);
			}
			else
			{
				fclose(hTimeInterval);
				return(iInterval);
			}
		}
		else
			continue;
	}
	fclose( hTimeInterval );
	return(DEFAULT_TIMEINTERVAL);
}

/*Read a line to a string from a file, \n is excluded.
  return 1 if sucessful, 0 otherwise
  Do not close the file.*/
int myapi_ReadALine( char* cLine, FILE* hTable )
{
	char cChar;
	int i=0;
	cLine[0] = '\0';
	while( (cChar = fgetc( hTable )) != '\n' )
	{
		if( cChar == EOF ) return(0);
		cLine[i++] = cChar;
		
		
		if( i >= MAX_LINE_LEN ) i=0;
		cLine[i] = '\0';
	}
	return(1);
}


int Snd_HZ_MQ( long int fsxh, char *fssbh, char *jssbh )
{
	char buf_hz[15];
	char c_fsxh[11];
	int  flag;

/*	itoa( fsxh, c_fsxh, 10 );
	strncat( buf_hz, c_fsxh, 11);
	strncat( buf_hz, fssbh, 3);
	strncat( buf_hz, jssbh, 3);
*/
	sprintf(buf_hz,"%10ld%2s%2s",fsxh,fssbh,jssbh);
	buf_hz[14]='\0';
/*  	flag = MQBKSend_Syn( 15, buf_hz, fssbh);*/
      	flag = MQBKSend_Syn( 15, buf_hz, 0, "HZ" );
      	if ( flag < 0 )
        {
   		ProcessError(LOG_ERROR,"PCS_RCV_YX: MQ hz did not send successful!",END);
            	return(-1);
       	}
       	flag = MQBKCmit();
      	if ( flag < 0 )
        {
   		ProcessError(LOG_ERROR,"PCS_RCV_YX: MQ hz did not commit successful!",END);
            	return(-1);
       	}
          
/*	printf("the hz msg is %s\n",buf_hz);
*/
	return(0);
}

int freect(void)
{
	EXEC SQL ROLLBACK WORK RELEASE;
	if( buf!=NULL )
	free(buf);
	MQBKClear();
	exit(0);
}

/***  handle the receive message from MQ  ***/
int PcsRcvYX( char *message, int msg_len )
{
	struct stYXMsg YXMsg;
	struct stYXSJ  YXSJ;
	int  rec_count;
	int  flag;
	char tempstring[MID_LEN];
	char fsxh[MID_LEN];
	char sjbh[SHORT_LEN];
	char jssbh[SHORT_LEN];
	char yxxl[SHORT_LEN];
	char wjbh[SHORT_LEN];
	int retcode=0;

	/* convert to stYXMsg */
	flag=YXRcv( message,&YXMsg );
	if( flag!=0 )
	{	
		ProcessError(LOG_ERROR,"PCS_RCV_YX: error when read yx message into struct YXMsg",END);
		return(-1);
	}
	/* convert to stYXSJ  */
	YXSJConv( YXMsg, &YXSJ );
	fsxh[0]='\0';
	sjbh[0]='\0';
	yxxl[0]='\0';
	wjbh[0]='\0';
	
	strcpy(fsxh,YXMsg.fsxh);
	strcpy(sjbh,YXSJ.sjbh);
	strcpy(yxxl,YXMsg.yxxl);
	strcpy(wjbh,YXMsg.wjbh);
	trim(fsxh);
	trim(sjbh);
	trim(yxxl);
	trim(wjbh);
/*	strcpy(tempstring,YXFILE_PATH);
	strcat(tempstring,"yx");
	strcat(tempstring,yxxl);
	strcat(tempstring,sjbh);
	strcat(tempstring,".");
	strcat(tempstring,wjbh);
	printf("tempstring is %s\n",tempstring);
*/	
	EXEC SQL SELECT count(*) INTO :rec_count FROM t_zcjssj 
		WHERE jsxh=:YXSJ.jsxh;
	
	if( rec_count==0 )
	{
		flag=CreateRecLogRec( DATATYPE_YX, YXSJ.sjbh, YXSJ.jsxh );
		if( flag!=1 )
		{
			ProcessError(LOG_ERROR,"PCS_RCV_YX: error when use function ","CreateRecLogRec");
			return(-1);
		}
		flag=InsertYXSJ( YXSJ );
		if( flag!=0 )
		{
			ProcessError(LOG_ERROR,"PCS_RCV_YX: error when use function ","InsertYXSJ");
			return(-1);
		}
	}
/*		
		fp=fopen(tempstring,"w");
		if( fp==NULL )
		{
			ProcessError(LOG_ERROR,"PCS_RCV_YX: error cannot open ",tempstring);
			return(-1);
		}
	}
	else 
	{	
		fp=fopen(tempstring,"a");
		if( fp==NULL )
		{
			ProcessError(LOG_ERROR,"PCS_RCV_YX: error cannot open ",tempstring);
			return(-1);
		}
	}
	

	fputs( YXMsg.yxnr,fp );
	fclose(fp);
*/	
	/* update receive log */
	flag=UpdateJSRZ_rec( YXSJ.jsxh );
	if( flag!=1 )
	{
		ProcessError(LOG_ERROR,"PCS_RCV_YX: error when use function ","UpdateJSRZ_rec");
		return(-1);
	}
	
	/* save to table zcjssj */
	flag=InsertZCSJ( YXMsg );
	if( flag!=0 )
	{
		ProcessError(LOG_ERROR,"PCS_RCV_YX: error when use function ","InsertZCSJ");
		return(-1);
	}
	
	
	EXEC SQL VAR jssbh IS STRING;
	EXEC SQL SELECT bsbh INTO :jssbh FROM T_BSBH;
	
	/* mqbksend () send back hz and so on */
	
	flag=Snd_HZ_MQ( YXSJ.jsxh,sjbh,jssbh );
	if( flag!=0 )
	{
		ProcessError(LOG_ERROR,"PCS_RCV_YX: error use function ", "Snd_HZ_MQ");
		return(-1);
	}
	
	return(0);
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -