/*
 * au_auth_distinctcard_dbio.pgc - au 모듈 DB 접근 함수 세트 (au_auth_distinctcard_dbio), archived into libaudbio.a.
 * ECPG (EXEC SQL) functions; run on the caller's XA branch. No EXEC SQL CONNECT.
 * Shape: 승인건에서 서로 다른 카드/가맹점 수를 count(DISTINCT) 로 동시 산출.
 */
#include <string.h>
#include <userlog.h>
#include "au_auth_distinctcard_dbio.h"

/* 당일 승인(A) 건에서 사용된 서로 다른 카드/가맹점 수를 out 에 채운다. */
int audb_auth_distinct_counts(const char *bizdate, au_distinct_rec_t *out)
{
    EXEC SQL BEGIN DECLARE SECTION;
    char h_bizdate[16];
    long h_cards = 0, h_merch = 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 card_no), count(DISTINCT merchant_id)
        INTO :h_cards, :h_merch FROM au_authorization
        WHERE biz_date = :h_bizdate AND status = 'A';
    if (sqlca.sqlcode < 0) {
        userlog("audb_auth_distinct_counts FAIL [%d] %s", sqlca.sqlcode, sqlca.sqlerrm.sqlerrmc);
        return -1;
    }
    out->distinct_cards = h_cards;
    out->distinct_merchants = h_merch;
    return 0;
}
