/* 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/mm_ol_0026.pgc"
/* @tier easy @module mm @transform master-online */
/*
 * mm_ol_0026.pgc - 공통코드 캐시조회 서비스 (MM_OL_0026)  [tier=easy]
 *
 * 코드그룹(grp_cd)을 키로 공통코드(mm_common_code) 중 사용중(use_yn='Y')
 * 인 대표 코드 건수와 그룹 내 최신 변경일을 집계 조회한다. 캐시 갱신
 * 판단용 메타(건수/최신변경일)를 응답한다. 조회 전용(읽기) 서비스.
 */
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#include "txcore.h"
#include "txcore_dbio.h"
#include "acq_util.h"
#include "mm_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 18 "app/online/mm_ol_0026.pgc"


TX_SERVICE(MM_OL_0026, ctx)
{
    /* exec sql begin declare section */
     
     
     
     
    
#line 23 "app/online/mm_ol_0026.pgc"
 char h_grp_cd [ 16 ] ;
 
#line 24 "app/online/mm_ol_0026.pgc"
 char h_grp_nm [ 32 ] ;
 
#line 25 "app/online/mm_ol_0026.pgc"
 char h_last_chg [ 9 ] ;
 
#line 26 "app/online/mm_ol_0026.pgc"
 long h_use_cnt ;
/* exec sql end declare section */
#line 27 "app/online/mm_ol_0026.pgc"


    char grp_cd[16];

    tx_log(TX_LOG_INFO, "[MM_OL_0026] 공통코드 캐시조회 서비스 진입");

    if (tx_buf_get(ctx->in, "GRPCD", grp_cd, sizeof(grp_cd)) != TX_OK) {
        tx_log(TX_LOG_ERROR, "[MM_OL_0026] GRPCD 누락");
        tx_buf_sets(ctx->out, "ERRCODE", "MM2600");
        tx_return(ctx, TX_EINVAL, ctx->out);
        return;
    }

    strncpy(h_grp_cd, grp_cd, sizeof(h_grp_cd) - 1);
    h_grp_cd[sizeof(h_grp_cd) - 1] = '\0';

    /* 1) 코드그룹 명칭 확인 */
    { ECPGdo(__LINE__, 0, 1, NULL, 0, ECPGst_normal, "select grp_nm from mm_code_group where grp_cd = $1 ", 
	ECPGt_char,(h_grp_cd),(long)16,(long)1,(16)*sizeof(char), 
	ECPGt_NO_INDICATOR, NULL , 0L, 0L, 0L, ECPGt_EOIT, 
	ECPGt_char,(h_grp_nm),(long)32,(long)1,(32)*sizeof(char), 
	ECPGt_NO_INDICATOR, NULL , 0L, 0L, 0L, ECPGt_EORT);}
#line 47 "app/online/mm_ol_0026.pgc"


    if (sqlca.sqlcode == TX_SQL_NOTFOUND) {
        tx_log(TX_LOG_WARN, "[MM_OL_0026] 코드그룹 없음 grp=%s", grp_cd);
        tx_buf_sets(ctx->out, "ERRCODE", "MM2601");
        tx_return(ctx, TX_ENOENT, ctx->out);
        return;
    }
    if (sqlca.sqlcode != TX_SQL_OK) {
        TX_DBIO_LOG_ERR("SELECT mm_code_group");
        tx_buf_sets(ctx->out, "ERRCODE", "MM2609");
        tx_return(ctx, TX_EDB, ctx->out);
        return;
    }

    /* 2) 사용중 코드 건수 + 최신 변경일 집계 */
    h_use_cnt = 0;
    strncpy(h_last_chg, "00000000", sizeof(h_last_chg));
    { ECPGdo(__LINE__, 0, 1, NULL, 0, ECPGst_normal, "select count ( * ) , coalesce ( MAX ( chg_date ) , '00000000' ) from mm_common_code where grp_cd = $1  and use_yn = 'Y'", 
	ECPGt_char,(h_grp_cd),(long)16,(long)1,(16)*sizeof(char), 
	ECPGt_NO_INDICATOR, NULL , 0L, 0L, 0L, ECPGt_EOIT, 
	ECPGt_long,&(h_use_cnt),(long)1,(long)1,sizeof(long), 
	ECPGt_NO_INDICATOR, NULL , 0L, 0L, 0L, 
	ECPGt_char,(h_last_chg),(long)9,(long)1,(9)*sizeof(char), 
	ECPGt_NO_INDICATOR, NULL , 0L, 0L, 0L, ECPGt_EORT);}
#line 70 "app/online/mm_ol_0026.pgc"


    if (sqlca.sqlcode != TX_SQL_OK && sqlca.sqlcode != TX_SQL_NOTFOUND) {
        TX_DBIO_LOG_ERR("SELECT mm_common_code agg");
        tx_buf_sets(ctx->out, "ERRCODE", "MM2609");
        tx_return(ctx, TX_EDB, ctx->out);
        return;
    }

    if (h_use_cnt <= 0)
        tx_log(TX_LOG_WARN, "[MM_OL_0026] 사용중 코드 없음 grp=%s", grp_cd);

    tx_buf_reset(ctx->out);
    tx_buf_sets(ctx->out, "GRPCD", grp_cd);
    tx_buf_sets(ctx->out, "GRPNM", h_grp_nm);
    tx_buf_setlong(ctx->out, "USECNT", h_use_cnt);
    tx_buf_sets(ctx->out, "LASTCHG", h_last_chg);
    tx_buf_sets(ctx->out, "ERRCODE", RESP_OK);
    tx_log(TX_LOG_INFO, "[MM_OL_0026] 캐시조회 완료 grp=%s 건수=%ld 최신=%s",
           grp_cd, h_use_cnt, h_last_chg);
    tx_return(ctx, TX_OK, ctx->out);
}
