/*
 * au_summary_rate_dbio.pgc - au 모듈 DB 접근 함수 세트 (au_summary_rate_dbio), archived into libaudbio.a.
 * ECPG (EXEC SQL) functions; run on the caller's XA branch. No EXEC SQL CONNECT.
 * Shape: computed rate = approve*100/auth over an aggregated table (승인율).
 */
#include <string.h>
#include <userlog.h>
#include "au_summary_rate_dbio.h"

/* 당일 전체 승인율(%) = sum(approve_count)*100/sum(auth_count). */
long audb_summary_approval_rate(const char *bizdate)
{
    EXEC SQL BEGIN DECLARE SECTION;
    char h_bizdate[16];
    long h_appr = 0, h_total = 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(approve_count),0), coalesce(sum(auth_count),0)
        INTO :h_appr, :h_total FROM au_summary WHERE biz_date = :h_bizdate;
    if (sqlca.sqlcode < 0) return -1;
    if (h_total <= 0) return 0;
    return h_appr * 100 / h_total;
}
