/* 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/ac_ol_0029.pgc"
/* @tier easy @module ac @transform acquire-online */
/*
 * ac_ol_0029.pgc - 매입 온라인 서비스 (AC_OL_0029)  [tier=easy]
 *
 * 매입 청구조회(EDI): 청구번호(CLAIMNO)로 매입 청구내역(ac_claim)을
 * 단건 조회한다. EDI 연계 파일로 실제 적재된 청구건의 가맹점/영업일/
 * 금액/파일순번/상태를 확인하기 위한 조회 전용 서비스이다.
 *
 * 청구번호는 EDI 파일 헤더에서 채번되어 전달되므로, 조회 전 앞뒤
 * 공백을 제거하여 정규화한 뒤 원장 키로 사용한다.
 */
#include <stdio.h>
#include <string.h>

#include "txcore.h"
#include "txcore_dbio.h"
#include "acq_util.h"
#include "ac_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 20 "app/online/ac_ol_0029.pgc"


TX_SERVICE(AC_OL_0029, ctx)
{
    /* ---- ECPG 호스트 변수 선언부 ---- */
    /* exec sql begin declare section */
     
     
     
     
     
     
    
#line 26 "app/online/ac_ol_0029.pgc"
 char h_claim_no [ 16 ] ;
 
#line 27 "app/online/ac_ol_0029.pgc"
 char h_merch_id [ 16 ] ;
 
#line 28 "app/online/ac_ol_0029.pgc"
 char h_biz_date [ 9 ] ;
 
#line 29 "app/online/ac_ol_0029.pgc"
 long h_amount ;
 
#line 30 "app/online/ac_ol_0029.pgc"
 long h_file_seq ;
 
#line 31 "app/online/ac_ol_0029.pgc"
 char h_status [ 2 ] ;
/* exec sql end declare section */
#line 32 "app/online/ac_ol_0029.pgc"


    char claimno[16];
    int  len;

    tx_log(TX_LOG_INFO, "[AC_OL_0029] 매입 청구조회(EDI) 서비스 진입");

    /* ---- 1. 입력 검증 ---- */
    if (tx_buf_get(ctx->in, "CLAIMNO", claimno, sizeof(claimno)) != TX_OK ||
        claimno[0] == '\0') {
        tx_log(TX_LOG_ERROR, "[AC_OL_0029] CLAIMNO 누락");
        tx_buf_reset(ctx->out);
        tx_buf_sets(ctx->out, "RESPCODE", RESP_INVALID);
        tx_return(ctx, TX_EINVAL, ctx->out);
        return;
    }

    /* ---- 2. 청구번호 정규화 (EDI 고정길이 전문의 트레일링 공백 제거) ---- */
    len = (int)strlen(claimno);
    while (len > 0 && claimno[len - 1] == ' ')
        claimno[--len] = '\0';

    if (len == 0) {
        tx_log(TX_LOG_ERROR, "[AC_OL_0029] CLAIMNO 공백 전문");
        tx_buf_reset(ctx->out);
        tx_buf_sets(ctx->out, "RESPCODE", RESP_INVALID);
        tx_return(ctx, TX_EINVAL, ctx->out);
        return;
    }

    memset(h_claim_no, 0, sizeof(h_claim_no));
    strncpy(h_claim_no, claimno, sizeof(h_claim_no) - 1);

    /* ---- 3. 청구내역 단건 조회 ---- */
    { ECPGdo(__LINE__, 0, 1, NULL, 0, ECPGst_normal, "select merch_id , biz_date , amount , file_seq , status from ac_claim where claim_no = $1 ", 
	ECPGt_char,(h_claim_no),(long)16,(long)1,(16)*sizeof(char), 
	ECPGt_NO_INDICATOR, NULL , 0L, 0L, 0L, ECPGt_EOIT, 
	ECPGt_char,(h_merch_id),(long)16,(long)1,(16)*sizeof(char), 
	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_long,&(h_amount),(long)1,(long)1,sizeof(long), 
	ECPGt_NO_INDICATOR, NULL , 0L, 0L, 0L, 
	ECPGt_long,&(h_file_seq),(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_EORT);}
#line 70 "app/online/ac_ol_0029.pgc"


    if (sqlca.sqlcode == TX_SQL_NOTFOUND) {
        tx_log(TX_LOG_WARN, "[AC_OL_0029] 청구건 없음 claim_no=%s", claimno);
        tx_buf_reset(ctx->out);
        tx_buf_sets(ctx->out, "RESPCODE", RESP_INVALID);
        tx_return(ctx, TX_ENOENT, ctx->out);
        return;
    }
    if (sqlca.sqlcode != TX_SQL_OK) {
        TX_DBIO_LOG_ERR("SELECT ac_claim");
        tx_buf_reset(ctx->out);
        tx_buf_sets(ctx->out, "RESPCODE", RESP_SYSERR);
        tx_return(ctx, TX_EDB, ctx->out);
        return;
    }

    /* ---- 4. EDI 연계 정합성 참고 확인 (파일순번 미채번 시 경고만 남김) ---- */
    if (h_file_seq <= 0) {
        tx_log(TX_LOG_WARN,
               "[AC_OL_0029] 파일순번 미채번 claim_no=%s file_seq=%ld",
               claimno, h_file_seq);
    }

    /* ---- 5. 상태코드 설명 참고 로그 (응답필드는 원본 코드값 그대로 반환) ---- */
    {
        const char *statusdesc = "알수없음";

        if (strcmp(h_status, "W") == 0)
            statusdesc = "대기";
        else if (strcmp(h_status, "S") == 0)
            statusdesc = "전송완료";
        else if (strcmp(h_status, "E") == 0)
            statusdesc = "오류";

        tx_log(TX_LOG_DEBUG, "[AC_OL_0029] 상태설명 claim_no=%s status=%s(%s)",
               claimno, h_status, statusdesc);
    }

    /* ---- 6. 응답 구성 ---- */
    tx_buf_reset(ctx->out);
    tx_buf_sets(ctx->out, "RESPCODE", RESP_OK);
    tx_buf_sets(ctx->out, "CLAIMNO", claimno);
    tx_buf_sets(ctx->out, "MERCHID", h_merch_id);
    tx_buf_sets(ctx->out, "BIZDATE", h_biz_date);
    tx_buf_setlong(ctx->out, "AMOUNT", h_amount);
    tx_buf_setlong(ctx->out, "FILESEQ", h_file_seq);
    tx_buf_sets(ctx->out, "STATUS", h_status);

    tx_log(TX_LOG_INFO,
           "[AC_OL_0029] 청구조회완료 claim_no=%s merch=%s amount=%ld status=%s",
           claimno, h_merch_id, h_amount, h_status);
    tx_return(ctx, TX_OK, ctx->out);
}
