/* Processed by ecpg (15.18 (Debian 15.18-0+deb12u1)) */
/* These include files are added by the preprocessor */
#include <ecpglib.h>
#include <ecpgerrno.h>
#include <sqlca.h>
/* End of automatic include section */

#line 1 "app/online/rc_ol_0026.pgc"
/* @tier hard @module rc @transform reconciliation-online */
/*
 * rc_ol_0026.pgc - 중복거래 대사 온라인 서비스 (RC_OL_0026)
 *
 * 동일 가맹점(MERCHID)/동일 금액(AMOUNT)/동일 영업일(BIZDATE) 조합의
 * 원장을 커서로 순회하여 중복 여부를 판정한다. 일치 건수(DUPCNT) 를
 * 세고 먼저 조회된 최대 2건의 처리키(KEY0/KEY1) 를 확보한다.
 *   DUPCNT > 1  → "DUPLICATE"
 *   DUPCNT == 1 → "UNIQUE"
 *   DUPCNT == 0 → "NOTFOUND"
 * 원장을 변경하지 않는 읽기전용 서비스이다.
 */
#include <stdio.h>
#include <string.h>

#include "txcore.h"
#include "txcore_dbio.h"
#include "acq_util.h"
#include "rc_core.h"


#line 1 "/usr/include/postgresql/sqlca.h"
#ifndef POSTGRES_SQLCA_H
#define POSTGRES_SQLCA_H

#ifndef PGDLLIMPORT
#if  defined(WIN32) || defined(__CYGWIN__)
#define PGDLLIMPORT __declspec (dllimport)
#else
#define PGDLLIMPORT
#endif							/* __CYGWIN__ */
#endif							/* PGDLLIMPORT */

#define SQLERRMC_LEN	150

#ifdef __cplusplus
extern "C"
{
#endif

struct sqlca_t
{
	char		sqlcaid[8];
	long		sqlabc;
	long		sqlcode;
	struct
	{
		int			sqlerrml;
		char		sqlerrmc[SQLERRMC_LEN];
	}			sqlerrm;
	char		sqlerrp[8];
	long		sqlerrd[6];
	/* Element 0: empty						*/
	/* 1: OID of processed tuple if applicable			*/
	/* 2: number of rows processed				*/
	/* after an INSERT, UPDATE or				*/
	/* DELETE statement					*/
	/* 3: empty						*/
	/* 4: empty						*/
	/* 5: empty						*/
	char		sqlwarn[8];
	/* Element 0: set to 'W' if at least one other is 'W'	*/
	/* 1: if 'W' at least one character string		*/
	/* value was truncated when it was			*/
	/* stored into a host variable.             */

	/*
	 * 2: if 'W' a (hopefully) non-fatal notice occurred
	 */	/* 3: empty */
	/* 4: empty						*/
	/* 5: empty						*/
	/* 6: empty						*/
	/* 7: empty						*/

	char		sqlstate[5];
};

struct sqlca_t *ECPGget_sqlca(void);

#ifndef POSTGRES_ECPG_INTERNAL
#define sqlca (*ECPGget_sqlca())
#endif

#ifdef __cplusplus
}
#endif

#endif

#line 21 "app/online/rc_ol_0026.pgc"


