
/* Result Sets Interface */
#ifndef SQL_CRSR
#  define SQL_CRSR
  struct sql_cursor
  {
    unsigned int curocn;
    void *ptr1;
    void *ptr2;
    unsigned int magic;
  };
  typedef struct sql_cursor sql_cursor;
  typedef struct sql_cursor SQL_CURSOR;
#endif /* SQL_CRSR */

/* Thread Safety */
typedef void * sql_context;
typedef void * SQL_CONTEXT;

/* Object support */
struct sqltvn
{
  unsigned char *tvnvsn; 
  unsigned short tvnvsnl; 
  unsigned char *tvnnm;
  unsigned short tvnnml; 
  unsigned char *tvnsnm;
  unsigned short tvnsnml;
};
typedef struct sqltvn sqltvn;

struct sqladts
{
  unsigned int adtvsn; 
  unsigned short adtmode; 
  unsigned short adtnum;  
  sqltvn adttvn[1];       
};
typedef struct sqladts sqladts;

static struct sqladts sqladt = {
  1,1,0,
};

/* Binding to PL/SQL Records */
struct sqltdss
{
  unsigned int tdsvsn; 
  unsigned short tdsnum; 
  unsigned char *tdsval[1]; 
};
typedef struct sqltdss sqltdss;
static struct sqltdss sqltds =
{
  1,
  0,
};

/* File name & Package Name */
struct sqlcxp
{
  unsigned short fillen;
           char  filnam[14];
};
static const struct sqlcxp sqlfpn =
{
    13,
    "ft_parsing.pc"
};


static unsigned int sqlctx = 634003;


static struct sqlexd {
   unsigned long  sqlvsn;
   unsigned int   arrsiz;
   unsigned int   iters;
   unsigned int   offset;
   unsigned short selerr;
   unsigned short sqlety;
   unsigned int   occurs;
      const short *cud;
   unsigned char  *sqlest;
      const char  *stmt;
   sqladts *sqladtp;
   sqltdss *sqltdsp;
   unsigned char  **sqphsv;
   unsigned long  *sqphsl;
            int   *sqphss;
            short **sqpind;
            int   *sqpins;
   unsigned long  *sqparm;
   unsigned long  **sqparc;
   unsigned short  *sqpadto;
   unsigned short  *sqptdso;
   unsigned int   sqlcmax;
   unsigned int   sqlcmin;
   unsigned int   sqlcincr;
   unsigned int   sqlctimeout;
   unsigned int   sqlcnowait;
            int   sqfoff;
   unsigned int   sqcmod;
   unsigned int   sqfmod;
   unsigned int   sqlpfmem;
   unsigned char  *sqhstv[1];
   unsigned long  sqhstl[1];
            int   sqhsts[1];
            short *sqindv[1];
            int   sqinds[1];
   unsigned long  sqharm[1];
   unsigned long  *sqharc[1];
   unsigned short  sqadto[1];
   unsigned short  sqtdso[1];
} sqlstm = {13,1};

/* SQLLIB Prototypes */
extern void sqlcxt (void **, unsigned int *,
                    struct sqlexd *, const struct sqlcxp *);
extern void sqlcx2t(void **, unsigned int *,
                    struct sqlexd *, const struct sqlcxp *);
extern void sqlbuft(void **, char *);
extern void sqlgs2t(void **, char *);
extern void sqlorat(void **, unsigned int *, void *);

/* Forms Interface */
static const int IAPSUCC = 0;
static const int IAPFAIL = 1403;
static const int IAPFTL  = 535;
extern void sqliem(unsigned char *, signed int *);

typedef struct { unsigned short len; unsigned char arr[1]; } VARCHAR;
typedef struct { unsigned short len; unsigned char arr[1]; } varchar;

/* cud (compilation unit data) array */
static const short sqlcud0[] =
{13,4274,1,0,0,
5,0,0,1,0,0,29,157,0,0,0,0,0,1,0,
20,0,0,2,0,0,31,180,0,0,0,0,0,1,0,
35,0,0,3,0,0,32,184,0,0,0,0,0,1,0,
50,0,0,4,0,0,29,235,0,0,0,0,0,1,0,
65,0,0,5,0,0,31,253,0,0,0,0,0,1,0,
80,0,0,6,0,0,29,270,0,0,0,0,0,1,0,
95,0,0,7,0,0,31,288,0,0,0,0,0,1,0,
};


