/* 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/cm_ol_0006.pgc"
/* @tier medium @module cm @transform common-online */
/*
 * cm_ol_0006.pgc - 순번 채번 서비스 (CM_OL_0006)  [tier=medium]
 *
 * 요청의 채번키(SEQKEY)로 공통 순번 마스터(cm_seq)를 UPDATE 로 1
 * 증가시킨 뒤 증가된 순번을 SELECT 하여 발급한다. 채번은 트랜잭션
 * 경계 안에서 원자적으로 수행한다(tx_begin/commit). 순환최대치를
 * 넘으면 1 로 리셋한다.
 * (ATMI void SVC(TPSVCINFO*) 대응)
 */
#include <stdio.h>
#include <string.h>

#include "txcore.h"
#include "txcore_dbio.h"
#include "acq_util.h"
#include "cm_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/cm_ol_0006.pgc"


TX_SERVICE(CM_OL_0006, ctx)
{
    /* exec sql begin declare section */
     
     
     
     
      
    
#line 24 "app/online/cm_ol_0006.pgc"
 char h_seqkey [ 16 ] ;
 
#line 25 "app/online/cm_ol_0006.pgc"
 char h_prefix [ 8 ] ;
 
#line 26 "app/online/cm_ol_0006.pgc"
 long h_seqval ;
 
#line 27 "app/online/cm_ol_0006.pgc"
 long h_maxval ;
 
#line 28 "app/online/cm_ol_0006.pgc"
 int h_width ;
/* exec sql end declare section */
#line 29 "app/online/cm_ol_0006.pgc"


    char seqkey[16];
    char issued[32];

    tx_log(TX_LOG_INFO, "[CM_OL_0006] 순번 채번 서비스 진입");

    memset(seqkey, 0, sizeof(seqkey));
    memset(issued, 0, sizeof(issued));

    if (tx_buf_get(ctx->in, "SEQKEY", seqkey, sizeof(seqkey)) != TX_OK) {
        tx_log(TX_LOG_ERROR, "[CM_OL_0006] 채번키(SEQKEY) 누락");
        tx_return(ctx, TX_EINVAL, ctx->out);
        return;
    }

    if (tx_begin() != TX_OK) {
        tx_log(TX_LOG_ERROR, "[CM_OL_0006] 트랜잭션 시작 실패");
        tx_return(ctx, TX_FAIL, ctx->out);
        return;
    }

    memset(h_seqkey, 0, sizeof(h_seqkey));
    memset(h_prefix, 0, sizeof(h_prefix));
    strncpy(h_seqkey, seqkey, sizeof(h_seqkey) - 1);
    h_seqval = 0;
    h_maxval = 0;
    h_width  = 0;

    /* 순환최대치/자리수/접두어 확보 (행잠금) */
    { ECPGdo(__LINE__, 0, 1, NULL, 0, ECPGst_normal, "select cur_val , max_val , seq_width , prefix from cm_seq where seq_key = $1  for update", 
	ECPGt_char,(h_seqkey),(long)16,(long)1,(16)*sizeof(char), 
	ECPGt_NO_INDICATOR, NULL , 0L, 0L, 0L, ECPGt_EOIT, 
	ECPGt_long,&(h_seqval),(long)1,(long)1,sizeof(long), 
	ECPGt_NO_INDICATOR, NULL , 0L, 0L, 0L, 
	ECPGt_long,&(h_maxval),(long)1,(long)1,sizeof(long), 
	ECPGt_NO_INDICATOR, NULL , 0L, 0L, 0L, 
	ECPGt_int,&(h_width),(long)1,(long)1,sizeof(int), 
	ECPGt_NO_INDICATOR, NULL , 0L, 0L, 0L, 
	ECPGt_char,(h_prefix),(long)8,(long)1,(8)*sizeof(char), 
	ECPGt_NO_INDICATOR, NULL , 0L, 0L, 0L, ECPGt_EORT);}
#line 63 "app/online/cm_ol_0006.pgc"


    if (sqlca.sqlcode == TX_SQL_NOTFOUND) {
        tx_log(TX_LOG_WARN, "[CM_OL_0006] 채번키 미등록 seqkey=%s", seqkey);
        tx_abort();
        tx_return(ctx, TX_ENOKEY, ctx->out);
        return;
    }
    if (sqlca.sqlcode != TX_SQL_OK) {
        TX_DBIO_LOG_ERR("CM_OL_0006 SELECT cm_seq");
        tx_abort();
        tx_return(ctx, TX_EDB, ctx->out);
        return;
    }

    /* 다음 순번 산출 (순환) */
    h_seqval = h_seqval + 1;
    if (h_maxval > 0 && h_seqval > h_maxval) {
        h_seqval = 1;
        tx_log(TX_LOG_INFO, "[CM_OL_0006] 순번 순환 리셋 seqkey=%s", seqkey);
    }

    { ECPGdo(__LINE__, 0, 1, NULL, 0, ECPGst_normal, "update cm_seq set cur_val = $1  , upd_ts = current_timestamp where seq_key = $2 ", 
	ECPGt_long,&(h_seqval),(long)1,(long)1,sizeof(long), 
	ECPGt_NO_INDICATOR, NULL , 0L, 0L, 0L, 
	ECPGt_char,(h_seqkey),(long)16,(long)1,(16)*sizeof(char), 
	ECPGt_NO_INDICATOR, NULL , 0L, 0L, 0L, ECPGt_EOIT, ECPGt_EORT);}
#line 88 "app/online/cm_ol_0006.pgc"


    if (sqlca.sqlcode != TX_SQL_OK) {
        TX_DBIO_LOG_ERR("CM_OL_0006 UPDATE cm_seq");
        tx_abort();
        tx_return(ctx, TX_EDB, ctx->out);
        return;
    }

    tx_commit();

    /* 접두어 + 자리수 zero-padding 으로 발급번호 조립 */
    if (h_width <= 0 || h_width > 18) {
        h_width = 10;
    }
    snprintf(issued, sizeof(issued), "%s%0*ld", h_prefix, h_width, h_seqval);

    tx_buf_reset(ctx->out);
    tx_buf_sets(ctx->out, "SEQKEY", seqkey);
    tx_buf_setlong(ctx->out, "SEQVAL", h_seqval);
    tx_buf_sets(ctx->out, "ISSUEDNO", issued);

    tx_log(TX_LOG_INFO, "[CM_OL_0006] 채번완료 seqkey=%s no=%s", seqkey, issued);
    tx_return(ctx, TX_OK, ctx->out);
}
