/*
 * au_fraud_intolog_dbio.pgc - au 모듈 DB 접근 함수 세트 (au_fraud_intolog_dbio), archived into libaudbio.a.
 * ECPG (EXEC SQL) functions; run on the caller's XA branch. No EXEC SQL CONNECT.
 * Shape: INSERT INTO au_fraud_log SELECT (고액 승인 자동 플래그), 삽입 건수 반환.
 */
#include <string.h>
#include <userlog.h>
#include "au_fraud_intolog_dbio.h"

/* threshold 초과 고액 승인을 부정거래 로그에 자동 등록(set-based)하고 건수 반환. */
long audb_autoflag_high_amount(const char *bizdate, long threshold)
{
    EXEC SQL BEGIN DECLARE SECTION;
    char h_bizdate[16];
    long h_thr = threshold, flagged = 0;
    EXEC SQL END DECLARE SECTION;
    strncpy(h_bizdate, bizdate, sizeof(h_bizdate)-1); h_bizdate[sizeof(h_bizdate)-1] = 0;
    EXEC SQL INSERT INTO au_fraud_log (fraud_id, auth_id, card_no, score, reason, biz_date)
        SELECT nextval('au_fraud_seq'), a.auth_id, a.card_no, 90, 'AUTOFLAG_HIGH', a.biz_date
        FROM au_authorization a
        WHERE a.biz_date = :h_bizdate AND a.amount > :h_thr;
    if (sqlca.sqlcode < 0) {
        userlog("audb_autoflag_high_amount FAIL [%d] %s", sqlca.sqlcode, sqlca.sqlerrm.sqlerrmc);
        return -1;
    }
    flagged = sqlca.sqlerrd[2];
    return flagged;
}
