#include "StdAfx.h"
#include "GLogDataAccountInfo.h"

CGLogDataAccountInfo::CGLogDataAccountInfo(_RecordsetPtr i_pSourceRecordSet,_CommandPtr i_pSourceCommand,_RecordsetPtr i_pDestRecordSet,_CommandPtr i_pDestCommand)
{
	this->Init(i_pSourceRecordSet,i_pSourceCommand,i_pDestRecordSet,i_pDestCommand);
}

CGLogDataAccountInfo::~CGLogDataAccountInfo(void)
{
}

void CGLogDataAccountInfo::ResetData()
{
	MEMSET_ZERO(m_arrAccountInfo,sizeof(CAccountInfo)*GLOG_DATA_MAX_CNT);
}

Err_t CGLogDataAccountInfo::GLogProcessing()
{
	int SelectCnt = 0;
	Err_t Err =	this->GLogDBSelect(&SelectCnt);
	if(ERR_NO_ERROR != Err)
	{
		return this->ErrorProcessing(Err);
	}
	g_pLogGlobal->m_pGlobalLogManager->WriteSystemLog(L"============== GLog CGLogDataAccountInfo Select Fin!!! ==============\r\n",TRUE);

	//Insert & Delete는 한행씩 처리.
	for(int Cnt = 0 ; Cnt < SelectCnt; Cnt++)
	{
		Err = this->GLogDBInsert(Cnt);
		if(ERR_NO_ERROR != Err
			&& ERR_DB_COM_DUPLICATE_INSERT != Err)
		{
			return this->ErrorProcessing(Err);
		}

		Err = this->GLogDBDelete(Cnt);
		if(ERR_NO_ERROR != Err)
		{
			return this->ErrorProcessing(Err);
		}
	}
	g_pLogGlobal->m_pGlobalLogManager->WriteSystemLog(L"============== GLog CGLogDataAccountInfo Update Fin!!! ==============\r\n",TRUE);

	this->ResetData();
	return ERR_NO_ERROR;
}

Err_t CGLogDataAccountInfo::GLogDBSelect(int * o_pSelectCnt)
{
	m_pSourceCommand->CommandText = QDB_GLOG_ACCOUNT_INFO_SELECT;
	try
	{
		m_pSourceRecordSet = m_pSourceCommand->Execute(NULL,NULL,adCmdStoredProc);  // 저장된 select문을 수행합니다.
	}
	catch(_com_error& e)
	{
		return ErrorLog(L"CGLogDataAccountInfo",e);
	}

	if(m_pSourceRecordSet->EndOfFile)     
	{
		m_pSourceRecordSet->Close();   // 레코드셋을 닫는다.(항상 레코드셋 오픈 후엔 닫아주는 작업을 수행해야 합니다.)
		return ERR_DB_NO_DATA;
	}   

	int				nCnt = 0;
	CAccountInfo	tmpAccountInfo;
	// 조회된 레코드의 끝까지 루핑을 돌며 데이터를 확인합니다.
	while(!m_pSourceRecordSet->EndOfFile)
	{	
		tmpAccountInfo.RegisteredDate		= m_pSourceRecordSet->Fields->GetItem("RegisteredDate")->GetValue();	
		tmpAccountInfo.Jointype				= m_pSourceRecordSet->Fields->GetItem("Jointype")->GetValue();
		tmpAccountInfo.AccountUniqueNumber	= m_pSourceRecordSet->Fields->GetItem("AccountUniqueNumber")->GetValue();
		LSTRNCPY_OLEDB(tmpAccountInfo.AccountName,m_pSourceRecordSet->Fields->GetItem("AccountName")->GetValue());
		LSTRNCPY_OLEDB(tmpAccountInfo.ExtCertifyCode,m_pSourceRecordSet->Fields->GetItem("ExtCertifyCode")->GetValue());	// 2014-02-24 by bckim, 채널링

		memcpy(&m_arrAccountInfo[nCnt],&tmpAccountInfo,sizeof(CAccountInfo));
		MEMSET_ZERO(&tmpAccountInfo,sizeof(CAccountInfo));
		// 리스트 컨트롤을 추가하셨거나 화면에 출력하실 생각이 있으시다면 
		// 루핑을 돌리며 값을 채워넣는 작업을 이부분에서 하시면 됩니다.
		m_pSourceRecordSet->MoveNext();    // 레코드셋을 다음으로 이동시킵니다.
		nCnt++;
	}

	*o_pSelectCnt = nCnt;
	m_pSourceRecordSet->Close();           // 레코드셋을 닫아줍니다.
	return ERR_NO_ERROR;
}

