/*
 * MM_MERCH_BULK.pgc - mm 모듈 서비스 MM_MERCH_BULK (one service = one file).
 * 기능: 가맹점 대량 등록 (T_COUNT개 파생 생성).
 * 기존 인라인 mm_svr.pgc에서 분리; caller의 XA 브랜치에서 실행(EXEC SQL CONNECT 없음).
 * mm_svr가 tpsvrinit에서 이 서비스를 tpadvertise 한다.
 */
#include "MM_MERCH_BULK.h"

void MM_MERCH_BULK(TPSVCINFO *p)
{
    UBFH *b = (UBFH *)p->data;
    char prefix[48], grade[8];
    long n, i, made = 0;
    EXEC SQL BEGIN DECLARE SECTION;
    char h_merch[64], h_name[128], h_grade[8];
    EXEC SQL END DECLARE SECTION;
    gets_(b, T_MERCHANT, prefix, sizeof(prefix)); if (prefix[0] == 0) strcpy(prefix, "MBULK");
    gets_(b, T_STR2, grade, sizeof(grade)); if (grade[0] == 0) strcpy(grade, "C");
    n = getl(b, T_COUNT); if (n < 1) n = 1; if (n > 1000) n = 1000;
    strncpy(h_grade, grade, sizeof(h_grade)-1); h_grade[sizeof(h_grade)-1] = 0;
    for (i = 1; i <= n; i++) {
        snprintf(h_merch, sizeof(h_merch), "%s-%04ld", prefix, i);
        snprintf(h_name,  sizeof(h_name),  "%s 지점%ld", prefix, i);
        EXEC SQL INSERT INTO mm_merchant_ext (merchant_id, name, grade)
            VALUES (:h_merch, :h_name, :h_grade)
            ON CONFLICT (merchant_id) DO NOTHING;
        if (sqlca.sqlcode < 0) { userlog("MM_MERCH_BULK FAIL [%d] %s", sqlca.sqlcode, sqlca.sqlerrm.sqlerrmc); FAIL(b); }
        made++;
    }
    setl(b, T_COUNT, made);
    userlog("MM_MERCH_BULK prefix=%s 생성=%ld", prefix, made);
    OK(b);
}
