/* Processed by ecpg (15.18 (Debian 15.18-0+deb12u1)) */
/* These include files are added by the preprocessor */
#include <ecpglib.h>
#include <ecpgerrno.h>
#include <sqlca.h>
/* End of automatic include section */

#line 1 "app/online/cm_ol_0009.pgc"
/* @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"


#line 1 "/usr/include/postgresql/sqlca.h"
#ifndef POSTGRES_SQLCA_H
#define POSTGRES_SQLCA_H

#ifndef PGDLLIMPORT
#if  defined(WIN32) || defined(__CYGWIN__)
#define PGDLLIMPORT __declspec (dllimport)
#else
#define PGDLLIMPORT
#endif							/* __CYGWIN__ */
#endif							/* PGDLLIMPORT */

#define SQLERRMC_LEN	150

#ifdef __cplusplus
extern "C"
{
#endif

struct sqlca_t
{
	char		sqlcaid[8];
	long		sqlabc;
	long		sqlcode;
	struct
	{
		int			sqlerrml;
		char		sqlerrmc[SQLERRMC_LEN];
	}			sqlerrm;
	char		sqlerrp[8];
	long		sqlerrd[6];
	/* Element 0: empty						*/
	/* 1: OID of processed tuple if applicable			*/
	/* 2: number of rows processed				*/
	/* after an INSERT, UPDATE or				*/
	/* DELETE statement					*/
	/* 3: empty						*/
	/* 4: empty						*/
	/* 5: empty						*/
	char		sqlwarn[8];
	/* Element 0: set to 'W' if at least one other is 'W'	*/
	/* 1: if 'W' at least one character string		*/
	/* value was truncated when it was			*/
	/* stored into a host variable.             */

	/*
	 * 2: if 'W' a (hopefully) non-fatal notice occurred
	 */	/* 3: empty */
	/* 4: empty						*/
	/* 5: empty						*/
	/* 6: empty						*/
	/* 7: empty						*/

	char		sqlstate[5];
};

struct sqlca_t *ECPGget_sqlca(void);

#ifndef POSTGRES_ECPG_INTERNAL
#define sqlca (*ECPGget_sqlca())
#endif

#ifdef __cplusplus
}
#endif

#endif

#line 19 "app/online/cm_ol_0009.pgc"


TX_SERVICE(CM_OL_0009, ctx)
{
    /* exec sql begin declare section */
     
     
     
     
      
    
#line 24 "app/online/cm_ol_0009.pgc"
 char h_cur [ 4 ] ;
 
#line 25 "app/online/cm_ol_0009.pgc"
 char h_basedate [ 9 ] ;
 
#line 26 "app/online/cm_ol_0009.pgc"
 char h_quotedate [ 9 ] ;
 
#line 27 "app/online/cm_ol_0009.pgc"
 long h_scaled ;
 
#line 28 "app/online/cm_ol_0009.pgc"
 int h_scale ;
/* exec sql end declare section */
#line 29 "app/online/cm_ol_0009.pgc"


    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건 */
    { ECPGdo(__LINE__, 0, 1, NULL, 0, ECPGst_normal, "select rate_scaled , rate_scale , quote_date from cm_rate where cur_cd = $1  and quote_date <= $2  and use_yn = 'Y' order by quote_date desc fetch first 1 row only", 
	ECPGt_char,(h_cur),(long)4,(long)1,(4)*sizeof(char), 
	ECPGt_NO_INDICATOR, NULL , 0L, 0L, 0L, 
	ECPGt_char,(h_basedate),(long)9,(long)1,(9)*sizeof(char), 
	ECPGt_NO_INDICATOR, NULL , 0L, 0L, 0L, ECPGt_EOIT, 
	ECPGt_long,&(h_scaled),(long)1,(long)1,sizeof(long), 
	ECPGt_NO_INDICATOR, NULL , 0L, 0L, 0L, 
	ECPGt_int,&(h_scale),(long)1,(long)1,sizeof(int), 
	ECPGt_NO_INDICATOR, NULL , 0L, 0L, 0L, 
	ECPGt_char,(h_quotedate),(long)9,(long)1,(9)*sizeof(char), 
	ECPGt_NO_INDICATOR, NULL , 0L, 0L, 0L, ECPGt_EORT);}
#line 76 "app/online/cm_ol_0009.pgc"


    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);
}
