//��ConnectSocket.cpp�ļ���ʵ�������׽��ֵĴ�����������CConnectThread���
//һ���֣��յ������ɺ���OnReceive()��ɴ����� movenext 4096
#include "stdafx.h"
#include "DBServer.h"
#include "theDBServer.h"
#include "ConnectSocket.h"
#include "ConnectThread.h"
#include "DBServerDlg.h"
#include "DataSocket.h"
#include "mysqldata.h"
#include "./include/netMain.h"
#include "./include/protocolhdr.h"
#include "./include/struct_def.h"
#include "Lzari.h"
#include "MyLock.h"
#include "Shlwapi.h"
#include "CalendarConvert.h"
#include "AddUserDlg.h"
#include "NetShareInfo.h"
#include "AdoImplSvr.h"
#include "DataDef.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
extern void CovertDate(CString &date, CString &date2);

extern void ConvertToPrice(CString &str);
extern CDBServerDlg*	g_pMainWnd;
extern CtheDBServer		theServer;
extern CtheDBServer*	g_pWndServer;

void CConnectSocket::GetDataFromCode5(BYTE &code, BYTE **szDataBuf, DWORD &nDataLen, DWORD &arraysize, CString filter)
{//m_List1array
	int nStep = 0;
	try
	{
		if (g_ListData[code])
			delete[](g_ListData[code]);
		g_ListData[code] = NULL;

		nStep = 1;
		DWORD ii = 0;
		CArray<CStringArray, CStringArray> listArray;
		try
		{
			switch (code)
			{
			case 1:				//ִ��sql���
				{
					if(m_pAdoImpl == NULL)
						return;

					if(filter == _T(""))
						return;

					TCHAR tszSql[4096] = {0};
					wsprintf(tszSql, _T("%s"), filter);
					m_pAdoImpl->Execute(tszSql);
				}
				break;
			case 2:				//log
				{
					int nFieldCount = 0;
					CDataBase* p = new Tbl2015;
					CTime tm = CTime::GetCurrentTime();
					CString strTableName = _T("");
					strTableName.Format(_T("[%s]"), tm.Format(_T("%Y")));
					CString strSQL = _T("");
					if(filter == _T(""))
						strSQL = _T("select * from ") + strTableName;
					else
						strSQL = _T("select * from ") + strTableName + _T(" where ") + filter;
					p->GetRecordSet(m_pAdoImpl, strTableName, filter.GetString(), strSQL, nFieldCount, listArray);
					delete p;
				}
				break;
			}
			
		}
#if JEFF_TEST_ON
		catch (CException* e)
		{
			TCHAR szError[1024] = { 0 };
			e->GetErrorMessage(szError, 1024);
			WriteLogin(CString(szError));
			e->Delete();
			CString ss;
			ss.Format("#�����ں���getdata��-code:%d/%d ip:%s", code, nStep, ((CConnectThread *)AfxGetThread())->m_strRemoteHost);
			WriteLogin(ss);
			return;
		}
#else
		catch(CDBException * e)
		{
			WriteLogin(e->m_strError);
			CString ss;
			ss.Format ("#�����ں���getdata��-code:%d/%d ip:%s", code, nStep, ((CConnectThread *)AfxGetThread())->m_strRemoteHost);
			WriteLogin(ss);	 		

			e->Delete();
			return; 
		}
#endif

		nStep = 2;


		CMemFile memfile;
		CArchive ar(&memfile, CArchive::store);
		for (ii = 0; ii < listArray.GetSize(); ii++)
		{
			listArray.ElementAt(ii).Serialize(ar);
		}
		ar.Close();
		nStep = 3;

		if (g_ListData[code])delete[](g_ListData[code]); g_ListData[code] = NULL;

		nStep = 4;
		int leng = memfile.GetLength();
		BOOL bZip = 0;
		BOOL bOK = 0;
		int lengtemp = leng;
		while (lengtemp)
		{
			if (lengtemp % 2 == 0)
			{
				lengtemp /= 2;
				if (lengtemp == 1)
				{
					bOK = 1;
					break;
				}
			}
			else
				break;
		}
		nStep = 5;
		if (leng < 10240 || bOK)
		{
			g_ListData[code] = memfile.Detach();
			nDataLen = g_ListLeng[code] = leng;
			*szDataBuf = g_ListData[code];
			arraysize = g_ListSize[code] = listArray.GetSize();
		}
		else
		{
			bZip = 1;
			BYTE *pData = memfile.Detach();
			const BYTE *lpszOut = NULL;
			int nOutSize = 0;
			LZARI Lzari;
			Lzari.Compress(pData, leng, lpszOut, nOutSize);
			delete[]pData;
			g_ListData[code] = new BYTE[nOutSize];
			memcpy(g_ListData[code], lpszOut, nOutSize);	Lzari.Release();
			nDataLen = g_ListLeng[code] = nOutSize;
			*szDataBuf = g_ListData[code];
			arraysize = g_ListSize[code] = listArray.GetSize();
		}
		code = bZip;
		nStep = 6;
	}
#if JEFF_TEST_ON
	catch (CException* e)
	{
		CString ss;
		ss.Format("�����ں���getdata��-code:%d/%d ip:%s", code, nStep, ((CConnectThread *)AfxGetThread())->m_strRemoteHost);
		WriteLogin(ss);
		TCHAR szError[1024] = { 0 };
		e->GetErrorMessage(szError, 1024);
		WriteLogin(CString(szError));
		e->Delete();
	}
#else
	catch(...)
	{
		CString ss;
		ss.Format ("�����ں���getdata��-code:%d/%d ip:%s", code, nStep, ((CConnectThread *)AfxGetThread())->m_strRemoteHost);
		WriteLogin(ss);	 
	}
#endif
}