/*
 * rc_match_dbio.pgc - 대사 로그/예외 DB 접근 (librcdbio.a).
 * ECPG 함수; 호출자의 XA 브랜치에서 수행. EXEC SQL CONNECT 없음.
 */
#include <string.h>
#include <userlog.h>
#include "rc_dbio.h"

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

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

long rcdb_insert_match(long pid, const char *mtype, long amount,
                       const char *status, const char *bizdate)
{
    EXEC SQL BEGIN DECLARE SECTION;
    long h_id, h_pid = pid, h_amt = amount;
    char h_type[24], h_status[16], h_bizdate[16];
    EXEC SQL END DECLARE SECTION;

    strncpy(h_type, mtype, sizeof(h_type) - 1);        h_type[sizeof(h_type)-1] = 0;
    strncpy(h_status, status, sizeof(h_status) - 1);   h_status[sizeof(h_status)-1] = 0;
    strncpy(h_bizdate, bizdate, sizeof(h_bizdate) - 1); h_bizdate[sizeof(h_bizdate)-1] = 0;

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

    EXEC SQL INSERT INTO rc_match_log (match_id, purchase_id, match_type, amount, status, biz_date)
        VALUES (:h_id, :h_pid, :h_type, :h_amt, :h_status, :h_bizdate);
    if (sqlca.sqlcode < 0) {
        userlog("rcdb_insert_match FAIL [%d] %s", sqlca.sqlcode, sqlca.sqlerrm.sqlerrmc);
        return -1;
    }
    return h_id;
}

int rcdb_update_match_status(long pid, const char *from_status, const char *to_status)
{
    EXEC SQL BEGIN DECLARE SECTION;
    long h_pid = pid;
    char h_from[16], h_to[16];
    EXEC SQL END DECLARE SECTION;

    strncpy(h_from, from_status, sizeof(h_from) - 1); h_from[sizeof(h_from)-1] = 0;
    strncpy(h_to, to_status, sizeof(h_to) - 1);       h_to[sizeof(h_to)-1] = 0;

    EXEC SQL UPDATE rc_match_log SET status = :h_to, updated_at = now()
        WHERE purchase_id = :h_pid AND status = :h_from;
    if (sqlca.sqlcode < 0) {
        userlog("rcdb_update_match_status FAIL [%d] %s", sqlca.sqlcode, sqlca.sqlerrm.sqlerrmc);
        return -1;
    }
    return 0;
}

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

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

    EXEC SQL SELECT count(*) INTO :h_cnt FROM rc_match_log
        WHERE biz_date = :h_bizdate AND status = :h_status;
    if (sqlca.sqlcode < 0) return -1;
    return h_cnt;
}

long rcdb_insert_exception(long pid, const char *reason, const char *bizdate)
{
    EXEC SQL BEGIN DECLARE SECTION;
    long h_id, h_pid = pid;
    char h_reason[128], h_bizdate[16];
    EXEC SQL END DECLARE SECTION;

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

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

    EXEC SQL INSERT INTO rc_exception (exc_id, purchase_id, reason, biz_date)
        VALUES (:h_id, :h_pid, :h_reason, :h_bizdate);
    if (sqlca.sqlcode < 0) {
        userlog("rcdb_insert_exception FAIL [%d] %s", sqlca.sqlcode, sqlca.sqlerrm.sqlerrmc);
        return -1;
    }
    return h_id;
}

int rcdb_resolve_exception(long pid)
{
    EXEC SQL BEGIN DECLARE SECTION;
    long h_pid = pid;
    EXEC SQL END DECLARE SECTION;

    EXEC SQL UPDATE rc_exception SET resolved = true
        WHERE purchase_id = :h_pid AND resolved = false;
    if (sqlca.sqlcode < 0) {
        userlog("rcdb_resolve_exception FAIL [%d] %s", sqlca.sqlcode, sqlca.sqlerrm.sqlerrmc);
        return -1;
    }
    return 0;
}
