/*
 * MM_BIN_REG.pgc - mm 모듈 서비스 MM_BIN_REG (one service = one file).
 * 기능: BIN 마스터 등록.
 * 기존 인라인 mm_svr.pgc에서 분리; caller의 XA 브랜치에서 실행(EXEC SQL CONNECT 없음).
 * mm_svr가 tpsvrinit에서 이 서비스를 tpadvertise 한다.
 */
#include "MM_BIN_REG.h"

void MM_BIN_REG(TPSVCINFO *p)
{
    UBFH *b = (UBFH *)p->data;
    char bin[16], issuer[32], brand[16];
    EXEC SQL BEGIN DECLARE SECTION;
    char h_bin[16], h_issuer[32], h_brand[16];
    EXEC SQL END DECLARE SECTION;
    gets_(b, T_KEY1, bin, sizeof(bin));
    if (bin[0] == 0) { userlog("MM_BIN_REG: T_KEY1(bin) 누락"); FAIL(b); }
    gets_(b, T_ISSUER, issuer, sizeof(issuer)); if (issuer[0] == 0) strcpy(issuer, "UNKNOWN");
    gets_(b, T_STR5, brand, sizeof(brand));     if (brand[0] == 0) strcpy(brand, "LOCAL");
    strncpy(h_bin,    bin,    sizeof(h_bin)-1);    h_bin[sizeof(h_bin)-1]       = 0;
    strncpy(h_issuer, issuer, sizeof(h_issuer)-1); h_issuer[sizeof(h_issuer)-1] = 0;
    strncpy(h_brand,  brand,  sizeof(h_brand)-1);  h_brand[sizeof(h_brand)-1]   = 0;

    EXEC SQL INSERT INTO mm_bin (bin, issuer_cd, card_brand)
        VALUES (:h_bin, :h_issuer, :h_brand)
        ON CONFLICT (bin) DO UPDATE
        SET issuer_cd = EXCLUDED.issuer_cd, card_brand = EXCLUDED.card_brand;
    if (sqlca.sqlcode < 0) { userlog("MM_BIN_REG FAIL [%d] %s", sqlca.sqlcode, sqlca.sqlerrm.sqlerrmc); FAIL(b); }
    OK(b);
}
