/* 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/mm_ol_0024.pgc"
/* @tier easy @module mm @transform master-online */
/*
 * mm_ol_0024.pgc - BIN 유효성조회 서비스 (MM_OL_0024)  [tier=easy]
 *
 * 카드 앞 6자리 BIN 이 마스터(mm_card_bin)에 존재하고, 매핑된 카드사
 * (mm_issuer)가 사용중(use_yn='Y')인지 조인 조회로 검증한다. 카드사코드/
 * 카드사명/카드종류를 응답한다. 조회 전용(읽기) 서비스. (마스터 참조)
 */
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

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


TX_SERVICE(MM_OL_0024, ctx)
{
    /* exec sql begin declare section */
     
     
     
     
     
    
#line 23 "app/online/mm_ol_0024.pgc"
 char h_bin [ 8 ] ;
 
#line 24 "app/online/mm_ol_0024.pgc"
 char h_issuer_cd [ 4 ] ;
 
#line 25 "app/online/mm_ol_0024.pgc"
 char h_issuer_nm [ 32 ] ;
 
#line 26 "app/online/mm_ol_0024.pgc"
 char h_card_type [ 2 ] ;
 
#line 27 "app/online/mm_ol_0024.pgc"
 char h_use_yn [ 2 ] ;
/* exec sql end declare section */
#line 28 "app/online/mm_ol_0024.pgc"


    char bin[8];
    char card_type_nm[16];
    int  i;

    tx_log(TX_LOG_INFO, "[MM_OL_0024] BIN 유효성조회 서비스 진입");

    if (tx_buf_get(ctx->in, "BIN", bin, sizeof(bin)) != TX_OK) {
        tx_log(TX_LOG_ERROR, "[MM_OL_0024] BIN 누락");
        tx_buf_sets(ctx->out, "ERRCODE", "MM2400");
        tx_return(ctx, TX_EINVAL, ctx->out);
        return;
    }

    /* BIN 은 정확히 6자리 숫자여야 한다 */
    if (strlen(bin) != 6) {
        tx_log(TX_LOG_WARN, "[MM_OL_0024] BIN 길이 오류 bin=%s", bin);
        tx_buf_sets(ctx->out, "ERRCODE", "MM2401");
        tx_return(ctx, TX_EINVAL, ctx->out);
        return;
    }
    for (i = 0; i < 6; i++) {
        if (bin[i] < '0' || bin[i] > '9') {
            tx_log(TX_LOG_WARN, "[MM_OL_0024] BIN 숫자 아님 bin=%s", bin);
            tx_buf_sets(ctx->out, "ERRCODE", "MM2401");
            tx_return(ctx, TX_EINVAL, ctx->out);
            return;
        }
    }

    strncpy(h_bin, bin, sizeof(h_bin) - 1);
    h_bin[sizeof(h_bin) - 1] = '\0';

    { ECPGdo(__LINE__, 0, 1, NULL, 0, ECPGst_normal, "select i . issuer_cd , i . issuer_nm , b . card_type , i . use_yn from mm_card_bin b join mm_issuer i on i . issuer_cd = b . issuer_cd where b . bin = $1 ", 
	ECPGt_char,(h_bin),(long)8,(long)1,(8)*sizeof(char), 
	ECPGt_NO_INDICATOR, NULL , 0L, 0L, 0L, ECPGt_EOIT, 
	ECPGt_char,(h_issuer_cd),(long)4,(long)1,(4)*sizeof(char), 
	ECPGt_NO_INDICATOR, NULL , 0L, 0L, 0L, 
	ECPGt_char,(h_issuer_nm),(long)32,(long)1,(32)*sizeof(char), 
	ECPGt_NO_INDICATOR, NULL , 0L, 0L, 0L, 
	ECPGt_char,(h_card_type),(long)2,(long)1,(2)*sizeof(char), 
	ECPGt_NO_INDICATOR, NULL , 0L, 0L, 0L, 
	ECPGt_char,(h_use_yn),(long)2,(long)1,(2)*sizeof(char), 
	ECPGt_NO_INDICATOR, NULL , 0L, 0L, 0L, ECPGt_EORT);}
#line 67 "app/online/mm_ol_0024.pgc"


    if (sqlca.sqlcode == TX_SQL_NOTFOUND) {
        tx_log(TX_LOG_WARN, "[MM_OL_0024] 미등록 BIN bin=%s", bin);
        tx_buf_reset(ctx->out);
        tx_buf_sets(ctx->out, "VALIDYN", "N");
        tx_buf_sets(ctx->out, "ERRCODE", "MM2402");
        tx_return(ctx, TX_ENOENT, ctx->out);
        return;
    }
    if (sqlca.sqlcode != TX_SQL_OK) {
        TX_DBIO_LOG_ERR("SELECT mm_card_bin JOIN mm_issuer");
        tx_buf_sets(ctx->out, "ERRCODE", "MM2409");
        tx_return(ctx, TX_EDB, ctx->out);
        return;
    }

    /* 카드사가 사용중지면 무효 처리 */
    if (strcmp(h_use_yn, "Y") != 0) {
        tx_log(TX_LOG_WARN, "[MM_OL_0024] 사용중지 카드사 bin=%s issuer=%s",
               bin, h_issuer_cd);
        tx_buf_reset(ctx->out);
        tx_buf_sets(ctx->out, "VALIDYN", "N");
        tx_buf_sets(ctx->out, "ISSUERCD", h_issuer_cd);
        tx_buf_sets(ctx->out, "ERRCODE", "MM2403");
        tx_return(ctx, TX_FAIL, ctx->out);
        return;
    }

    if (strcmp(h_card_type, "C") == 0)
        strncpy(card_type_nm, "신용", sizeof(card_type_nm) - 1);
    else if (strcmp(h_card_type, "D") == 0)
        strncpy(card_type_nm, "체크", sizeof(card_type_nm) - 1);
    else
        strncpy(card_type_nm, "기타", sizeof(card_type_nm) - 1);

    tx_buf_reset(ctx->out);
    tx_buf_sets(ctx->out, "BIN", bin);
    tx_buf_sets(ctx->out, "VALIDYN", "Y");
    tx_buf_sets(ctx->out, "ISSUERCD", h_issuer_cd);
    tx_buf_sets(ctx->out, "ISSUERNM", h_issuer_nm);
    tx_buf_sets(ctx->out, "CARDTYPE", h_card_type);
    tx_buf_sets(ctx->out, "CARDTYPENM", card_type_nm);
    tx_buf_sets(ctx->out, "ERRCODE", RESP_OK);
    tx_log(TX_LOG_INFO, "[MM_OL_0024] BIN 유효 bin=%s issuer=%s(%s)",
           bin, h_issuer_cd, h_issuer_nm);
    tx_return(ctx, TX_OK, ctx->out);
}
