/* @tier medium @module cl @transform closing-dbio */
/*
 * cl_dbio_main.pgc - 마감 표준 DBIO (ECPG 임베디드 SQL)
 *
 * cl_core.h 의 표준 API 를 cl_ledger 테이블에 대해 구현한다.
 * 온라인/배치 프로그램은 이 함수들만 호출한다 (self-contained).
 */
#include <stdio.h>
#include <string.h>

#include "txcore.h"
#include "txcore_dbio.h"
#include "cl_core.h"

EXEC SQL INCLUDE sqlca;

int cl_dbio_connect(const char *target)
{
    EXEC SQL BEGIN DECLARE SECTION;
    const char *h_target;
    EXEC SQL END DECLARE SECTION;

    h_target = (target && target[0]) ? target : "acquire@localhost";

    EXEC SQL CONNECT TO :h_target;
    if (sqlca.sqlcode != TX_SQL_OK) {
        TX_DBIO_LOG_ERR("cl CONNECT");
        return TX_EDB;
    }
    tx_log(TX_LOG_INFO, "[cl] DB 접속 완료 target=%s", h_target);
    return TX_OK;
}

int cl_dbio_disconnect(void)
{
    EXEC SQL DISCONNECT CURRENT;
    return TX_OK;
}

int cl_rec_insert(const cl_rec_t *rec)
{
    EXEC SQL BEGIN DECLARE SECTION;
    char h_key[16];
    char h_merch_id[16];
    char h_card_no[20];
    long h_amount;
    char h_biz_date[9];
    char h_status[2];
    long h_fee;
    EXEC SQL END DECLARE SECTION;

    if (!rec)
        return TX_EINVAL;

    strncpy(h_key, rec->key, sizeof(h_key) - 1);
    h_key[sizeof(h_key) - 1] = '\0';
    strncpy(h_merch_id, rec->merch_id, sizeof(h_merch_id) - 1);
    h_merch_id[sizeof(h_merch_id) - 1] = '\0';
    strncpy(h_card_no, rec->card_no, sizeof(h_card_no) - 1);
    h_card_no[sizeof(h_card_no) - 1] = '\0';
    strncpy(h_biz_date, rec->biz_date, sizeof(h_biz_date) - 1);
    h_biz_date[sizeof(h_biz_date) - 1] = '\0';
    strncpy(h_status, rec->status, sizeof(h_status) - 1);
    h_status[sizeof(h_status) - 1] = '\0';
    h_amount = rec->amount;
    h_fee    = rec->fee;

    EXEC SQL INSERT INTO cl_ledger
        (key, merch_id, card_no, amount, biz_date, status, fee)
        VALUES
        (:h_key, :h_merch_id, :h_card_no, :h_amount, :h_biz_date, :h_status, :h_fee);

    if (sqlca.sqlcode != TX_SQL_OK) {
        TX_DBIO_LOG_ERR("cl_rec_insert");
        return TX_EDB;
    }
    return TX_OK;
}

int cl_rec_select(const char *key, cl_rec_t *out)
{
    EXEC SQL BEGIN DECLARE SECTION;
    char h_key[16];
    char h_merch_id[16];
    char h_card_no[20];
    long h_amount;
    char h_biz_date[9];
    char h_status[2];
    long h_fee;
    EXEC SQL END DECLARE SECTION;

    if (!key || !out)
        return TX_EINVAL;

    strncpy(h_key, key, sizeof(h_key) - 1);
    h_key[sizeof(h_key) - 1] = '\0';

    EXEC SQL SELECT merch_id, card_no, amount, biz_date, status, fee
        INTO :h_merch_id, :h_card_no, :h_amount, :h_biz_date, :h_status, :h_fee
        FROM cl_ledger
        WHERE key = :h_key;

    if (sqlca.sqlcode == TX_SQL_NOTFOUND)
        return TX_ENOENT;
    if (sqlca.sqlcode != TX_SQL_OK) {
        TX_DBIO_LOG_ERR("cl_rec_select");
        return TX_EDB;
    }

    memset(out, 0, sizeof(*out));
    strncpy(out->key, h_key, sizeof(out->key) - 1);
    strncpy(out->merch_id, h_merch_id, sizeof(out->merch_id) - 1);
    strncpy(out->card_no, h_card_no, sizeof(out->card_no) - 1);
    strncpy(out->biz_date, h_biz_date, sizeof(out->biz_date) - 1);
    strncpy(out->status, h_status, sizeof(out->status) - 1);
    out->amount = h_amount;
    out->fee    = h_fee;
    return TX_OK;
}

int cl_rec_update_status(const char *key, const char *status)
{
    EXEC SQL BEGIN DECLARE SECTION;
    char h_key[16];
    char h_status[2];
    EXEC SQL END DECLARE SECTION;

    if (!key || !status)
        return TX_EINVAL;

    strncpy(h_key, key, sizeof(h_key) - 1);
    h_key[sizeof(h_key) - 1] = '\0';
    strncpy(h_status, status, sizeof(h_status) - 1);
    h_status[sizeof(h_status) - 1] = '\0';

    EXEC SQL UPDATE cl_ledger
        SET status = :h_status, upd_ts = now()
        WHERE key = :h_key;

    if (sqlca.sqlcode != TX_SQL_OK) {
        TX_DBIO_LOG_ERR("cl_rec_update_status");
        return TX_EDB;
    }
    if (sqlca.sqlerrd[2] == 0)
        return TX_ENOENT;
    return TX_OK;
}

long cl_rec_count(const char *biz_date, const char *status)
{
    EXEC SQL BEGIN DECLARE SECTION;
    char h_biz_date[9];
    char h_status[2];
    int  h_cnt;
    EXEC SQL END DECLARE SECTION;

    if (!biz_date || !status)
        return -1;

    strncpy(h_biz_date, biz_date, sizeof(h_biz_date) - 1);
    h_biz_date[sizeof(h_biz_date) - 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 cl_ledger
        WHERE biz_date = :h_biz_date
          AND status   = :h_status;

    if (sqlca.sqlcode != TX_SQL_OK) {
        TX_DBIO_LOG_ERR("cl_rec_count");
        return -1;
    }
    return (long)h_cnt;
}

int cl_rec_sum(const char *biz_date, long *out_sum)
{
    EXEC SQL BEGIN DECLARE SECTION;
    char h_biz_date[9];
    long h_sum;
    EXEC SQL END DECLARE SECTION;

    if (!biz_date || !out_sum)
        return TX_EINVAL;

    strncpy(h_biz_date, biz_date, sizeof(h_biz_date) - 1);
    h_biz_date[sizeof(h_biz_date) - 1] = '\0';

    EXEC SQL SELECT coalesce(sum(amount), 0)
        INTO :h_sum
        FROM cl_ledger
        WHERE biz_date = :h_biz_date;

    if (sqlca.sqlcode != TX_SQL_OK) {
        TX_DBIO_LOG_ERR("cl_rec_sum");
        return TX_EDB;
    }
    *out_sum = h_sum;
    return TX_OK;
}
