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

📄 myapi.pc

📁 unix 下用pro*c tuxedo 开发的东西
💻 PC
字号:
/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
*	File name		: PCS_Snd_BSFXML_P	                       *				
*	Module ID		:				               *
*	Module Name	:                                                      *
*	Purpose		: Get bsfxml Message from database And send to MQ      *
*	Author             :                                                   *
*	Date Created	: 1999,07,14                                           *
*	Version		: Version 1.0                                          *
*	Environment	: Digital UNIX                                         *
*	Portability	: UNIX Platform                                        *
*	Warnings		:                                              *
*	References	:                                                      *
*	Calling Syntax	:                                                      *
*	Parameters	: None                                                 *
*	Returns		: void                                                 *
*	Calling Function:                                                      *
*	Called Functions: When system startup                                  *
*	Datastores and usages:                                                 *                                       *
*        	   (a) Input                                                   *
* 	           (b) OutPut                                                  *       
*	Report		: None                                                 *
*	Screens		: None                                                 *
*	Messages Files	: None                                                 *
*	Change Log	:                                                      *
*	Date								       *
*	Author								       *
*	Reason For Change                      				       *
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * 
*/

#include "stdio.h"
#include <stdlib.h>
#include <sqlcpr.h>
#include "/usr/bkfx/include/Pcs_Snd_BSFX_P/cmdef.h"
#include "/usr/bkfx/include/Pcs_Snd_BSFX_P/Pcs_Snd_BSFX_P.h"
#include "/usr/bkfx/include/Pcs_Snd_BSFX_P/Function_define.h"

/*define the global varible which will get the sequence number*/
unsigned long lg_Key;  
struct stBKMLDBRec_id stDBRecord_id;

/* define the time used in the searching of the database */
int iTInterval;
int whichline=0;
char bkdh[8];
int init_time=0;

char *freememory;

/*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 %s,The file may not exist!", 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);
}

/* connect to the database and initialize the errorlog file */
void InitSndBSFXML_P()
{
	/* define the varible to connect to the database*/ 	
	VARCHAR userid[20];
	VARCHAR password[20];
	
	/*Get the sleep  time interval*/
	iTInterval = GetTimeInterval("PCS_SND_BSFX_P");
	
	strcpy( userid.arr, "PCSDBA" );
	userid.len = strlen( userid.arr );
	strcpy( password.arr, "DBA" );
	password.len = strlen( password.arr );
	
	EXEC SQL WHENEVER SQLERROR DO sqlerror();
	EXEC SQL CONNECT :userid IDENTIFIED BY :password;
#ifdef DEBUG
	ProcessError(LOG_NORMAL,"Connected to Oracle as user:%s!", userid.arr);
#endif
}

/* write the errorlog file when the database is wrong */
void sqlerror()
{
	char ERR_CODE[10];
	char Error_Msg[100];

	EXEC SQL WHENEVER SQLERROR CONTINUE;
	ProcessError(LOG_ERROR,   "database error:%s!", sqlca.sqlerrm.sqlerrmc );
	if (whichline == 0)
	{
		sprintf(Error_Msg, "%ld", lg_Key);
		ProcessError(LOG_ERROR,"Error occured in record which fsxh is %s!", Error_Msg);
	}
	else
	{
		sprintf(Error_Msg, "%ld and bkdh is %s", lg_Key, bkdh);
		ProcessError(LOG_ERROR,"Error occured in record which fsxh is %s!", Error_Msg);
	}
	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, "Could not connect to the database, so exit now!",END);
			if ( freememory != NULL )
			free( freememory );
			MQBKClear();
			EXEC SQL ROLLBACK RELEASE;
			exit(1);
		}
		InitSndBSFXML_P();
		return;
	}
	return;
}

/* free the memory and close the file etc. */
void terminate()
{
	ProcessError(LOG_NORMAL, "The application is terminated by user!",END);
	if ( freememory != NULL )
	free( freememory );
	MQBKClear();
	EXEC SQL ROLLBACK RELEASE;
	exit(1);
}

