/* 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/vl_ol_0002.pgc"
/* @tier medium @module vl @transform validation-online */
/*
 * vl_ol_0002.pgc - 정합성 접수등록 서비스 (VL_OL_0002)
 *
 * 매입 정합성 대상 거래를 접수한다. 필수필드(처리키/가맹점/영업일/금액)를
 * 검증한 뒤 표준 DBIO 로 원장에 접수(vl_rec_insert)하고, 접수이력 테이블
 * (vl_recv_hist) 에 감사 로그를 남긴다. 마지막으로 당일 접수건수를 집계하여
 * 응답에 실어 반환한다.
 */
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#include "txcore.h"
#include "txcore_dbio.h"
#include "acq_util.h"
#include "vl_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 19 "app/online/vl_ol_0002.pgc"


TX_SERVICE(VL_OL_0002, ctx)
{
    /* exec sql begin declare section */
     
     
     
     
     
     
    
#line 24 "app/online/vl_ol_0002.pgc"
 char h_key [ 16 ] ;
 
#line 25 "app/online/vl_ol_0002.pgc"
 char h_merch [ 16 ] ;
 
#line 26 "app/online/vl_ol_0002.pgc"
 char h_bizdate [ 9 ] ;
 
#line 27 "app/online/vl_ol_0002.pgc"
 char h_status [ 2 ] ;
 
#line 28 "app/online/vl_ol_0002.pgc"
 long h_amount ;
 
#line 29 "app/online/vl_ol_0002.pgc"
 char h_recv_time [ 7 ] ;
/* exec sql end declare section */
#line 30 "app/online/vl_ol_0002.pgc"


    vl_rec_t rec;
    char key[16], merch[16], bizdate[16];
    long amount = 0, cnt;
    int  rc;

    tx_log(TX_LOG_INFO, "[VL_OL_0002] 정합성 접수등록 서비스 진입");

    /* 1) 필수필드 검증 */
    if (tx_buf_get(ctx->in, "KEY", key, sizeof(key)) != TX_OK ||
        tx_buf_get(ctx->in, "MERCHID", merch, sizeof(merch)) != TX_OK ||
        tx_buf_get(ctx->in, "BIZDATE", bizdate, sizeof(bizdate)) != TX_OK) {
        tx_log(TX_LOG_ERROR, "[VL_OL_0002] 필수 필드 누락");
        tx_buf_sets(ctx->out, "ERRCODE", "VL0200");
        tx_return(ctx, TX_EINVAL, ctx->out);
        return;
    }
    tx_buf_getlong(ctx->in, "AMOUNT", &amount);

    if (!amount_is_valid(amount) || !date_is_valid(bizdate)) {
        tx_log(TX_LOG_WARN, "[VL_OL_0002] 입력값 검증 실패 amount=%ld date=%s",
               amount, bizdate);
        tx_buf_sets(ctx->out, "ERRCODE", "VL0201");
        tx_return(ctx, TX_EINVAL, ctx->out);
        return;
    }

    /* 2) 원장 접수 (표준 DBIO) */
    memset(&rec, 0, sizeof(rec));
    strncpy(rec.key, key, sizeof(rec.key) - 1);
    strncpy(rec.merch_id, merch, sizeof(rec.merch_id) - 1);
    strncpy(rec.biz_date, bizdate, sizeof(rec.biz_date) - 1);
    strncpy(rec.status, VL_ST_RECV, sizeof(rec.status) - 1);
    rec.amount = amount;
    rec.fee    = amount / 100;

    rc = vl_rec_insert(&rec);
    if (rc != TX_OK) {
        tx_log(TX_LOG_ERROR, "[VL_OL_0002] 원장접수 실패 rc=%d(%s)",
               rc, tx_strerror(rc));
        tx_buf_sets(ctx->out, "ERRCODE", "VL0202");
        tx_return(ctx, rc, ctx->out);
        return;
    }

    /* 3) 접수이력 INSERT (감사 로그) */
    strncpy(h_key, key, sizeof(h_key) - 1);        h_key[sizeof(h_key) - 1] = '\0';
    strncpy(h_merch, merch, sizeof(h_merch) - 1);  h_merch[sizeof(h_merch) - 1] = '\0';
    strncpy(h_bizdate, bizdate, sizeof(h_bizdate) - 1);
    h_bizdate[sizeof(h_bizdate) - 1] = '\0';
    strncpy(h_status, VL_ST_RECV, sizeof(h_status) - 1);
    h_status[sizeof(h_status) - 1] = '\0';
    h_amount = amount;
    strncpy(h_recv_time, "000000", sizeof(h_recv_time) - 1);
    h_recv_time[sizeof(h_recv_time) - 1] = '\0';

    { ECPGdo(__LINE__, 0, 1, NULL, 0, ECPGst_normal, "insert into vl_recv_hist ( recv_key , merch_id , biz_date , recv_amount , recv_status , recv_time ) values ( $1  , $2  , $3  , $4  , $5  , $6  )", 
	ECPGt_char,(h_key),(long)16,(long)1,(16)*sizeof(char), 
	ECPGt_NO_INDICATOR, NULL , 0L, 0L, 0L, 
	ECPGt_char,(h_merch),(long)16,(long)1,(16)*sizeof(char), 
	ECPGt_NO_INDICATOR, NULL , 0L, 0L, 0L, 
	ECPGt_char,(h_bizdate),(long)9,(long)1,(9)*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_status),(long)2,(long)1,(2)*sizeof(char), 
	ECPGt_NO_INDICATOR, NULL , 0L, 0L, 0L, 
	ECPGt_char,(h_recv_time),(long)7,(long)1,(7)*sizeof(char), 
	ECPGt_NO_INDICATOR, NULL , 0L, 0L, 0L, ECPGt_EOIT, ECPGt_EORT);}
#line 91 "app/online/vl_ol_0002.pgc"


    if (sqlca.sqlcode != TX_SQL_OK) {
        TX_DBIO_LOG_ERR("INSERT vl_recv_hist");
        tx_buf_sets(ctx->out, "ERRCODE", "VL0209");
        tx_return(ctx, TX_EDB, ctx->out);
        return;
    }

    /* 4) 당일 접수건수 집계 */
    cnt = vl_rec_count(bizdate, VL_ST_RECV);
    if (cnt < 0)
        cnt = 0;

    tx_buf_reset(ctx->out);
    tx_buf_sets(ctx->out, "RESPCODE", RESP_OK);
    tx_buf_sets(ctx->out, "ERRCODE", RESP_OK);
    tx_buf_sets(ctx->out, "KEY", key);
    tx_buf_setlong(ctx->out, "RECVAMT", amount);
    tx_buf_setlong(ctx->out, "DAYCNT", cnt);
    tx_log(TX_LOG_INFO, "[VL_OL_0002] 접수완료 key=%s 당일건수=%ld", key, cnt);
    tx_return(ctx, TX_OK, ctx->out);
}
