/* @tier hard @module mm @transform master-online */
/*
 * mm_ol_0022.pgc - 수수료율 이력등록 서비스 (MM_OL_0022)  [tier=hard]
 *
 * 가맹점 수수료율 변경건을 감사 목적의 이력 테이블(mm_fee_hist)에 적재한다.
 * 적용일(유효성 검증)/신규 수수료율(bp, 0~10000)/변경자 를 받아, 같은
 * 적용일에 이미 등록된 이력이 있으면 중복 반려한다. (이력 감사 등록)
 */
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

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

EXEC SQL INCLUDE sqlca;

TX_SERVICE(MM_OL_0022, ctx)
{
    EXEC SQL BEGIN DECLARE SECTION;
    char h_merch_id[16];
    char h_apply_date[9];
    char h_oper[16];
    char h_reg_date[9];
    long h_rate_bp;
    long h_dup;
    EXEC SQL END DECLARE SECTION;

    char merch_id[16];
    char apply_date[9];
    char oper[16];
    long rate_bp = 0;

    tx_log(TX_LOG_INFO, "[MM_OL_0022] 수수료율 이력등록 서비스 진입");

    if (tx_buf_get(ctx->in, "MERCHID", merch_id, sizeof(merch_id)) != TX_OK ||
        tx_buf_get(ctx->in, "APPLYDATE", apply_date, sizeof(apply_date)) != TX_OK ||
        tx_buf_get(ctx->in, "OPER", oper, sizeof(oper)) != TX_OK) {
        tx_log(TX_LOG_ERROR, "[MM_OL_0022] 필수 필드 누락");
        tx_buf_sets(ctx->out, "ERRCODE", "MM2200");
        tx_return(ctx, TX_EINVAL, ctx->out);
        return;
    }
    tx_buf_getlong(ctx->in, "RATEBP", &rate_bp);

    /* 적용일 형식 및 수수료율 범위(0~10000 bp = 0~100%) 검증 */
    if (!date_is_valid(apply_date)) {
        tx_log(TX_LOG_WARN, "[MM_OL_0022] 적용일 오류 date=%s", apply_date);
        tx_buf_sets(ctx->out, "ERRCODE", "MM2201");
        tx_return(ctx, TX_EINVAL, ctx->out);
        return;
    }
    if (rate_bp < 0 || rate_bp > 10000) {
        tx_log(TX_LOG_WARN, "[MM_OL_0022] 수수료율 범위 오류 bp=%ld", rate_bp);
        tx_buf_sets(ctx->out, "ERRCODE", "MM2202");
        tx_return(ctx, TX_EINVAL, ctx->out);
        return;
    }

    strncpy(h_merch_id, merch_id, sizeof(h_merch_id) - 1);
    h_merch_id[sizeof(h_merch_id) - 1] = '\0';
    strncpy(h_apply_date, apply_date, sizeof(h_apply_date) - 1);
    h_apply_date[sizeof(h_apply_date) - 1] = '\0';
    strncpy(h_oper, oper, sizeof(h_oper) - 1);
    h_oper[sizeof(h_oper) - 1] = '\0';
    h_rate_bp = rate_bp;
    date_today(h_reg_date);

    /* 동일 적용일 중복 이력 확인 */
    EXEC SQL
        SELECT COUNT(*) INTO :h_dup
          FROM mm_fee_hist
         WHERE merch_id = :h_merch_id
           AND apply_date = :h_apply_date;

    if (sqlca.sqlcode != TX_SQL_OK && sqlca.sqlcode != TX_SQL_NOTFOUND) {
        TX_DBIO_LOG_ERR("SELECT mm_fee_hist dup");
        tx_buf_sets(ctx->out, "ERRCODE", "MM2209");
        tx_return(ctx, TX_EDB, ctx->out);
        return;
    }
    if (h_dup > 0) {
        tx_log(TX_LOG_WARN, "[MM_OL_0022] 동일 적용일 이력 중복 merch=%s date=%s",
               merch_id, apply_date);
        tx_buf_sets(ctx->out, "ERRCODE", "MM2203");
        tx_return(ctx, TX_FAIL, ctx->out);
        return;
    }

    if (tx_begin() != TX_OK)
        tx_log(TX_LOG_WARN, "[MM_OL_0022] tx_begin 경고");

    EXEC SQL
        INSERT INTO mm_fee_hist
               (merch_id, apply_date, rate_bp, oper, reg_date)
        VALUES (:h_merch_id, :h_apply_date, :h_rate_bp, :h_oper, :h_reg_date);

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

    tx_commit();

    tx_buf_reset(ctx->out);
    tx_buf_sets(ctx->out, "MERCHID", merch_id);
    tx_buf_sets(ctx->out, "APPLYDATE", h_apply_date);
    tx_buf_setlong(ctx->out, "RATEBP", h_rate_bp);
    tx_buf_sets(ctx->out, "REGDATE", h_reg_date);
    tx_buf_sets(ctx->out, "ERRCODE", RESP_OK);
    tx_log(TX_LOG_INFO, "[MM_OL_0022] 수수료율 이력등록 완료 merch=%s bp=%ld",
           merch_id, h_rate_bp);
    tx_return(ctx, TX_OK, ctx->out);
}
