/****************************************************************
// source file name		: WMAD_file.cc
// date of preperation	: 2016.03.08
// date of last update	: 2016.03.08
// developer			: mskim
// description 			: 선불 발행사 파일처리
****************************************************************/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <malloc.h>
#include <sys/file.h>
#include <sqlca.h>
#include "dir_path.h"
#include "crd_def.h"
#include "util.h"
#include "iss_comp.h"
#include "ora_sql.h"
#include "lo_typ_chg_def.h"
#include "func_list.h"

//#define _ORACLE_ 1

extern	char 	gacHomePath[128];
extern  char	gacCenterPlzId[128];

static struct {
    char issue_id[7];
	char card_issue_id[2];
} rcv_card_code[20] = {
	{"2000100", "01"},
	{"2000200", "02"},
	{"2000700", "03"},
	{"2000400", "04"},
	{"2000500", "05"},
	{"2000600", "06"},
	{"2000300", "07"},
	{"2000800", "08"},
	{"2000900", "09"},
	{"2001000", "0a"},
	{"2001100", "0b"},
	{"2001400", "0c"},
	{"0000000", ""  }
};

int term_WMSF(char* pcData, BLTM_BINTBL* pstBintbl)
{
	if(0x00 != pcData) 			free(pcData);
	if(0x00 != pstBintbl)	 	free(pstBintbl);
	pcData = 0x00;
	pstBintbl = 0x00;
	
	return 0;
}

