/*
 * cl_snap_top_dbio.pgc - cl 모듈 DB 접근 함수 (cl_snap_top_dbio), archived into libcldbio.a.
 * 마감 내 최대 매출 가맹점.
 * ECPG (EXEC SQL) functions; run on the caller's XA branch. No EXEC SQL CONNECT.
 */
#include <string.h>
#include <userlog.h>
#include "cl_snap_top_dbio.h"

/* 스냅샷에서 gross 최대 가맹점 (order by limit 1). */
int clqdb_snap_top(long cid, char *merch_out, long *gross_out)
{
    EXEC SQL BEGIN DECLARE SECTION;
    long h_cid = cid, h_gross;
    char h_merch[64];
    EXEC SQL END DECLARE SECTION;
    EXEC SQL SELECT merchant_id, gross_amount INTO :h_merch, :h_gross
        FROM cl_snapshot WHERE close_id = :h_cid ORDER BY gross_amount DESC LIMIT 1;
    if (sqlca.sqlcode == 100) { merch_out[0] = 0; *gross_out = 0; return 0; }
    if (sqlca.sqlcode < 0) return -1;
    strcpy(merch_out, h_merch); *gross_out = h_gross;
    return 0;
}
