/* @tier medium @module cl @transform closing-batch */ /* * cl_bt_0007.pgc - 재마감 배치 (취소 후 잔여건 재마감) [tier=medium] * * 마감 취소 이후 남아 있는 처리완료('M')/불일치('U') 건을 커서로 순회하여 * 정산완료('S')로 재마감한다. 재마감 건수와 처리 후 잔여(M/U) 건수를 * 재확인하여 리포트한다. 배치 main 엔트리 포함. * * 실행: cl_bt_0007 [db@host] */ #include #include #include #include "txcore.h" #include "txcore_dbio.h" #include "acq_util.h" #include "cl_core.h" EXEC SQL INCLUDE sqlca; static int run_cl_bt_0007(const char *biz_date) { EXEC SQL BEGIN DECLARE SECTION; char h_biz_date[9]; char h_key[16]; long h_amount; EXEC SQL END DECLARE SECTION; long total = 0, reclose = 0, errcnt = 0, sum_amt = 0; long remain_m, remain_u; char won[32]; int rc; strncpy(h_biz_date, biz_date, sizeof(h_biz_date) - 1); h_biz_date[sizeof(h_biz_date) - 1] = '\0'; EXEC SQL DECLARE cur_cl_bt_0007 CURSOR FOR SELECT key, amount FROM cl_ledger WHERE biz_date = :h_biz_date AND status IN ('M', 'U') ORDER BY key; EXEC SQL OPEN cur_cl_bt_0007; if (sqlca.sqlcode != TX_SQL_OK) { TX_DBIO_LOG_ERR("OPEN cur_cl_bt_0007"); return TX_EDB; } if (tx_begin() != TX_OK) tx_log(TX_LOG_WARN, "[cl_bt_0007] tx_begin 경고"); for (;;) { EXEC SQL FETCH cur_cl_bt_0007 INTO :h_key, :h_amount; if (sqlca.sqlcode == TX_SQL_NOTFOUND) break; if (sqlca.sqlcode != TX_SQL_OK) { TX_DBIO_LOG_ERR("FETCH cur_cl_bt_0007"); errcnt++; break; } total++; rc = cl_rec_update_status(h_key, CL_ST_SETTLED); if (rc != TX_OK) { tx_log(TX_LOG_ERROR, "[cl_bt_0007] 재마감 실패 key=%s rc=%d", h_key, rc); errcnt++; continue; } reclose++; sum_amt += h_amount; } EXEC SQL CLOSE cur_cl_bt_0007; if (errcnt == 0) tx_commit(); else tx_abort(); /* 처리 후 잔여 확인 */ remain_m = cl_rec_count(biz_date, CL_ST_DONE); remain_u = cl_rec_count(biz_date, CL_ST_FAIL); amount_format_won(sum_amt, won, sizeof(won)); tx_log(TX_LOG_INFO, "[cl_bt_0007] 요약 date=%s 대상=%ld 재마감=%ld 잔여M=%ld 잔여U=%ld 오류=%ld", biz_date, total, reclose, remain_m, remain_u, errcnt); printf("========== 재마감 배치 결과 [cl_bt_0007] ==========\n"); printf(" 영업일 : %s\n", biz_date); printf(" 재마감 대상 : %ld\n", total); printf(" 재마감(S) : %ld\n", reclose); printf(" 재마감 금액 : %s\n", won); printf(" 잔여 처리(M) : %ld\n", remain_m); printf(" 잔여 불일치(U): %ld\n", remain_u); printf(" 오류 건수 : %ld\n", errcnt); printf("=================================================\n"); return (errcnt == 0) ? TX_OK : TX_FAIL; } int main(int argc, char **argv) { const char *biz_date; const char *target; int rc; tx_set_loglevel(TX_LOG_INFO); if (argc < 2) { fprintf(stderr, "사용법: %s [db@host]\n", argv[0]); return 2; } biz_date = argv[1]; target = (argc >= 3) ? argv[2] : NULL; if (!date_is_valid(biz_date)) { fprintf(stderr, "오류: 유효하지 않은 영업일 '%s'\n", biz_date); return 2; } tx_log(TX_LOG_INFO, "[cl_bt_0007] 배치 시작 date=%s", biz_date); rc = cl_dbio_connect(target); if (rc != TX_OK) { tx_log(TX_LOG_ERROR, "[cl_bt_0007] DB 접속 실패 rc=%d", rc); return 1; } rc = run_cl_bt_0007(biz_date); cl_dbio_disconnect(); tx_log(TX_LOG_INFO, "[cl_bt_0007] 배치 종료 rc=%d(%s)", rc, tx_strerror(rc)); return (rc == TX_OK) ? 0 : 1; }