/*
 * py_summary_dbio.pgc - py 모듈 DB 접근 함수 세트 (py_summary_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_summary_dbio.h"

/* GROUP BY ... HAVING: 당일 지급총액이 임계 이상인 가맹점 수. */
long pydb_summary_active_merchants(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 count(*) INTO :h_v FROM (
        SELECT merchant_id FROM py_summary WHERE biz_date = :h_bizdate
        GROUP BY merchant_id HAVING sum(payout_amount) >= :h_thr) t;
    if (sqlca.sqlcode < 0) { userlog("pydb_summary_active_merchants FAIL [%d] %s", sqlca.sqlcode, sqlca.sqlerrm.sqlerrmc); return -1; }
    return h_v;
}
