/*
 * au_auth_overavg_dbio.pgc - au 모듈 DB 접근 함수 세트 (au_auth_overavg_dbio), archived into libaudbio.a.
 * ECPG (EXEC SQL) functions; run on the caller's XA branch. No EXEC SQL CONNECT.
 * Shape: scalar-subquery comparison WHERE amount > (SELECT avg ...) (평균초과 승인 건수).
 */
#include <string.h>
#include <userlog.h>
#include "au_auth_overavg_dbio.h"

/* 당일 평균 승인금액을 초과하는 승인 건수 (스칼라 서브쿼리 비교). */
long audb_auth_over_avg_count(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_authorization
        WHERE biz_date = :h_bizdate
          AND amount > (SELECT coalesce(avg(amount),0) FROM au_authorization WHERE biz_date = :h_bizdate);
    if (sqlca.sqlcode < 0) return -1;
    return h_v;
}
