/* @tier medium @module vl @transform validation-online */
/*
 * vl_ol_0002.pgc - 정합성 접수등록 서비스 (VL_OL_0002)
 *
 * 매입 정합성 대상 거래를 접수한다. 필수필드(처리키/가맹점/영업일/금액)를
 * 검증한 뒤 표준 DBIO 로 원장에 접수(vl_rec_insert)하고, 접수이력 테이블
 * (vl_recv_hist) 에 감사 로그를 남긴다. 마지막으로 당일 접수건수를 집계하여
 * 응답에 실어 반환한다.
 */
#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_0002, ctx)
{
    EXEC SQL BEGIN DECLARE SECTION;
    char h_key[16];
    char h_merch[16];
    char h_bizdate[9];
    char h_status[2];
    long h_amount;
    char h_recv_time[7];
    EXEC SQL END DECLARE SECTION;

    vl_rec_t rec;
    char key[16], merch[16], bizdate[16];
    long amount = 0, cnt;
    int  rc;

    tx_log(TX_LOG_INFO, "[VL_OL_0002] 정합성 접수등록 서비스 진입");

    /* 1) 필수필드 검증 */
    if (tx_buf_get(ctx->in, "KEY", key, sizeof(key)) != TX_OK ||
        tx_buf_get(ctx->in, "MERCHID", merch, sizeof(merch)) != TX_OK ||
        tx_buf_get(ctx->in, "BIZDATE", bizdate, sizeof(bizdate)) != TX_OK) {
        tx_log(TX_LOG_ERROR, "[VL_OL_0002] 필수 필드 누락");
        tx_buf_sets(ctx->out, "ERRCODE", "VL0200");
        tx_return(ctx, TX_EINVAL, ctx->out);
        return;
    }
    tx_buf_getlong(ctx->in, "AMOUNT", &amount);

    if (!amount_is_valid(amount) || !date_is_valid(bizdate)) {
        tx_log(TX_LOG_WARN, "[VL_OL_0002] 입력값 검증 실패 amount=%ld date=%s",
               amount, bizdate);
        tx_buf_sets(ctx->out, "ERRCODE", "VL0201");
        tx_return(ctx, TX_EINVAL, ctx->out);
        return;
    }

    /* 2) 원장 접수 (표준 DBIO) */
    memset(&rec, 0, sizeof(rec));
    strncpy(rec.key, key, sizeof(rec.key) - 1);
    strncpy(rec.merch_id, merch, sizeof(rec.merch_id) - 1);
    strncpy(rec.biz_date, bizdate, sizeof(rec.biz_date) - 1);
    strncpy(rec.status, VL_ST_RECV, sizeof(rec.status) - 1);
    rec.amount = amount;
    rec.fee    = amount / 100;

    rc = vl_rec_insert(&rec);
    if (rc != TX_OK) {
        tx_log(TX_LOG_ERROR, "[VL_OL_0002] 원장접수 실패 rc=%d(%s)",
               rc, tx_strerror(rc));
        tx_buf_sets(ctx->out, "ERRCODE", "VL0202");
        tx_return(ctx, rc, ctx->out);
        return;
    }

    /* 3) 접수이력 INSERT (감사 로그) */
    strncpy(h_key, key, sizeof(h_key) - 1);        h_key[sizeof(h_key) - 1] = '\0';
    strncpy(h_merch, merch, sizeof(h_merch) - 1);  h_merch[sizeof(h_merch) - 1] = '\0';
    strncpy(h_bizdate, bizdate, sizeof(h_bizdate) - 1);
    h_bizdate[sizeof(h_bizdate) - 1] = '\0';
    strncpy(h_status, VL_ST_RECV, sizeof(h_status) - 1);
    h_status[sizeof(h_status) - 1] = '\0';
    h_amount = amount;
    strncpy(h_recv_time, "000000", sizeof(h_recv_time) - 1);
    h_recv_time[sizeof(h_recv_time) - 1] = '\0';

    EXEC SQL INSERT INTO vl_recv_hist
            (recv_key, merch_id, biz_date, recv_amount, recv_status, recv_time)
        VALUES
            (:h_key, :h_merch, :h_bizdate, :h_amount, :h_status, :h_recv_time);

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

    /* 4) 당일 접수건수 집계 */
    cnt = vl_rec_count(bizdate, VL_ST_RECV);
    if (cnt < 0)
        cnt = 0;

    tx_buf_reset(ctx->out);
    tx_buf_sets(ctx->out, "RESPCODE", RESP_OK);
    tx_buf_sets(ctx->out, "ERRCODE", RESP_OK);
    tx_buf_sets(ctx->out, "KEY", key);
    tx_buf_setlong(ctx->out, "RECVAMT", amount);
    tx_buf_setlong(ctx->out, "DAYCNT", cnt);
    tx_log(TX_LOG_INFO, "[VL_OL_0002] 접수완료 key=%s 당일건수=%ld", key, cnt);
    tx_return(ctx, TX_OK, ctx->out);
}
