/*
 * vl_check_dbio.pgc - 의심거래 적재 + 검증 로그 기록 (libvldbio.a).
 * ECPG functions; run on the caller's XA branch. No EXEC SQL CONNECT.
 */
#include <string.h>
#include <userlog.h>
#include "vl_dbio.h"

long vldb_next_suspect_id(void)
{
    EXEC SQL BEGIN DECLARE SECTION;
    long h_id;
    EXEC SQL END DECLARE SECTION;

    EXEC SQL SELECT nextval('vl_suspect_seq') INTO :h_id;
    if (sqlca.sqlcode < 0) {
        userlog("vldb_next_suspect_id FAIL [%d] %s", sqlca.sqlcode, sqlca.sqlerrm.sqlerrmc);
        return -1;
    }
    return h_id;
}

long vldb_insert_suspect(long purchase_id, const char *rule_code,
                         const char *severity, const char *detail,
                         const char *bizdate)
{
    EXEC SQL BEGIN DECLARE SECTION;
    long h_id, h_pid = purchase_id;
    char h_rule[32], h_sev[8], h_detail[128], h_bizdate[16];
    EXEC SQL END DECLARE SECTION;

    strncpy(h_rule,    rule_code, sizeof(h_rule) - 1);   h_rule[sizeof(h_rule)-1]     = 0;
    strncpy(h_sev,     severity,  sizeof(h_sev) - 1);    h_sev[sizeof(h_sev)-1]       = 0;
    strncpy(h_detail,  detail,    sizeof(h_detail) - 1); h_detail[sizeof(h_detail)-1] = 0;
    strncpy(h_bizdate, bizdate,   sizeof(h_bizdate) - 1);h_bizdate[sizeof(h_bizdate)-1]= 0;

    EXEC SQL SELECT nextval('vl_suspect_seq') INTO :h_id;
    if (sqlca.sqlcode < 0) return -1;

    EXEC SQL INSERT INTO vl_suspect
        (suspect_id, purchase_id, rule_code, severity, detail, biz_date)
        VALUES (:h_id, :h_pid, :h_rule, :h_sev, :h_detail, :h_bizdate);
    if (sqlca.sqlcode < 0) {
        userlog("vldb_insert_suspect FAIL [%d] %s", sqlca.sqlcode, sqlca.sqlerrm.sqlerrmc);
        return -1;
    }
    return h_id;
}

long vldb_insert_check_log(const char *check_type, long target_id,
                           long result_code, const char *detail,
                           const char *bizdate)
{
    EXEC SQL BEGIN DECLARE SECTION;
    long h_id, h_target = target_id, h_rc = result_code;
    char h_type[32], h_detail[128], h_bizdate[16];
    EXEC SQL END DECLARE SECTION;

    strncpy(h_type,    check_type, sizeof(h_type) - 1);  h_type[sizeof(h_type)-1]     = 0;
    strncpy(h_detail,  detail,     sizeof(h_detail) - 1);h_detail[sizeof(h_detail)-1] = 0;
    strncpy(h_bizdate, bizdate,    sizeof(h_bizdate) - 1);h_bizdate[sizeof(h_bizdate)-1]= 0;

    EXEC SQL SELECT nextval('vl_checklog_seq') INTO :h_id;
    if (sqlca.sqlcode < 0) return -1;

    EXEC SQL INSERT INTO vl_check_log
        (log_id, check_type, target_id, result_code, detail, biz_date)
        VALUES (:h_id, :h_type, :h_target, :h_rc, :h_detail, :h_bizdate);
    if (sqlca.sqlcode < 0) {
        userlog("vldb_insert_check_log FAIL [%d] %s", sqlca.sqlcode, sqlca.sqlerrm.sqlerrmc);
        return -1;
    }
    return h_id;
}

long vldb_suspect_count(const char *bizdate)
{
    EXEC SQL BEGIN DECLARE SECTION;
    long h_cnt;
    char h_bizdate[16];
    EXEC SQL END DECLARE SECTION;

    strncpy(h_bizdate, bizdate, sizeof(h_bizdate) - 1); h_bizdate[sizeof(h_bizdate)-1] = 0;

    EXEC SQL SELECT count(*) INTO :h_cnt FROM vl_suspect
        WHERE biz_date = :h_bizdate AND status = 'OPEN';
    if (sqlca.sqlcode < 0) {
        userlog("vldb_suspect_count FAIL [%d] %s", sqlca.sqlcode, sqlca.sqlerrm.sqlerrmc);
        return -1;
    }
    return h_cnt;
}
