/* 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/vl_ol_0016.pgc"
/* @tier easy @module vl @transform validation-online */
/*
 * vl_ol_0016.pgc - 전문 포맷 검증 서비스 (VL_OL_0016)
 *
 * 입력 RAW 전문을 고정길이 레이아웃(acq_msg_t) 으로 언팩하여 길이/필수필드
 * (msg_validate) 를 검증하고, 전문유형코드(msg_type) 를 전문유형 마스터
 * (vl_msg_type) 에서 조회하여 등록/사용 여부를 확인한다. 실패 시 불일치(VL16xx).
 */
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#include "txcore.h"
#include "txcore_dbio.h"
#include "acq_util.h"
#include "vl_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/vl_ol_0016.pgc"


TX_SERVICE(VL_OL_0016, ctx)
{
    /* exec sql begin declare section */
     
     
     
    
#line 23 "app/online/vl_ol_0016.pgc"
 char h_msg_type [ 5 ] ;
 
#line 24 "app/online/vl_ol_0016.pgc"
 char h_use_yn [ 2 ] ;
 
#line 25 "app/online/vl_ol_0016.pgc"
 char h_type_name [ 41 ] ;
/* exec sql end declare section */
#line 26 "app/online/vl_ol_0016.pgc"


    acq_msg_t msg;
    char raw[128];
    int  rawlen;

    tx_log(TX_LOG_INFO, "[VL_OL_0016] 전문 포맷 검증 서비스 진입");

    if (tx_buf_get(ctx->in, "RAW", raw, sizeof(raw)) != TX_OK) {
        tx_log(TX_LOG_ERROR, "[VL_OL_0016] RAW 전문 누락");
        tx_buf_sets(ctx->out, "ERRCODE", "VL1600");
        tx_return(ctx, TX_EINVAL, ctx->out);
        return;
    }

    /* 1) 길이 검증 */
    rawlen = (int)strlen(raw);
    if (rawlen < MSG_ACQ_LEN) {
        tx_log(TX_LOG_WARN, "[VL_OL_0016] 전문 길이 부족 len=%d need=%d",
               rawlen, MSG_ACQ_LEN);
        tx_buf_sets(ctx->out, "RESULT", "MISMATCH");
        tx_buf_sets(ctx->out, "ERRCODE", "VL1601");
        tx_return(ctx, TX_EINVAL, ctx->out);
        return;
    }

    /* 2) 언팩 + 필수필드 검증 */
    memset(&msg, 0, sizeof(msg));
    if (msg_unpack(&msg, raw, rawlen) != 0 || msg_validate(&msg) != 0) {
        tx_log(TX_LOG_WARN, "[VL_OL_0016] 전문 검증 실패(msg_validate)");
        tx_buf_sets(ctx->out, "RESULT", "MISMATCH");
        tx_buf_sets(ctx->out, "ERRCODE", "VL1602");
        tx_return(ctx, TX_EINVAL, ctx->out);
        return;
    }

    /* 3) 전문유형코드 유효성 조회 */
    memset(h_msg_type, 0, sizeof(h_msg_type));
    memcpy(h_msg_type, msg.msg_type, FLD_MSG_TYPE_LEN);
    h_msg_type[FLD_MSG_TYPE_LEN] = '\0';
    memset(h_use_yn, 0, sizeof(h_use_yn));
    memset(h_type_name, 0, sizeof(h_type_name));

    { ECPGdo(__LINE__, 0, 1, NULL, 0, ECPGst_normal, "select use_yn , type_name from vl_msg_type where msg_type = $1 ", 
	ECPGt_char,(h_msg_type),(long)5,(long)1,(5)*sizeof(char), 
	ECPGt_NO_INDICATOR, NULL , 0L, 0L, 0L, ECPGt_EOIT, 
	ECPGt_char,(h_use_yn),(long)2,(long)1,(2)*sizeof(char), 
	ECPGt_NO_INDICATOR, NULL , 0L, 0L, 0L, 
	ECPGt_char,(h_type_name),(long)41,(long)1,(41)*sizeof(char), 
	ECPGt_NO_INDICATOR, NULL , 0L, 0L, 0L, ECPGt_EORT);}
#line 73 "app/online/vl_ol_0016.pgc"


    if (sqlca.sqlcode == TX_SQL_NOTFOUND) {
        tx_log(TX_LOG_WARN, "[VL_OL_0016] 미등록 전문유형 type=%s", h_msg_type);
        tx_buf_sets(ctx->out, "RESULT", "MISMATCH");
        tx_buf_sets(ctx->out, "ERRCODE", "VL1603");
        tx_return(ctx, TX_ENOENT, ctx->out);
        return;
    }
    if (sqlca.sqlcode != TX_SQL_OK) {
        TX_DBIO_LOG_ERR("SELECT vl_msg_type");
        tx_buf_sets(ctx->out, "ERRCODE", "VL1609");
        tx_return(ctx, TX_EDB, ctx->out);
        return;
    }

    tx_buf_reset(ctx->out);
    tx_buf_sets(ctx->out, "MSGTYPE", h_msg_type);
    tx_buf_sets(ctx->out, "TYPENAME", h_type_name);
    tx_buf_sets(ctx->out, "USEYN", h_use_yn);
    tx_buf_setlong(ctx->out, "RAWLEN", (long)rawlen);

    if (memcmp(h_use_yn, "Y", 1) != 0) {
        tx_log(TX_LOG_WARN, "[VL_OL_0016] 비활성 전문유형 type=%s use=%s",
               h_msg_type, h_use_yn);
        tx_buf_sets(ctx->out, "RESULT", "MISMATCH");
        tx_buf_sets(ctx->out, "ERRCODE", "VL1604");
        tx_return(ctx, TX_OK, ctx->out);
        return;
    }

    tx_buf_sets(ctx->out, "RESULT", "MATCH");
    tx_buf_sets(ctx->out, "ERRCODE", RESP_OK);
    tx_log(TX_LOG_INFO, "[VL_OL_0016] 전문 포맷 정상 type=%s name=%s",
           h_msg_type, h_type_name);
    tx_return(ctx, TX_OK, ctx->out);
}
