/*
 * cl_close_dbio.pgc - 마감이력(close_log) + 순번관리 DB 접근 (part of libcldbio.a).
 * ECPG functions; run on the caller's XA branch. No EXEC SQL CONNECT.
 */
#include <string.h>
#include <userlog.h>
#include "cl_dbio.h"

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

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

int cldb_insert_close(long cid, const char *ctype, const char *period,
                      const char *bizdate, const char *status,
                      long txn_count, long total_amount, const char *memo)
{
    EXEC SQL BEGIN DECLARE SECTION;
    long h_cid = cid, h_cnt = txn_count, h_amt = total_amount;
    char h_type[16], h_period[16], h_bizdate[16], h_status[8], h_memo[128];
    EXEC SQL END DECLARE SECTION;

    strncpy(h_type, ctype, sizeof(h_type) - 1);        h_type[sizeof(h_type)-1] = 0;
    strncpy(h_period, period, sizeof(h_period) - 1);   h_period[sizeof(h_period)-1] = 0;
    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;
    strncpy(h_memo, memo, sizeof(h_memo) - 1);         h_memo[sizeof(h_memo)-1] = 0;

    EXEC SQL INSERT INTO cl_close_log
        (close_id, close_type, period_key, biz_date, status, txn_count, total_amount, memo)
        VALUES (:h_cid, :h_type, :h_period, :h_bizdate, :h_status, :h_cnt, :h_amt, :h_memo);
    if (sqlca.sqlcode < 0) {
        userlog("cldb_insert_close FAIL [%d] %s", sqlca.sqlcode, sqlca.sqlerrm.sqlerrmc);
        return -1;
    }
    return 0;
}

int cldb_update_close_status(long cid, const char *status)
{
    EXEC SQL BEGIN DECLARE SECTION;
    long h_cid = cid;
    char h_status[8];
    EXEC SQL END DECLARE SECTION;

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

    EXEC SQL UPDATE cl_close_log SET status = :h_status, updated_at = now()
        WHERE close_id = :h_cid;
    if (sqlca.sqlcode < 0 || sqlca.sqlcode == 100) {
        userlog("cldb_update_close_status FAIL [%d] %s", sqlca.sqlcode, sqlca.sqlerrm.sqlerrmc);
        return -1;
    }
    return 0;
}

int cldb_get_close_status(long cid, char *out)
{
    EXEC SQL BEGIN DECLARE SECTION;
    long h_cid = cid;
    char h_status[8];
    EXEC SQL END DECLARE SECTION;

    EXEC SQL SELECT status INTO :h_status FROM cl_close_log WHERE close_id = :h_cid;
    if (sqlca.sqlcode < 0 || sqlca.sqlcode == 100) {
        userlog("cldb_get_close_status FAIL [%d] %s", sqlca.sqlcode, sqlca.sqlerrm.sqlerrmc);
        return -1;
    }
    out[0] = h_status[0];
    out[1] = 0;
    return 0;
}

int cldb_update_close_totals(long cid, long txn_count, long total_amount)
{
    EXEC SQL BEGIN DECLARE SECTION;
    long h_cid = cid, h_cnt = txn_count, h_amt = total_amount;
    EXEC SQL END DECLARE SECTION;

    EXEC SQL UPDATE cl_close_log
        SET txn_count = :h_cnt, total_amount = :h_amt, updated_at = now()
        WHERE close_id = :h_cid;
    if (sqlca.sqlcode < 0 || sqlca.sqlcode == 100) {
        userlog("cldb_update_close_totals FAIL [%d] %s", sqlca.sqlcode, sqlca.sqlerrm.sqlerrmc);
        return -1;
    }
    return 0;
}

/* close_type 별 마지막 순번을 1 증가시켜 반환 (upsert). */
long cldb_bump_seq(const char *ctype)
{
    EXEC SQL BEGIN DECLARE SECTION;
    long h_seq;
    char h_type[16];
    EXEC SQL END DECLARE SECTION;

    strncpy(h_type, ctype, sizeof(h_type) - 1); h_type[sizeof(h_type)-1] = 0;

    EXEC SQL INSERT INTO cl_seq_reg (close_type, last_seq)
        VALUES (:h_type, 1)
        ON CONFLICT (close_type) DO UPDATE
        SET last_seq = cl_seq_reg.last_seq + 1, updated_at = now();
    if (sqlca.sqlcode < 0) {
        userlog("cldb_bump_seq FAIL [%d] %s", sqlca.sqlcode, sqlca.sqlerrm.sqlerrmc);
        return -1;
    }
    EXEC SQL SELECT last_seq INTO :h_seq FROM cl_seq_reg WHERE close_type = :h_type;
    if (sqlca.sqlcode < 0 || sqlca.sqlcode == 100) return -1;
    return h_seq;
}
