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

/* 당일 반려(J) 지급 건수. */
long pydb_payout_reject_count(const char *bizdate)
{
    EXEC SQL BEGIN DECLARE SECTION;
    char h_bizdate[16];
    long 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 count(*) INTO :h_v FROM py_payout WHERE biz_date = :h_bizdate AND status = 'J';
    if (sqlca.sqlcode < 0) return -1;
    return h_v;
}

/* 단건 반려 사유 문자열 회신. */
int pydb_payout_reject_reason(long payout_id, char *out)
{
    EXEC SQL BEGIN DECLARE SECTION;
    long h_pay = payout_id;
    char h_reason[128];
    EXEC SQL END DECLARE SECTION;
    EXEC SQL SELECT coalesce(reject_reason,'') INTO :h_reason FROM py_payout WHERE payout_id = :h_pay;
    if (sqlca.sqlcode < 0 || sqlca.sqlcode == 100) { userlog("pydb_payout_reject_reason FAIL [%d] %s", sqlca.sqlcode, sqlca.sqlerrm.sqlerrmc); return -1; }
    strncpy(out, h_reason, 127); out[127] = 0;
    return 0;
}
