/*
 * py_payout_weighted_dbio.pgc - py 모듈 DB 접근 함수 세트 (py_payout_weighted_dbio), archived into libpydbio.a.
 * 금액 가중 수수료율(bps) = sum(fee)*10000 / sum(amount).
 * ECPG (EXEC SQL) functions; run on the caller's XA branch. No EXEC SQL CONNECT.
 */
#include <string.h>
#include <userlog.h>
#include "py_payout_weighted_dbio.h"

/* 가중평균 수수료율(bps): 총수수료를 총금액으로 나눈 값. */
long pydb_payout_weighted_bps(const char *bizdate)
{
    EXEC SQL BEGIN DECLARE SECTION;
    char h_bizdate[16];
    long h_bps = 0;
    EXEC SQL END DECLARE SECTION;
    strncpy(h_bizdate, bizdate, sizeof(h_bizdate)-1); h_bizdate[sizeof(h_bizdate)-1] = 0;
    EXEC SQL SELECT CASE WHEN coalesce(sum(amount),0) > 0
                         THEN sum(fee) * 10000 / sum(amount) ELSE 0 END
        INTO :h_bps FROM py_payout WHERE biz_date = :h_bizdate;
    if (sqlca.sqlcode < 0) { userlog("pydb_payout_weighted_bps FAIL [%d] %s", sqlca.sqlcode, sqlca.sqlerrm.sqlerrmc); return -1; }
    return h_bps;
}
