/*
 * mm_fee_in_dbio.pgc - mm 모듈 DB 접근 함수 (mm_fee_in_dbio), archived into libmmdbio.a.
 * 특정 등급 가맹점의 평균 수수료율 (IN 서브셀렉트).
 * ECPG (EXEC SQL) functions; run on the caller's XA branch. No EXEC SQL CONNECT.
 */
#include <string.h>
#include <userlog.h>
#include "mm_fee_in_dbio.h"

/* grade 에 해당하는 가맹점(IN) 의 CREDIT 수수료율 평균(bps). */
long mmdb_fee_in_avg(const char *grade)
{
    EXEC SQL BEGIN DECLARE SECTION;
    char h_grade[8];
    long h_v = 0;
    EXEC SQL END DECLARE SECTION;
    strncpy(h_grade, grade, sizeof(h_grade)-1); h_grade[sizeof(h_grade)-1] = 0;
    EXEC SQL SELECT coalesce(avg(rate_bps),0)::bigint INTO :h_v FROM mm_fee_rate
        WHERE card_type = 'CREDIT'
          AND merchant_id IN (SELECT merchant_id FROM mm_merchant_ext WHERE grade = :h_grade);
    if (sqlca.sqlcode < 0) return -1;
    return h_v;
}
