/*
 * py_summary_upsert_dbio.pgc - py 모듈 DB 접근 함수 세트 (py_summary_upsert_dbio), archived into libpydbio.a.
 * ECPG (EXEC SQL) functions; run on the caller's XA branch. No EXEC SQL CONNECT.
 */
#include <string.h>
#include <userlog.h>
#include "py_summary_upsert_dbio.h"

/* ON CONFLICT upsert: 가맹점 당일 집계를 0으로 리셋(재집계 준비). */
int pydb_summary_reset(const char *merch, const char *bizdate)
{
    EXEC SQL BEGIN DECLARE SECTION;
    char h_merch[64], h_bizdate[16];
    EXEC SQL END DECLARE SECTION;
    strncpy(h_merch, merch, sizeof(h_merch)-1); h_merch[sizeof(h_merch)-1] = 0;
    strncpy(h_bizdate, bizdate, sizeof(h_bizdate)-1); h_bizdate[sizeof(h_bizdate)-1] = 0;
    EXEC SQL INSERT INTO py_summary (merchant_id, biz_date, payout_count, payout_amount)
        VALUES (:h_merch, :h_bizdate, 0, 0)
        ON CONFLICT (merchant_id, biz_date) DO UPDATE
        SET payout_count = 0, payout_amount = 0, updated_at = now();
    if (sqlca.sqlcode < 0) { userlog("pydb_summary_reset FAIL [%d] %s", sqlca.sqlcode, sqlca.sqlerrm.sqlerrmc); return -1; }
    return 0;
}
