// scbc.tools.cpp : 定义 DLL 应用程序的导出函数。
//

#include "stdafx.h"
#include "scbc.tools.h"
#include "CurlClient.h"
#include "DataImpl.h"
#include "Global.h"
#include "SDK.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#endif

#ifdef TEST_MODE
// 唯一的应用程序对象

CWinApp theApp;

using namespace std;

void TEST_Download()
{
	printf("开始压测下载……\n");
	TCHAR szDir[MAX_PATH] = {0};
	_stprintf_s(szDir, _T("%s%s.exe"), Global::g_szCurModuleDir, "SunloginClient");
	for (int i = 0; i < 300000; i++)
	{
		CCurlClient curl;
		curl.Initialize();
		curl.DownloadEx("http://download.oray.com/sunlogin/windows/SunloginClient10.1.exe", szDir);
		DeleteFile(szDir);
		printf("\n\t完成第%d次\n\n", i);
	}
	printf("结束压测下载……\n");
}

void TEST_GetMidInfo()
{
	CSDK sdk;
	STMid mid;
	sdk.DownloadMidData("IDL144240G");
	Sleep(60000);
}

void TEST_InsertMidInfo()
{
	CDataImpl db;
	if ( db.Open() )
	{
		STMid mid;
		char szdata[10] = {0};
		for ( int i = 0; i < 100; i++ )
		{
			_itoa_s(i, szdata, 10);
			mid.order = szdata;
			mid.ctype = szdata;
			mid.version = "V8-SCBC098-LV1096";
			mid.number = "1000";
			mid.pid = "102";
			mid.pmd5 = "dfeqadfadfer546489er4adf";
			mid.purl = "https://www.baidu.com";
			db.InsertMidInfo(mid);
		}

		for ( int i = 0; i < 100; i++)
		{
			_itoa_s(i, szdata, 10);
			if ( i%2 )
				db.UpdateKeyReportStatus(szdata);
		}
	}
}

void TEST_InertKeys()
{
	CDataImpl db;
	if ( db.Open() )
	{
		STKeys keys;
		char szdata[10] = {0};
		db.TransactionBegin();
		for ( int i = 0; i < 50000; i++ )
		{
			_itoa_s(i, szdata, 10);
			keys.bid = "1";
			keys.pid = "102";
			keys.sn = szdata;
			keys.keys = "{\"did\":\"did\",\"mac\":\"mac\",\"cikey\":\"cikey\",\"hdcp\":\"hdcp\",\"hdcp22\":\"hdcp22\",\"widi\":\"widi\",\"esn\":\"esn\",\"widevine\":\"widevine\"}";
			db.InsertKeyInfo(keys);
		}
		db.TransactionCommit();
	}
}

void TEST_InertKeys2()
{
	CDataImpl db;
	if ( db.Open() )
	{
		char szdata[10] = {0};
		printf("批量插入数据:%s\n",CTime::GetCurrentTime().Format("%Y-%m-%d %H:%M:%S\n").GetString());
		
		std::vector<STKeys> vtKeys;
		vtKeys.resize(200);
		std::vector<STKeys>::iterator it = vtKeys.begin();
		for ( int i = 0; it != vtKeys.end(); it++, i++ )
		{
			_itoa_s(i, szdata, 10);
			it->bid = "1";
			it->pid = "102";
			it->sn = szdata;
			it->keys = "{\"did\":\"did\",\"mac\":\"mac\",\"cikey\":\"cikey\",\"hdcp\":\"hdcp\",\"hdcp22\":\"hdcp22\",\"widi\":\"widi\",\"esn\":\"esn\",\"widevine\":\"widevine\"}";
		}

		printf("开始批量插入数据:%s\n",CTime::GetCurrentTime().Format("%Y-%m-%d %H:%M:%S\n").GetString());
		db.BatchInsertKeyInfo(vtKeys);
		printf("完成批量插入数据:%s\n",CTime::GetCurrentTime().Format("%Y-%m-%d %H:%M:%S\n").GetString());
	}
}

void TEST_InertLog()
{
	CDataImpl db;
	if ( db.Open() )
	{
		STLog log;
		char szdata[10] = {0};
		db.TransactionBegin();
		for ( int i = 0; i < 200; i++ )
		{
			_itoa_s(i, szdata, 10);
			log.bid = "1";
			log.sn = szdata;
			log.type = "1";
			log.content = "测试内容：东方丽景散热地觚单位 加菲软糖加工厂饮用水";
			if ( i%2 )
				log.report_date = CTime::GetCurrentTime().Format("%Y-%m-%d %H:%M:%S").GetString();
			db.InsertLogInfo(log);
		}
		db.TransactionCommit();

		std::vector<STLog> vtLog;
		db.QueryUnReportLogInfo(vtLog);
		db.UpdateLogReportStatus("0", "1");
	}
}

void TEST_QueryMidInfo()
{
	CDataImpl db;
	if ( db.Open() )
	{
		STMid mid;
		db.QueryMidInfo("1", mid);

		printf("查询MID=>order=%s,ctype=%s,ver=%s, pid=%s, psize=%s, purl=%s, pmd5=%s \n", 
			mid.order.c_str(), mid.ctype.c_str(), mid.version.c_str(), mid.pid.c_str(), mid.psize.c_str(), mid.purl.c_str(), mid.pmd5.c_str());
	}
}