/* get the relative record and come into being the Message, and then send
   to the MQ, update the t_fsrz and t_hz table. */
void PcsSndBSFXML_P()
{
	int count_rec=0;
	char sjzt;
	char sjfl;
	int i;
		
	while(1)
	{
		EXEC SQL WHENEVER SQLERROR DO sqlerror();
		sjzt = FSRZ_WAITFOR_SND;
		sjfl = SJFL_BSFX;
		EXEC SQL SELECT COUNT(*) INTO :count_rec FROM T_FSRZ WHERE
		SJFL = :sjfl AND SJZT = :sjzt;
		/* if have the relative t_fsrz records then find the t_fsbsfxmlnr record */
		if( count_rec > 0 )
		{
			EXEC SQL DECLARE xh_cursor CURSOR FOR
			SELECT FSXH FROM T_FSRZ WHERE SJFL = :sjfl AND SJZT = :sjzt;
			EXEC SQL OPEN xh_cursor;
			if ( MQBKInit("CSYW") < 0 )
		      	{
 		      		ProcessError(LOG_ERROR,"Calling Funtion MQBKInit() Failed!",END);
				continue;
    		 	}
			for(i=1;i<=count_rec;i++)
			{
				EXEC SQL FETCH xh_cursor into :lg_Key;
				PcsSndBSFXML2();
			}
			if ( MQBKClear() < 0 )
		      	{
 		      		ProcessError(LOG_ERROR,"Calling Funtion MQBKClear() Failed!",END);
    		 	}
			EXEC SQL CLOSE xh_cursor;
			
		}
		else
		{
/*#ifdef DEBUG			
			printf("no relative record!\n");
			break;
#endif			*/
			sleep( iTInterval );
		}
		
	}
	
	
}

