/*
 * st_feefilter_dbio.pgc - st 모듈 DB 접근 함수 세트 (st_feefilter_dbio), archived into libstdbio.a.
 * ECPG (EXEC SQL) functions; run on the caller's XA branch. No EXEC SQL CONNECT.
 * MDR/VAN FILTER 분리 집계 - MDR 합 반환.
 */
#include <string.h>
#include <userlog.h>
#include "st_feefilter_dbio.h"

long stdb_fee_mdr_filter(const char *bizdate)
{
    EXEC SQL BEGIN DECLARE SECTION;
    char h_bizdate[16];
    long h_mdr = 0, h_van = 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(amount) FILTER (WHERE fee_type = 'MDR'), 0),
        coalesce(sum(amount) FILTER (WHERE fee_type = 'VAN'), 0)
        INTO :h_mdr, :h_van FROM st_settle_dtl WHERE biz_date = :h_bizdate;
    if (sqlca.sqlcode < 0) return -1;
    userlog("stdb_fee_mdr_filter van=%ld", h_van);
    return h_mdr;
}
