/*
 * mg_peak_count_dbio.pgc - mg 모듈 DB 접근 함수 (mg_peak_count_dbio), archived into libmgdbio.a.
 * ECPG (EXEC SQL) functions; run on the caller's XA branch. No EXEC SQL CONNECT.
 * 최고 금액 전문 건수.
 */
#include <string.h>
#include <userlog.h>
#include "mg_peak_count_dbio.h"

long mgdb_msg_peak_count(const char *bizdate)
{
    EXEC SQL BEGIN DECLARE SECTION;
    char h_bizdate[16];
    long h_peak = 0, h_cnt = 0;
    EXEC SQL END DECLARE SECTION;
    strncpy(h_bizdate, bizdate, sizeof(h_bizdate)-1); h_bizdate[sizeof(h_bizdate)-1] = 0;
    EXEC SQL SELECT coalesce(max(amount),0) INTO :h_peak FROM mg_msg_log WHERE biz_date = :h_bizdate;
    if (sqlca.sqlcode < 0) return -1;
    EXEC SQL SELECT count(*) INTO :h_cnt FROM mg_msg_log
        WHERE biz_date = :h_bizdate AND amount = :h_peak;
    if (sqlca.sqlcode < 0) return -1;
    return h_cnt;
}
