/*
 * au_auth_agg_dbio.pgc - au 모듈 DB 접근 함수 세트 (au_auth_agg_dbio), archived into libaudbio.a.
 * ECPG (EXEC SQL) functions; run on the caller's XA branch. No EXEC SQL CONNECT.
 * Shape: aggregate sum/count (일자별 집계).
 */
#include <string.h>
#include <userlog.h>
#include "au_auth_agg_dbio.h"

/* 당일 승인 총액. */
long audb_auth_gross_by_date(const char *bizdate)
{
    EXEC SQL BEGIN DECLARE SECTION;
    char h_bizdate[16];
    long h_v = 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(sum(amount),0) INTO :h_v FROM au_authorization
        WHERE biz_date = :h_bizdate AND status = 'A';
    if (sqlca.sqlcode < 0) { userlog("audb_auth_gross_by_date FAIL [%d] %s", sqlca.sqlcode, sqlca.sqlerrm.sqlerrmc); return -1; }
    return h_v;
}

/* 당일 승인 건수. */
long audb_auth_count_by_date(const char *bizdate)
{
    EXEC SQL BEGIN DECLARE SECTION;
    char h_bizdate[16];
    long h_v = 0;
    EXEC SQL END DECLARE SECTION;
    strncpy(h_bizdate, bizdate, sizeof(h_bizdate)-1); h_bizdate[sizeof(h_bizdate)-1] = 0;
    EXEC SQL SELECT count(*) INTO :h_v FROM au_authorization WHERE biz_date = :h_bizdate;
    if (sqlca.sqlcode < 0) { userlog("audb_auth_count_by_date FAIL [%d] %s", sqlca.sqlcode, sqlca.sqlerrm.sqlerrmc); return -1; }
    return h_v;
}
