/*
 * py_account_having_dbio.pgc - py 모듈 DB 접근 함수 세트 (py_account_having_dbio), archived into libpydbio.a.
 * 가맹점 계좌 합산잔액이 기준 미만인 가맹점 수를 GROUP BY HAVING 으로 센다.
 * ECPG (EXEC SQL) functions; run on the caller's XA branch. No EXEC SQL CONNECT.
 */
#include <string.h>
#include <userlog.h>
#include "py_account_having_dbio.h"

/* GROUP BY HAVING: 합산잔액 < floor 인 가맹점 수. */
long pydb_account_low_balance_merchants(long floor_bal)
{
    EXEC SQL BEGIN DECLARE SECTION;
    long h_floor = floor_bal, h_cnt = 0;
    EXEC SQL END DECLARE SECTION;
    EXEC SQL SELECT count(*) INTO :h_cnt FROM (
        SELECT merchant_id FROM py_account
        GROUP BY merchant_id HAVING coalesce(sum(balance),0) < :h_floor) t;
    if (sqlca.sqlcode < 0) { userlog("pydb_account_low_balance_merchants FAIL [%d] %s", sqlca.sqlcode, sqlca.sqlerrm.sqlerrmc); return -1; }
    return h_cnt;
}
