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

void MM_MCC_REG(TPSVCINFO *p)
{
    UBFH *b = (UBFH *)p->data;
    char mcc[16], name[64];
    EXEC SQL BEGIN DECLARE SECTION;
    long h_risk;
    char h_mcc[16], h_name[64];
    EXEC SQL END DECLARE SECTION;
    gets_(b, T_STR6, mcc, sizeof(mcc));
    if (mcc[0] == 0) { userlog("MM_MCC_REG: T_STR6(mcc) 누락"); FAIL(b); }
    gets_(b, T_STR1, name, sizeof(name));
    h_risk = getl(b, T_ARG1); if (h_risk <= 0) h_risk = 1;
    strncpy(h_mcc,  mcc,  sizeof(h_mcc)-1);  h_mcc[sizeof(h_mcc)-1]   = 0;
    strncpy(h_name, name, sizeof(h_name)-1); h_name[sizeof(h_name)-1] = 0;
    EXEC SQL INSERT INTO mm_mcc (mcc, mcc_name, risk_level)
        VALUES (:h_mcc, :h_name, :h_risk)
        ON CONFLICT (mcc) DO UPDATE
        SET mcc_name = EXCLUDED.mcc_name, risk_level = EXCLUDED.risk_level;
    if (sqlca.sqlcode < 0) { userlog("MM_MCC_REG FAIL [%d] %s", sqlca.sqlcode, sqlca.sqlerrm.sqlerrmc); FAIL(b); }
    OK(b);
}
