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

/* FILTER 집계로 실패(J)건과 재시도 누계를 한 번에 뽑아 재시도 합을 반환. */
long pydb_payout_retry_load(const char *bizdate)
{
    EXEC SQL BEGIN DECLARE SECTION;
    char h_bizdate[16];
    long h_retry = 0, h_fail = 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(retry_count),0),
        coalesce(count(*) FILTER (WHERE status = 'J'),0)
        INTO :h_retry, :h_fail FROM py_payout WHERE biz_date = :h_bizdate;
    if (sqlca.sqlcode < 0) return -1;
    if (h_fail < 0) return -1;
    return h_retry;
}
