/*
 * au_auth_bucket_dbio.pgc - au 모듈 DB 접근 함수 세트 (au_auth_bucket_dbio), archived into libaudbio.a.
 * ECPG (EXEC SQL) functions; run on the caller's XA branch. No EXEC SQL CONNECT.
 * Shape: CASE-based bucketing aggregate (금액대별 버킷팅, 고액 버킷 건수).
 */
#include <string.h>
#include <userlog.h>
#include "au_auth_bucket_dbio.h"

/* 당일 승인을 금액대(소/중/대)로 CASE 버킷팅하여 고액(>=1,000,000) 버킷 건수 반환. */
long audb_auth_large_bucket(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(
                CASE WHEN amount >= 1000000 THEN 1
                     WHEN amount >= 100000 THEN 0
                     ELSE 0 END), 0) INTO :h_v
        FROM au_authorization WHERE biz_date = :h_bizdate;
    if (sqlca.sqlcode < 0) return -1;
    return h_v;
}
