📄 myapi.pc
字号:
/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
* File name : PCS_Snd_TJBB_P.x *
* Module ID : *
* Module Name : *
* Purpose : Get TJBB Data from database And send to DB *
* Author : Mr. laishihong *
* 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_TJBB_P/cmdef.h"
#include "/usr/bkfx/include/Pcs_Snd_TJBB_P/Function_define.h"
/* define the time used in the searching of the database */
int iTInterval;
/*define the global varible which will get the sequence number*/
unsigned long lg_Key;
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, "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 InitSndTJBB_P()
{
/* define the varible to connect to the database*/
VARCHAR userid[20];
VARCHAR password[20];
/*Get the sleep time interval*/
iTInterval = GetTimeInterval("PCS_SND_TJBB_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[10];
EXEC SQL WHENEVER SQLERROR CONTINUE;
ProcessError(LOG_ERROR, "database error:%s!", sqlca.sqlerrm.sqlerrmc );
sprintf(Error_Msg, "%ld", lg_Key);
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);
}
InitSndTJBB_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 PcsSndTJBB_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_TJBB;
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_fstjbb 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("CSGL") < 0 )
{
ProcessError(LOG_ERROR,"Calling Funtion MQBKInit() Failed!",END);
continue;
}
for(i=1;i<=count_rec;i++)
{
/*get a fsxh */
EXEC SQL FETCH xh_cursor into :lg_Key;
PcsSndTJBB2();
}
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 PcsSndTJBB2()
{
/*define the data structure of the 'TJBB'*/
struct stTJBBInfo
{
char TJBB_Code[TJBB_CODE_LEN+1];
int nf;
int yf;
char bbnr[MAX_BBNR_LEN];
long lg_Key;
}stTJBBRec;
/*define the Message body structure */
struct stTJBBMsgHeader
{
char fsxh[11];
char sjbh[3];
char BBBH[TJBB_CODE_LEN+1];
char nf[5];
char yf[3];
}MsgHeader;
char *Message;
size_t MsgSize;
int nf;
int cal_rec;
int i;
EXEC SQL WHENEVER SQLERROR DO sqlerror();
EXEC SQL SELECT BSBH INTO :MsgHeader.sjbh FROM T_BSBH;
/*malloc the memory for the Message*/
MsgSize=sizeof(struct stTJBBMsgHeader )+ MAX_BBNR_LEN;
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 body */
EXEC SQL VAR stTJBBRec.bbnr IS STRING(MAX_BBNR_LEN);
EXEC SQL SELECT * into :stTJBBRec FROM T_FSTJBB WHERE FSXH = :lg_Key;
sprintf( MsgHeader.fsxh, "%-10ld", lg_Key);
strcat( Message, MsgHeader.fsxh);
strcat( Message, MsgHeader.sjbh);
strcat( Message, stTJBBRec.TJBB_Code);
sprintf( MsgHeader.nf, "%4d", stTJBBRec.nf);
strcat( Message, MsgHeader.nf);
sprintf( MsgHeader.yf, "%2d", stTJBBRec.yf);
strcat( Message, MsgHeader.yf);
strcat( Message, stTJBBRec.bbnr);
/* outputmessage(Message);*/
if(MQBKSend_Syn(strlen(Message), Message, 0, "TJBB") <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 + -