/* @tier easy @module vl @transform validation-online */
/*
 * vl_ol_0030.pgc - 정합성 승인처리 서비스 (VL_OL_0030)  [tier=easy]
 *
 * 검토완료된 원장건을 완료(M) 상태로 전이하고, 승인이력(vl_approval_hist)
 * 에 승인자/승인일시를 INSERT 한다.
 */
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

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

EXEC SQL INCLUDE sqlca;

TX_SERVICE(VL_OL_0030, ctx)
{
    EXEC SQL BEGIN DECLARE SECTION;
    char h_key[16];
    char h_approver[16];
    EXEC SQL END DECLARE SECTION;

    vl_rec_t rec;
    char key[16];
    char approver[16];
    int  rc;

    tx_log(TX_LOG_INFO, "[VL_OL_0030] 정합성 승인처리 서비스 진입");

    if (tx_buf_get(ctx->in, "KEY", key, sizeof(key)) != TX_OK ||
        tx_buf_get(ctx->in, "APPROVER", approver, sizeof(approver)) != TX_OK) {
        tx_log(TX_LOG_ERROR, "[VL_OL_0030] 필수 필드(KEY/APPROVER) 누락");
        tx_buf_sets(ctx->out, "ERRCODE", "VL3000");
        tx_return(ctx, TX_EINVAL, ctx->out);
        return;
    }

    memset(&rec, 0, sizeof(rec));
    rc = vl_rec_select(key, &rec);
    if (rc != TX_OK) {
        tx_log(TX_LOG_WARN, "[VL_OL_0030] 원장 없음 key=%s rc=%d(%s)",
               key, rc, tx_strerror(rc));
        tx_buf_sets(ctx->out, "ERRCODE", "VL3001");
        tx_return(ctx, rc, ctx->out);
        return;
    }

    /* 이미 완료/정산된 건은 승인 대상이 아니다 */
    if (strcmp(rec.status, VL_ST_DONE) == 0 ||
        strcmp(rec.status, VL_ST_SETTLED) == 0) {
        tx_log(TX_LOG_WARN, "[VL_OL_0030] 승인불가 상태 key=%s status=%s",
               key, rec.status);
        tx_buf_reset(ctx->out);
        tx_buf_sets(ctx->out, "KEY", key);
        tx_buf_sets(ctx->out, "STATUS", rec.status);
        tx_buf_sets(ctx->out, "RESULT", "ALREADY");
        tx_buf_sets(ctx->out, "ERRCODE", "VL3002");
        tx_return(ctx, TX_OK, ctx->out);
        return;
    }

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

    rc = vl_rec_update_status(key, VL_ST_DONE);
    if (rc != TX_OK) {
        tx_log(TX_LOG_ERROR, "[VL_OL_0030] 상태전이(M) 실패 key=%s rc=%d(%s)",
               key, rc, tx_strerror(rc));
        tx_abort();
        tx_buf_sets(ctx->out, "ERRCODE", "VL3004");
        tx_return(ctx, rc, ctx->out);
        return;
    }

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

    EXEC SQL
        INSERT INTO vl_approval_hist
               (txn_key, approver_id, appr_dt, prev_status)
        VALUES (:h_key, :h_approver,
                TO_CHAR(SYSTIMESTAMP, 'YYYYMMDDHH24MISS'), 'R');

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

    if (tx_commit() != TX_OK) {
        tx_log(TX_LOG_ERROR, "[VL_OL_0030] 커밋 실패 key=%s", key);
        tx_abort();
        tx_buf_sets(ctx->out, "ERRCODE", "VL3005");
        tx_return(ctx, TX_EDB, ctx->out);
        return;
    }

    tx_buf_reset(ctx->out);
    tx_buf_sets(ctx->out, "KEY", key);
    tx_buf_sets(ctx->out, "APPROVER", approver);
    tx_buf_sets(ctx->out, "STATUS", VL_ST_DONE);
    tx_buf_sets(ctx->out, "RESULT", "APPROVED");
    tx_buf_sets(ctx->out, "ERRCODE", RESP_OK);

    tx_log(TX_LOG_INFO, "[VL_OL_0030] 승인처리 완료 key=%s approver=%s",
           key, approver);
    tx_return(ctx, TX_OK, ctx->out);
}