void PcsSndBSFXML2()
{
	struct stBSFXMLDBRec stDBRecord;
	struct BSFXMLMsgHeader MsgHeader;
	struct stBSFXMLDBMsg MsgBody;
	
	char *Message;
	char single_record_Message[MAX_LINE_LEN];
	size_t MsgSize;
	int nf;
	int cal_rec;
	int i;
	char space;

	space=' ';
	
	EXEC SQL SELECT BSBH INTO :MsgHeader.sjbh FROM T_BSBH;
	/* find the t_fsbsfxmlnr record count from the fsxh */
	EXEC SQL WHENEVER SQLERROR DO sqlerror();
	EXEC SQL SELECT COUNT(*) INTO :cal_rec FROM T_FSBSFXMLNR WHERE FSXH = :lg_Key; 
	
	/* malloc the memory for the Message */
	MsgSize=sizeof(struct BSFXMLMsgHeader )+cal_rec*sizeof(struct stBSFXMLDBRec );
	if((Message = (char *)malloc( MsgSize ))==NULL)
	{
		ProcessError(LOG_ERROR,"variable <Message> memory Allocation error - aborting.",END);
		MQBKClear();
		EXEC SQL ROLLBACK RELEASE;
		exit(1);	
	}
	
	freememory = Message;
	Message[0] = '\0';
	
	/* construct the Message Header */
	EXEC SQL SELECT NF INTO :nf FROM T_FSBSFXML WHERE FSXH = :lg_Key;
				
	sprintf( MsgHeader.fsxh, "%-10ld", lg_Key);
	sprintf( MsgHeader.nf, "%4d", nf);
				
	strcat( Message, MsgHeader.fsxh);
	strcat( Message, MsgHeader.nf);
	strcat( Message, MsgHeader.sjbh);
				
	/* construct the Message body */	
	EXEC SQL DECLARE nr_cursor CURSOR FOR
	SELECT * FROM T_FSBSFXMLNR WHERE FSXH = :lg_Key;
	EXEC SQL OPEN nr_cursor;
	for(i=1;i<=cal_rec;i++)
	{
/*		printf("i=%d\n",i);
*/		
		EXEC SQL FETCH nr_cursor into :stDBRecord.bkdh, :stDBRecord.bkmc :stDBRecord_id.bkmc_id,
					  :stDBRecord.fbkj :stDBRecord_id.fbkjmc_id, :stDBRecord.cbrq :stDBRecord_id.cbrq_id,
					  :stDBRecord.dj :stDBRecord_id.dj_id, :stDBRecord.yj :stDBRecord_id.yj_id, 
					  :stDBRecord.jj :stDBRecord_id.jj_id, :stDBRecord.bnj :stDBRecord_id.bnj_id,
					  :stDBRecord.nj :stDBRecord_id.nj_id, :stDBRecord.hzddjdrq :stDBRecord_id.hzddrq_id,
					  :stDBRecord.fxfw :stDBRecord_id.fxfw_id, :stDBRecord.kq :stDBRecord_id.kq_id,
					  :stDBRecord.tykh :stDBRecord_id.tykh_id, :stDBRecord.jb :stDBRecord_id.jb_id, 
					  :stDBRecord.pc :stDBRecord_id.pc_id, :stDBRecord.kz :stDBRecord_id.kz_id,
					  :stDBRecord.bym :stDBRecord_id.bym_id, :stDBRecord.xyf :stDBRecord_id.xyf_id, 
					  :stDBRecord.bbdyfl :stDBRecord_id.bbdyfl_id, :stDBRecord.wbdyfl :stDBRecord_id.wbdyfl_id,
					  :stDBRecord.bblsfl :stDBRecord_id.bblsfl_id, :stDBRecord.wblsfl :stDBRecord_id.wblsfl_id,
					  :stDBRecord.bz :stDBRecord_id.bz_id, :stDBRecord.key;

		whichline++;
		strcpy( bkdh, stDBRecord.bkdh );

		MsgBody.dj[0] = '\0';
		if (stDBRecord_id.dj_id != -1)
		sprintf( MsgBody.dj, "%.2f", stDBRecord.dj );
		
		MsgBody.yj[0] = '\0';
		if (stDBRecord_id.yj_id != -1)
		sprintf( MsgBody.yj, "%.2f", stDBRecord.yj );
		
		MsgBody.jj[0] = '\0';
		if (stDBRecord_id.jj_id != -1)
		sprintf( MsgBody.jj, "%.2f", stDBRecord.jj );
		
		MsgBody.bnj[0] = '\0';
		if (stDBRecord_id.bnj_id != -1)
		sprintf( MsgBody.bnj, "%.2f", stDBRecord.bnj );
		
		MsgBody.nj[0] = '\0';
		if (stDBRecord_id.nj_id != -1)
		sprintf( MsgBody.nj, "%.2f", stDBRecord.nj );
		
		MsgBody.pc[0] = '\0';
		if (stDBRecord_id.pc_id != -1)
		sprintf( MsgBody.pc, "%d", stDBRecord.pc );
		
		MsgBody.kz[0] = '\0';
		if (stDBRecord_id.kz_id != -1)
		sprintf( MsgBody.kz, "%d", stDBRecord.kz );
		
		MsgBody.bym[0] = '\0';
		if (stDBRecord_id.bym_id != -1)
		sprintf( MsgBody.bym, "%d", stDBRecord.bym );
		
		MsgBody.bbdyfl[0] = '\0';
		if (stDBRecord_id.bbdyfl_id != -1)
		sprintf( MsgBody.bbdyfl, "%.2f", stDBRecord.bbdyfl );
		
		MsgBody.wbdyfl[0] = '\0';
		if (stDBRecord_id.wbdyfl_id != -1)
		sprintf( MsgBody.wbdyfl, "%.2f", stDBRecord.wbdyfl );
		
		MsgBody.bblsfl[0] = '\0';
		if (stDBRecord_id.bblsfl_id != -1)
		sprintf( MsgBody.bblsfl, "%.2f", stDBRecord.bblsfl );
		
		MsgBody.wblsfl[0] = '\0';
		if (stDBRecord_id.wblsfl_id != -1)
		sprintf( MsgBody.wblsfl, "%.2f", stDBRecord.wblsfl );
			
		single_record_Message[0]='\0';
		sprintf(single_record_Message, "%-7s%-40s%-16s%-10s%-6s%-6s%-6s%-7s%-7s%-24s%-4s%-6s%-12s%-6s%-2s%-2s%-4s%-1s%-5s%-5s%-5s%-5s%-160s",
			stDBRecord.bkdh, stDBRecord.bkmc, stDBRecord.fbkj, stDBRecord.cbrq,
			MsgBody.dj, MsgBody.yj, MsgBody.jj, MsgBody.bnj, MsgBody.nj,
			stDBRecord.hzddjdrq, stDBRecord.fxfw, stDBRecord.kq, stDBRecord.tykh, stDBRecord.jb,
			MsgBody.pc, MsgBody.kz, MsgBody.bym, stDBRecord.xyf, MsgBody.bbdyfl, MsgBody.wbdyfl,
			MsgBody.bblsfl, MsgBody.wblsfl, stDBRecord.bz);
		strcat(Message, single_record_Message);
				
	}
	/*outputmessage(Message);*/
	EXEC SQL CLOSE nr_cursor;
				
	if(MQBKSend_Syn(strlen(Message), Message, 0, "BSFX") <0)
	{
      		ProcessError(LOG_ERROR,"Calling Funtion MQBKSend_Syn() Failed!",END);
		MQBKBack();
	}
	else
	{
		if(MQBKCmit() < 0)
      		{
			ProcessError(LOG_ERROR,"Calling Funtion MQBKCmit() Failed!",END);
			MQBKBack();
		}
		else
		{
			UpdateSndLogRecStatus();
			UpdateHZRec();
		}
	}
	free(Message);
}