int set_WMSF_data(char *pcFileName)
{
	char				acTmp[128];
	char				*pcData=0x00;
	char				acSrcFileFullPath[512]; 
	char				acDstFileFullPath[512];
	char 				path[128], spath[128], tpath[128];
	char 				acCmd[256];
	S_ISSUER_HEAD		stSIssHeader;
	S_ISSUER_DATA		stSIssData;
	BLTM_BINTBL			*pstBintbl=0x00;
	int					cnt, iTotDataCnt=0, iOffSet=0;
	int					errflg, iIssCnt=0;
	int  				isfd, isrc, i;
    int  				tfd, tsrc;
	long 				lFileSize, lReadSize;
	unsigned short		uTemp;
	unsigned char		acTemp[2];
	char swap_seq[4];
//EXEC SQL BEGIN DECLARE SECTION;
    char srch_iss_cd[7+1];
    char tmp_crd_cd[7+1];
    char db_card_comp[2+1];
    char db_comp_code[4+1];
    char db_bin_code[8+1];
    char db_iss_code[7+1];
//EXEC SQL END DECLARE SECTION;
	struct iss_comp_tbl iss_comp_tbl_f;
	FILE *fp = NULL;
	char msg[128]; memset(msg, 0x00, sizeof(msg));
    char tmp_bin_cd[6];
	char tmp_seq[4];
    char tmp_iss_cd[8];
    char tmp_comp_cd[4];

	struct {
        char app_date[7];
        char app_seq;
        char seq[3];
    } wmsf_h;
    
    struct {
        char bin_code[7];
        char clss;
        char use_cd;
    } wmsf_f;
    
    struct {
        char app_date[7];
        char app_seq[2];
    } wnsf_h;
    
    struct {
        char bin_code[3];
        char clss[2];
        char iss_cd[4];
    } wnsf_f;

    memset(path, 0x00, sizeof(path));
    memset(spath, 0x00, sizeof(spath));
	memset(tpath, 0x00, sizeof(spath));
	memset(tmp_crd_cd, 0x00, sizeof(tmp_crd_cd));
    memset(&iss_comp_tbl_f, 0, sizeof(struct iss_comp_tbl));
	proc_log("WMSF FILE HANDLE START..");

	memset(acSrcFileFullPath, 0x00, sizeof(acSrcFileFullPath));
	memset(acDstFileFullPath, 0x00, sizeof(acDstFileFullPath));

	memset(&stSIssHeader, 0x00, sizeof(stSIssHeader));
	memset(&pstBintbl, 0x00, sizeof(pstBintbl));

	sprintf(acSrcFileFullPath, "%s/FILE/RECV/%.8s", DIR_TCS, pcFileName);
	sprintf(acDstFileFullPath, "%s/FILE/RECV/SAVE/%.8s", DIR_TCS, pcFileName);

	sprintf(path, "%s/%s/ISSBEFORE_S", DIR_TCS, DIR_BASEINFO);
	unlink(path);

	sprintf(tpath, "%s/FILE/SEND/T_PREFIX_INFO_B.CSV", DIR_TCS);
	unlink(tpath);

	isfd=lb_build(path, FINOUT+FC_EXCLLOCK);
    if(isfd < 0)
    {
        sprintf(msg, "ISSBEFORE:파일생성 실패[%d]", errno);
        lb_err_log("manager", msg, 4);
        proc_log( "ISSBEFORE File lb_fbuild error[%d]\n", errno );
        return(-1);
    }

	tfd=lb_build(tpath, FINOUT+FC_EXCLLOCK);
    if(tfd < 0)
    {
        sprintf(msg, "ISSBEFORE CSV :파일생성 실패[%d]", errno);
        lb_err_log("manager", msg, 4);
        proc_log( "ISSAFTER CSV File lb_fbuild error[%d]\n", errno );
        return(-1);
    }

	fp = fopen(acSrcFileFullPath, "r+");

    if(fp == NULL)
    {
        proc_log("선불 BIN File Not Found!(%s)\n", acSrcFileFullPath);
		errflg = 1;
    }else
	{
		fread(&wmsf_h, 11, 1, fp);

		cnt = lb_data2atoi(wmsf_h.seq, 3);

proc_log("WMSF[%d]\n", cnt);
#ifdef _ORACLE_
        if(c_DbConnect()<0)
        {
            proc_log("WMSF DB 접속에러 %d", sqlca.sqlcode);
        }
#endif
		for(i = 0; i < cnt; i++)
		{
			fseek(fp, 9*i+11, SEEK_SET);
			fread(&wmsf_f, 9, 1, fp);
	
			sprintf((char *)iss_comp_tbl_f.crd_co, "00");
			sprintf((char *)iss_comp_tbl_f.comp_cd, "0000");
			sprintf((char *)iss_comp_tbl_f.bin_code, "%08d", lb_data2atoi(wmsf_f.bin_code, 7));
			sprintf((char *)iss_comp_tbl_f.issue_cd, "%07d", lb_data2atoi(wmsf_f.bin_code, 7));
	
			isrc = write(isfd, &iss_comp_tbl_f, sizeof(struct iss_comp_tbl));
			if (isrc == F_FAILURE)
			{
				sprintf(msg, "ISSBEFORE:파일쓰기 실패[%d]", errno);
				lb_err_log("oltp_in", msg, 4);
				errflg = 1;
				proc_log( "OPRM_ISS_COMP_INFO => [%d]: isrc[%d] \n", sqlca.sqlcode, isrc );
				break;
			}
            
            snprintf(db_card_comp, 2+1, "%s", iss_comp_tbl_f.crd_co);
            snprintf(db_comp_code, 4+1, "%s", iss_comp_tbl_f.comp_cd);
            snprintf(db_bin_code, 8+1, "%s", iss_comp_tbl_f.bin_code);
            snprintf(db_iss_code, 7+1, "%s", iss_comp_tbl_f.issue_cd);
#ifdef _ORACLE_
            EXEC SQL
            INSERT INTO T_PREFIX_INFO
            (CARD_COMP, COMP_CODE, BIN_CODE, ISS_CODE)
            VALUES
            (:db_card_comp, :db_comp_code, :db_bin_code, :db_iss_code);
            
            if(sqlca.sqlcode < 0 && sqlca.sqlcode != -1)
            {
                proc_log("선불 BIN[%s] File DB Proc Error!(%d)[%s]\n", db_bin_code, sqlca.sqlcode, sqlca.sqlerrm.sqlerrmc);
            }
#endif
#if 1
            memset(msg, 0x00, sizeof(msg));
            sprintf(msg, "%.2s,%.4s,%.8s,%.7s%c%c", iss_comp_tbl_f.crd_co, iss_comp_tbl_f.comp_cd, iss_comp_tbl_f.bin_code, iss_comp_tbl_f.issue_cd, '\n', '\0');
            tsrc = write(tfd, &msg, 25);
			if (isrc == F_FAILURE)
			{
				proc_log("ISSBEFORE_CSV:파일쓰기 실패[%d]", errno);
				break;
			}
#endif
		}
#ifdef _ORACLE_
        EXEC SQL COMMIT WORK;
            
        disconnect_db();
#endif
//proc_log("ISS File 4\n");
		fclose(fp);
	}
    lb_close(tfd);
	lb_close(isfd);
	
    if (errflg)
    {
        proc_log( "BIN File lb_writel error[%d]\n", errno );
        return(1);
    }
            
    memset(msg, 0x00, sizeof(msg));
    sprintf(msg, "%s/putFILE.sh ISS T_PREFIX_INFO_B", DIR_BIN);
    system(msg);

	proc_log("WMSF FILE READ END");

	// to do... DB 처리
	//WMSF_INFO_UPDATE(pstBintbl, iTotDataCnt);
	// end

	sprintf(path, "%s/%s/ISSBEFOR", DIR_TCS, DIR_BASEINFO);
	sprintf(spath, "%s/%s/ISSBEFORE_O", DIR_TCS, DIR_BASEINFO);
	lb_rename(path, spath);

	sprintf(path, "%s/%s/ISSBEFORE_S", DIR_TCS, DIR_BASEINFO);
	sprintf(spath, "%s/%s/ISSBEFORE", DIR_TCS, DIR_BASEINFO);
	lb_rename(path, spath);

	// 처리 완료된 파일은 옮긴다

	memset(acCmd, 0x00, sizeof(acCmd));
	
	sprintf(acCmd, "mv -f %s %s", acSrcFileFullPath, acDstFileFullPath);
	system(acCmd);
		
	proc_log("FILE MOVE [%s]", acCmd);

	term_WMSF(pcData, pstBintbl);
	
	proc_log("WNAW FILE HANDLE END..");

	return 0;
}
