/* 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_0012.pgc"
/* @tier easy @module rc @transform reconciliation-online */
/*
 * rc_ol_0012.pgc - 가맹점별 대사 조회 (CURSOR) 온라인 서비스 (RC_OL_0012)
 *
 * 요청 MERCHID(가맹점번호) 의 원장을 처리키 순으로 커서 순회하며 상태별
 * 건수와 금액을 집계한다.
 * 업무규칙:
 *   - 총건수(CNT), 금액합계(SUMAMT) 누적
 *   - 상태별: 매입확정('M')=DONE, 불일치/보류('U')=UNMATCH,
 *             정산완료('S')=SETTLED
 *   - 대사완료율 RECRATE = (DONE + SETTLED) * 100 / CNT (0 나눗셈 방어)
 * 응답 MERCHID, CNT, DONE, UNMATCH, SETTLED, SUMAMT, RECRATE. (읽기 전용)
 */
#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 22 "app/online/rc_ol_0012.pgc"


TX_SERVICE(RC_OL_0012, ctx)
{
    /* exec sql begin declare section */
     
     
     
    
#line 27 "app/online/rc_ol_0012.pgc"
 char h_merch_id [ 16 ] ;
 
#line 28 "app/online/rc_ol_0012.pgc"
 char h_status [ 2 ] ;
 
#line 29 "app/online/rc_ol_0012.pgc"
 long h_amount ;
/* exec sql end declare section */
#line 30 "app/online/rc_ol_0012.pgc"


    char merchid[16];
    long cnt = 0, done = 0, unmatch = 0, settled = 0;
    long sumamt = 0, recrate = 0;
    int  opened = 0;

    tx_log(TX_LOG_INFO, "[RC_OL_0012] 가맹점별 대사 조회 서비스 진입");

    if (tx_buf_get(ctx->in, "MERCHID", merchid, sizeof(merchid)) != TX_OK) {
        tx_log(TX_LOG_ERROR, "[RC_OL_0012] MERCHID 누락");
        tx_return(ctx, TX_EINVAL, ctx->out);
        return;
    }

    memset(h_merch_id, 0, sizeof(h_merch_id));
    strncpy(h_merch_id, merchid, sizeof(h_merch_id) - 1);

    ECPGset_var( 0, ( h_merch_id ), __LINE__);\
 /* declare cur_rc_ol_0012 cursor for select status , amount from rc_ledger where merch_id = $1  order by key */
#line 52 "app/online/rc_ol_0012.pgc"

#line 52 "app/online/rc_ol_0012.pgc"


    { ECPGdo(__LINE__, 0, 1, NULL, 0, ECPGst_normal, "declare cur_rc_ol_0012 cursor for select status , amount from rc_ledger where merch_id = $1  order by key", 
	ECPGt_char,(h_merch_id),(long)16,(long)1,(16)*sizeof(char), 
	ECPGt_NO_INDICATOR, NULL , 0L, 0L, 0L, ECPGt_EOIT, ECPGt_EORT);}
#line 54 "app/online/rc_ol_0012.pgc"

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

    for (;;) {
        { ECPGdo(__LINE__, 0, 1, NULL, 0, ECPGst_normal, "fetch cur_rc_ol_0012", ECPGt_EOIT, 
	ECPGt_char,(h_status),(long)2,(long)1,(2)*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_EORT);}
#line 63 "app/online/rc_ol_0012.pgc"


        if (sqlca.sqlcode == TX_SQL_NOTFOUND)
            break;
        if (sqlca.sqlcode != TX_SQL_OK) {
            TX_DBIO_LOG_ERR("FETCH cur_rc_ol_0012");
            break;
        }

        cnt++;
        sumamt += h_amount;

        if (strcmp(h_status, RC_ST_DONE) == 0)
            done++;
        else if (strcmp(h_status, RC_ST_FAIL) == 0)
            unmatch++;
        else if (strcmp(h_status, RC_ST_SETTLED) == 0)
            settled++;
    }

    if (opened)
        { ECPGdo(__LINE__, 0, 1, NULL, 0, ECPGst_normal, "close cur_rc_ol_0012", ECPGt_EOIT, ECPGt_EORT);}
#line 84 "app/online/rc_ol_0012.pgc"


    recrate = (cnt > 0) ? ((done + settled) * 100 / cnt) : 0;

    tx_buf_reset(ctx->out);
    tx_buf_sets(ctx->out, "MERCHID", merchid);
    tx_buf_setlong(ctx->out, "CNT", cnt);
    tx_buf_setlong(ctx->out, "DONE", done);
    tx_buf_setlong(ctx->out, "UNMATCH", unmatch);
    tx_buf_setlong(ctx->out, "SETTLED", settled);
    tx_buf_setlong(ctx->out, "SUMAMT", sumamt);
    tx_buf_setlong(ctx->out, "RECRATE", recrate);
    tx_log(TX_LOG_INFO,
           "[RC_OL_0012] 가맹점별 집계 merch=%s 건수=%ld 완료율=%ld%%",
           merchid, cnt, recrate);
    tx_return(ctx, TX_OK, ctx->out);
}
