#include "CivProjInpDbHelper.h"
#include "CivConnection.h"
#include "CivTableFields.h"

CivProjInpDbHelper::CivProjInpDbHelper(const std::string& uri)
	:CivInpHelperAbs(uri)
{
	
}

CivProjInpDbHelper::~CivProjInpDbHelper()
{

}

void CivProjInpDbHelper::setProjCode(const std::string& projCode)
{
	mPorjCode = projCode;
	std::string mCondition = PROJFILED + " = " + projCode + "or "+PROJFILED +" is null or "+ PROJFILED +"=''";
}

bool CivProjInpDbHelper::getNode(CivNode& node)
{
	JunctionTable nodeTableFields;

	std::vector<std::string> fields;
	fields.push_back(nodeTableFields.sn);
	fields.push_back(nodeTableFields.elev);
	fields.push_back(nodeTableFields.baseDemand);
	fields.push_back(nodeTableFields.demandPattern);

	std::vector<std::map<std::string, std::string>> resultVector;
	mDbConn->query(PIPENODE, fields, resultVector,mCondition);

	// ±à¼­°ü×°½á¹û
	size_t total = resultVector.size();
	for (int i = 0; i < total; i++)
	{
		std::map<std::string, std::string> map = resultVector[i];
		CivNode::NodeTable nodeTable;

		nodeTable.ID = map.find(nodeTableFields.sn)->second;
		nodeTable.Elev = map.find(nodeTableFields.elev)->second;
		nodeTable.Demand = map.find(nodeTableFields.baseDemand)->second;
		nodeTable.PatternId = map.find(nodeTableFields.demandPattern)->second;

		node.addItem(nodeTable);
	}
	return true;
}

bool CivProjInpDbHelper::getPipe(CivPipe& pipes)
{
	PipeTable pipeTable;

	std::vector<std::string> fields;
	
	fields.push_back(pipeTable.snNo);
	fields.push_back(pipeTable.startPoint);
	fields.push_back(pipeTable.endPoint);
	fields.push_back(pipeTable.length);
	fields.push_back(pipeTable.diameter);
	fields.push_back(pipeTable.friction);
	fields.push_back(pipeTable.localLoss);
	fields.push_back(pipeTable.status);
	

	std::vector<std::map<std::string, std::string>> resultVector;
	mDbConn->query(PIPELINE, fields, resultVector);

	size_t total = resultVector.size();
	for (int i = 0; i < total; i++)
	{
		CivPipe::PipesTable pipe;

		std::map<std::string, std::string> map = resultVector[i];

		pipe.ID = map.find(pipeTable.snNo)->second;
		pipe.Node1 = map.find(pipeTable.startPoint)->second;
		pipe.Node2 = map.find(pipeTable.endPoint)->second;
		pipe.Length = map.find(pipeTable.length)->second;
		pipe.Diameter = map.find(pipeTable.diameter)->second;
		pipe.Roughness = map.find(pipeTable.friction)->second;
		pipe.MinorLoss = map.find(pipeTable.localLoss)->second;
		pipe.Status = map.find(pipeTable.status)->second;

		pipes.addItem(pipe);
	}
	return true;
}

bool CivProjInpDbHelper::getTank(CivTank& tanks)
{
	TankTable tankTable;

	std::vector<std::string> fields;

	fields.push_back(tankTable.sn);
	fields.push_back(tankTable.elev);
	fields.push_back(tankTable.initLevel);
	fields.push_back(tankTable.minLevel);
	fields.push_back(tankTable.maxLevel);
	fields.push_back(tankTable.diametor);
	fields.push_back(tankTable.minVolume);
	fields.push_back(tankTable.volumeCurve);


	std::vector<std::map<std::string, std::string>> resultVector;
	mDbConn->query(TANK, fields, resultVector);

	size_t total = resultVector.size();
	for (int i = 0; i < total; i++)
	{
		CivTank::TankTable tank;
		std::map<std::string, std::string>map = resultVector[i];

		tank.ID = map.find(tankTable.sn)->second;
		tank.Elev = map.find(tankTable.elev)->second;
		tank.InitLevel = map.find(tankTable.initLevel)->second;
		tank.MinLevel = map.find(tankTable.minLevel)->second;
		tank.MaxLevel = map.find(tankTable.maxLevel)->second;
		tank.Diameter = map.find(tankTable.diametor)->second;
		tank.MinVol = map.find(tankTable.minVolume)->second;
		tank.VolCurve = map.find(tankTable.volumeCurve)->second;

		tanks.addItem(tank);
	}
	return true;
}