/*
 *========================================================================
 *
 *   시스템명 : 요금징수 시스템
 *   파 일 명 : fp_parsing.pc
 *   작 성 자 : 조보용 (catacath@neighbor21.co.kr)
 *   작 성 일 : 2012.10.10
 *   수 정 일 :
 *   설    명 : 파일/테이블 변환 개별 모듈 호출
 *
 *========================================================================
*/

#include <stdio.h>
#include <stdlib.h>
#include <fcntl.h>
#include <time.h>
#include <sys/time.h>
#include <errno.h>
#include <unistd.h>
#include <string.h>
#include <sqlca.h>

#include "lb_func_list.h"
#include "chg_struc.h"
#include "ora_sql.h"
#include "file_no.h"
#include "file_nm.h"
#include "file_lb.h"
#include "func_list.h"

#include "tcs_db_def.h"

#if 0
#define  USERID  "gntcs"
#define  PASSWD  "gntcs"
#define  TNSNAME "tcs"
#endif

extern    int    chng_t2f_basictbl();
extern    int    chng_t2f_wrkrpersonalinfo();
#if 0
extern    int    chng_t2f_holitbl();
#endif
extern    int    chng_t2f_isscomptbl();

extern    int    chng_t2f_immcarnotbl();
extern    int    chng_t2f_immcardtbl();
extern    int    chng_t2f_befmst();
extern    int    chng_t2f_befchg();
extern    int    chng_t2f_blmst();
extern    int    chng_t2f_blchg();
extern    int    chng_t2f_plmst();
extern    int    chng_t2f_plchg();
extern    int    chng_t2f_redcarnotbl();

int    logon_ora_svr = 1;        /* connected to ORACLE        */

struct    file_tbl {
    int    file_id;          /* 변환 FILE ID              */
    int    (*func)();        /* 변환 FUNCTION             */
} file_tbl[] = {             /* 내용 생성 변환            */
    { FNO_BASICTBL       , chng_t2f_basictbl         }, /* 기초사항테이블        */
    { FNO_WRKRINFO       , chng_t2f_wrkrpersonalinfo }, /* 근무자인적테이블      */
    { FNO_HOLITBL        , 0                         }, /* 휴일정보테이블        */
    { FNO_ISSCOMPTBL     , chng_t2f_isscomptbl       }, /* 발행사테이블          */
    { FNO_IMM_CARNO      , 0      }, /* 차량번호면제테이블    */
    { FNO_IMM_CARDNO     , 0       }, /* 카드번호면제테이블    */
    { FNO_BEFMST         , 0           }, /* 사전등록마스터테이블  */
    { FNO_PLMST          , chng_t2f_plmst            }, /* PL마스터테이블        */
    { FNO_BLMST          , chng_t2f_blmst            }, /* BL마스터테이블        */
    { FNO_RED_CARNO      , 0      }, /* 차량번호감면테이블    */
    { FNO_BEFCNG         , 0           }, /* 사전등록변경분테이블  */
    { FNO_PLCNG          , chng_t2f_plchg            }, /* PL변경분테이블        */
    { FNO_BLCNG          , chng_t2f_blchg            }, /* BL변경분테이블        */
    { FNO_IMM_CARNO_CHG  , 0      }, /* 차량번호면제테이블    */
    { FNO_IMM_CARDNO_CHG , 0       }, /* 카드번호면제테이블    */
    { FNO_RED_CARNO_CHG  , 0      }, /* 차량번호감면테이블    */
    { -1                 , NULL                      }  /* End of tables         */
};

int chg_struc_log( struct chg_struc *cs )
{
    if( !cs )
    {
        printf( " chg_struc print NULL !!! \n" );
        return 1;
    }

    proc_log( "차로번호[%d] FID[%d]\n", cs->lane_no, cs->file_id);
    proc_log( "0:F->T,1:T->F [%d]\n", cs->tbl2file_div);

    return 0;
}

