/* 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_0012.pgc"
/* @tier easy @module cm @transform common-online */
/*
 * cm_ol_0012.pgc - 코드 변환(내부→대외) 서비스 (CM_OL_0012)  [tier=easy]
 *
 * 요청의 원천시스템(SRCSYS)/내부코드(SRCCD) 로 코드매핑 마스터
 * (cm_code_map)를 조회하여 대외 표준코드(대상시스템/대상코드/대상명)를
 * 응답 TXBUF 로 반환한다. 대외기관 연계용 코드 변환 게이트웨이.
 * (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_0012.pgc"


TX_SERVICE(CM_OL_0012, ctx)
{
    /* exec sql begin declare section */
     
     
     
     
     
    
#line 23 "app/online/cm_ol_0012.pgc"
 char h_src_sys [ 16 ] ;
 
#line 24 "app/online/cm_ol_0012.pgc"
 char h_src_cd [ 16 ] ;
 
#line 25 "app/online/cm_ol_0012.pgc"
 char h_dst_sys [ 16 ] ;
 
#line 26 "app/online/cm_ol_0012.pgc"
 char h_dst_cd [ 20 ] ;
 
#line 27 "app/online/cm_ol_0012.pgc"
 char h_dst_name [ 64 ] ;
/* exec sql end declare section */
#line 28 "app/online/cm_ol_0012.pgc"


    char src_sys[16];
    char src_cd[16];

    tx_log(TX_LOG_INFO, "[CM_OL_0012] 코드 변환 서비스 진입");

    if (tx_buf_get(ctx->in, "SRCSYS", src_sys, sizeof(src_sys)) != TX_OK ||
        tx_buf_get(ctx->in, "SRCCD",  src_cd,  sizeof(src_cd))  != TX_OK) {
        tx_log(TX_LOG_ERROR, "[CM_OL_0012] SRCSYS/SRCCD 누락");
        tx_return(ctx, TX_EINVAL, ctx->out);
        return;
    }

    memset(h_src_sys,  0, sizeof(h_src_sys));
    memset(h_src_cd,   0, sizeof(h_src_cd));
    memset(h_dst_sys,  0, sizeof(h_dst_sys));
    memset(h_dst_cd,   0, sizeof(h_dst_cd));
    memset(h_dst_name, 0, sizeof(h_dst_name));
    strncpy(h_src_sys, src_sys, sizeof(h_src_sys) - 1);
    strncpy(h_src_cd,  src_cd,  sizeof(h_src_cd) - 1);

    { ECPGdo(__LINE__, 0, 1, NULL, 0, ECPGst_normal, "select dst_sys , dst_cd , dst_name from cm_code_map where src_sys = $1  and src_cd = $2  and use_yn = 'Y'", 
	ECPGt_char,(h_src_sys),(long)16,(long)1,(16)*sizeof(char), 
	ECPGt_NO_INDICATOR, NULL , 0L, 0L, 0L, 
	ECPGt_char,(h_src_cd),(long)16,(long)1,(16)*sizeof(char), 
	ECPGt_NO_INDICATOR, NULL , 0L, 0L, 0L, ECPGt_EOIT, 
	ECPGt_char,(h_dst_sys),(long)16,(long)1,(16)*sizeof(char), 
	ECPGt_NO_INDICATOR, NULL , 0L, 0L, 0L, 
	ECPGt_char,(h_dst_cd),(long)20,(long)1,(20)*sizeof(char), 
	ECPGt_NO_INDICATOR, NULL , 0L, 0L, 0L, 
	ECPGt_char,(h_dst_name),(long)64,(long)1,(64)*sizeof(char), 
	ECPGt_NO_INDICATOR, NULL , 0L, 0L, 0L, ECPGt_EORT);}
#line 55 "app/online/cm_ol_0012.pgc"


    if (sqlca.sqlcode == TX_SQL_NOTFOUND) {
        tx_log(TX_LOG_WARN, "[CM_OL_0012] 매핑 없음 sys=%s cd=%s", src_sys, src_cd);
        tx_return(ctx, TX_ENOENT, ctx->out);
        return;
    }
    if (sqlca.sqlcode != TX_SQL_OK) {
        TX_DBIO_LOG_ERR("CM_OL_0012 SELECT cm_code_map");
        tx_return(ctx, TX_EDB, ctx->out);
        return;
    }

    tx_buf_reset(ctx->out);
    tx_buf_sets(ctx->out, "SRCSYS", src_sys);
    tx_buf_sets(ctx->out, "SRCCD",  src_cd);
    tx_buf_sets(ctx->out, "DSTSYS", h_dst_sys);
    tx_buf_sets(ctx->out, "DSTCD",  h_dst_cd);
    tx_buf_sets(ctx->out, "DSTNAME", h_dst_name);
    tx_log(TX_LOG_INFO, "[CM_OL_0012] 변환완료 %s:%s -> %s:%s",
           src_sys, src_cd, h_dst_sys, h_dst_cd);
    tx_return(ctx, TX_OK, ctx->out);
}
