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

/* 지정 채널의 당일 매입 총액 (channel 인자). */
long acdb_channel_gross(const char *channel, const char *bizdate)
{
    EXEC SQL BEGIN DECLARE SECTION;
    char h_ch[16], h_bizdate[16];
    long h_v = 0;
    EXEC SQL END DECLARE SECTION;
    strncpy(h_ch, channel, sizeof(h_ch)-1); h_ch[sizeof(h_ch)-1] = 0;
    strncpy(h_bizdate, bizdate, sizeof(h_bizdate)-1); h_bizdate[sizeof(h_bizdate)-1] = 0;
    EXEC SQL SELECT coalesce(sum(amount),0) INTO :h_v FROM purchase
        WHERE channel = :h_ch AND biz_date = :h_bizdate;
    if (sqlca.sqlcode < 0) return -1;
    return h_v;
}

/* 당일 사용된 채널 가짓수 (POS/EDC/EDI/FOREIGN 중 몇 종). */
long acdb_channel_variety(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 count(DISTINCT channel) INTO :h_v FROM purchase WHERE biz_date = :h_bizdate;
    if (sqlca.sqlcode < 0) return -1;
    return h_v;
}
