/****************************************************************
// source file name		: WNAD_file.cc
// date of preperation	: 2016.03.09
// date of last update	: 2016.03.09
// 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"},
	{"2001900", "0d"},
	{"0000000", ""  }
};

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

int set_WNSF_data(char *pcFileName)
{
	char				*pcData=0x00;
	char				acSrcFileFullPath[512], acDstFileFullPath[512];
	char 				path[128], spath[128], tpath[128];
	char 				acCmd[256];
	E_ISSUER_HEAD		setEissHeader;
	E_ISSUER_DATA		stEIssData;
	BLTM_BINTBL			*pstBintbl=0x00;
	int					cnt, iTotDataCnt=0, iOffSet=0;
	int					errflg, iIssCnt=0;
	int  				isfd, isrc, i, ii;
	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];
	char t_msg[128]; memset(t_msg, 0x00, sizeof(t_msg));
	
	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("WNSF FILE HANDLE START..");

	memset(acSrcFileFullPath, 0x00, sizeof(acSrcFileFullPath));
	memset(acDstFileFullPath, 0x00, sizeof(acDstFileFullPath));
	
	memset(&setEissHeader, 0x00, sizeof(setEissHeader));
	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/ISSAFTER_S", DIR_TCS, DIR_BASEINFO);
	unlink(path);

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

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

	tfd=lb_build(tpath, FINOUT+FC_EXCLLOCK);
    if(tfd < 0)
    {
        sprintf(msg, "TIBERO CSV :파일생성 실패[%d]", errno);
        lb_err_log("manager", msg, 4);
        proc_log( "ISSAFTER File lb_fbuild error[%d]\n", errno );
        return(-1);
    }
#ifdef _ORACLE_
	if(c_DbConnect()<0)
	{
		printf_s( "worksvr db connect error\n");
		disconnect_db();
		return 3;
	}
#endif

	fp = fopen(acSrcFileFullPath, "r+");
	if(fp == NULL)
    {
        printf("후불 BIN File Not Found!(%s)\n", acSrcFileFullPath);
		errflg = 1;
    }else
	{
		fseek(fp, 0, SEEK_SET);
		fread(&wnsf_h, 9, 1, fp);

		smemcpy(D_TO_A, L_TO_H, tmp_seq, wnsf_h.app_seq, 4, 0);
//proc_log("[%02x][%02x]\n", wnsf_h.app_seq[0], wnsf_h.app_seq[1]);
		sprintf(swap_seq, "%.2s%.2s", tmp_seq+2, tmp_seq);
//proc_log("[%.4s][%.4s]\n", tmp_seq, swap_seq);
		cnt = strtoul(swap_seq, NULL, 16);
//proc_log("ISS File 6[%d]\n", cnt);

		for(i = 0; i < cnt; i++)
		{
			fseek(fp, 9*i+9, SEEK_SET);
			fread(&wnsf_f, 9, 1, fp);
	
			smemcpy(D_TO_A, L_TO_H, tmp_bin_cd, wnsf_f.bin_code, 6, 0);
			smemcpy(D_TO_A, L_TO_H, tmp_iss_cd, wnsf_f.iss_cd, 8, 0);
			smemcpy(D_TO_A, L_TO_H, tmp_comp_cd, wnsf_f.clss, 4, 0);
			sprintf(srch_iss_cd, "%.7s", tmp_iss_cd+1);
#ifdef _ORACLE_
			EXEC SQL
			SELECT CARD_ISSUE_ID
			INTO :tmp_crd_cd
			FROM JOIN_CARD_INFO
			WHERE ISSUE_ID = :srch_iss_cd
			;
	
			if (SQLCODE < 0)
			{
				proc_log("[%s]\n", tmp_crd_cd);
				proc_log("SQLCODE = %d[%s] CARD_ISSUE_CD Select Fail\n", SQLCODE, sqlca.sqlerrm.sqlerrmc);
				return(1);
			}
			
			sprintf((char *)iss_comp_tbl_f.crd_co, "0%c", tmp_crd_cd[0]);
#else
            for(ii = 0; strncmp(rcv_card_code[ii].issue_id, "0000000", 7) != 0; ii++)
			{
				//proc_log("#######  Get FILE ISSUE CODE [%.7s]", srch_iss_cd);
				if(strncmp(rcv_card_code[ii].issue_id, srch_iss_cd, 7) == 0)
				{
					proc_log("########## Check ISSUE_CODE [%.7s]", srch_iss_cd);
					break;
				}
			}
			
			proc_log("############## Get Card CODE[%d] [%.2s][%.7s]", ii, rcv_card_code[ii].card_issue_id, rcv_card_code[ii].issue_id);
			
			sprintf((char *)iss_comp_tbl_f.crd_co, "%.2s", rcv_card_code[ii].card_issue_id);
#endif
//printf("[%c][%s]\n", tmp_crd_cd[0], srch_iss_cd);
			sprintf((char *)iss_comp_tbl_f.comp_cd, "%.4s", tmp_comp_cd);
			sprintf((char *)iss_comp_tbl_f.bin_code, "%08d", lb_data2atoi(tmp_bin_cd, 6));
			sprintf((char *)iss_comp_tbl_f.issue_cd, "%.7s", srch_iss_cd);
	
			isrc = write(isfd, &iss_comp_tbl_f, sizeof(struct iss_comp_tbl));
			if (isrc == F_FAILURE)
			{
				sprintf(msg, "ISSAFTER:파일쓰기 실패[%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("ISSAFTER_CSV:파일쓰기 실패[%d]", errno);
				break;
			}
#endif
		}
#ifdef _ORACLE_
        EXEC SQL COMMIT WORK;
#endif
//proc_log("ISS File 7\n");
		fclose(fp);
	}
    lb_close(tfd);
	lb_close(isfd);

#ifdef _ORACLE_
    disconnect_db();
#endif

    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_A", DIR_BIN);
    system(msg);

	proc_log("WNSF FILE READ END");

	sprintf(path, "%s/%s/ISSAFTER", DIR_TCS, DIR_BASEINFO);
	sprintf(spath, "%s/%s/ISSAFTER_O", DIR_TCS, DIR_BASEINFO);
	lb_rename(path, spath);

	sprintf(path, "%s/%s/ISSAFTER_S", DIR_TCS, DIR_BASEINFO);
	sprintf(spath, "%s/%s/ISSAFTER", DIR_TCS, DIR_BASEINFO);
	lb_rename(path, spath);

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

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

	term_WNSF(pcData, pstBintbl);
	
	proc_log("WNSF FILE HANDLE END..");

	return 0;
}
