/*
 * ac_feeadj_query_dbio.pgc - ac 모듈 DB 접근 함수 세트 (ac_feeadj_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_feeadj_query_dbio.h"

/* 정산완료(status='S') 매입에 걸린 수수료조정 delta 합계 (JOIN). */
long acdb_feeadj_settled_delta(const char *bizdate)
{
    EXEC SQL BEGIN DECLARE SECTION;
    char h_bizdate[16];
    long h_v = 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(a.delta),0) INTO :h_v
        FROM ac_fee_adj a JOIN purchase p ON p.purchase_id = a.purchase_id
        WHERE a.biz_date = :h_bizdate AND p.status = 'S';
    if (sqlca.sqlcode < 0) { userlog("acdb_feeadj_settled_delta FAIL [%d]", sqlca.sqlcode); return -1; }
    return h_v;
}

/* 당일 최대 절대 조정폭 (max(abs(delta))). */
long acdb_feeadj_max_abs(const char *bizdate)
{
    EXEC SQL BEGIN DECLARE SECTION;
    char h_bizdate[16];
    long h_v = 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(max(abs(delta)),0) INTO :h_v FROM ac_fee_adj WHERE biz_date = :h_bizdate;
    if (sqlca.sqlcode < 0) return -1;
    return h_v;
}
