/*
 * au_limit_upsert_dbio.pgc - au 모듈 DB 접근 함수 세트 (au_limit_upsert_dbio), archived into libaudbio.a.
 * ECPG (EXEC SQL) functions; run on the caller's XA branch. No EXEC SQL CONNECT.
 * Shape: ON CONFLICT upsert (임시한도 초기화 행 upsert).
 */
#include <string.h>
#include <userlog.h>
#include "au_limit_upsert_dbio.h"

/* 카드 한도행을 보장하고 임시한도(temp_limit)를 0 으로 초기화(upsert). */
int audb_limit_temp_clear(const char *card)
{
    EXEC SQL BEGIN DECLARE SECTION;
    char h_card[24];
    EXEC SQL END DECLARE SECTION;
    strncpy(h_card, card, sizeof(h_card)-1); h_card[sizeof(h_card)-1] = 0;
    EXEC SQL INSERT INTO card_limit (card_no, daily_limit, used_amount, temp_limit)
        VALUES (:h_card, 0, 0, 0)
        ON CONFLICT (card_no) DO UPDATE
        SET temp_limit = 0, updated_at = now();
    if (sqlca.sqlcode < 0) {
        userlog("audb_limit_temp_clear FAIL [%d] %s", sqlca.sqlcode, sqlca.sqlerrm.sqlerrmc);
        return -1;
    }
    return 0;
}
