/*
 * mm_grade_stat_dbio.pgc - 등급 통계 upsert (libmmdbio.a).
 * ECPG functions; run on the caller's XA branch. No EXEC SQL CONNECT.
 */
#include <string.h>
#include <userlog.h>
#include "mm_grade_stat_dbio.h"

int mmdb_upsert_grade_stat(const char *statdate, const char *grade, long cnt)
{
    EXEC SQL BEGIN DECLARE SECTION;
    long h_cnt = cnt; char h_date[16], h_grade[8];
    EXEC SQL END DECLARE SECTION;
    strncpy(h_date, statdate, sizeof(h_date)-1); h_date[sizeof(h_date)-1] = 0;
    strncpy(h_grade, grade, sizeof(h_grade)-1); h_grade[sizeof(h_grade)-1] = 0;
    EXEC SQL INSERT INTO mm_grade_stat (stat_date, grade, merch_cnt)
        VALUES (:h_date, :h_grade, :h_cnt)
        ON CONFLICT (stat_date, grade) DO UPDATE
        SET merch_cnt = EXCLUDED.merch_cnt, updated_at = now();
    if (sqlca.sqlcode < 0) { userlog("mmdb_upsert_grade_stat FAIL [%d]", sqlca.sqlcode); return -1; }
    return 0;
}
