#include "CivSimulResDbHelper.h"
#include "CivPgDbConnection.h"
#include "CivTypes.h"
#include "CivTableFields.h"
#include "CivCommonUtils.h"

CivSimulResDbHelper::CivSimulResDbHelper(const std::string& uri)
{
	mConn = new CivPgDbConnection();
	mConn->connect(uri);

	NodeResultTableField tableFields;
	mNodeFields =
	{
		"code",
		//tableFields.Number,	
		tableFields.dDemand,	
		tableFields.dPressure,
		tableFields.dQuality,
		tableFields.dHead,
		tableFields.dDate,
		tableFields.dInterval,
		tableFields.dModifyTime,
		tableFields.chlorine
	};

	// 
	PipeResultTableField pipeFields;
	mLinkFileds = {
		"code",
		//pipeFields.szNo,
		pipeFields.dFlow,
		pipeFields.dVelocity,
		pipeFields.dUnitHeadloss,
		pipeFields.dQuality,
		pipeFields.szStatus,
		pipeFields.dDate,
		pipeFields.dInterval,
		pipeFields.dModifyTime,
		pipeFields.chlorine
	};
}

CivSimulResDbHelper::~CivSimulResDbHelper()
{
	mConn->disconnect();
	delete mConn;
}

bool CivSimulResDbHelper::insertLinksBulk(const std::map<int, LinkResultItems>& linksMap)
{
	// 拼接sql语句
	// 拼接要插入的字段
	std::string sql = "INSERT INTO  \"" + PIPERESULTTABLE + "\"(";
	size_t total = mLinkFileds.size();
	for (int i = 0; i < total; i++)
	{
		std::string  field = mLinkFileds[i];
		sql.append(field);
		sql.append(",");
	}
	sql = sql.substr(0, sql.length() - 1);
	sql.append(") values");

	for (auto iter = linksMap.begin(); iter != linksMap.end(); iter++)
	{
		LinkResultItems linkItems = iter->second;
		insertLinks(linkItems);
		/*std::string condition;
		for (auto iter = linkItems.begin(); iter != linkItems.end(); iter++)
		{
			LinkResultItem linkItem = iter->second;
			std::string sn = linkItem.code;
			std::string status = linkItem.szStatus;
		
			sql.append("(");
			sql.append("\'" + sn + "\'");
			sql.append(",");
			sql.append(std::to_string(linkItem.dFlow));
			sql.append(",");
			sql.append(std::to_string(linkItem.dVelocity));
			sql.append(",");
			sql.append(std::to_string(linkItem.dHeadloss));
			sql.append(",");
			sql.append(std::to_string(linkItem.dQuality));
			sql.append(",");
			sql.append("\'" + status + "\'");
			sql.append(",");
			sql.append("\'" + linkItem.dDate + "\'");
			sql.append(",");
			sql.append("\'" + linkItem.dTime + "\'");
			sql.append(",");
			sql.append("\'" + CurrentTime + "\'");
			sql.append(",");
			sql.append(std::to_string(linkItem.dChlorine));
			sql.append("),");currentTime

			condition = "\'" + linkItem.dTime + "\'";
		}


		if (!mConn->del(PIPERESULTTABLE, " 时间点 = " + condition))
			return false;*/
	}

	/*sql = sql.substr(0, sql.length() - 1);
	if (!mConn->execSql(sql))
		return false;*/

	return true;
}

