/* @tier medium @module cm @transform common-online */
/*
 * cm_ol_0009.pgc - 환율 조회 서비스 (CM_OL_0009)  [tier=medium]
 *
 * 요청의 통화코드(CUR)/기준일자(BASEDATE)로 공통 환율 테이블(cm_rate)
 * 에서 매매기준율을 조회한다. 소수 스케일은 정수(rate_scaled)+스케일
 * 자리수로 관리되므로, 기준일 이하 최근 고시분을 조회해 표기 문자열로
 * 재구성하여 응답한다.
 * (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;

TX_SERVICE(CM_OL_0009, ctx)
{
    EXEC SQL BEGIN DECLARE SECTION;
    char h_cur[4];
    char h_basedate[9];
    char h_quotedate[9];
    long h_scaled;
    int  h_scale;
    EXEC SQL END DECLARE SECTION;

    char cur[4];
    char basedate[9];
    char rate_disp[32];
    long ipart;
    long fpart;
    int  i;
    long divisor;

    tx_log(TX_LOG_INFO, "[CM_OL_0009] 환율 조회 서비스 진입");

    memset(cur, 0, sizeof(cur));
    memset(basedate, 0, sizeof(basedate));
    memset(rate_disp, 0, sizeof(rate_disp));

    if (tx_buf_get(ctx->in, "CUR", cur, sizeof(cur)) != TX_OK) {
        tx_log(TX_LOG_ERROR, "[CM_OL_0009] 통화코드(CUR) 누락");
        tx_return(ctx, TX_EINVAL, ctx->out);
        return;
    }
    /* 기준일자 미지정 시 당일 기준 */
    if (tx_buf_get(ctx->in, "BASEDATE", basedate, sizeof(basedate)) != TX_OK) {
        date_today(basedate);
    }
    if (!date_is_valid(basedate)) {
        tx_log(TX_LOG_WARN, "[CM_OL_0009] 기준일자 오류 date=%s", basedate);
        tx_return(ctx, TX_EINVAL, ctx->out);
        return;
    }

    memset(h_cur, 0, sizeof(h_cur));
    memset(h_basedate, 0, sizeof(h_basedate));
    memset(h_quotedate, 0, sizeof(h_quotedate));
    strncpy(h_cur, cur, sizeof(h_cur) - 1);
    strncpy(h_basedate, basedate, sizeof(h_basedate) - 1);
    h_scaled = 0;
    h_scale  = 0;

    /* 기준일 이하 최근 고시 1건 */
    EXEC SQL SELECT rate_scaled, rate_scale, quote_date
          INTO :h_scaled, :h_scale, :h_quotedate
          FROM cm_rate
         WHERE cur_cd    = :h_cur
           AND quote_date <= :h_basedate
           AND use_yn    = 'Y'
         ORDER BY quote_date DESC
         FETCH FIRST 1 ROW ONLY;

    if (sqlca.sqlcode == TX_SQL_NOTFOUND) {
        tx_log(TX_LOG_WARN, "[CM_OL_0009] 환율 없음 cur=%s date=%s", cur, basedate);
        tx_return(ctx, TX_ENOENT, ctx->out);
        return;
    }
    if (sqlca.sqlcode != TX_SQL_OK) {
        TX_DBIO_LOG_ERR("CM_OL_0009 SELECT cm_rate");
        tx_return(ctx, TX_EDB, ctx->out);
        return;
    }

    /* 스케일 방어 후 정수부/소수부 분리 표기 */
    if (h_scale < 0 || h_scale > 8) {
        h_scale = 2;
    }
    divisor = 1;
    for (i = 0; i < h_scale; i++) {
        divisor *= 10;
    }
    ipart = h_scaled / divisor;
    fpart = h_scaled % divisor;
    if (fpart < 0) {
        fpart = -fpart;
    }
    if (h_scale > 0) {
        snprintf(rate_disp, sizeof(rate_disp), "%ld.%0*ld", ipart, h_scale, fpart);
    } else {
        snprintf(rate_disp, sizeof(rate_disp), "%ld", ipart);
    }

    tx_buf_reset(ctx->out);
    tx_buf_sets(ctx->out, "CUR", cur);
    tx_buf_sets(ctx->out, "BASEDATE", basedate);
    tx_buf_sets(ctx->out, "QUOTEDATE", h_quotedate);
    tx_buf_setlong(ctx->out, "RATESCALED", h_scaled);
    tx_buf_setlong(ctx->out, "SCALE", (long)h_scale);
    tx_buf_sets(ctx->out, "RATE", rate_disp);

    tx_log(TX_LOG_INFO, "[CM_OL_0009] 조회완료 cur=%s rate=%s (고시일 %s)",
           cur, rate_disp, h_quotedate);
    tx_return(ctx, TX_OK, ctx->out);
}
