/*
 * mm_risk_case_dbio.pgc - mm 모듈 DB 접근 함수 (mm_risk_case_dbio), archived into libmmdbio.a.
 * MCC 위험도 구간별 건수 (CASE 집계).
 * ECPG (EXEC SQL) functions; run on the caller's XA branch. No EXEC SQL CONNECT.
 */
#include <string.h>
#include <userlog.h>
#include "mm_risk_case_dbio.h"

/* risk_level 을 저/중/고 구간으로 CASE 분류해 각 건수 반환. */
int mmdb_risk_case(long *lo, long *mid, long *hi)
{
    EXEC SQL BEGIN DECLARE SECTION;
    long h_lo = 0, h_mid = 0, h_hi = 0;
    EXEC SQL END DECLARE SECTION;
    EXEC SQL SELECT
        coalesce(sum(CASE WHEN risk_level <= 1 THEN 1 ELSE 0 END),0),
        coalesce(sum(CASE WHEN risk_level = 2 THEN 1 ELSE 0 END),0),
        coalesce(sum(CASE WHEN risk_level >= 3 THEN 1 ELSE 0 END),0)
        INTO :h_lo, :h_mid, :h_hi FROM mm_mcc;
    if (sqlca.sqlcode < 0) return -1;
    *lo = h_lo; *mid = h_mid; *hi = h_hi;
    return 0;
}
