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

void CivSimulResultCache::addNodeQuality(float quality, int interval, Str 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, Str 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(const NodeResultItem& nodeItem)
{
	mNodeItemsMap[nodeItem.dInterval][nodeItem.szNo] = nodeItem;
}

void CivSimulResultCache::addLinkItems(const LinkResultItem& linkItem)
{
	mLinkItemsMap[linkItem.dInterval][linkItem.szNo] = linkItem;
}

bool CivSimulResultCache::updateToDb(CivDbConn* dbConn)
{
	CivSysLog::getInstance()->error("开始写入数据库", "CivSimulResultCache", __FUNCTION__);
	if (dbConn == nullptr)
	{
		CivSysLog::getInstance()->error("dbConn == nullptr", "CivSimulResultCache", __FUNCTION__);
		return false;
	}

	for (auto iter = mNodeItemsMap.begin(); iter != mNodeItemsMap.end(); iter++)
	{
		NodeResultItems nodeItems = iter->second;

		if (!dbConn->updateNode(nodeItems))
		{
			CivSysLog::getInstance()->error("存储节点数据失败", "CivSimulResultCache", __FUNCTION__);
		}
	}

	for (auto iter = mLinkItemsMap.begin(); iter != mLinkItemsMap.end(); iter++)
	{
		LinkResultItems nodeItems = iter->second;
		if (!dbConn->updateLink(nodeItems))
		{
			CivSysLog::getInstance()->error("存储管段数据失败", "CivSimulResultCache", __FUNCTION__);
		}
	}
	CivSysLog::getInstance()->error("写入数据库完成", "CivSimulResultCache", __FUNCTION__);
	return true;
}

bool CivSimulResultCache::updateToDb(CivDbConnection* dbConn)
{
	CivSysLog::getInstance()->error("开始写入数据库", "CivSimulResultCache", __FUNCTION__);
	if (dbConn == nullptr)
	{
		CivSysLog::getInstance()->error("dbConn == nullptr", "CivSimulResultCache", __FUNCTION__);
		return false;
	}
		
	for (auto iter = mNodeItemsMap.begin(); iter != mNodeItemsMap.end(); iter++)
	{
		NodeResultItems nodeItems = iter->second;

		if (!dbConn->updateNode(nodeItems))
		{
			CivSysLog::getInstance()->error("存储节点数据失败", "CivSimulResultCache", __FUNCTION__);
		}
	}

	for (auto iter = mLinkItemsMap.begin(); iter != mLinkItemsMap.end(); iter++)
	{
		LinkResultItems nodeItems = iter->second;
		if (!dbConn->updateLink(nodeItems))
		{
			CivSysLog::getInstance()->error("存储管段数据失败", "CivSimulResultCache", __FUNCTION__);
		}
	}
	CivSysLog::getInstance()->error("写入数据库完成", "CivSimulResultCache", __FUNCTION__);
	clear();
	return true;
}

void CivSimulResultCache::getResultByTime(int time, NodeResultItems& nodeItems, LinkResultItems& linkItems)
{
	nodeItems.clear();
	linkItems.clear();

	auto nodeIter = mNodeItemsMap.find(time);
	auto linkIter = mLinkItemsMap.find(time);
	if (nodeIter == mNodeItemsMap.end() || linkIter == mLinkItemsMap.end())
	{
		CivSysLog::getInstance()->error("存储数据出错了，找不到该对应时段的值", "CivSimulResultCache", __FUNCTION__);
		return;
	}
	
	// 避免发生异常
	try
	{
		nodeItems = nodeIter->second;
		linkItems = linkIter->second;
	}
	catch (const std::exception& e)
	{
		CivSysLog::getInstance()->error(e.what(), "CivSimulResultCache", __FUNCTION__);
	}
}

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

