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

void MM_FX_REG(TPSVCINFO *p)
{
    UBFH *b = (UBFH *)p->data;
    char ccy[8];
    EXEC SQL BEGIN DECLARE SECTION;
    long h_rate;
    char h_ccy[8];
    EXEC SQL END DECLARE SECTION;
    gets_(b, T_CCY, ccy, sizeof(ccy)); if (ccy[0] == 0) strcpy(ccy, "USD");
    h_rate = getl(b, T_ARG2); if (h_rate <= 0) h_rate = 1350;
    strncpy(h_ccy, ccy, sizeof(h_ccy)-1); h_ccy[sizeof(h_ccy)-1] = 0;
    EXEC SQL INSERT INTO mm_fx_rate (ccy, rate_bps, base_date)
        VALUES (:h_ccy, :h_rate, current_date)
        ON CONFLICT (ccy) DO UPDATE
        SET rate_bps = EXCLUDED.rate_bps, base_date = current_date, updated_at = now();
    if (sqlca.sqlcode < 0) { userlog("MM_FX_REG FAIL [%d] %s", sqlca.sqlcode, sqlca.sqlerrm.sqlerrmc); FAIL(b); }
    OK(b);
}
