/* Processed by ecpg (15.18 (Debian 15.18-0+deb12u1)) */ /* These include files are added by the preprocessor */ #include #include #include /* End of automatic include section */ #line 1 "app/online/cl_ol_0017.pgc" /* @tier easy @module cl @transform closing-online */ /* * cl_ol_0017.pgc - 마감 온라인 서비스 (CL_OL_0017) [tier=easy] * * 일마감 검증 * ---------------------------------------------------------------------- * 요청 영업일(BIZDATE)에 대해 아직 마감되지 않은 미마감(R) 건이 하나도 * 없는지 확인하여 일마감 가능 여부를 판정한다. 미마감 건수가 0 이면 * 해당 영업일은 일마감 유효(VALID=Y), 1건이라도 남아 있으면 미마감 * 잔량이 존재하므로 일마감 불가(VALID=N)로 응답한다. * * 검증의 판단 근거를 함께 제공하기 위해 상태별 건수(R/M/U/S)를 모두 * 수집하여 응답하고, 마감 확정 시 정산이 이루어질 예정영업일(다음 * 영업일)을 계산하여 참고 필드로 반환한다. 요청 감사추적을 위해 * 선택 입력 REQID/OPER 를 로그와 응답에 반영한다. */ #include #include #include "txcore.h" #include "txcore_dbio.h" #include "acq_util.h" #include "cl_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 25 "app/online/cl_ol_0017.pgc" TX_SERVICE(CL_OL_0017, ctx) { /* exec sql begin declare section */ #line 30 "app/online/cl_ol_0017.pgc" char h_biz_date [ 9 ] ; #line 31 "app/online/cl_ol_0017.pgc" long h_db_total ; #line 32 "app/online/cl_ol_0017.pgc" long h_db_amt ; /* exec sql end declare section */ #line 33 "app/online/cl_ol_0017.pgc" char bizdate[16]; char reqid[32]; char oper[32]; char nextbiz[9]; char summary[TX_VAL_LEN]; long recv_cnt, done_cnt, fail_cnt, settled_cnt; long total_cnt, processed_cnt, complete_rate; long db_diff; int valid; tx_log(TX_LOG_INFO, "[CL_OL_0017] 일마감 검증 서비스 진입"); /* 1) 필수 입력 수신 */ if (tx_buf_get(ctx->in, "BIZDATE", bizdate, sizeof(bizdate)) != TX_OK) { tx_log(TX_LOG_ERROR, "[CL_OL_0017] BIZDATE 누락"); tx_buf_reset(ctx->out); tx_buf_sets(ctx->out, "RESPCODE", RESP_INVALID); tx_return(ctx, TX_EINVAL, ctx->out); return; } /* 감사추적용 선택 입력 (없으면 기본값) */ if (tx_buf_get(ctx->in, "REQID", reqid, sizeof(reqid)) != TX_OK) strncpy(reqid, "-", sizeof(reqid) - 1); if (tx_buf_get(ctx->in, "OPER", oper, sizeof(oper)) != TX_OK) strncpy(oper, "BATCH", sizeof(oper) - 1); /* 2) 영업일 형식 검증 */ if (!date_is_valid(bizdate)) { tx_log(TX_LOG_WARN, "[CL_OL_0017] 영업일 형식 오류 date=%s reqid=%s", bizdate, reqid); tx_buf_reset(ctx->out); tx_buf_sets(ctx->out, "RESPCODE", RESP_INVALID); tx_return(ctx, TX_EINVAL, ctx->out); return; } /* 3) 상태별 건수 수집 - 표준 DBIO 채널 이용 (read-only) */ recv_cnt = cl_rec_count(bizdate, CL_ST_RECV); done_cnt = cl_rec_count(bizdate, CL_ST_DONE); fail_cnt = cl_rec_count(bizdate, CL_ST_FAIL); settled_cnt = cl_rec_count(bizdate, CL_ST_SETTLED); if (recv_cnt < 0 || done_cnt < 0 || fail_cnt < 0 || settled_cnt < 0) { tx_log(TX_LOG_ERROR, "[CL_OL_0017] 상태별 건수 조회 실패 date=%s", bizdate); tx_buf_reset(ctx->out); tx_buf_sets(ctx->out, "RESPCODE", RESP_SYSERR); tx_return(ctx, TX_EDB, ctx->out); return; } total_cnt = recv_cnt + done_cnt + fail_cnt + settled_cnt; /* * 3-1) 원장 직접 집계로 상태별 DBIO 합계를 교차검증한다. * DBIO 채널 합계(total_cnt)와 원장 실집계(h_db_total)가 * 다르면 대사 불일치로 간주하여 감사 로그를 남긴다. */ strncpy(h_biz_date, bizdate, sizeof(h_biz_date) - 1); h_biz_date[sizeof(h_biz_date) - 1] = '\0'; h_db_total = 0; h_db_amt = 0; { ECPGdo(__LINE__, 0, 1, NULL, 0, ECPGst_normal, "select count ( * ) , coalesce ( sum ( amount ) , 0 ) from cl_ledger where biz_date = $1 ", ECPGt_char,(h_biz_date),(long)9,(long)1,(9)*sizeof(char), ECPGt_NO_INDICATOR, NULL , 0L, 0L, 0L, ECPGt_EOIT, ECPGt_long,&(h_db_total),(long)1,(long)1,sizeof(long), ECPGt_NO_INDICATOR, NULL , 0L, 0L, 0L, ECPGt_long,&(h_db_amt),(long)1,(long)1,sizeof(long), ECPGt_NO_INDICATOR, NULL , 0L, 0L, 0L, ECPGt_EORT);} #line 100 "app/online/cl_ol_0017.pgc" if (sqlca.sqlcode != TX_SQL_OK && sqlca.sqlcode != TX_SQL_NOTFOUND) { TX_DBIO_LOG_ERR("CL_OL_0017 direct total"); tx_buf_reset(ctx->out); tx_buf_sets(ctx->out, "RESPCODE", RESP_SYSERR); tx_return(ctx, TX_EDB, ctx->out); return; } db_diff = total_cnt - h_db_total; if (db_diff != 0) tx_log(TX_LOG_WARN, "[CL_OL_0017] 집계 대사 불일치 dbio=%ld ledger=%ld", total_cnt, h_db_total); /* 4) 일마감 유효성 판정: 미마감 잔량이 0 이어야 유효 */ valid = (recv_cnt == 0) ? 1 : 0; /* 5) 처리 진행률 = (마감 대상에서 미마감을 제외한 비율) */ processed_cnt = done_cnt + fail_cnt + settled_cnt; complete_rate = (total_cnt > 0) ? (processed_cnt * 100 / total_cnt) : 100; /* 6) 마감 확정 시 정산 예정영업일 = 다음 영업일 */ if (date_next_business(bizdate, nextbiz) != 0) { memcpy(nextbiz, bizdate, 8); nextbiz[8] = '\0'; } /* 7) 판정 요약 문구 구성 */ snprintf(summary, sizeof(summary), "영업일 %s 마감대상 %ld건 중 미마감 %ld건 (처리율 %ld%%)", bizdate, total_cnt, recv_cnt, complete_rate); /* 8) 응답 구성 */ tx_buf_reset(ctx->out); tx_buf_sets(ctx->out, "RESPCODE", RESP_OK); tx_buf_sets(ctx->out, "BIZDATE", bizdate); tx_buf_sets(ctx->out, "REQID", reqid); tx_buf_sets(ctx->out, "OPER", oper); tx_buf_setlong(ctx->out, "RECVCNT", recv_cnt); tx_buf_setlong(ctx->out, "DONECNT", done_cnt); tx_buf_setlong(ctx->out, "FAILCNT", fail_cnt); tx_buf_setlong(ctx->out, "SETTLEDCNT", settled_cnt); tx_buf_setlong(ctx->out, "TOTALCNT", total_cnt); tx_buf_setlong(ctx->out, "DBTOTAL", h_db_total); tx_buf_setlong(ctx->out, "DBAMT", h_db_amt); tx_buf_setlong(ctx->out, "DBDIFF", db_diff); tx_buf_sets(ctx->out, "RECONMATCH", (db_diff == 0) ? "Y" : "N"); tx_buf_setlong(ctx->out, "PROCESSEDCNT", processed_cnt); tx_buf_setlong(ctx->out, "COMPLETERATE", complete_rate); tx_buf_sets(ctx->out, "NEXTBIZ", nextbiz); tx_buf_sets(ctx->out, "SUMMARY", summary); tx_buf_sets(ctx->out, "VALID", valid ? "Y" : "N"); tx_buf_sets(ctx->out, "MESSAGE", valid ? "일마감 가능(미마감 없음)" : "미마감 잔량 존재로 일마감 불가"); tx_log(TX_LOG_INFO, "[CL_OL_0017] 완료 date=%s R=%ld M=%ld U=%ld S=%ld valid=%s reqid=%s", bizdate, recv_cnt, done_cnt, fail_cnt, settled_cnt, valid ? "Y" : "N", reqid); tx_return(ctx, TX_OK, ctx->out); }