/* Processed by ecpg (15.18 (Debian 15.18-0+deb12u1)) */
/* These include files are added by the preprocessor */
#include <ecpglib.h>
#include <ecpgerrno.h>
#include <sqlca.h>
/* End of automatic include section */

#line 1 "app/online/cm_ol_0007.pgc"
/* @tier medium @module cm @transform common-online */
/*
 * cm_ol_0007.pgc - 로그 공통 기록 서비스 (CM_OL_0007)  [tier=medium]
 *
 * 요청의 로그레벨(LEVEL)/모듈(MODULE)/메시지(MESSAGE)를 받아 공통
 * 로그 테이블(cm_log)에 INSERT 하고, 채번된 로그일련번호를 응답한다.
 * 일련번호는 당일 최대치 +1 로 산출하며 트랜잭션 안에서 기록한다.
 * (ATMI void SVC(TPSVCINFO*) 대응)
 */
#include <stdio.h>
#include <string.h>

#include "txcore.h"
#include "txcore_dbio.h"
#include "acq_util.h"
#include "cm_core.h"


#line 1 "/usr/include/postgresql/sqlca.h"
#ifndef POSTGRES_SQLCA_H
#define POSTGRES_SQLCA_H

#ifndef PGDLLIMPORT
#if  defined(WIN32) || defined(__CYGWIN__)
#define PGDLLIMPORT __declspec (dllimport)
#else
#define PGDLLIMPORT
#endif							/* __CYGWIN__ */
#endif							/* PGDLLIMPORT */

#define SQLERRMC_LEN	150

#ifdef __cplusplus
extern "C"
{
#endif

struct sqlca_t
{
	char		sqlcaid[8];
	long		sqlabc;
	long		sqlcode;
	struct
	{
		int			sqlerrml;
		char		sqlerrmc[SQLERRMC_LEN];
	}			sqlerrm;
	char		sqlerrp[8];
	long		sqlerrd[6];
	/* Element 0: empty						*/
	/* 1: OID of processed tuple if applicable			*/
	/* 2: number of rows processed				*/
	/* after an INSERT, UPDATE or				*/
	/* DELETE statement					*/
	/* 3: empty						*/
	/* 4: empty						*/
	/* 5: empty						*/
	char		sqlwarn[8];
	/* Element 0: set to 'W' if at least one other is 'W'	*/
	/* 1: if 'W' at least one character string		*/
	/* value was truncated when it was			*/
	/* stored into a host variable.             */

	/*
	 * 2: if 'W' a (hopefully) non-fatal notice occurred
	 */	/* 3: empty */
	/* 4: empty						*/
	/* 5: empty						*/
	/* 6: empty						*/
	/* 7: empty						*/

	char		sqlstate[5];
};

struct sqlca_t *ECPGget_sqlca(void);

#ifndef POSTGRES_ECPG_INTERNAL
#define sqlca (*ECPGget_sqlca())
#endif

#ifdef __cplusplus
}
#endif

#endif

#line 18 "app/online/cm_ol_0007.pgc"


/* 로그레벨 문자열 → 유효성(허용레벨만 기록) */
static int level_is_allowed(const char *lv)
{
    return (strcmp(lv, "DEBUG") == 0 || strcmp(lv, "INFO") == 0 ||
            strcmp(lv, "WARN") == 0 || strcmp(lv, "ERROR") == 0);
}

