/*
 * st_payinsub_dbio.pgc - 활성 가맹점 지급 합계 (st_payinsub_dbio), libstdbio.a.
 * ECPG; merchant_id IN (SELECT ... FROM st_merch_settle WHERE txn_count > n) IN 서브셀렉트로 지급합.
 */
#include <string.h>
#include <userlog.h>
#include "st_payinsub_dbio.h"

long stdb_pay_for_active_merch(const char *bizdate, long min_txn)
{
    EXEC SQL BEGIN DECLARE SECTION;
    char h_bizdate[16];
    long h_min = min_txn, 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(net_amount), 0) INTO :h_v FROM st_payment
        WHERE biz_date = :h_bizdate
          AND merchant_id IN (SELECT merchant_id FROM st_merch_settle
                              WHERE biz_date = :h_bizdate AND txn_count > :h_min);
    if (sqlca.sqlcode < 0) { userlog("stdb_pay_for_active_merch FAIL [%d] %s", sqlca.sqlcode, sqlca.sqlerrm.sqlerrmc); return -1; }
    return h_v;
}
