/*
 * MM_MERCH_UPD.pgc - mm 모듈 서비스 MM_MERCH_UPD (one service = one file).
 * 기능: 가맹점명/계좌 수정 (직접 UPDATE + 이력).
 * 기존 인라인 mm_svr.pgc에서 분리; caller의 XA 브랜치에서 실행(EXEC SQL CONNECT 없음).
 * mm_svr가 tpsvrinit에서 이 서비스를 tpadvertise 한다.
 */
#include "MM_MERCH_UPD.h"

void MM_MERCH_UPD(TPSVCINFO *p)
{
    UBFH *b = (UBFH *)p->data;
    char merch[64], name[128], acct[64];
    EXEC SQL BEGIN DECLARE SECTION;
    char h_merch[64], h_name[128], h_acct[64];
    EXEC SQL END DECLARE SECTION;

    gets_(b, T_MERCHANT, merch, sizeof(merch));
    gets_(b, T_STR1, name, sizeof(name));  if (name[0] == 0) strcpy(name, "무명가맹점");
    gets_(b, T_STR3, acct, sizeof(acct));
    strncpy(h_merch, merch, sizeof(h_merch)-1); h_merch[sizeof(h_merch)-1] = 0;
    strncpy(h_name,  name,  sizeof(h_name)-1);  h_name[sizeof(h_name)-1]   = 0;
    strncpy(h_acct,  acct,  sizeof(h_acct)-1);  h_acct[sizeof(h_acct)-1]   = 0;

    EXEC SQL UPDATE mm_merchant_ext
        SET name = :h_name, settle_acct = :h_acct, updated_at = now()
        WHERE merchant_id = :h_merch;
    if (sqlca.sqlcode < 0 || sqlca.sqlcode == 100) {
        userlog("MM_MERCH_UPD FAIL [%d] %s", sqlca.sqlcode, sqlca.sqlerrm.sqlerrmc); FAIL(b);
    }
    if (mmdb_insert_merch_hist(merch, "UPD", "", name, "정보 수정") < 0) FAIL(b);
    OK(b);
}