/*******************************************************************************
** 1. 모 듈 명 :  file_tbl_chng
** 2. 모듈기능 :  FILE & DB TABLE 변환 각 모듈 호출
**
** 3. 입    력 :
**    struct xchg *xchg
** 4. 입 출 력 :
**
** 5. 출    력 :
**
** 6. Fuction Return:
**    정상 : 0
**    SAM 파일이상: 1
**    SQL Error : 2
** 7. 변경이력 :
**------------------------------------------------------------------------------
** Version  Date        Comment
** 1.00     2012/07/23  - TCS VERSION(C/S 환경)
*******************************************************************************/
#if 0
int file_tbl_chng(struct chg_struc *cs)
#else
int file_tbl_chng(char *buffer)
#endif
{
    int    retc = 0;
    int    ii;
//printf("2222222\n");
    if(c_DbConnect()<0)
    {
        printf_s( "worksvr db connect error\n");
        disconnect_db();
        return 3;
    }
//printf("33333333\n");
#if 0
    for (ii = 0; file_tbl[ii].file_id != -1; ii++)
    {
        if (file_tbl[ii].file_id == cs->file_id)
            break;
    }

    if (file_tbl[ii].file_id == -1)        /* not found file id.    */
    {
        return (0);
    }

    /*--------------------------------------------------------------*/
    /* ORACLE/DB CONNECTION 상태가 정상이 아닌 경우 향후 재처리     */
    /* 가능토록 -1 RETURN 한다. 따라서 각 변환 PROCEDURE는 복구가능 */
    /* 유무에 의한 반환값을 정확히 RETURN 하여야 한다.              */
    /* 변환처리 오류시, 반환값은 0: 정상, 1:SAM-F ERROR, 2:sqlerr   */
    /* sql 오류시 sqlca struct를 보존하여 RETRUN 할 것.             */
    /*--------------------------------------------------------------*/

    retc = (*file_tbl[ii].func)(cs);        /* conversion funcedure    */

    switch (retc)
    {
        /* 정상인 경우 */
    case 0:
        /* EXEC SQL COMMIT WORK; */ 

{
        struct sqlexd sqlstm;
        sqlorat((void **)0, &sqlctx, &oraca);
        sqlstm.sqlvsn = 13;
        sqlstm.arrsiz = 0;
        sqlstm.sqladtp = &sqladt;
        sqlstm.sqltdsp = &sqltds;
        sqlstm.iters = (unsigned int  )1;
        sqlstm.offset = (unsigned int  )5;
        sqlstm.cud = sqlcud0;
        sqlstm.sqlest = (unsigned char  *)&sqlca;
        sqlstm.sqlety = (unsigned short)4352;
        sqlstm.occurs = (unsigned int  )0;
        sqlcxt((void **)0, &sqlctx, &sqlstm, &sqlfpn);
}


        break;

        /* 파일오류 - DB변화 없음 */
    case 1:
        proc_log("%s SAM파일오류 %04d/%02d/%02d %02d:%02d:%02d (%d)차로 연번(%d) 종별(%d)",
                 file_nm[file_tbl[ii].file_id].file_nm, cs->year, cs->mon, cs->day, cs->hr, cs->min, cs->sec, cs->lane_no, cs->seq_no, cs->knd);
        break;

        /* SQL오류 - 재처리필요 */
    case 3:
        proc_log("%s FILE WRITE 중.. 재처리필요", file_nm[file_tbl[ii].file_id].file_nm);
        break;

    default:
        proc_log("%s DB/FILE변환오류 (%d)차로 연번(%d) %.*s",
                  file_nm[file_tbl[ii].file_id].file_nm,
                  cs->lane_no,
                  cs->seq_no, sqlca.sqlerrm.sqlerrml,
                  sqlca.sqlerrm.sqlerrmc );

        if (sqlca.sqlcode >= 0 || sqlca.sqlcode == -1)
        {
            /* EXEC SQL ROLLBACK WORK; */ 

{
            struct sqlexd sqlstm;
            sqlorat((void **)0, &sqlctx, &oraca);
            sqlstm.sqlvsn = 13;
            sqlstm.arrsiz = 0;
            sqlstm.sqladtp = &sqladt;
            sqlstm.sqltdsp = &sqltds;
            sqlstm.iters = (unsigned int  )1;
            sqlstm.offset = (unsigned int  )20;
            sqlstm.cud = sqlcud0;
            sqlstm.sqlest = (unsigned char  *)&sqlca;
            sqlstm.sqlety = (unsigned short)4352;
            sqlstm.occurs = (unsigned int  )0;
            sqlcxt((void **)0, &sqlctx, &sqlstm, &sqlfpn);
}


            break;
        }

        /* EXEC SQL ROLLBACK WORK RELEASE; */ 

{
        struct sqlexd sqlstm;
        sqlorat((void **)0, &sqlctx, &oraca);
        sqlstm.sqlvsn = 13;
        sqlstm.arrsiz = 0;
        sqlstm.sqladtp = &sqladt;
        sqlstm.sqltdsp = &sqltds;
        sqlstm.iters = (unsigned int  )1;
        sqlstm.offset = (unsigned int  )35;
        sqlstm.cud = sqlcud0;
        sqlstm.sqlest = (unsigned char  *)&sqlca;
        sqlstm.sqlety = (unsigned short)4352;
        sqlstm.occurs = (unsigned int  )0;
        sqlcxt((void **)0, &sqlctx, &sqlstm, &sqlfpn);
}



        logon_ora_svr = 0;
    }
#else
    struct chg_struc cs;
    int gdiv = 0;
    char temp[MAX_STRING_SIZE + 1]; memset(temp, 0x00, sizeof(temp));
    char plz_id[2]; memset(plz_id, 0x00, sizeof(plz_id));
    char datetm[14]; memset(datetm, 0x00, sizeof(datetm));
    char job[4]; memset(job, 0x00, sizeof(job));
    char file[20]; memset(file, 0x30, sizeof(file));

    snprintf(temp, sizeof(temp)+1, "%s", buffer);
//printf("4444444\n");
    proc_log("PARSING [%s]\n", temp);
    memcpy(plz_id, temp+4, sizeof(plz_id));
    memcpy(job, temp+11, sizeof(job));
    memcpy(datetm, temp+23, sizeof(datetm));
    memcpy(file, temp+41, sizeof(file));
    printf("FILE [%s]\n", file);
    printf("PLZ_ID[%.2s] JOB[%.4s]\n", plz_id, job);
    
    if(strncmp(job, "0100", 4) == 0) gdiv = 0;
    else gdiv = 1;

    printf("GDIV[%x][%.14s]\n", gdiv, datetm);
    
    memcpy(cs.year, &datetm[0], 4);
    printf("FILE[%s]\n", file);
    memcpy(cs.mon, &datetm[4], 2);
    memcpy(cs.day, &datetm[6], 2);
    memcpy(cs.hr, &datetm[8], 2);
    memcpy(cs.min, &datetm[10], 2);
    memcpy(cs.sec, &datetm[12], 2);
    printf("YYYY[%.4s]MM[%.2s]DD[%.2s]\n", cs.year, cs.mon, cs.day);
    memcpy(cs.plz_id, plz_id, 2);
    printf("PLZ_ID[%.2s]\n", cs.plz_id);

    for (ii = 1; file_tbl[ii].file_id != -1; ii++)
    {
printf("55555\n");
        if(file[ii] == 0x30) continue;
        
        cs.file_id = ii;
        retc = (*file_tbl[ii].func)(&cs);        /* conversion funcedure    */
proc_log("NEXT [%d]\n", ii);
        switch (retc)
        {
            /* 정상인 경우 */
            case 0:
            /* EXEC SQL COMMIT WORK; */ 

{
            struct sqlexd sqlstm;
            sqlorat((void **)0, &sqlctx, &oraca);
            sqlstm.sqlvsn = 13;
            sqlstm.arrsiz = 0;
            sqlstm.sqladtp = &sqladt;
            sqlstm.sqltdsp = &sqltds;
            sqlstm.iters = (unsigned int  )1;
            sqlstm.offset = (unsigned int  )50;
            sqlstm.cud = sqlcud0;
            sqlstm.sqlest = (unsigned char  *)&sqlca;
            sqlstm.sqlety = (unsigned short)4352;
            sqlstm.occurs = (unsigned int  )0;
            sqlcxt((void **)0, &sqlctx, &sqlstm, &sqlfpn);
}


            break;
        
            /* 파일오류 - DB변화 없음 */
            case 1:
            proc_log("%s SAM파일오류 %14s file_id(%d)", datetm, ii);
            break;
        
            /* SQL오류 - 재처리필요 */
            case 3:
            proc_log("%s FILE WRITE 중.. 재처리필요", file_nm[file_tbl[ii].file_id].file_nm);
            break;
        
            default:
            proc_log("%s DB/FILE변환오류 %.*s", file_nm[file_tbl[ii].file_id].file_nm, sqlca.sqlerrm.sqlerrml, sqlca.sqlerrm.sqlerrmc );
        
            if (sqlca.sqlcode >= 0 || sqlca.sqlcode == -1)
            {
                /* EXEC SQL ROLLBACK WORK; */ 

{
                struct sqlexd sqlstm;
                sqlorat((void **)0, &sqlctx, &oraca);
                sqlstm.sqlvsn = 13;
                sqlstm.arrsiz = 0;
                sqlstm.sqladtp = &sqladt;
                sqlstm.sqltdsp = &sqltds;
                sqlstm.iters = (unsigned int  )1;
                sqlstm.offset = (unsigned int  )65;
                sqlstm.cud = sqlcud0;
                sqlstm.sqlest = (unsigned char  *)&sqlca;
                sqlstm.sqlety = (unsigned short)4352;
                sqlstm.occurs = (unsigned int  )0;
                sqlcxt((void **)0, &sqlctx, &sqlstm, &sqlfpn);
}


                break;
            }
        }
        
        if(retc != 0) break;
    }
//proc_log("NEXT 1-1[%d][%c]\n", retc, file[0]);
    if(retc == 0 && file[0] != 0x30)
    {
//proc_log("NEXT 2-1[%d]\n", FNO_BASICTBL);
        retc = (*file_tbl[0].func)(&cs);        /* conversion funcedure    */
//proc_log("NEXT 2-2[%d]\n", retc);
        switch (retc)
        {
            /* 정상인 경우 */
            case 0:
            /* EXEC SQL COMMIT WORK; */ 

{
            struct sqlexd sqlstm;
            sqlorat((void **)0, &sqlctx, &oraca);
            sqlstm.sqlvsn = 13;
            sqlstm.arrsiz = 0;
            sqlstm.sqladtp = &sqladt;
            sqlstm.sqltdsp = &sqltds;
            sqlstm.iters = (unsigned int  )1;
            sqlstm.offset = (unsigned int  )80;
            sqlstm.cud = sqlcud0;
            sqlstm.sqlest = (unsigned char  *)&sqlca;
            sqlstm.sqlety = (unsigned short)4352;
            sqlstm.occurs = (unsigned int  )0;
            sqlcxt((void **)0, &sqlctx, &sqlstm, &sqlfpn);
}


            break;
        
            /* 파일오류 - DB변화 없음 */
            case 1:
            proc_log("%s SAM파일오류 %14s file_id(%d)", datetm, 0);
            break;
        
            /* SQL오류 - 재처리필요 */
            case 3:
            proc_log("%s FILE WRITE 중.. 재처리필요", file_nm[file_tbl[0].file_id].file_nm);
            break;
        
            default:
            proc_log("%s DB/FILE변환오류 %.*s", file_nm[file_tbl[0].file_id].file_nm, sqlca.sqlerrm.sqlerrml, sqlca.sqlerrm.sqlerrmc );
        
            if (sqlca.sqlcode >= 0 || sqlca.sqlcode == -1)
            {
                /* EXEC SQL ROLLBACK WORK; */ 

{
                struct sqlexd sqlstm;
                sqlorat((void **)0, &sqlctx, &oraca);
                sqlstm.sqlvsn = 13;
                sqlstm.arrsiz = 0;
                sqlstm.sqladtp = &sqladt;
                sqlstm.sqltdsp = &sqltds;
                sqlstm.iters = (unsigned int  )1;
                sqlstm.offset = (unsigned int  )95;
                sqlstm.cud = sqlcud0;
                sqlstm.sqlest = (unsigned char  *)&sqlca;
                sqlstm.sqlety = (unsigned short)4352;
                sqlstm.occurs = (unsigned int  )0;
                sqlcxt((void **)0, &sqlctx, &sqlstm, &sqlfpn);
}


                break;
            }
        }
    }
#endif
    disconnect_db();
    return (retc);
}
