/*
 * au_cancel_agg_dbio.pgc - au 모듈 DB 접근 함수 세트 (au_cancel_agg_dbio), archived into libaudbio.a.
 * ECPG (EXEC SQL) functions; run on the caller's XA branch. No EXEC SQL CONNECT.
 * Shape: FILTER (WHERE ...) aggregate (취소유형별 분리 집계).
 */
#include <string.h>
#include <userlog.h>
#include "au_cancel_agg_dbio.h"

/* FULL/PARTIAL 취소를 한 번에 분리 집계하고 PARTIAL 합 반환. */
long audb_cancel_partial_sum(const char *bizdate)
{
    EXEC SQL BEGIN DECLARE SECTION;
    char h_bizdate[16];
    long h_full = 0, h_part = 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(amount) FILTER (WHERE cancel_type = 'FULL'), 0),
        coalesce(sum(amount) FILTER (WHERE cancel_type = 'PARTIAL'), 0)
        INTO :h_full, :h_part FROM au_cancel WHERE biz_date = :h_bizdate;
    if (sqlca.sqlcode < 0) return -1;
    if (h_full < 0) return -1;
    return h_part;
}
