/* @tier easy @module mm @transform master-online */
/*
 * mm_ol_0006.pgc - 수수료율 등록/변경 서비스 (MM_OL_0006)  [tier=easy]
 *
 * 가맹점+상품구분의 수수료율을 신규 적용한다. 기존 유효 rate 를 전일자로
 * 종료처리(UPDATE end_date) 하고, 신규 rate 를 INSERT 한 뒤 변경이력
 * (mm_fee_hist) 에 남긴다. (CRUD + 이력)
 */
#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_0006, ctx)
{
    EXEC SQL BEGIN DECLARE SECTION;
    char h_merch_id[16];
    char h_prod_gb[4];
    char h_today[9];
    char h_far[9];
    long h_new_bp;
    long h_chg_by;
    EXEC SQL END DECLARE SECTION;

    char merch_id[16];
    char prod_gb[4];
    char rate_str[16];
    long rate_bp = 0;
    long chg_by = 0;

    tx_log(TX_LOG_INFO, "[MM_OL_0006] 수수료율 등록/변경 서비스 진입");

    if (tx_buf_get(ctx->in, "MERCHID", merch_id, sizeof(merch_id)) != TX_OK ||
        tx_buf_get(ctx->in, "PRODGB", prod_gb, sizeof(prod_gb)) != TX_OK) {
        tx_log(TX_LOG_ERROR, "[MM_OL_0006] 필수 필드 누락");
        tx_buf_sets(ctx->out, "ERRCODE", "MM0600");
        tx_return(ctx, TX_EINVAL, ctx->out);
        return;
    }
    tx_buf_getlong(ctx->in, "RATEBP", &rate_bp);
    tx_buf_getlong(ctx->in, "CHGBY", &chg_by);

    /* 수수료율 범위검증: 0 ~ 1000bp (0~10%) */
    if (rate_bp <= 0 || rate_bp > 1000) {
        tx_log(TX_LOG_WARN, "[MM_OL_0006] 수수료율 범위오류 bp=%ld", rate_bp);
        tx_buf_sets(ctx->out, "ERRCODE", "MM0601");
        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_prod_gb, prod_gb, sizeof(h_prod_gb) - 1);
    h_prod_gb[sizeof(h_prod_gb) - 1] = '\0';
    h_new_bp = rate_bp;
    h_chg_by = chg_by;
    date_today(h_today);
    strncpy(h_far, "99991231", sizeof(h_far) - 1);
    h_far[sizeof(h_far) - 1] = '\0';

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

    /* 기존 유효 rate 종료처리 (없어도 정상) */
    EXEC SQL
        UPDATE mm_fee_rate
           SET end_date = :h_today
         WHERE merch_id = :h_merch_id
           AND prod_gb  = :h_prod_gb
           AND end_date = '99991231';

    if (sqlca.sqlcode != TX_SQL_OK && sqlca.sqlcode != TX_SQL_NOTFOUND) {
        TX_DBIO_LOG_ERR("UPDATE end_date mm_fee_rate");
        tx_abort();
        tx_buf_sets(ctx->out, "ERRCODE", "MM0602");
        tx_return(ctx, TX_EDB, ctx->out);
        return;
    }

    /* 신규 rate INSERT */
    EXEC SQL
        INSERT INTO mm_fee_rate
               (merch_id, prod_gb, rate_bp, start_date, end_date)
        VALUES (:h_merch_id, :h_prod_gb, :h_new_bp, :h_today, :h_far);

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

    /* 변경이력 INSERT */
    EXEC SQL
        INSERT INTO mm_fee_hist
               (merch_id, prod_gb, rate_bp, apply_date, chg_by)
        VALUES (:h_merch_id, :h_prod_gb, :h_new_bp, :h_today, :h_chg_by);

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

    tx_commit();

    snprintf(rate_str, sizeof(rate_str), "%ld.%02ld",
             h_new_bp / 100, h_new_bp % 100);

    tx_buf_reset(ctx->out);
    tx_buf_sets(ctx->out, "MERCHID", merch_id);
    tx_buf_sets(ctx->out, "PRODGB", prod_gb);
    tx_buf_setlong(ctx->out, "RATEBP", h_new_bp);
    tx_buf_sets(ctx->out, "RATEPCT", rate_str);
    tx_buf_sets(ctx->out, "STARTDATE", h_today);
    tx_buf_sets(ctx->out, "ERRCODE", RESP_OK);
    tx_log(TX_LOG_INFO, "[MM_OL_0006] 수수료율 변경완료 merch=%s prod=%s rate=%s%%",
           merch_id, prod_gb, rate_str);
    tx_return(ctx, TX_OK, ctx->out);
}
