#include "CivProjSmulResultCache.h"
#include "CIvProjSimulResHelper.h"

CivProjSmulResultCache::CivProjSmulResultCache()
{

}

CivProjSmulResultCache::~CivProjSmulResultCache()
{

}

void CivProjSmulResultCache::addNodeItem(ProjNodeResultItem& nodeItem)
{
	std::map<std::string, std::string>::iterator iter = mNodeCodeSnMap.find(nodeItem.szNo);
	if (iter == mNodeCodeSnMap.end())
		return;

	std::string code = iter->second;
	nodeItem.code = code;
	mNodeCache.insert(std::pair<std::string, ProjNodeResultItem>(code,nodeItem));
}

void CivProjSmulResultCache::addPipeItem(PorjPipeResultItem& pipeItem)
{
	std::map<std::string, std::string>::iterator iter = mLineCodeSnMap.find(pipeItem.szNo);
	if (iter == mLineCodeSnMap.end())
		return;

	std::string code = iter->second;
	pipeItem.code = code;
	mPipeCache.insert(std::pair<std::string, PorjPipeResultItem>(code, pipeItem));
}

bool CivProjSmulResultCache::saveToUri(const std::string& dbUri,const std::string& time,const std::string& projCode)
{
	CIvProjSimulResHelper simulResHelper(dbUri);
	simulResHelper.deleteByTimeAndPorjcode(time, projCode);


	std::vector< ProjNodeResultItem> nodeVec;
	for (auto iter = mNodeCache.begin(); iter != mNodeCache.end(); iter++)
	{
		ProjNodeResultItem nodeItem = iter->second;
		nodeVec.push_back(nodeItem);
	}

	std::vector<PorjPipeResultItem> pipeVec;
	for (auto iter = mPipeCache.begin(); iter != mPipeCache.end(); iter++)
	{
		PorjPipeResultItem pipeItem = iter->second;
		pipeVec.push_back(pipeItem);
	}
	
	simulResHelper.saveNodeToDb(nodeVec);
	simulResHelper.savePipeToDb(pipeVec);

	return true;
}

void CivProjSmulResultCache::addNodeHead(const std::string& nodeCode, float head)
{
	std::map<std::string, std::string>::iterator iter = mNodeCodeSnMap.find(nodeCode);
	if (iter == mNodeCodeSnMap.end())
		return;

	std::string code = iter->second;

	mNodeCache[code].dHead = head;
}

void CivProjSmulResultCache::addNodePressure(const std::string& nodeCode, float pressure)
{
	std::map<std::string, std::string>::iterator iter = mNodeCodeSnMap.find(nodeCode);
	if (iter == mNodeCodeSnMap.end())
		return;

	std::string code = iter->second;
	mNodeCache[code].dPressure = pressure;
}

void CivProjSmulResultCache::addPipeFlow(const std::string& pipeSn, float flow)
{
	std::map<std::string, std::string>::iterator iter = mLineCodeSnMap.find(pipeSn);
	if (iter == mLineCodeSnMap.end())
		return;

	std::string code = iter->second;
	mPipeCache[code].dFlow = flow;
}

void CivProjSmulResultCache::addPipeVelcoity(const std::string& pipeSn, float velcocity)
{
	std::map<std::string, std::string>::iterator iter = mLineCodeSnMap.find(pipeSn);
	if (iter == mLineCodeSnMap.end())
		return;

	std::string code = iter->second;
	mPipeCache[code].dVelocity = velcocity;
}

void CivProjSmulResultCache::addPipeHeadLoss(const std::string& pipeSn, float headLoss)
{
	std::map<std::string, std::string>::iterator iter = mLineCodeSnMap.find(pipeSn);
	if (iter == mLineCodeSnMap.end())
		return;

	std::string code = iter->second;
	mPipeCache[code].dHeadLoss = headLoss;
}

void CivProjSmulResultCache::addProjNodeHead(const std::string& nodeCode, float head)
{
	std::map<std::string, std::string>::iterator iter = mNodeCodeSnMap.find(nodeCode);
	if (iter == mNodeCodeSnMap.end())
		return;

	std::string code = iter->second;
	mNodeCache[code].dProjHead = head;
}

void CivProjSmulResultCache::addProjNodePressure(const std::string& nodeCode, float pressure)
{
	std::map<std::string, std::string>::iterator iter = mNodeCodeSnMap.find(nodeCode);
	if (iter == mNodeCodeSnMap.end())
		return;

	std::string code = iter->second;
	mNodeCache[code].dProjPressure = pressure;
}

void CivProjSmulResultCache::addProjPipeFlow(const std::string& pipeSn, float flow)
{
	std::map<std::string, std::string>::iterator iter = mLineCodeSnMap.find(pipeSn);
	if (iter == mLineCodeSnMap.end())
		return;

	std::string code = iter->second;
	mPipeCache[code].dProjFlow = flow;
}

void CivProjSmulResultCache::addProjPipeVelcoity(const std::string& pipeSn, float velcocity)
{
	std::map<std::string, std::string>::iterator iter = mLineCodeSnMap.find(pipeSn);
	if (iter == mLineCodeSnMap.end())
		return;

	std::string code = iter->second;
	mPipeCache[code].dProjVelocity = velcocity;
}

void CivProjSmulResultCache::addProjPipeHeadLoss(const std::string& pipeSn, float headLoss)
{
	std::map<std::string, std::string>::iterator iter = mLineCodeSnMap.find(pipeSn);
	if (iter == mLineCodeSnMap.end())
		return;

	std::string code = iter->second;
	mPipeCache[code].dPorjHeadLoss = headLoss;
}

void CivProjSmulResultCache::getCodeToSnMap(const std::string& uri)
{
	CivSimulResHelperAbs* helper = new CIvProjSimulResHelper(uri);
	helper->getNodeCodeSnMap(mNodeCodeSnMap);
	helper->getLinCodeSnMap(mLineCodeSnMap);
	delete helper;
}