/*
 * st_netminmax_dbio.pgc - 상태별 순액 스팬 (st_netminmax_dbio), libstdbio.a.
 * ECPG; 특정 status 지급의 min/max/sum(net_amount) 을 한 번에 구해 합계를 반환.
 */
#include <string.h>
#include <userlog.h>
#include "st_netminmax_dbio.h"

long stdb_net_span(const char *bizdate, const char *status, long *min_out, long *max_out)
{
    EXEC SQL BEGIN DECLARE SECTION;
    char h_bizdate[16], h_status[16];
    long h_min = 0, h_max = 0, h_sum = 0;
    EXEC SQL END DECLARE SECTION;
    strncpy(h_bizdate, bizdate, sizeof(h_bizdate)-1); h_bizdate[sizeof(h_bizdate)-1] = 0;
    strncpy(h_status, status, sizeof(h_status)-1); h_status[sizeof(h_status)-1] = 0;
    EXEC SQL SELECT coalesce(min(net_amount),0), coalesce(max(net_amount),0), coalesce(sum(net_amount),0)
        INTO :h_min, :h_max, :h_sum FROM st_payment
        WHERE biz_date = :h_bizdate AND status = :h_status;
    if (sqlca.sqlcode < 0) { userlog("stdb_net_span FAIL [%d] %s", sqlca.sqlcode, sqlca.sqlerrm.sqlerrmc); return -1; }
    if (min_out) *min_out = h_min;
    if (max_out) *max_out = h_max;
    return h_sum;
}