bool CivProjInpDbHelper::getValve(CivValve& valves)
{
	ValveTable vaveTable;
	std::vector<std::string> fields;

	fields.push_back(vaveTable.sn);
	fields.push_back(vaveTable.startPoint);
	fields.push_back(vaveTable.endPoint);
	fields.push_back(vaveTable.length);
	fields.push_back(vaveTable.type);
	fields.push_back(vaveTable.setting);
	fields.push_back(vaveTable.lossCoeff);

	std::vector<std::map<std::string, std::string>> resultVector;
	mDbConn->query(VALVE, fields, resultVector);

	size_t totals = resultVector.size();
	for (int i = 0; i < totals; i++)
	{
		CivValve::ValveTable vave;
		std::map<std::string, std::string> map = resultVector[i];

		vave.ID = map.find(vaveTable.sn)->second;
		vave.Node1 = map.find(vaveTable.startPoint)->second;
		vave.Node2 = map.find(vaveTable.endPoint)->second;
		vave.Diameter = map.find(vaveTable.length)->second;
		vave.Type = map.find(vaveTable.type)->second;
		vave.Setting = map.find(vaveTable.setting)->second;
		vave.MinorLoss = map.find(vaveTable.lossCoeff)->second;

		valves.addItem(vave);
	}
	return true;
}

bool CivProjInpDbHelper::getPumps(CivPumps& pumps)
{
	PumpTable pmTable;

	std::vector<std::string> fields;	
	fields.push_back(pmTable.sn);
	fields.push_back(pmTable.startPoint);
	fields.push_back(pmTable.endPoint);
	fields.push_back(pmTable.headCurve);
	fields.push_back(pmTable.power);
	

	std::vector<std::map<std::string, std::string>> resultVector;
	mDbConn->query(PUMP, fields, resultVector);

	size_t totals = resultVector.size();
	for (int i = 0; i < totals; i++)
	{
		CivPumps::PumpTable pump;
		std::map<std::string, std::string> map = resultVector[i];

		pump.ID = map.find(pmTable.sn)->second;
		pump.Node1 = map.find(pmTable.startPoint)->second;
		pump.Node2 = map.find(pmTable.endPoint)->second;
		std::string param = "Head 1";

		std::string curve = map.find(pmTable.headCurve)->second;
		if (curve != "")
			param = "head " + curve;

		std::string power = map.find(pmTable.power)->second;
		if (power != "")
			param.append(" power " + power);

		pump.Parameters = param;
		pumps.addItem(pump);
	}
	return true;
}

bool CivProjInpDbHelper::getReservoirs(CivReservoirs& reservoirs)
{
	ResourcesTable restemTable;

	std::vector<std::string> fields;
	fields.push_back(restemTable.sn);
	fields.push_back(restemTable.headPattern);

	std::vector<std::map<std::string, std::string>> resultVector;
	mDbConn->query(RESIVOIR, fields, resultVector);

	size_t totals = resultVector.size();
	for (int i = 0; i < totals; i++)
	{
		CivReservoirs::ReservoirsTable resTable;
		std::map<std::string, std::string> map = resultVector[i];

		resTable.ID = map.find(restemTable.sn)->second;
		resTable.Head = map.find(restemTable.totalHead)->second;
		resTable.Pattern = map.find(restemTable.headPattern)->second;

		reservoirs.addItem(resTable);
	}
	return true;
}

bool CivProjInpDbHelper::getCoordinates(CivCoordinates& coord)
{
	JunctionTable nodeTable;

	std::vector<std::string> fields;

	fields.push_back(nodeTable.sn);
	fields.push_back(nodeTable.xCoord);
	fields.push_back(nodeTable.yCoord);

	std::vector<std::map<std::string, std::string>> resultVector;
	mDbConn->query(PIPENODE, fields, resultVector);
	mDbConn->query(TANK, fields, resultVector);
	mDbConn->query(RESIVOIR, fields, resultVector);

	size_t totals = resultVector.size();
	for (int i = 0; i < totals; i++)
	{
		CivCoordinates::CoordTable coordTable;
		std::map<std::string, std::string> map = resultVector[i];

		coordTable.ID = map.find(fields[0])->second;
		coordTable.XCoord = map.find(fields[1])->second;
		coordTable.YCoord = map.find(fields[2])->second;

		coord.addItem(coordTable);
	}

	return true;
}

bool CivProjInpDbHelper::getQuality(CivQuality& quality)
{
	JunctionTable nodeTable;

	std::vector<std::string> fields; 

	fields.push_back(nodeTable.sn);
	fields.push_back(nodeTable.initQuality);
	

	std::vector<std::map<std::string, std::string>> resultVector;
	mDbConn->query(PIPENODE, fields, resultVector);

	
	size_t totals = resultVector.size();
	for (int i = 0; i < totals; i++)
	{
		CivQuality::QualityTable quliTable;

		std::map<std::string, std::string> map = resultVector[i];
		quliTable.ID = map.find(fields[0])->second;
		quliTable.InitQuality = map.find(fields[1])->second;;

		quality.addItem(quliTable);
	}

	return true;
}

