/*
 * MG_RESEND.pgc - mg 모듈 서비스 MG_RESEND (one service = one file).
 * Split out of the former inline mg_svr.pgc; runs on the caller's XA
 * branch (no EXEC SQL CONNECT). Advertised by mg_svr at tpsvrinit.
 */
#include "MG_RESEND.h"

/* 7. MG_RESEND - 전문 재전송: STAN 으로 원전문 찾아 큐에 적재. */
void MG_RESEND(TPSVCINFO *p)
{
    UBFH *b = (UBFH *)p->data;
    char chan[16], mti[8], raw[512], bizdate[16];
    long stan, msg_id, qid;

    stan = getl(b, T_ARG1);
    gets_(b, T_CHANNEL, chan, sizeof(chan)); if (chan[0] == 0) strcpy(chan, "CH01");
    gets_(b, T_STR1, mti, sizeof(mti));      if (mti[0] == 0) strcpy(mti, "0200");
    gets_(b, T_STR3, raw, sizeof(raw));
    gets_(b, T_BIZDATE, bizdate, sizeof(bizdate));

    msg_id = mgdb_find_msg_by_stan(stan);
    if (msg_id < 0) { userlog("MG_RESEND 원전문 없음 stan=%ld", stan); FAIL(b); }

    qid = mgdb_next_queue_id(); if (qid < 0) FAIL(b);
    if (mgdb_enqueue(qid, stan, chan, mti, raw, bizdate) < 0) FAIL(b);
    setl(b, T_ID2, qid);
    userlog("MG_RESEND 재전송 큐적재 stan=%ld qid=%ld", stan, qid);
    OK(b);
}
