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

void MM_ISSUER_REG(TPSVCINFO *p)
{
    UBFH *b = (UBFH *)p->data;
    char cd[32], name[64], country[8];
    EXEC SQL BEGIN DECLARE SECTION;
    char h_cd[32], h_name[64], h_country[8];
    EXEC SQL END DECLARE SECTION;
    gets_(b, T_ISSUER, cd, sizeof(cd));
    if (cd[0] == 0) { userlog("MM_ISSUER_REG: T_ISSUER 누락"); FAIL(b); }
    gets_(b, T_STR1, name, sizeof(name));
    gets_(b, T_STR2, country, sizeof(country)); if (country[0] == 0) strcpy(country, "KR");
    strncpy(h_cd,      cd,      sizeof(h_cd)-1);      h_cd[sizeof(h_cd)-1]           = 0;
    strncpy(h_name,    name,    sizeof(h_name)-1);    h_name[sizeof(h_name)-1]       = 0;
    strncpy(h_country, country, sizeof(h_country)-1); h_country[sizeof(h_country)-1] = 0;
    EXEC SQL INSERT INTO mm_issuer (issuer_cd, issuer_name, country)
        VALUES (:h_cd, :h_name, :h_country)
        ON CONFLICT (issuer_cd) DO UPDATE
        SET issuer_name = EXCLUDED.issuer_name, country = EXCLUDED.country;
    if (sqlca.sqlcode < 0) { userlog("MM_ISSUER_REG FAIL [%d] %s", sqlca.sqlcode, sqlca.sqlerrm.sqlerrmc); FAIL(b); }
    OK(b);
}
