/*
 * ACQ_RECLASS.pgc - ac 모듈 서비스 ACQ_RECLASS (one service = one file).
 * 업종/분류 재지정: category 갱신 후 같은 가맹점에서 동일 분류 건수를 집계 회신.
 * Runs on the caller's XA branch (no EXEC SQL CONNECT). Advertised by ac_svr.
 */
#include "ACQ_RECLASS.h"

void ACQ_RECLASS(TPSVCINFO *p)
{
    UBFH *b = (UBFH *)p->data;
    char cat[32];
    EXEC SQL BEGIN DECLARE SECTION;
    long h_pid, h_cnt = 0;
    char h_cat[32];
    EXEC SQL END DECLARE SECTION;

    h_pid = getl(b, T_PURCHASE_ID);
    gets_(b, T_CATEGORY, cat, sizeof(cat)); if (cat[0] == 0) strcpy(cat, "GEN");
    strncpy(h_cat, cat, sizeof(h_cat)-1); h_cat[sizeof(h_cat)-1] = 0;

    EXEC SQL UPDATE purchase SET category = :h_cat, updated_at = now() WHERE purchase_id = :h_pid;
    if (sqlca.sqlcode < 0 || sqlca.sqlcode == 100) FAIL(b);
    /* how many of this merchant's purchases now carry the new 분류 */
    EXEC SQL SELECT count(*) INTO :h_cnt FROM purchase
        WHERE merchant_id = (SELECT merchant_id FROM purchase WHERE purchase_id = :h_pid)
          AND category = :h_cat;
    if (sqlca.sqlcode < 0) FAIL(b);
    setl(b, T_COUNT, h_cnt);
    OK(b);
}