void TEST_QueryUnReportKey()
{
	CDataImpl db;
	if ( db.Open() )
	{
		std::vector<STKeys> vtKeys;
		db.QueryUnReportKeyInfo(vtKeys);
		std::vector<STKeys>::iterator it = vtKeys.begin();
		for (; it != vtKeys.end(); it++ )
		{
			printf("sn:%s\n", it->sn.c_str());
			//printf("pid:%s\n", it->pid.c_str());
			//printf("keys:%s\n", it->keys.c_str());
			printf("copy_date:%s\n", it->copy_date.c_str());
			//printf("copy_status:%s\n", it->copy_status.c_str());
			//printf("report_date:%s\n", it->report_date.c_str());
			//printf("report_status:%s\n", it->report_status.c_str());
		}
	}
}

void TEST_QueryKeyInfo()
{
	CDataImpl db;
	if ( db.Open() )
	{
		STKeys key;
		db.QueryKeyInfo("1", key);
		printf("查询SNKey=>bid=%s, sn=%s,pid=%s,keys=%s, copy_date=%s, report_date=%s \n", key.bid.c_str(),
			key.sn.c_str(), key.pid.c_str(), key.keys.c_str(), key.copy_date.c_str(), key.report_date.c_str());
	}
}

void TEST_QueryLogInfo()
{
	CDataImpl db;
	if ( db.Open() )
	{
		STLog log;
		db.QueryLogInfo("1", log);
		printf("查询Log=>bid=%s, sn=%s,type=%s, content=%s, gdate=%s, report_date=%s \n", log.bid.c_str(),
			log.sn.c_str(), log.type.c_str(), log.content.c_str(), log.gdate.c_str(), log.report_date.c_str());

	}
}

void TEST_QueryUnReportLogInfo()
{
	CDataImpl db;
	if ( db.Open() )
	{
		std::vector<STLog> vtlog;
		db.QueryUnReportLogInfo(vtlog);
		std::vector<STLog>::iterator it = vtlog.begin();
		for (; it != vtlog.end(); it++)
		{
			printf("查询Log=>bid=%s, sn=%s,type=%s, content=%s, gdate=%s, report_date=%s \n", it->bid.c_str(),
				it->sn.c_str(), it->type.c_str(), it->content.c_str(), it->gdate.c_str(), it->report_date.c_str());
		}
	}
}


int _tmain(int argc, TCHAR* argv[], TCHAR* envp[])
{
	int nRetCode = 0;
	Global::Init();
	// 初始化 MFC 并在失败时显示错误
	if (!AfxWinInit(::GetModuleHandle(NULL), NULL, ::GetCommandLine(), 0))
	{
		// TODO: 更改错误代码以符合您的需要
		_tprintf(_T("错误: MFC 初始化失败\n"));
		nRetCode = 1;
	}
	else
	{
		std::string md5 = GetFileMD5("D:\\bin\\scbc.tools\\scbc.tools.pdb");
		// TODO: 在此处为应用程序的行为编写代码。
		//Sleep(10000);
		//TEST_Download();
		//TEST_GetMidInfo();
		
		TEST_InsertMidInfo();
		TEST_QueryMidInfo();
		TEST_InertKeys();
		TEST_InertKeys2();
		TEST_InertLog();
		TEST_QueryKeyInfo();		
		TEST_QueryUnReportKey();
		TEST_QueryLogInfo();
		TEST_QueryUnReportLogInfo();
	
	}

	system("pause");

	return nRetCode;
}

#else
// 全局数据库对象;
CDataImpl g_db;
CSDK g_sdk;
int QueryMidInfo(const char* lpOrder)
{
	if ( lpOrder == NULL || lpOrder[0] == '\0' )
		return -1;

	return g_sdk.QueryMidInfo(lpOrder);
}

int DownloadMidData(const char* lpOrder)
{
	if ( lpOrder == NULL || lpOrder[0] == '\0' )
		return -1;
	
	return g_sdk.DownloadMidData(lpOrder);
}

int QueryKeyInfo(const char* lpSN, SNKeys &snKeys)
{
	if ( lpSN == NULL || lpSN[0] == '\0' )
		return -1;

	STKeys keys;
	int nRet = g_sdk.QueryKeyInfo(lpSN, keys);
	if ( nRet == 1 )
	{
		snKeys.pid = atoi(keys.pid.c_str());
		nRet = CSDK::ParserSNKey(keys.keys, snKeys);
	}

	return nRet;
}

int UpdateKeyCopyStatus(const char* lpSN)
{
	if ( lpSN == NULL || lpSN[0] == '\0' )
		return -1;

	return g_sdk.UpdateKeyCopyStatus(lpSN);
}

int UpdateKeyReportStatus(const char* lpSN)
{
	if ( lpSN == NULL || lpSN[0] == '\0' )
		return -1;

	return g_sdk.UpdateKeyReportStatus(lpSN);
}

int ReportKeyCopyResults(const char* lpSN)
{
	if ( lpSN == NULL || lpSN[0] == '\0' )
		return -1;

	return 0;
}

int QueryBidInfo(const char* lpOrder, BidInfo& binfo)
{
	if ( lpOrder == NULL || lpOrder[0] == '\0' )
		return -1;

	CDataImpl db;
	if ( db.Open() )
	{
		int nRet = db.QueryBidInfo(lpOrder, binfo);
		if ( nRet == 0 )
			return 0;
		else if ( nRet == -1 )
			return -3;
		else if ( nRet == 1 )
			return 1;
		else if ( nRet > 1 )
			return -4;
	}

	return -2;
}

int RemoveBidData(const char* lpOrder)
{
	if ( lpOrder == NULL || lpOrder[0] == '\0' )
		return -1;

	CDataImpl db;
	if ( db.Open() )
	{
		if( db.RemoveBidData(lpOrder) == FALSE)
			return 0;
		
		return 1;
	}

	return -2;
}
#endif