/*
 * ac_fx_query_dbio.pgc - ac 모듈 DB 접근 함수 세트 (ac_fx_query_dbio), archived into libacdbio.a.
 * ECPG (EXEC SQL) functions; run on the caller's XA branch. No EXEC SQL CONNECT.
 */
#include <string.h>
#include <userlog.h>
#include "ac_fx_query_dbio.h"

/* 원통화합/원화합을 함께 조회하고 원화 환산 총액 반환. */
long acdb_fx_krw_total(const char *bizdate)
{
    EXEC SQL BEGIN DECLARE SECTION;
    char h_bizdate[16];
    long h_krw = 0, h_cnt = 0;
    EXEC SQL END DECLARE SECTION;
    strncpy(h_bizdate, bizdate, sizeof(h_bizdate)-1); h_bizdate[sizeof(h_bizdate)-1] = 0;
    EXEC SQL SELECT coalesce(sum(krw_amount),0), count(*)
        INTO :h_krw, :h_cnt FROM ac_fx WHERE biz_date = :h_bizdate;
    if (sqlca.sqlcode < 0) return -1;
    if (h_cnt == 0) return 0;
    return h_krw;
}

/* 가중평균 환율(1/100 KRW) = sum(krw)*100 / sum(fx). */
long acdb_fx_wavg_rate(const char *bizdate)
{
    EXEC SQL BEGIN DECLARE SECTION;
    char h_bizdate[16];
    long h_krw = 0, h_fx = 0;
    EXEC SQL END DECLARE SECTION;
    strncpy(h_bizdate, bizdate, sizeof(h_bizdate)-1); h_bizdate[sizeof(h_bizdate)-1] = 0;
    EXEC SQL SELECT coalesce(sum(krw_amount),0), coalesce(sum(fx_amount),0)
        INTO :h_krw, :h_fx FROM ac_fx WHERE biz_date = :h_bizdate;
    if (sqlca.sqlcode < 0) return -1;
    if (h_fx <= 0) return 0;
    return h_krw * 100 / h_fx;
}
