#include "CivSimulResultCache.h"
#include"CivSysLog.h"
#include "CivSimulResDbHelper.h"

void CivSimulResultCache::addNodeQuality(float quality, int interval, const std::string& sNo)
{
	if (mNodeItemsMap.find(interval) == mNodeItemsMap.end())
		return;

	auto nodeMap = mNodeItemsMap.find(interval)->second;
	if (nodeMap.find(sNo) == nodeMap.end())
		return;

	mNodeItemsMap[interval][sNo].dQuality = quality;
}

void CivSimulResultCache::addLinkQuality(float quality, int interval,const std::string& sNo)
{
	if (mLinkItemsMap.find(interval) == mLinkItemsMap.end())
		return;

	auto linkMap = mLinkItemsMap.find(interval)->second;
	if (linkMap.find(sNo) == linkMap.end())
		return;

	mLinkItemsMap[interval][sNo].dQuality = quality;
}

void CivSimulResultCache::addNodeItems(int internal,NodeResultItem& nodeItem)
{
	if (mNodeCodeSnMap.find(nodeItem.szNo) != mNodeCodeSnMap.end())
		nodeItem.code = mNodeCodeSnMap.find(nodeItem.szNo)->second;
	mNodeItemsMap[internal][nodeItem.szNo] = nodeItem;
}

void CivSimulResultCache::addLinkItems(int interval,LinkResultItem& linkItem)
{
	if(mLineCodeSnMap.find(linkItem.szNo)!= mLineCodeSnMap.end())
		linkItem.code = mLineCodeSnMap.find(linkItem.szNo)->second;

	mLinkItemsMap[interval][linkItem.szNo] = linkItem;
}

void CivSimulResultCache::getPipesSnsByQuality(std::vector<std::vector<std::string>>& sns)
{
	for (auto iter = mLinkItemsMap.begin(); iter != mLinkItemsMap.end(); iter++)
	{
	
		std::vector<std::string> vec;
		std::map<std::string,LinkResultItem> subMap = iter->second;
		for (auto subIter = subMap.begin(); subIter != subMap.end(); subIter++)
		{
			LinkResultItem item = subIter->second;
			if (item.dQuality > 0)
				vec.push_back(item.szNo);
		}

		sns.push_back(vec);
	}
}

void CivSimulResultCache::clear()
{
	mLinkItemsMap.clear();
	mNodeItemsMap.clear();
}

bool CivSimulResultCache::save(const std::string& uri)
{
	CivSysLog::getInstance()->info("开始存入数据", "CivSimulResultCache", __FUNCTION__);
	CivSimulResDbHelper helper(uri);
	for (auto iter = mNodeItemsMap.begin(); iter != mNodeItemsMap.end(); iter++)
	{
		helper.insertNodes(iter->second);
	}

	for (auto iter = mLinkItemsMap.begin(); iter != mLinkItemsMap.end(); iter++)
	{
		helper.insertLinks(iter->second);
	}

	CivSysLog::getInstance()->info("数据存入完成", "CivSimulResultCache", __FUNCTION__);
	return true;
	
}

void CivSimulResultCache::getAllNodeCoords(const std::string& uri,NodeCoords& nodeCoords)
{
	CivSimulResDbHelper helper(uri);
	helper.getAllNodeCoords(nodeCoords);
}

void CivSimulResultCache::getAllPipeCoords(const std::string& uri,PipeCoords& pipeCoords)
{
	CivSimulResDbHelper helper(uri);
	helper.getAllPipeCoords(pipeCoords);
}

void CivSimulResultCache::getNodeItemByInterval(int time, NodeResultItems& nodeItems)
{
	auto iter = mNodeItemsMap.find(time);
	if (iter != mNodeItemsMap.end())
		nodeItems = iter->second;
}

void CivSimulResultCache::getPipeItemByInterval(int time, LinkResultItems& linkItems)
{
	auto iter = mLinkItemsMap.find(time);
	if (iter != mLinkItemsMap.end())
		linkItems = iter->second;
}

void CivSimulResultCache::getCodeToSnMap(const std::string& uri)
{
	CivSimulResDbHelper helper(uri);
	helper.getNodeCodeSnMap(mNodeCodeSnMap);
	helper.getLinCodeSnMap(mLineCodeSnMap);
}