bool CivSimulResDbHelper::insertNodeBulk(const std::map<int, NodeResultItems>& nodesMap)
{

	// 拼接sql语句
	// 拼接要插入的字段
	std::string sql = "INSERT INTO  \"" + NODERESULTTABLE + "\"(";
	size_t total = mNodeFields.size();
	for (int i = 0; i < total; i++)
	{
		std::string  field = mNodeFields[i];
		sql.append(field);
		sql.append(",");
	}
	sql = sql.substr(0, sql.length() - 1);
	sql.append(") values");

	// 拼接要插入的结果值
	for (auto iter = nodesMap.begin(); iter != nodesMap.end(); iter++)
	{
		std::string condition;
		NodeResultItems nodeitems = iter->second;
		insertNodes(nodeitems);
		/*for (auto iter = nodeitems.begin(); iter != nodeitems.end(); iter++)
		{
			NodeResultItem nodeItem = iter->second;

			std::string sn = nodeItem.code;

			sql.append("(");
			sql.append("\'" + sn + "\'");
			sql.append(",");
			sql.append(std::to_string(nodeItem.dDemand));
			sql.append(",");
			sql.append(std::to_string(nodeItem.dPressure));
			sql.append(",");
			sql.append(std::to_string(nodeItem.dQuality));
			sql.append(",");
			sql.append(std::to_string(nodeItem.dHead));
			sql.append(",");
			sql.append("\'" + nodeItem.dDate + "\'");
			sql.append(",");
			sql.append("\'" + nodeItem.dTime + "\'");
			sql.append(",");
			sql.append("\'" + CurrentTime + "\'");
			sql.append(",");
			sql.append(std::to_string(nodeItem.dChlorine));
			sql.append("),");

			condition = "\'" + nodeItem.dTime + "\'";
		}
		if (!mConn->del(NODERESULTTABLE, " 时间点 = " + condition))
			return false;*/
	}


	/*sql = sql.substr(0, sql.length() - 1);
	if (!mConn->execSql(sql))
		return false;*/

	return true;
}

bool CivSimulResDbHelper::insertNodes(const NodeResultItems& nodeitems)
{
	std::vector<std::vector<std::string>> vecVec;
	std::string condition;
	for (auto iter = nodeitems.begin(); iter != nodeitems.end(); iter++)
	{
		NodeResultItem nodeItem = iter->second;
		
		std::string sn = nodeItem.code;
		std::vector<std::string> vecRes;
		vecRes.push_back("\'" + sn + "\'");
		vecRes.push_back(std::to_string(nodeItem.dDemand));
		vecRes.push_back(std::to_string(nodeItem.dPressure));
		vecRes.push_back(std::to_string(nodeItem.dQuality));
		vecRes.push_back(std::to_string(nodeItem.dHead));
		vecRes.push_back("\'" + nodeItem.dDate + "\'");
		vecRes.push_back("\'" + nodeItem.dTime + "\'");
		vecRes.push_back("\'" + CurrentTime + "\'");
		vecRes.push_back(std::to_string(nodeItem.dChlorine));
		vecVec.push_back(vecRes);
		condition = "\'" + nodeItem.dTime + "\'";
	}

	if (!mConn->del(NODERESULTTABLE, " 时间点 = "+condition))
		return false;

	if (!mConn->insertBulk(NODERESULTTABLE, mNodeFields, vecVec))
		return false;

	return true;
}

bool CivSimulResDbHelper::insertLinks(const LinkResultItems& linkMap)
{
	std::vector<std::vector<std::string>> vecVec;
	std::string condition;
	for (auto iter = linkMap.begin(); iter != linkMap.end(); iter++)
	{
		LinkResultItem linkItem = iter->second;
		std::string sn = linkItem.code;
		std::string status = linkItem.szStatus;
		std::vector<std::string> vecRes;
	
		vecRes.push_back("\'" + sn + "\'");
		vecRes.push_back(std::to_string(linkItem.dFlow));
		vecRes.push_back(std::to_string(linkItem.dVelocity));
		vecRes.push_back(std::to_string(linkItem.dHeadloss));
		vecRes.push_back(std::to_string(linkItem.dQuality));
		vecRes.push_back("\'" + status + "\'");
		vecRes.push_back("\'" + linkItem.dDate + "\'");
		vecRes.push_back("\'" + linkItem.dTime + "\'");
		vecRes.push_back("\'" + CurrentTime + "\'");
		vecRes.push_back(std::to_string(linkItem.dChlorine));
	
		vecVec.push_back(vecRes);
		condition = "\'" + linkItem.dTime + "\'";
	}


	if (!mConn->del(PIPERESULTTABLE, " 时间点 = " + condition))
		return false;

	if (!mConn->insertBulk(PIPERESULTTABLE, mLinkFileds, vecVec))
		return false;

	return true;
}

bool CivSimulResDbHelper::insertTanks(const TankResultItems& tankMap)
{
	return true;
}