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

/* 당일 매입 금액 스프레드 = max(amount) - min(amount). */
long acdb_daily_amount_spread(const char *bizdate)
{
    EXEC SQL BEGIN DECLARE SECTION;
    char h_bizdate[16];
    long h_max = 0, h_min = 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(amount),0), coalesce(min(amount),0)
        INTO :h_max, :h_min FROM purchase WHERE biz_date = :h_bizdate;
    if (sqlca.sqlcode < 0) return -1;
    return h_max - h_min;
}
