/* @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"

EXEC SQL INCLUDE sqlca;

TX_SERVICE(MM_OL_0024, ctx)
{
    EXEC SQL BEGIN DECLARE SECTION;
    char h_bin[8];
    char h_issuer_cd[4];
    char h_issuer_nm[32];
    char h_card_type[2];
    char h_use_yn[2];
    EXEC SQL END DECLARE SECTION;

    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';

    EXEC SQL
        SELECT i.issuer_cd, i.issuer_nm, b.card_type, i.use_yn
          INTO :h_issuer_cd, :h_issuer_nm, :h_card_type, :h_use_yn
          FROM mm_card_bin b
          JOIN mm_issuer i ON i.issuer_cd = b.issuer_cd
         WHERE b.bin = :h_bin;

    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);
}
