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

void MM_TERMINAL_REG(TPSVCINFO *p)
{
    UBFH *b = (UBFH *)p->data;
    char term[32], merch[64], model[32];
    EXEC SQL BEGIN DECLARE SECTION;
    char h_term[32], h_merch[64], h_model[32];
    EXEC SQL END DECLARE SECTION;
    gets_(b, T_STR5, term, sizeof(term));
    if (term[0] == 0) { userlog("MM_TERMINAL_REG: T_STR5(terminal) 누락"); FAIL(b); }
    gets_(b, T_MERCHANT, merch, sizeof(merch));
    gets_(b, T_STR6, model, sizeof(model)); if (model[0] == 0) strcpy(model, "STD");
    strncpy(h_term,  term,  sizeof(h_term)-1);  h_term[sizeof(h_term)-1]   = 0;
    strncpy(h_merch, merch, sizeof(h_merch)-1); h_merch[sizeof(h_merch)-1] = 0;
    strncpy(h_model, model, sizeof(h_model)-1); h_model[sizeof(h_model)-1] = 0;
    EXEC SQL INSERT INTO mm_terminal (terminal_id, merchant_id, model)
        VALUES (:h_term, :h_merch, :h_model)
        ON CONFLICT (terminal_id) DO UPDATE
        SET merchant_id = EXCLUDED.merchant_id, model = EXCLUDED.model;
    if (sqlca.sqlcode < 0) { userlog("MM_TERMINAL_REG FAIL [%d] %s", sqlca.sqlcode, sqlca.sqlerrm.sqlerrmc); FAIL(b); }
    OK(b);
}
