/*
 * py_payout_case_dbio.pgc - py 모듈 DB 접근 함수 세트 (py_payout_case_dbio), archived into libpydbio.a.
 * ECPG (EXEC SQL) functions; run on the caller's XA branch. No EXEC SQL CONNECT.
 */
#include <string.h>
#include <userlog.h>
#include "py_payout_case_dbio.h"

/* CASE 버킷 집계: 임계금액 이상 지급건 수를 sum(CASE ...) 로 산출. */
long pydb_payout_large_count(const char *bizdate, long threshold)
{
    EXEC SQL BEGIN DECLARE SECTION;
    char h_bizdate[16];
    long h_thr = threshold, 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 >= :h_thr THEN 1 ELSE 0 END),0) INTO :h_v
        FROM py_payout WHERE biz_date = :h_bizdate;
    if (sqlca.sqlcode < 0) { userlog("pydb_payout_large_count FAIL [%d] %s", sqlca.sqlcode, sqlca.sqlerrm.sqlerrmc); return -1; }
    return h_v;
}
