/*
 * au_preauth_notexists_dbio.pgc - au 모듈 DB 접근 함수 세트 (au_preauth_notexists_dbio), archived into libaudbio.a.
 * ECPG (EXEC SQL) functions; run on the caller's XA branch. No EXEC SQL CONNECT.
 * Shape: NOT EXISTS 상관 서브쿼리(복합조건) - 대응 승인이 없는 미확정 예비승인.
 */
#include <string.h>
#include <userlog.h>
#include "au_preauth_notexists_dbio.h"

/* 상태 P(예비) 이면서 같은 카드/금액의 승인행이 없는 예비승인 건수. */
long audb_preauth_unconfirmed(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(*) INTO :h_v FROM au_preauth p
        WHERE p.biz_date = :h_bizdate AND p.status = 'P'
          AND NOT EXISTS (SELECT 1 FROM au_authorization a
                          WHERE a.card_no = p.card_no AND a.amount = p.amount);
    if (sqlca.sqlcode < 0) return -1;
    return h_v;
}