Err_t CGLogDataAccountInfo::GLogDBInsert(int i_nCnt)
{

	try
	{
		// 프로시저 설정
		m_pDestCommand->CommandText = QDB_GLOG_ACCOUNT_INFO_INSERT;

		// 프로시저에 들어갈 인자값 설정
		this->DBInsertParametersAppend(i_nCnt);

		// 프로시저 실행
		m_pDestCommand->Execute(NULL,NULL,adCmdStoredProc);

		// 인자값 초기화
		this->DBInsertParametersDelete();
	}
	catch(_com_error& e)
	{
		// 인자값 초기화
		this->DBInsertParametersDelete();
		return ErrorLog(L"CGLogDataCAccountInfo",e);
	}
	return ERR_NO_ERROR;
}

Err_t CGLogDataAccountInfo::GLogDBDelete(int i_nCnt)
{
	try
	{
		// 프로시저 설정
		m_pSourceCommand->CommandText = QDB_GLOG_ACCOUNT_INFO_DELETE;

		// 프로시저에 들어갈 인자값 설정
		m_pSourceCommand->Parameters->Append(m_pSourceCommand->CreateParameter(_bstr_t("AccountUniqueNumber"),adBigInt,adParamInput,8,_variant_t(m_arrAccountInfo[i_nCnt].AccountUniqueNumber)));

		// 프로시저 실행
		m_pSourceCommand->Execute(NULL,NULL,adCmdStoredProc);
		// 인자값 초기화
		m_pSourceCommand->Parameters->Delete(_bstr_t("AccountUniqueNumber"));
	}
	catch(_com_error& e)
	{
		return ErrorLog(L"CGLogDataCAccountInfoPlayTime",e);
	}
	return ERR_NO_ERROR;
}

void CGLogDataAccountInfo::DBInsertParametersAppend(int i_nCnt)
{
	m_pDestCommand->Parameters->Append(m_pDestCommand->CreateParameter(_bstr_t("RegisteredDate"),adDate,adParamInput,8,_variant_t(m_arrAccountInfo[i_nCnt].RegisteredDate)));
	m_pDestCommand->Parameters->Append(m_pDestCommand->CreateParameter(_bstr_t("Jointype"),adInteger,adParamInput,0,_variant_t(m_arrAccountInfo[i_nCnt].Jointype)));
	m_pDestCommand->Parameters->Append(m_pDestCommand->CreateParameter(_bstr_t("AccountUniqueNumber"),adBigInt,adParamInput,8,_variant_t(m_arrAccountInfo[i_nCnt].AccountUniqueNumber)));
	VARIANT vAccountName;
	vAccountName.vt = VT_BSTR;
	vAccountName.bstrVal = _bstr_t(m_arrAccountInfo[i_nCnt].AccountName); 
	m_pDestCommand->Parameters->Append(m_pDestCommand->CreateParameter(_bstr_t("AccountName"),adWChar,adParamInput,SIZE_MAX_ACCOUNT_NAME+1,vAccountName));

	// 2014-02-24 by bckim, 채널링
	VARIANT vExtCertifyCode;
	vExtCertifyCode.vt = VT_BSTR;
	vExtCertifyCode.bstrVal = _bstr_t(m_arrAccountInfo[i_nCnt].ExtCertifyCode); 
	m_pDestCommand->Parameters->Append(m_pDestCommand->CreateParameter(_bstr_t("ExtCertifyCode"),adWChar,adParamInput,USER_CI_SIZE+1,vExtCertifyCode));
	// End.2014-02-24 by bckim, 채널링

}

void CGLogDataAccountInfo::DBInsertParametersDelete()
{
	m_pDestCommand->Parameters->Delete(_bstr_t("Jointype"));
	m_pDestCommand->Parameters->Delete(_bstr_t("RegisteredDate"));		
	m_pDestCommand->Parameters->Delete(_bstr_t("AccountUniqueNumber"));
	m_pDestCommand->Parameters->Delete(_bstr_t("AccountName"));
	m_pDestCommand->Parameters->Delete(_bstr_t("ExtCertifyCode"));		// 2014-02-24 by bckim, 채널링
}
