/* @tier medium @module cm @transform common-online */
/*
 * cm_ol_0007.pgc - 로그 공통 기록 서비스 (CM_OL_0007)  [tier=medium]
 *
 * 요청의 로그레벨(LEVEL)/모듈(MODULE)/메시지(MESSAGE)를 받아 공통
 * 로그 테이블(cm_log)에 INSERT 하고, 채번된 로그일련번호를 응답한다.
 * 일련번호는 당일 최대치 +1 로 산출하며 트랜잭션 안에서 기록한다.
 * (ATMI void SVC(TPSVCINFO*) 대응)
 */
#include <stdio.h>
#include <string.h>

#include "txcore.h"
#include "txcore_dbio.h"
#include "acq_util.h"
#include "cm_core.h"

EXEC SQL INCLUDE sqlca;

/* 로그레벨 문자열 → 유효성(허용레벨만 기록) */
static int level_is_allowed(const char *lv)
{
    return (strcmp(lv, "DEBUG") == 0 || strcmp(lv, "INFO") == 0 ||
            strcmp(lv, "WARN") == 0 || strcmp(lv, "ERROR") == 0);
}

TX_SERVICE(CM_OL_0007, ctx)
{
    EXEC SQL BEGIN DECLARE SECTION;
    char h_bizdate[9];
    char h_level[8];
    char h_module[16];
    char h_message[256];
    long h_logseq;
    long h_xid;
    EXEC SQL END DECLARE SECTION;

    char level[8];
    char module[16];
    char message[256];

    tx_log(TX_LOG_INFO, "[CM_OL_0007] 로그 공통 기록 서비스 진입");

    memset(level, 0, sizeof(level));
    memset(module, 0, sizeof(module));
    memset(message, 0, sizeof(message));

    if (tx_buf_get(ctx->in, "LEVEL", level, sizeof(level)) != TX_OK ||
        tx_buf_get(ctx->in, "MODULE", module, sizeof(module)) != TX_OK ||
        tx_buf_get(ctx->in, "MESSAGE", message, sizeof(message)) != TX_OK) {
        tx_log(TX_LOG_ERROR, "[CM_OL_0007] 로그 필수 필드 누락");
        tx_return(ctx, TX_EINVAL, ctx->out);
        return;
    }

    if (!level_is_allowed(level)) {
        tx_log(TX_LOG_WARN, "[CM_OL_0007] 미허용 로그레벨 level=%s", level);
        tx_return(ctx, TX_EINVAL, ctx->out);
        return;
    }

    memset(h_bizdate, 0, sizeof(h_bizdate));
    memset(h_level, 0, sizeof(h_level));
    memset(h_module, 0, sizeof(h_module));
    memset(h_message, 0, sizeof(h_message));
    date_today(h_bizdate);
    strncpy(h_level, level, sizeof(h_level) - 1);
    strncpy(h_module, module, sizeof(h_module) - 1);
    strncpy(h_message, message, sizeof(h_message) - 1);
    h_logseq = 0;
    h_xid    = tx_current_xid();

    if (tx_begin() != TX_OK) {
        tx_log(TX_LOG_ERROR, "[CM_OL_0007] 트랜잭션 시작 실패");
        tx_return(ctx, TX_FAIL, ctx->out);
        return;
    }

    /* 당일 로그일련번호 채번 (당일 최대치 +1) */
    EXEC SQL SELECT COALESCE(MAX(log_seq), 0) + 1
          INTO :h_logseq
          FROM cm_log
         WHERE biz_date = :h_bizdate;

    if (sqlca.sqlcode != TX_SQL_OK && sqlca.sqlcode != TX_SQL_NOTFOUND) {
        TX_DBIO_LOG_ERR("CM_OL_0007 SELECT MAX cm_log");
        tx_abort();
        tx_return(ctx, TX_EDB, ctx->out);
        return;
    }
    if (h_logseq <= 0) {
        h_logseq = 1;
    }

    EXEC SQL INSERT INTO cm_log
                    (biz_date, log_seq, log_level, module_id, log_msg, xid, reg_ts)
             VALUES (:h_bizdate, :h_logseq, :h_level, :h_module, :h_message,
                     :h_xid, CURRENT_TIMESTAMP);

    if (sqlca.sqlcode != TX_SQL_OK) {
        TX_DBIO_LOG_ERR("CM_OL_0007 INSERT cm_log");
        tx_abort();
        tx_return(ctx, TX_EDB, ctx->out);
        return;
    }

    tx_commit();

    tx_buf_reset(ctx->out);
    tx_buf_sets(ctx->out, "BIZDATE", h_bizdate);
    tx_buf_setlong(ctx->out, "LOGSEQ", h_logseq);
    tx_buf_setlong(ctx->out, "XID", h_xid);

    tx_log(TX_LOG_INFO, "[CM_OL_0007] 로그기록 완료 date=%s seq=%ld level=%s",
           h_bizdate, h_logseq, level);
    tx_return(ctx, TX_OK, ctx->out);
}
