/*
 * au_decline_reason_dbio.pgc - au 모듈 DB 접근 함수 세트 (au_decline_reason_dbio), archived into libaudbio.a.
 * ECPG (EXEC SQL) functions; run on the caller's XA branch. No EXEC SQL CONNECT.
 * Shape: cursor over GROUP BY decline_reason, accumulating counts (거절사유 누적).
 */
#include <string.h>
#include <userlog.h>
#include "au_decline_reason_dbio.h"

/* 거절(D) 건을 사유별로 커서 순회하며 총 거절 건수를 누적. */
long audb_decline_total_by_reason(const char *bizdate)
{
    EXEC SQL BEGIN DECLARE SECTION;
    char h_bizdate[16], h_reason[128];
    long h_cnt, total = 0;
    EXEC SQL END DECLARE SECTION;
    strncpy(h_bizdate, bizdate, sizeof(h_bizdate)-1); h_bizdate[sizeof(h_bizdate)-1] = 0;
    EXEC SQL DECLARE drc CURSOR FOR
        SELECT decline_reason, count(*) FROM au_authorization
        WHERE biz_date = :h_bizdate AND status = 'D'
        GROUP BY decline_reason;
    EXEC SQL OPEN drc;
    if (sqlca.sqlcode < 0) return -1;
    for (;;) {
        EXEC SQL FETCH drc INTO :h_reason, :h_cnt;
        if (sqlca.sqlcode == 100) break;
        if (sqlca.sqlcode < 0) { EXEC SQL CLOSE drc; return -1; }
        total = total + h_cnt;
    }
    EXEC SQL CLOSE drc;
    return total;
}
