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

/* 상태별 건수를 커서로 순회하여 지급완료(P) 건수만 추출. */
long pydb_payout_paid_count(const char *bizdate)
{
    EXEC SQL BEGIN DECLARE SECTION;
    char h_bizdate[16], h_st[8];
    long h_cnt, paid = 0;
    EXEC SQL END DECLARE SECTION;
    strncpy(h_bizdate, bizdate, sizeof(h_bizdate)-1); h_bizdate[sizeof(h_bizdate)-1] = 0;
    EXEC SQL DECLARE pysc CURSOR FOR
        SELECT status, count(*) FROM py_payout WHERE biz_date = :h_bizdate GROUP BY status;
    EXEC SQL OPEN pysc;
    if (sqlca.sqlcode < 0) return -1;
    for (;;) {
        EXEC SQL FETCH pysc INTO :h_st, :h_cnt;
        if (sqlca.sqlcode == 100) break;
        if (sqlca.sqlcode < 0) { EXEC SQL CLOSE pysc; return -1; }
        if (h_st[0] == 'P') paid = h_cnt;
    }
    EXEC SQL CLOSE pysc;
    return paid;
}