TX_SERVICE(CM_OL_0007, ctx)
{
    /* exec sql begin declare section */
     
     
     
     
     
     
    
#line 30 "app/online/cm_ol_0007.pgc"
 char h_bizdate [ 9 ] ;
 
#line 31 "app/online/cm_ol_0007.pgc"
 char h_level [ 8 ] ;
 
#line 32 "app/online/cm_ol_0007.pgc"
 char h_module [ 16 ] ;
 
#line 33 "app/online/cm_ol_0007.pgc"
 char h_message [ 256 ] ;
 
#line 34 "app/online/cm_ol_0007.pgc"
 long h_logseq ;
 
#line 35 "app/online/cm_ol_0007.pgc"
 long h_xid ;
/* exec sql end declare section */
#line 36 "app/online/cm_ol_0007.pgc"


    char level[8];
    char module[16];
    char message[256];

    tx_log(TX_LOG_INFO, "[CM_OL_0007] 로그 공통 기록 서비스 진입");

    memset(level, 0, sizeof(level));
    memset(module, 0, sizeof(module));
    memset(message, 0, sizeof(message));

    if (tx_buf_get(ctx->in, "LEVEL", level, sizeof(level)) != TX_OK ||
        tx_buf_get(ctx->in, "MODULE", module, sizeof(module)) != TX_OK ||
        tx_buf_get(ctx->in, "MESSAGE", message, sizeof(message)) != TX_OK) {
        tx_log(TX_LOG_ERROR, "[CM_OL_0007] 로그 필수 필드 누락");
        tx_return(ctx, TX_EINVAL, ctx->out);
        return;
    }

    if (!level_is_allowed(level)) {
        tx_log(TX_LOG_WARN, "[CM_OL_0007] 미허용 로그레벨 level=%s", level);
        tx_return(ctx, TX_EINVAL, ctx->out);
        return;
    }

    memset(h_bizdate, 0, sizeof(h_bizdate));
    memset(h_level, 0, sizeof(h_level));
    memset(h_module, 0, sizeof(h_module));
    memset(h_message, 0, sizeof(h_message));
    date_today(h_bizdate);
    strncpy(h_level, level, sizeof(h_level) - 1);
    strncpy(h_module, module, sizeof(h_module) - 1);
    strncpy(h_message, message, sizeof(h_message) - 1);
    h_logseq = 0;
    h_xid    = tx_current_xid();

    if (tx_begin() != TX_OK) {
        tx_log(TX_LOG_ERROR, "[CM_OL_0007] 트랜잭션 시작 실패");
        tx_return(ctx, TX_FAIL, ctx->out);
        return;
    }

    /* 당일 로그일련번호 채번 (당일 최대치 +1) */
    { ECPGdo(__LINE__, 0, 1, NULL, 0, ECPGst_normal, "select coalesce ( MAX ( log_seq ) , 0 ) + 1 from cm_log where biz_date = $1 ", 
	ECPGt_char,(h_bizdate),(long)9,(long)1,(9)*sizeof(char), 
	ECPGt_NO_INDICATOR, NULL , 0L, 0L, 0L, ECPGt_EOIT, 
	ECPGt_long,&(h_logseq),(long)1,(long)1,sizeof(long), 
	ECPGt_NO_INDICATOR, NULL , 0L, 0L, 0L, ECPGt_EORT);}
#line 84 "app/online/cm_ol_0007.pgc"


    if (sqlca.sqlcode != TX_SQL_OK && sqlca.sqlcode != TX_SQL_NOTFOUND) {
        TX_DBIO_LOG_ERR("CM_OL_0007 SELECT MAX cm_log");
        tx_abort();
        tx_return(ctx, TX_EDB, ctx->out);
        return;
    }
    if (h_logseq <= 0) {
        h_logseq = 1;
    }

    { ECPGdo(__LINE__, 0, 1, NULL, 0, ECPGst_normal, "insert into cm_log ( biz_date , log_seq , log_level , module_id , log_msg , xid , reg_ts ) values ( $1  , $2  , $3  , $4  , $5  , $6  , current_timestamp )", 
	ECPGt_char,(h_bizdate),(long)9,(long)1,(9)*sizeof(char), 
	ECPGt_NO_INDICATOR, NULL , 0L, 0L, 0L, 
	ECPGt_long,&(h_logseq),(long)1,(long)1,sizeof(long), 
	ECPGt_NO_INDICATOR, NULL , 0L, 0L, 0L, 
	ECPGt_char,(h_level),(long)8,(long)1,(8)*sizeof(char), 
	ECPGt_NO_INDICATOR, NULL , 0L, 0L, 0L, 
	ECPGt_char,(h_module),(long)16,(long)1,(16)*sizeof(char), 
	ECPGt_NO_INDICATOR, NULL , 0L, 0L, 0L, 
	ECPGt_char,(h_message),(long)256,(long)1,(256)*sizeof(char), 
	ECPGt_NO_INDICATOR, NULL , 0L, 0L, 0L, 
	ECPGt_long,&(h_xid),(long)1,(long)1,sizeof(long), 
	ECPGt_NO_INDICATOR, NULL , 0L, 0L, 0L, ECPGt_EOIT, ECPGt_EORT);}
#line 100 "app/online/cm_ol_0007.pgc"


    if (sqlca.sqlcode != TX_SQL_OK) {
        TX_DBIO_LOG_ERR("CM_OL_0007 INSERT cm_log");
        tx_abort();
        tx_return(ctx, TX_EDB, ctx->out);
        return;
    }

    tx_commit();

    tx_buf_reset(ctx->out);
    tx_buf_sets(ctx->out, "BIZDATE", h_bizdate);
    tx_buf_setlong(ctx->out, "LOGSEQ", h_logseq);
    tx_buf_setlong(ctx->out, "XID", h_xid);

    tx_log(TX_LOG_INFO, "[CM_OL_0007] 로그기록 완료 date=%s seq=%ld level=%s",
           h_bizdate, h_logseq, level);
    tx_return(ctx, TX_OK, ctx->out);
}
