/*
 * au_fraud_join_dbio.pgc - au 모듈 DB 접근 함수 세트 (au_fraud_join_dbio), archived into libaudbio.a.
 * ECPG (EXEC SQL) functions; run on the caller's XA branch. No EXEC SQL CONNECT.
 * Shape: 2-table JOIN with predicate (고위험 부정거래 연계 승인금액).
 */
#include <string.h>
#include <userlog.h>
#include "au_fraud_join_dbio.h"

/* fraud_log(score>=50) 와 연결된 승인원장 금액 합계를 조인으로 산출. */
long audb_fraud_high_risk_amount(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 coalesce(sum(a.amount),0) INTO :h_v
        FROM au_fraud_log f
        JOIN au_authorization a ON f.auth_id = a.auth_id
        WHERE f.biz_date = :h_bizdate AND f.score >= 50;
    if (sqlca.sqlcode < 0) { userlog("audb_fraud_high_risk_amount FAIL [%d] %s", sqlca.sqlcode, sqlca.sqlerrm.sqlerrmc); return -1; }
    return h_v;
}
