/*
 * PAY_RESULT.pgc - py 모듈 서비스 PAY_RESULT (one service = one file).
 * 지급 결과 반영: 편성/요청(F,R) 상태만 지급완료(P)로 전이, 반영 건수를 T_RC 로.
 * Inline UPDATE with IN(...) guard + affected-count via sqlca.sqlerrd[2].
 * Runs on the caller's XA branch (no EXEC SQL CONNECT). Advertised by py_svr.
 */
#include "PAY_RESULT.h"

void PAY_RESULT(TPSVCINFO *p)
{
    UBFH *b = (UBFH *)p->data;
    EXEC SQL BEGIN DECLARE SECTION;
    long h_pay, h_aff;
    EXEC SQL END DECLARE SECTION;

    h_pay = getl(b, T_ID1);
    /* only a filed(F)/requested(R) payout may be marked paid(P) */
    EXEC SQL UPDATE py_payout SET status = 'P', updated_at = now()
        WHERE payout_id = :h_pay AND status IN ('F', 'R');
    if (sqlca.sqlcode < 0) { userlog("PAY_RESULT FAIL [%d] %s", sqlca.sqlcode, sqlca.sqlerrm.sqlerrmc); FAIL(b); }
    h_aff = sqlca.sqlerrd[2];
    if (h_aff > 0) Bchg(b, T_STATUS, 0, "P", 0L);
    setl(b, T_RC, h_aff > 0 ? 0 : 1);
    OK(b);
}
