/* 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_0005.pgc"
/* @tier medium @module cm @transform common-online */
/*
 * cm_ol_0005.pgc - 에러/메시지 공통 조회 서비스 (CM_OL_0005)  [tier=medium]
 *
 * 요청의 에러코드(ERRCD)로 공통 메시지 마스터(cm_msg)를 직접 SELECT
 * 하여 메시지문/심각도/처리구분을 응답한다. 심각도에 따라 응답코드를
 * 표준 응답코드로 매핑한다. 캐시 미적용 직접조회.
 * (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_0005.pgc"


TX_SERVICE(CM_OL_0005, ctx)
{
    /* exec sql begin declare section */
     
     
     
     
     
    
#line 23 "app/online/cm_ol_0005.pgc"
 char h_errcd [ 8 ] ;
 
#line 24 "app/online/cm_ol_0005.pgc"
 char h_lang [ 4 ] ;
 
#line 25 "app/online/cm_ol_0005.pgc"
 char h_msgtext [ 128 ] ;
 
#line 26 "app/online/cm_ol_0005.pgc"
 char h_severity [ 2 ] ;
 
#line 27 "app/online/cm_ol_0005.pgc"
 char h_action [ 8 ] ;
/* exec sql end declare section */
#line 28 "app/online/cm_ol_0005.pgc"


    char errcd[8];
    char lang[4];

    tx_log(TX_LOG_INFO, "[CM_OL_0005] 에러메시지 조회 서비스 진입");

    memset(errcd, 0, sizeof(errcd));
    memset(lang, 0, sizeof(lang));

    if (tx_buf_get(ctx->in, "ERRCD", errcd, sizeof(errcd)) != TX_OK) {
        tx_log(TX_LOG_ERROR, "[CM_OL_0005] 에러코드(ERRCD) 누락");
        tx_return(ctx, TX_EINVAL, ctx->out);
        return;
    }
    /* 언어구분 미지정 시 한국어(KO) 기본 */
    if (tx_buf_get(ctx->in, "LANG", lang, sizeof(lang)) != TX_OK) {
        strncpy(lang, "KO", sizeof(lang) - 1);
    }

    memset(h_errcd, 0, sizeof(h_errcd));
    memset(h_lang, 0, sizeof(h_lang));
    memset(h_msgtext, 0, sizeof(h_msgtext));
    memset(h_severity, 0, sizeof(h_severity));
    memset(h_action, 0, sizeof(h_action));
    strncpy(h_errcd, errcd, sizeof(h_errcd) - 1);
    strncpy(h_lang, lang, sizeof(h_lang) - 1);

    { ECPGdo(__LINE__, 0, 1, NULL, 0, ECPGst_normal, "select msg_text , severity , action_cd from cm_msg where err_cd = $1  and lang_cd = $2  and use_yn = 'Y'", 
	ECPGt_char,(h_errcd),(long)8,(long)1,(8)*sizeof(char), 
	ECPGt_NO_INDICATOR, NULL , 0L, 0L, 0L, 
	ECPGt_char,(h_lang),(long)4,(long)1,(4)*sizeof(char), 
	ECPGt_NO_INDICATOR, NULL , 0L, 0L, 0L, ECPGt_EOIT, 
	ECPGt_char,(h_msgtext),(long)128,(long)1,(128)*sizeof(char), 
	ECPGt_NO_INDICATOR, NULL , 0L, 0L, 0L, 
	ECPGt_char,(h_severity),(long)2,(long)1,(2)*sizeof(char), 
	ECPGt_NO_INDICATOR, NULL , 0L, 0L, 0L, 
	ECPGt_char,(h_action),(long)8,(long)1,(8)*sizeof(char), 
	ECPGt_NO_INDICATOR, NULL , 0L, 0L, 0L, ECPGt_EORT);}
#line 62 "app/online/cm_ol_0005.pgc"


    if (sqlca.sqlcode == TX_SQL_NOTFOUND) {
        tx_log(TX_LOG_WARN, "[CM_OL_0005] 메시지 없음 errcd=%s lang=%s", errcd, lang);
        tx_return(ctx, TX_ENOENT, ctx->out);
        return;
    }
    if (sqlca.sqlcode != TX_SQL_OK) {
        TX_DBIO_LOG_ERR("CM_OL_0005 SELECT cm_msg");
        tx_return(ctx, TX_EDB, ctx->out);
        return;
    }

    tx_buf_reset(ctx->out);
    tx_buf_sets(ctx->out, "ERRCD", errcd);
    tx_buf_sets(ctx->out, "LANG", lang);
    tx_buf_sets(ctx->out, "MSGTEXT", h_msgtext);
    tx_buf_sets(ctx->out, "SEVERITY", h_severity);
    tx_buf_sets(ctx->out, "ACTION", h_action);
    /* 심각도 E(Error) 는 시스템오류 응답, 그 외는 정상 응답코드 */
    tx_buf_sets(ctx->out, "RESPCODE",
                (h_severity[0] == 'E') ? RESP_SYSERR : RESP_OK);

    tx_log(TX_LOG_INFO, "[CM_OL_0005] 조회완료 errcd=%s sev=%s msg=%s",
           errcd, h_severity, h_msgtext);
    tx_return(ctx, TX_OK, ctx->out);
}