TX_SERVICE(RC_OL_0026, ctx)
{
    /* exec sql begin declare section */
     
     
     
     
    
#line 26 "app/online/rc_ol_0026.pgc"
 char h_merch_id [ 16 ] ;
 
#line 27 "app/online/rc_ol_0026.pgc"
 char h_biz_date [ 9 ] ;
 
#line 28 "app/online/rc_ol_0026.pgc"
 long h_amount ;
 
#line 29 "app/online/rc_ol_0026.pgc"
 char h_key [ 16 ] ;
/* exec sql end declare section */
#line 30 "app/online/rc_ol_0026.pgc"


    char merch[16];
    char bizdate[9];
    char sample[2][16];
    long amount = 0;
    long dupcnt = 0;
    int  captured = 0;
    int  i;
    const char *result;

    tx_log(TX_LOG_INFO, "[RC_OL_0026] 중복거래 대사 서비스 진입");

    if (tx_buf_get(ctx->in, "MERCHID", merch, sizeof(merch)) != TX_OK) {
        tx_log(TX_LOG_ERROR, "[RC_OL_0026] MERCHID(가맹점번호) 누락");
        tx_return(ctx, TX_EINVAL, ctx->out);
        return;
    }
    if (tx_buf_getlong(ctx->in, "AMOUNT", &amount) != TX_OK) {
        tx_log(TX_LOG_ERROR, "[RC_OL_0026] AMOUNT(거래금액) 누락");
        tx_return(ctx, TX_EINVAL, ctx->out);
        return;
    }
    if (tx_buf_get(ctx->in, "BIZDATE", bizdate, sizeof(bizdate)) != TX_OK) {
        tx_log(TX_LOG_ERROR, "[RC_OL_0026] BIZDATE(영업일) 누락");
        tx_return(ctx, TX_EINVAL, ctx->out);
        return;
    }

    if (!amount_is_valid(amount)) {
        tx_log(TX_LOG_WARN, "[RC_OL_0026] 금액 유효성 오류 amount=%ld", amount);
        tx_return(ctx, TX_EINVAL, ctx->out);
        return;
    }
    if (!date_is_valid(bizdate)) {
        tx_log(TX_LOG_WARN, "[RC_OL_0026] 영업일 형식 오류 date=%s", bizdate);
        tx_return(ctx, TX_EINVAL, ctx->out);
        return;
    }

    for (i = 0; i < 2; i++)
        sample[i][0] = '\0';

    strncpy(h_merch_id, merch, sizeof(h_merch_id) - 1);
    h_merch_id[sizeof(h_merch_id) - 1] = '\0';
    strncpy(h_biz_date, bizdate, sizeof(h_biz_date) - 1);
    h_biz_date[sizeof(h_biz_date) - 1] = '\0';
    h_amount = amount;

    /* 가맹점/금액/영업일이 동일한 원장을 처리키 순으로 순회 */
    ECPGset_var( 0, ( h_biz_date ), __LINE__);\
 ECPGset_var( 1, &( h_amount ), __LINE__);\
 ECPGset_var( 2, ( h_merch_id ), __LINE__);\
 /* declare cur_rc_ol_0026 cursor for select key from rc_ledger where merch_id = $1  and amount = $2  and biz_date = $3  order by key */
#line 86 "app/online/rc_ol_0026.pgc"

#line 86 "app/online/rc_ol_0026.pgc"


    { ECPGdo(__LINE__, 0, 1, NULL, 0, ECPGst_normal, "declare cur_rc_ol_0026 cursor for select key from rc_ledger where merch_id = $1  and amount = $2  and biz_date = $3  order by key", 
	ECPGt_char,(h_merch_id),(long)16,(long)1,(16)*sizeof(char), 
	ECPGt_NO_INDICATOR, NULL , 0L, 0L, 0L, 
	ECPGt_long,&(h_amount),(long)1,(long)1,sizeof(long), 
	ECPGt_NO_INDICATOR, NULL , 0L, 0L, 0L, 
	ECPGt_char,(h_biz_date),(long)9,(long)1,(9)*sizeof(char), 
	ECPGt_NO_INDICATOR, NULL , 0L, 0L, 0L, ECPGt_EOIT, ECPGt_EORT);}
#line 88 "app/online/rc_ol_0026.pgc"

    if (sqlca.sqlcode != TX_SQL_OK) {
        TX_DBIO_LOG_ERR("OPEN cur_rc_ol_0026");
        tx_return(ctx, TX_EDB, ctx->out);
        return;
    }

    for (;;) {
        { ECPGdo(__LINE__, 0, 1, NULL, 0, ECPGst_normal, "fetch cur_rc_ol_0026", ECPGt_EOIT, 
	ECPGt_char,(h_key),(long)16,(long)1,(16)*sizeof(char), 
	ECPGt_NO_INDICATOR, NULL , 0L, 0L, 0L, ECPGt_EORT);}
#line 96 "app/online/rc_ol_0026.pgc"


        if (sqlca.sqlcode == TX_SQL_NOTFOUND)
            break;
        if (sqlca.sqlcode != TX_SQL_OK) {
            TX_DBIO_LOG_ERR("FETCH cur_rc_ol_0026");
            { ECPGdo(__LINE__, 0, 1, NULL, 0, ECPGst_normal, "close cur_rc_ol_0026", ECPGt_EOIT, ECPGt_EORT);}
#line 102 "app/online/rc_ol_0026.pgc"

            tx_return(ctx, TX_EDB, ctx->out);
            return;
        }

        dupcnt++;

        if (captured < 2) {
            strncpy(sample[captured], h_key, sizeof(sample[captured]) - 1);
            sample[captured][sizeof(sample[captured]) - 1] = '\0';
            captured++;
        }
    }

    { ECPGdo(__LINE__, 0, 1, NULL, 0, ECPGst_normal, "close cur_rc_ol_0026", ECPGt_EOIT, ECPGt_EORT);}
#line 116 "app/online/rc_ol_0026.pgc"


    /* 중복 판정 */
    if (dupcnt > 1)
        result = "DUPLICATE";
    else if (dupcnt == 1)
        result = "UNIQUE";
    else
        result = "NOTFOUND";

    tx_log(TX_LOG_INFO,
           "[RC_OL_0026] 중복대사 merch=%s amount=%ld date=%s 건수=%ld 결과=%s",
           merch, amount, bizdate, dupcnt, result);

    tx_buf_reset(ctx->out);
    tx_buf_sets(ctx->out, "MERCHID", merch);
    tx_buf_setlong(ctx->out, "AMOUNT", amount);
    tx_buf_sets(ctx->out, "BIZDATE", bizdate);
    tx_buf_setlong(ctx->out, "DUPCNT", dupcnt);
    tx_buf_sets(ctx->out, "RESULT", result);
    tx_buf_sets(ctx->out, "KEY0", sample[0]);
    tx_buf_sets(ctx->out, "KEY1", sample[1]);
    tx_return(ctx, TX_OK, ctx->out);
}
