/*
 * au_fraud_agg_dbio.pgc - au 모듈 DB 접근 함수 세트 (au_fraud_agg_dbio), archived into libaudbio.a.
 * ECPG (EXEC SQL) functions; run on the caller's XA branch. No EXEC SQL CONNECT.
 * Shape: computed avg = sum/count (건당 평균 스코어).
 */
#include <string.h>
#include <userlog.h>
#include "au_fraud_agg_dbio.h"

/* 당일 건당 평균 부정거래 스코어 = sum(score)/count(*). */
long audb_fraud_avg_score(const char *bizdate)
{
    EXEC SQL BEGIN DECLARE SECTION;
    char h_bizdate[16];
    long h_sum = 0, h_cnt = 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(score),0), count(*)
        INTO :h_sum, :h_cnt FROM au_fraud_log WHERE biz_date = :h_bizdate;
    if (sqlca.sqlcode < 0) return -1;
    if (h_cnt <= 0) return 0;
    return h_sum / h_cnt;
}
