/*
 * py_payout_rejrate_dbio.pgc - py 모듈 DB 접근 함수 세트 (py_payout_rejrate_dbio), archived into libpydbio.a.
 * 반려(J) 지급건 비율(퍼센트)을 CASE 분기 나눗셈으로 산출한다.
 * ECPG (EXEC SQL) functions; run on the caller's XA branch. No EXEC SQL CONNECT.
 */
#include <string.h>
#include <userlog.h>
#include "py_payout_rejrate_dbio.h"

/* 비율(CASE): 반려 건수 비중(%) = count(J)*100 / count(*). */
long pydb_payout_reject_rate(const char *bizdate)
{
    EXEC SQL BEGIN DECLARE SECTION;
    char h_bizdate[16];
    long h_rate = 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 count(*) > 0
                         THEN count(*) FILTER (WHERE status = 'J') * 100 / count(*)
                         ELSE 0 END
        INTO :h_rate FROM py_payout WHERE biz_date = :h_bizdate;
    if (sqlca.sqlcode < 0) { userlog("pydb_payout_reject_rate FAIL [%d] %s", sqlca.sqlcode, sqlca.sqlerrm.sqlerrmc); return -1; }
    return h_rate;
}
