/* 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/mg_ol_0031.pgc"
/* @tier easy @module mg @transform msg-gateway-online */
/*
 * mg_ol_0031.pgc - 전문게이트웨이 전문 스냅샷 조회 서비스 (MG_OL_0031)  [tier=easy]
 *
 * 처리키(KEY)로 원장 레코드를 조회(mg_rec_select)한 뒤, 금액/상태/수수료를
 * 포함한 전체 필드 스냅샷을 응답 TXBUF 로 구성한다. 상태코드는 사람이 읽을
 * 수 있는 라벨로 함께 내려준다. (ATMI void SVC(TPSVCINFO*) 대응)
 */
#include <stdio.h>
#include <string.h>

#include "txcore.h"
#include "txcore_dbio.h"
#include "acq_util.h"
#include "mg_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 17 "app/online/mg_ol_0031.pgc"


TX_SERVICE(MG_OL_0031, ctx)
{
    mg_rec_t rec;
    char key[16];
    char won[40];
    const char *st_label;
    long snap_peer = 0;
    int  rc;

    tx_log(TX_LOG_INFO, "[MG_OL_0031] 전문 스냅샷 조회 서비스 진입");

    /* 1) 처리키 수신 */
    if (tx_buf_get(ctx->in, "KEY", key, sizeof(key)) != TX_OK) {
        tx_log(TX_LOG_ERROR, "[MG_OL_0031] KEY 누락");
        tx_return(ctx, TX_EINVAL, ctx->out);
        return;
    }

    /* 2) 원장 단건 조회 */
    memset(&rec, 0, sizeof(rec));
    rc = mg_rec_select(key, &rec);
    if (rc != TX_OK) {
        tx_log(TX_LOG_WARN, "[MG_OL_0031] 스냅샷 대상 없음 key=%s rc=%d(%s)",
               key, rc, tx_strerror(rc));
        tx_return(ctx, rc, ctx->out);
        return;
    }

    /* 2-b) 동일 영업일/동일 상태 동종 건수를 임베디드 SQL 로 집계 (참고정보) */
    {
        /* exec sql begin declare section */
         
         
         
        
#line 50 "app/online/mg_ol_0031.pgc"
 char h_snap_bd [ 9 ] ;
 
#line 51 "app/online/mg_ol_0031.pgc"
 char h_snap_st [ 2 ] ;
 
#line 52 "app/online/mg_ol_0031.pgc"
 long h_snap_peer ;
/* exec sql end declare section */
#line 53 "app/online/mg_ol_0031.pgc"


        strncpy(h_snap_bd, rec.biz_date, sizeof(h_snap_bd) - 1);
        h_snap_bd[sizeof(h_snap_bd) - 1] = '\0';
        strncpy(h_snap_st, rec.status, sizeof(h_snap_st) - 1);
        h_snap_st[sizeof(h_snap_st) - 1] = '\0';
        h_snap_peer = 0;

        { ECPGdo(__LINE__, 0, 1, NULL, 0, ECPGst_normal, "select count ( * ) from mg_ledger where biz_date = $1  and status = $2 ", 
	ECPGt_char,(h_snap_bd),(long)9,(long)1,(9)*sizeof(char), 
	ECPGt_NO_INDICATOR, NULL , 0L, 0L, 0L, 
	ECPGt_char,(h_snap_st),(long)2,(long)1,(2)*sizeof(char), 
	ECPGt_NO_INDICATOR, NULL , 0L, 0L, 0L, ECPGt_EOIT, 
	ECPGt_long,&(h_snap_peer),(long)1,(long)1,sizeof(long), 
	ECPGt_NO_INDICATOR, NULL , 0L, 0L, 0L, ECPGt_EORT);}
#line 65 "app/online/mg_ol_0031.pgc"

        if (sqlca.sqlcode != TX_SQL_OK && sqlca.sqlcode != TX_SQL_NOTFOUND) {
            TX_DBIO_LOG_ERR("SELECT COUNT mg_ledger snapshot peers");
            h_snap_peer = 0;
        }
        snap_peer = h_snap_peer;
    }

    /* 3) 상태코드 -> 표시 라벨 매핑 */
    if (strcmp(rec.status, MG_ST_RECV) == 0)
        st_label = "접수";
    else if (strcmp(rec.status, MG_ST_DONE) == 0)
        st_label = "처리완료";
    else if (strcmp(rec.status, MG_ST_FAIL) == 0)
        st_label = "실패";
    else if (strcmp(rec.status, MG_ST_SETTLED) == 0)
        st_label = "정산완료";
    else
        st_label = "미상";

    /* 4) 전체 필드 스냅샷 구성 */
    amount_format_won(rec.amount, won, sizeof(won));

    tx_buf_reset(ctx->out);
    tx_buf_sets(ctx->out, "RESP", RESP_OK);
    tx_buf_sets(ctx->out, "KEY", rec.key);
    tx_buf_sets(ctx->out, "MERCHID", rec.merch_id);
    tx_buf_sets(ctx->out, "CARDNO", rec.card_no);
    tx_buf_sets(ctx->out, "BIZDATE", rec.biz_date);
    tx_buf_setlong(ctx->out, "AMOUNT", rec.amount);
    tx_buf_sets(ctx->out, "AMOUNTWON", won);
    tx_buf_setlong(ctx->out, "FEE", rec.fee);
    tx_buf_sets(ctx->out, "STATUS", rec.status);
    tx_buf_sets(ctx->out, "STATUSLABEL", st_label);
    tx_buf_setlong(ctx->out, "PEERCNT", snap_peer);

    tx_log(TX_LOG_INFO, "[MG_OL_0031] 스냅샷 조회완료 key=%s 상태=%s 금액=%ld 수수료=%ld",
           key, st_label, rec.amount, rec.fee);
    tx_return(ctx, TX_OK, ctx->out);
}
