/*
 * lg_top_merch_dbio.pgc - lg 모듈 DB 접근 함수 (lg_top_merch_dbio), archived into liblgdbio.a.
 * 최대 잔액 가맹점 (order by limit 1).
 * ECPG (EXEC SQL) functions; run on the caller's XA branch. No EXEC SQL CONNECT.
 */
#include <string.h>
#include <userlog.h>
#include "lg_top_merch_dbio.h"

/* 당일 잔액이 가장 큰 가맹점과 잔액. */
int lgqdb_top_merch(const char *bizdate, char *merch_out, long *bal_out)
{
    EXEC SQL BEGIN DECLARE SECTION;
    char h_bizdate[16], h_merch[64];
    long h_bal;
    EXEC SQL END DECLARE SECTION;
    strncpy(h_bizdate, bizdate, sizeof(h_bizdate)-1); h_bizdate[sizeof(h_bizdate)-1] = 0;
    EXEC SQL SELECT merchant_id, balance INTO :h_merch, :h_bal
        FROM lg_merch_bal WHERE biz_date = :h_bizdate
        ORDER BY balance DESC LIMIT 1;
    if (sqlca.sqlcode == 100) { merch_out[0] = 0; *bal_out = 0; return 0; }
    if (sqlca.sqlcode < 0) return -1;
    strcpy(merch_out, h_merch); *bal_out = h_bal;
    return 0;
}