/* update the table to locate the relative flag*/
int UpdateSndLogRecStatus()
{
	char sjzt = FSRZ_SENDED;
	
	EXEC SQL WHENEVER SQLERROR DO sqlerror();
	EXEC SQL UPDATE T_FSRZ SET SJZT = :sjzt, JSSJ = SYSDATE
			WHERE FSXH = :lg_Key;
	return(1);
}

/*update the table to locate the relative flag*/
int UpdateHZRec()
{
	char hzzt = HZ_SENDED;
	
	EXEC SQL WHENEVER SQLERROR DO sqlerror();
	EXEC SQL UPDATE T_HZ SET HZZT = :hzzt, HZJSSJ = SYSDATE
		WHERE FSXH = :lg_Key;
	EXEC SQL COMMIT WORK;
	return(1);
} 

/*void outputmessage(char *outmessage)
{
	FILE* hFile;
		
	hFile = fopen( OUTPUT_MESSAGE, "a" );
	if( hFile == NULL )
	{
		ProcessError(LOG_ERROR,"Critical! Cannot open OUTPUT_MESSAGE file for append or cannot create!\n");
		return;
	}
	fseek( hFile, 0, SEEK_END );
	fputs( outmessage, hFile );
	fclose( hFile );
}*/

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;
		}
			
	}
	
	
	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)
	{
		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);
	printf("\n%s",LOG);
	fputs( sErrorMessage1,hErrorFile );
	printf("%s",sErrorMessage1);
	fputs( sErrorMessage2,hErrorFile );
	printf("%s",sErrorMessage2);
	fputs( tempstr,hErrorFile );
	printf("%s\n",tempstr);
	fputs( "\n",hErrorFile);
	fclose( hErrorFile );
	sprintf( cDateTime, "date >> %s\n", ERROR_LOG_FILE );
	system( cDateTime );
	system( "date");
}

⌨️ 快捷键说明

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