#include "CivSimulResHelperAbs.h"
#include "CivConnection.h"
#include "CivTypes.h"

CivSimulResHelperAbs::CivSimulResHelperAbs()
{

}

CivSimulResHelperAbs::~CivSimulResHelperAbs()
{
}

bool CivSimulResHelperAbs::insertNodes(const NodeResultItems& nodeMap)
{
	return true;
}

void CivSimulResHelperAbs::saveNodeToDb(const std::vector<ProjNodeResultItem>& nodeItems)
{

}

void CivSimulResHelperAbs::savePipeToDb(const std::vector<PorjPipeResultItem>& pipeItems)
{

}

bool CivSimulResHelperAbs::insertLinks(const LinkResultItems& linkMap)
{
	return true;
}

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

bool CivSimulResHelperAbs::insertLinksBulk(const std::map<int, LinkResultItems>& linksMap)
{
	return true;
}

bool CivSimulResHelperAbs::insertNodeBulk(const std::map<int, TankResultItems>& nodesMap)
{
	return true;
}

void CivSimulResHelperAbs::getAllNodeCoords(NodeCoords& nodeCoords)
{
	if (!mConn)
		return;

	std::vector<std::string> fileds = {
		"本点号","横坐标","纵坐标"
	};

	std::vector<std::map<std::string, std::string>> res;
	if (!mConn->query(PIPENODE, fileds, res))
		return;

	size_t total = res.size();
	for (int i = 0; i < total; i++)
	{
		std::map<std::string, std::string> nodeInfo = res[i];
		NodeCoord nodeCoord;

		std::string key = nodeInfo.find(fileds[0])->second;
		nodeCoord.xCoord = nodeInfo.find(fileds[1])->second;
		nodeCoord.yCoord = nodeInfo.find(fileds[2])->second;

		nodeCoords[key] = nodeCoord;
	}

}

void CivSimulResHelperAbs::getAllPipeCoords(PipeCoords& pipeCoords)
{
	if (!mConn)
		return;

	std::vector<std::string> fileds = {
		"ST_X(ST_StartPoint(ST_GeometryN(geom,1))) as startx",
		"ST_Y(ST_StartPoint(ST_GeometryN(geom,1))) as starty",
		"ST_X(ST_EndPoint(ST_GeometryN(geom,1))) as endX" ,
		"ST_Y(ST_EndPoint(ST_GeometryN(geom,1))) as endY",
		"编号"
	};

	std::vector<std::map<std::string, std::string>> res;
	if (!mConn->query(PIPELINE, fileds, res))
		return;

	size_t total = res.size();
	for (int i = 0; i < total; i++)
	{
		std::map<std::string, std::string> nodeInfo = res[i];
		PipeCoord pipeCoord;

		std::string key = nodeInfo.find("编号")->second;
		pipeCoord.startX = nodeInfo.find("startx")->second;
		pipeCoord.startY = nodeInfo.find("starty")->second;
		pipeCoord.endX = nodeInfo.find("endx")->second;
		pipeCoord.endY = nodeInfo.find("endy")->second;
		pipeCoords[key] = pipeCoord;
	}
}

void CivSimulResHelperAbs::getLinCodeSnMap(std::map<std::string, std::string>& map)
{
	if (!mConn)
		return;

	std::vector<std::string> fileds = {
		"\"编号\"",
		"\"code\""
	};

	std::vector<std::map<std::string, std::string>> res;
	mConn->query(PIPELINE, fileds, res);


	size_t total = res.size();
	for (int i = 0; i < total; i++)
	{
		std::map<std::string, std::string> nodeInfo = res[i];
		PipeCoord pipeCoord;

		std::string sn = nodeInfo.find("编号")->second;
		std::string code = nodeInfo.find("code")->second;
		map[sn] = code;
	}


}

void CivSimulResHelperAbs::getNodeCodeSnMap(std::map<std::string, std::string>& map)
{
	if (!mConn)
		return;

	std::vector<std::string> fileds = {
	"\"本点号\"",
	"\"code\""
	};

	std::vector<std::map<std::string, std::string>> res;
	mConn->query(PIPENODE, fileds, res);

	size_t total = res.size();
	for (int i = 0; i < total; i++)
	{
		std::map<std::string, std::string> nodeInfo = res[i];
		PipeCoord pipeCoord;

		std::string sn = nodeInfo.find("本点号")->second;
		std::string code = nodeInfo.find("code")->second;
		map[sn] = code;
	}
}


