#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;
}

void CivProjInpDbHelper::handlePump()
{
	// 处理水泵连接的管段

}

void CivProjInpDbHelper::sortProjDeatil()
{
	// 查找方案详情表修改的属性
	std::string detailSql = "select detail.\"code\",detail.\"图层\",detail.\"属性\",detail.\"属性原值\",detail.\"属性新值\"\
		 from \"方案详情表\" as detail where \"方案编码\"='" + mPorjCode +"' and \"操作类型\"=1";

	if (!mDbConn->execSql(detailSql))
		return;

	std::vector<std::map<std::string, std::string>> detailVec;
	mDbConn->queryResult(detailVec);

	size_t detailVecSize = detailVec.size();

	// 将方案详情表查出来的数据
	for (int i = 0; i < detailVecSize; i++)
	{
		std::map<std::string, std::string> tempMap = detailVec[i];
		std::string layerName = tempMap.find("图层")->second;

		mDetailMap[layerName].push_back(tempMap);
	}
}

std::string CivProjInpDbHelper::splicingConditions(const std::string& layer)
{
	std::string condition = "\"code\" not in(select  \"code\" from \"方案详情表\" \
				where \"方案编码\" = '" + mPorjCode + "' and \"图层\" = '"+ layer +"' and \"操作类型\"=0)";

	return condition;
}

void CivProjInpDbHelper::handleValve()
{
	// 查出方案详情表的信息
	sortProjDeatil();

	// 抽取相同字符串部分
	std::string baseSql = "select pipe.\"编号\", ('UP' || pipe.\"起始节点\") as \"起始节点\", pipe.\"终止节点\", \
			pipe.\"管长\", pipe.\"管径\",\
			pipe.\"摩阻系数\" ,pipe.\"局损系数\"";

	//阀门的下游管段
	std::string dwPipeSql = baseSql + "from \"管段\" as pipe where "+splicingConditions("阀门")+" and (pipe.\"from_layer\" ='阀门' or pipe.\"from_layer\" ='方案阀门' ) \
			union "+baseSql+" from \"方案管段\" as pipe where pipe.\"方案编码\" ='"+mPorjCode+"' and (pipe.\"from_layer\" ='阀门' or pipe.\"from_layer\" ='方案阀门' ) ";

	if (!mDbConn->execSql(dwPipeSql))
		return;

	std::vector<std::map<std::string, std::string>> upPipes;
	mDbConn->queryResult(upPipes);

	size_t total = upPipes.size();
	for (int i = 0; i < total; i++)
	{
		std::map<std::string, std::string> pipe = upPipes[i];

		CivPipe::Table pipeStruct;
		pipeStruct.ID = pipe.find("编号")->second;
		pipeStruct.code = pipe.find("code")->second;
		pipeStruct.Node1 = pipe.find("起始节点")->second;
		pipeStruct.Node2 = pipe.find("终止节点")->second;
		pipeStruct.Length = pipe.find("管长")->second;
		pipeStruct.Diameter = pipe.find("管径")->second;
		pipeStruct.Roughness = pipe.find("摩阻系数")->second;
		pipeStruct.Status = "Open";
		pipeStruct.MinorLoss = pipe.find("局损系数")->second;

		mPipesVec.push_back(pipeStruct);
	}

	// 阀门的上游管段
	std::string baseSql2 = "select pipe.\"编号\", pipe.\"起始节点\", ('DW' || pipe.\"终止节点\") as \"终止节点\", \
			pipe.\"管长\", pipe.\"管径\", \pipe.\"摩阻系数\",pipe.\"局损系数\"";

	std::string upPipeSql = baseSql2 + " from \"管段\" as pipe where  " +splicingConditions("阀门")+" and  (pipe.\"to_layer\" = '阀门'  or pipe.\"to_layer\" = '方案阀门') \
			union " +baseSql2 + " from \"方案管段\" as pipe where   pipe.\"方案编码\" ='" + mPorjCode + "' and (pipe.\"to_layer\" = '阀门'  or pipe.\"to_layer\" = '方案阀门') " ;

	if (!mDbConn->execSql(upPipeSql))
		return;

	std::vector<std::map<std::string, std::string>> downPipes;
	mDbConn->queryResult(downPipes);

	size_t dtotal = downPipes.size();
	for (int i = 0; i < dtotal; i++)
	{
		std::map<std::string, std::string> pipe = downPipes[i];

		CivPipe::Table pipeStruct;
		pipeStruct.ID = pipe.find("编号")->second;
		pipeStruct.code = pipe.find("code")->second;
		pipeStruct.Node1 = pipe.find("起始节点")->second;
		pipeStruct.Node2 = pipe.find("终止节点")->second;
		pipeStruct.Length = pipe.find("管长")->second;
		pipeStruct.Diameter = pipe.find("管径")->second;
		pipeStruct.Roughness = pipe.find("摩阻系数")->second;
		pipeStruct.Status = "Open";
		pipeStruct.MinorLoss = pipe.find("局损系数")->second;

		mPipesVec.push_back(pipeStruct);
	}

	//  阀门信息 
	std::string startSql = "SELECT  va.\"code\",'UP' || va.\"本点号\" AS \"本点号\", vnode.\"高程\", vnode.\"基本需水量\", vnode.\"需水量模式\" \
		, (va.\"横坐标\" + 1) AS \"横坐标\", (va.\"纵坐标\" + 1) AS \"纵坐标\", va.\"本点号\" as \"阀门本点号\", \
		va.\"类型\" as \"阀门类型\", ('UP' || va.\"本点号\") as \"阀门起始节点\",\
		va.\"直径\" as \"阀门直径\", va.\"固定状态\" as \"阀门状态\", va.\"设置\" as \"阀门设置\", va.\"水损系数\"\
		FROM \"阀门\" va, ( \
			SELECT node.\"本点号\", node.\"高程\", node.\"基本需水量\", node.\"需水量模式\", node.\"横坐标\", node.\"纵坐标\", va.\"终止节点\" \
			FROM \"节点\" node, ( \
				SELECT  pipe.\"起始节点\", pipe.\"终止节点\" FROM \"管段\" pipe \
				WHERE pipe.\"to_layer\" = '阀门' \
				and \"code\" not in( \
					select \"code\" from \"方案详情表\" \
					where \"图层\" = '管段' and \"操作类型\" = 0 and \"方案编码\" = '" + mPorjCode + "' \
				) \
				union \
				select pipe.\"起始节点\", pipe.\"终止节点\" FROM \"方案管段\" pipe where pipe.\"to_layer\" = '阀门' and \"方案编码\" = '" + mPorjCode + "' \
				) va \
			WHERE node.\"本点号\" = va.\"起始节点\" and node.\"code\" not in(select \"code\" from \"方案详情表\" \
				where \"图层\" = '节点' and \"操作类型\" = 0 and \"方案编码\" = '" + mPorjCode + "') \
			union \
			SELECT node.\"本点号\", node.\"高程\", node.\"基本需水量\", node.\"需水量模式\", node.\"横坐标\", node.\"纵坐标\", va.\"终止节点\" \
			FROM \"方案节点\" node, ( \
				SELECT  pipe.\"起始节点\", pipe.\"终止节点\" FROM \"管段\" pipe \
				WHERE pipe.\"to_layer\" = '阀门' \
				and \"code\" not in( \
					select \"code\" from \"方案详情表\" \
					where \"图层\" = '管段' and \"操作类型\" = 0 and \"方案编码\" = '" + mPorjCode + "' \
				) \
				union \
				select pipe.\"起始节点\", pipe.\"终止节点\" FROM \"方案管段\" pipe where pipe.\"to_layer\" = '阀门' and \"方案编码\" = '" + mPorjCode + "' \
				) va \
			WHERE node.\"本点号\" = va.\"起始节点\" and node.\"方案编码\" = '" + mPorjCode + "' \
			) vnode \
		WHERE vnode.\"终止节点\" = va.\"本点号\" and va.\"code\" not in(select \"code\" from \"方案详情表\" \
			where \"图层\" = '阀门' and \"操作类型\" = 0 and \"方案编码\" = '" + mPorjCode + "') \
		union \
		SELECT  va.\"code\", 'UP' || va.\"本点号\" AS \"本点号\", vnode.\"高程\", vnode.\"基本需水量\", vnode.\"需水量模式\" \
		, (va.\"横坐标\" +1) AS \"横坐标\", (va.\"纵坐标\" + 1) AS \"纵坐标\", va.\"本点号\" as \"阀门本点号\", \
		va.\"类型\" as \"阀门类型\", ('UP' || va.\"本点号\") as \"阀门起始节点\", \
		va.\"直径\" as \"阀门直径\", va.\"固定状态\" as \"阀门状态\", va.\"设置\" as \"阀门设置\", va.\"水损系数\" \
		FROM \"方案阀门\" va, ( \
			SELECT node.\"本点号\", node.\"高程\", node.\"基本需水量\", node.\"需水量模式\", node.\"横坐标\", node.\"纵坐标\", va.\"终止节点\" \
			FROM \"节点\" node, ( \
				SELECT  pipe.\"起始节点\", pipe.\"终止节点\" FROM \"管段\" pipe \
				WHERE pipe.\"to_layer\" = '阀门' \
				and \"code\" not in( \
					select \"code\" from \"方案详情表\" \
					where \"图层\" = '管段' and \"操作类型\" = 0 and \"方案编码\" = '" + mPorjCode + "' \
				) \
				union \
				select pipe.\"起始节点\", pipe.\"终止节点\" FROM \"方案管段\" pipe where pipe.\"to_layer\" = '阀门' and \"方案编码\" = '" + mPorjCode + "' \
				) va \
			WHERE node.\"本点号\" = va.\"起始节点\" and node.\"code\" not in(select \"code\" from \"方案详情表\" \
				where \"图层\" = '节点' and \"操作类型\" = 0 and \"方案编码\" = '" + mPorjCode + "') \
			union \
			SELECT node.\"本点号\", node.\"高程\", node.\"基本需水量\", node.\"需水量模式\", node.\"横坐标\", node.\"纵坐标\", va.\"终止节点\" \
			FROM \"方案节点\" node, ( \
				SELECT  pipe.\"起始节点\", pipe.\"终止节点\" FROM \"管段\" pipe \
				WHERE pipe.\"to_layer\" = '阀门' \
				and \"code\" not in( \
					select \"code\" from \"方案详情表\" \
					where \"图层\" = '管段' and \"操作类型\" = 0 and \"方案编码\" = '" + mPorjCode + "' \
				) \
				union \
				select pipe.\"起始节点\", pipe.\"终止节点\" FROM \"方案管段\" pipe where pipe.\"to_layer\" = '阀门' and \"方案编码\" = '" + mPorjCode + "' \
				) va \
			WHERE node.\"本点号\" = va.\"起始节点\" and node.\"方案编码\" = '" + mPorjCode + "' \
			) vnode \
		WHERE vnode.\"终止节点\" = va.\"本点号\" and va.\"方案编码\" = '" + mPorjCode + "'";

	if (!mDbConn->execSql(startSql))
		return;

	std::vector<std::map<std::string, std::string>> valvesVec;
	mDbConn->queryResult(valvesVec);
	
	// 找出节点图层修改的值
	std::vector<std::map<std::string, std::string>> valvesDetailVec;
	auto iter = mDetailMap.find("阀门");
	if (iter != mDetailMap.end())
		valvesDetailVec = iter->second;


	std::map<std::string, CivValve::ValveTable> valveTableMap;
	std::map<std::string, CivPipe::PipesTable> pipeTableMap;
	size_t valvsSize = valvesVec.size();
	for (int i = 0; i < valvsSize; i++)
	{
		std::map<std::string, std::string> valveMap = valvesVec[i];

		// 构造节点
		CivNode::NodeTable nodeTable;
		std::string szNo = valveMap.find("本点号")->second;
		nodeTable.ID = szNo;
		nodeTable.Elev = valveMap.find("高程")->second;

		std::string patternID = valveMap.find("需水量模式")->second;
		std::string demand = valveMap.find("基本需水量")->second;
		handleDelayPattern(demand, patternID, nodeTable.Demand, nodeTable.PatternId);

		mNodeVec.push_back(nodeTable);

		// 节点坐标
		CivCoordinates::CoordTable coordTable;
		coordTable.ID = szNo;
		coordTable.XCoord = valveMap.find("横坐标")->second;
		coordTable.YCoord = valveMap.find("纵坐标")->second;

		mCoords.push_back(coordTable);

		std::string valveSn = valveMap.find("阀门本点号")->second;
		std::string valveType = valveMap.find("阀门类型")->second;
		std::string valveStatus = valveMap.find("阀门状态")->second;
		std::string code = valveMap.find("code")->second;

		// 根据code匹配
		for (int i = 0; i < valvesDetailVec.size(); i++)
		{
			auto detailMap = valvesDetailVec[i];

			//code 不匹配，继续下一次循环查找
			auto codeIter = detailMap.find("code");
			if (codeIter == detailMap.end())
				continue;

			// 详情表code 与原管网code不想等, 不符合条件
			if (codeIter->second == code)
				continue;

			// 根据属性修改对应的值
			std::string attri = detailMap.find("属性")->second;
			std::string newValue = detailMap.find("属性新值")->second;
			if (attri == "固定状态")
			{
				valveStatus = newValue;
			}
		}

		if (valveType == "BV") // 止回阀，转成管段
		{
			CivPipe::Table pipeStruct;

			pipeStruct.ID = valveSn;
			pipeStruct.Node1 = szNo;
			pipeStruct.Length = "2";
			pipeStruct.Diameter = valveMap.find("阀门直径")->second;
			pipeStruct.Roughness = std::string("130");//valveMap.find("摩阻系数")->second;
			pipeStruct.Status = (valveStatus== "开") ? "CV" : "Open";
			pipeStruct.MinorLoss = "0";

			auto pter = std::make_pair(valveSn, pipeStruct);
			pipeTableMap.insert(pter);
		}
		else
		{
			// 阀门
			CivValve::ValveTable valveTable;
			valveTable.ID = valveSn;
			valveTable.Node1 = szNo;
			valveTable.Diameter = valveMap.find("阀门直径")->second;
			valveTable.Type = valveType;
			valveTable.Setting = valveMap.find("阀门设置")->second;
			valveTable.MinorLoss = valveMap.find("水损系数")->second;

			valveTableMap.insert(std::make_pair(szNo, valveTable));

			// 阀门状态组件
			CivStatus::StatusTable statusTable;
			statusTable.ID = valveSn;
			statusTable.Setting = valveStatus;

			mStatusVec.push_back(statusTable);
		}

	}


	//--构造阀门终止节点
	std::string vaEndSql = "SELECT 'DW' || va.\"本点号\" AS \"本点号\", vnode.\"高程\", vnode.\"基本需水量\", vnode.\"需水量模式\"\
		, (va.\"横坐标\" - 1) AS \"横坐标\", (va.\"纵坐标\" - 1) AS \"纵坐标\", va.\"本点号\" as \"阀门本点号\",\
		va.\"类型\" as \"阀门类型\", ('DW' || va.\"本点号\") as \"阀门终止节点\"\
		FROM \"阀门\" va, (\
			SELECT node.\"本点号\", node.\"高程\", node.\"基本需水量\", node.\"需水量模式\", node.\"横坐标\", node.\"纵坐标\", va.\"起始节点\"\
			FROM \"节点\" node, (\
				SELECT pipe.\"编号\", pipe.\"起始节点\", pipe.\"终止节点\", pipe.\"管长\", pipe.\"管径\", pipe.\"摩阻系数\"\
				FROM \"管段\" pipe\
				WHERE pipe.\"from_layer\" = '阀门' and pipe.\"code\"\
				not in(select \"code\" from \"方案详情表\" where \"图层\" = '管段' and \"操作类型\" = 0 and \"方案编码\" = '"+mPorjCode +"')\
				union\
				SELECT pipe.\"编号\", pipe.\"起始节点\", pipe.\"终止节点\", pipe.\"管长\", pipe.\"管径\", pipe.\"摩阻系数\"\
				FROM \"方案管段\" pipe\
				WHERE pipe.\"from_layer\" = '阀门' and pipe.\"方案编码\" = '"+mPorjCode +"'\
				) va \
			WHERE node.\"本点号\" = va.\"终止节点\" and node.\"code\"\
			not in(select \"code\" from \"方案详情表\" where \"图层\" = '管段' and \"操作类型\" = 0 and \"方案编码\" = '"+mPorjCode +"')\
			union\
			SELECT node.\"本点号\", node.\"高程\", node.\"基本需水量\", node.\"需水量模式\", node.\"横坐标\", node.\"纵坐标\", va.\"起始节点\"\
			FROM \"方案节点\" node, (\
				SELECT pipe.\"编号\", pipe.\"起始节点\", pipe.\"终止节点\", pipe.\"管长\", pipe.\"管径\", pipe.\"摩阻系数\"\
				FROM \"管段\" pipe\
				WHERE pipe.\"from_layer\" = '阀门' and pipe.\"code\"\
				not in(select \"code\" from \"方案详情表\" where \"图层\" = '管段' and \"操作类型\" = 0 and \"方案编码\" = '"+mPorjCode +"')\
				union\
				SELECT pipe.\"编号\", pipe.\"起始节点\", pipe.\"终止节点\", pipe.\"管长\", pipe.\"管径\", pipe.\"摩阻系数\"\
				FROM \"方案管段\" pipe\
				WHERE pipe.\"from_layer\" = '阀门' and pipe.\"方案编码\" = '"+mPorjCode +"'\
				) va\
			WHERE node.\"本点号\" = va.\"终止节点\" and node.\"方案编码\" = '"+mPorjCode +"'\
			) vnode\
		WHERE vnode.\"起始节点\" = va.\"本点号\"\
		and va.\"code\" not in(select \"code\" from \"方案详情表\" where \"图层\" = '阀门' and \"操作类型\" = 0 and \"方案编码\" = '"+mPorjCode +"')\
		union\
		SELECT 'DW' || va.\"本点号\" AS \"本点号\", vnode.\"高程\", vnode.\"基本需水量\", vnode.\"需水量模式\"\
		, (va.\"横坐标\" - 1) AS \"横坐标\", (va.\"纵坐标\" -1) AS \"纵坐标\", va.\"本点号\" as \"阀门本点号\",\
		va.\"类型\" as \"阀门类型\", ('DW' || va.\"本点号\") as \"阀门终止节点\"\
		FROM \"方案阀门\" va, (\
			SELECT node.\"本点号\", node.\"高程\", node.\"基本需水量\", node.\"需水量模式\", node.\"横坐标\", node.\"纵坐标\", va.\"起始节点\"\
			FROM \"节点\" node, (\
				SELECT pipe.\"编号\", pipe.\"起始节点\", pipe.\"终止节点\", pipe.\"管长\", pipe.\"管径\", pipe.\"摩阻系数\"\
				FROM \"管段\" pipe\
				WHERE pipe.\"from_layer\" = '阀门' and pipe.\"code\"\
				not in(select \"code\" from \"方案详情表\" where \"图层\" = '管段' and \"操作类型\" = 0 and \"方案编码\" = '"+mPorjCode +"')\
				union\
				SELECT pipe.\"编号\", pipe.\"起始节点\", pipe.\"终止节点\", pipe.\"管长\", pipe.\"管径\", pipe.\"摩阻系数\"\
				FROM \"方案管段\" pipe\
				WHERE pipe.\"from_layer\" = '阀门' and pipe.\"方案编码\" = '"+mPorjCode +"'\
				) va\
			WHERE node.\"本点号\" = va.\"终止节点\" and node.\"code\"\
			not in(select \"code\" from \"方案详情表\" where \"图层\" = '管段' and \"操作类型\" = 0 and \"方案编码\" = '"+mPorjCode +"')\
			union\
			SELECT node.\"本点号\", node.\"高程\", node.\"基本需水量\", node.\"需水量模式\", node.\"横坐标\", node.\"纵坐标\", va.\"起始节点\"\
			FROM \"方案节点\" node, (\
				SELECT pipe.\"编号\", pipe.\"起始节点\", pipe.\"终止节点\", pipe.\"管长\", pipe.\"管径\", pipe.\"摩阻系数\"\
				FROM \"管段\" pipe\
				WHERE pipe.\"from_layer\" = '阀门' and pipe.\"code\"\
				not in(select \"code\" from \"方案详情表\" where \"图层\" = '管段' and \"操作类型\" = 0 and \"方案编码\" = '"+mPorjCode +"')\
				union\
				SELECT pipe.\"编号\", pipe.\"起始节点\", pipe.\"终止节点\", pipe.\"管长\", pipe.\"管径\", pipe.\"摩阻系数\"\
				FROM \"方案管段\" pipe\
				WHERE pipe.\"from_layer\" = '阀门' and pipe.\"方案编码\" = '"+mPorjCode +"'\
				) va\
			WHERE node.\"本点号\" = va.\"终止节点\" and node.\"方案编码\" = '"+mPorjCode +"'\
			) vnode\
		WHERE vnode.\"起始节点\" = va.\"本点号\" and va.\"方案编码\" = '"+mPorjCode +"'";

	if (!mDbConn->execSql(vaEndSql))
		return;

	std::vector<std::map<std::string, std::string>> valvesEndVec;
	mDbConn->queryResult(valvesEndVec);

	size_t endVecSize = valvesEndVec.size();
	for (int i = 0; i < endVecSize; i++)
	{
		std::map<std::string, std::string> valveMap = valvesEndVec[i];

		// 构造节点
		CivNode::NodeTable nodeTable;
		std::string szNo = valveMap.find("本点号")->second;
		nodeTable.ID = szNo;
		nodeTable.Elev = valveMap.find("高程")->second;

		std::string patternID = valveMap.find("需水量模式")->second;
		std::string demand = valveMap.find("基本需水量")->second;
		handleDelayPattern(demand, patternID, nodeTable.Demand, nodeTable.PatternId);

		mNodeVec.push_back(nodeTable);

		// 节点坐标
		CivCoordinates::CoordTable coordTable;
		coordTable.ID = szNo;
		coordTable.XCoord = valveMap.find("横坐标")->second;
		coordTable.YCoord = valveMap.find("纵坐标")->second;

		mCoords.push_back(coordTable);

		std::string valveSn = valveMap.find("阀门本点号")->second;
		std::string valveType = valveMap.find("阀门类型")->second;

		if (valveType == "BV") // 止回阀，转成管段
		{
			auto iter = pipeTableMap.find(valveSn);
			if (iter != pipeTableMap.end())
			{
				pipeTableMap[valveSn].Node2 = szNo;
				mPipesVec.push_back(iter->second);
			}
		}
		else
		{
			// 阀门
			auto iter = valveTableMap.find(valveSn);
			if (iter != valveTableMap.end())
			{
				valveTableMap[valveSn].Node2 = szNo;
				mValuvesVec.push_back(iter->second);
			}
		}
	}
}

bool CivProjInpDbHelper::getNode(CivNode& node)
{
	// 查找，节点表数据
	JunctionTable nodeTableFields;

	std::string sql = "select  \"code\",  \"本点号\", \"高程\", \"基本需水量\", \"需水量模式\", \"横坐标\",\"纵坐标\" \
							from  \"节点\"   where\
							\"code\" not in(select det.\"code\" from \"方案详情表\" as det where det.\"方案编码\" = '"+mPorjCode+"' and \"图层\" = '节点' and \"操作类型\"=0)\
							union   select   \"code\",  \"本点号\", \"高程\", \"基本需水量\", \"需水量模式\", \"横坐标\",\"纵坐标\" \
							from   \"方案节点\"  where    \"方案编码\" = '"+ mPorjCode +"' ";

	if (!mDbConn->execSql(sql))
		return false;

	std::vector<std::map<std::string, std::string>> resultVector;
	mDbConn->queryResult(resultVector);

	// 找出节点图层修改的值
	std::vector<std::map<std::string, std::string>> nodeDetailVec;
	auto iter = mDetailMap.find(PIPENODE);
	if (iter != mDetailMap.end())
		nodeDetailVec = iter->second;

	// 编辑包装结果
	size_t total = resultVector.size();
	for (int i = 0; i < total; i++)
	{
		std::map<std::string, std::string> map = resultVector[i];
		CivNode::NodeTable nodeTable;

		std::string code = map.find(nodeTableFields.code)->second;
		std::string elev = map.find(nodeTableFields.elev)->second;
		std::string patternID = map.find(nodeTableFields.demandPattern)->second;
		std::string demand = map.find(nodeTableFields.baseDemand)->second;

		// 根据code匹配
		for (int i = 0; i < nodeDetailVec.size(); i++)
		{
			auto detailMap = nodeDetailVec[i];

			//code 不匹配，继续下一次循环查找
			auto codeIter = detailMap.find(nodeTableFields.code);
			if (codeIter == detailMap.end())
				continue;

			// 详情表code 与原管网code不想等, 不符合条件
			if (codeIter->second  == code)
				continue;

			// 根据属性修改对应的值
			std::string attri = detailMap.find("属性")->second;
			std::string newValue = detailMap.find("属性新值")->second;
			if (attri == nodeTableFields.elev)
			{
				elev = newValue;
			}
			else if (attri == nodeTableFields.demandPattern)
			{
				patternID = newValue;
			}
			else if (attri == nodeTableFields.baseDemand)
			{
				demand = newValue;
			}

		}

		nodeTable.ID = map.find("本点号")->second;
		nodeTable.Elev = elev;
		handleDelayPattern(demand, patternID, nodeTable.Demand, nodeTable.PatternId);

		node.addItem(nodeTable);
	}

	// 添加处理的阀门产生的节点
	size_t size = mNodeVec.size();
	for (int i = 0; i < size; i++)
	{
		node.addItem(mNodeVec[i]);
	}

	return true;
}

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

	std::string sql = "select  \"code\",\"编号\",\"起始节点\",\"终止节点\",\"管长\",\"管径\",\"摩阻系数\",\"局损系数\",\"初始状态\"\
							 from \"管段\" \
							where \"from_layer\" not in('阀门','方案阀门','水泵','方案水泵') \
							and \"to_layer\" not in('阀门','方案阀门','水泵','方案水泵') \
							and \"code\" not in(select det.\"code\" from \"方案详情表\" as det where det.\"方案编码\" = '"+mPorjCode+"' and \"图层\"='管段')\
							union select \
							\"code\",\"编号\",\"起始节点\",\"终止节点\",\"管长\",\"管径\",\"摩阻系数\",\"局损系数\",\"初始状态\" from \"方案管段\" where \
							\"方案编码\" = '"+mPorjCode+"'";

	if (!mDbConn->execSql(sql))
		return false;

	std::vector<std::map<std::string, std::string>> resultVector;
	mDbConn->queryResult(resultVector);

	// 找出管段图层修改的值
	std::vector<std::map<std::string, std::string>> lineDetailVec;
	auto iter = mDetailMap.find(PIPELINE);
	if (iter != mDetailMap.end())
		lineDetailVec = iter->second;

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

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

		std::string code = map.find("code")->second;
		pipe.ID = map.find(pipeTable.snNo)->second;
		pipe.Node1 = map.find(pipeTable.startPoint)->second;
		pipe.Node2 = map.find(pipeTable.endPoint)->second;
		pipe.Status = map.find(pipeTable.status)->second;

		std::string Length = map.find(pipeTable.length)->second;
		std::string Diameter = map.find(pipeTable.diameter)->second;
		std::string Roughness = map.find(pipeTable.friction)->second;
		std::string MinorLoss = map.find(pipeTable.localLoss)->second;

		for (int i = 0; i < lineDetailVec.size(); i++)
		{
			std::map<std::string, std::string>  detailMap = lineDetailVec[i];

			//code 不匹配，继续下一次循环查找
			auto codeIter = detailMap.find("code");
			if (codeIter == detailMap.end())
				continue;

			// 详情表code 与原管网code不想等, 不符合条件
			if (codeIter->second != code)
				continue;

			// 根据属性修改对应的值
			std::string attri = detailMap.find("属性")->second;
			if (attri == pipeTable.length)
			{
				Length = detailMap.find("属性新值")->second;
			}
			else if (attri == pipeTable.diameter)
			{
				Diameter = detailMap.find("属性新值")->second;
			}
			else if (attri == pipeTable.friction)
			{
				Roughness = detailMap.find("属性新值")->second;
			}
			else if (attri == pipeTable.localLoss)
			{
				MinorLoss = detailMap.find("属性新值")->second;
			}
		}

		pipe.Length = Length;
		pipe.Diameter = Diameter;
		pipe.Roughness = Roughness;
		pipe.MinorLoss = MinorLoss;
		
		pipes.addItem(pipe);
	}
	// 加上处理阀门和水泵产生的虚拟管段
	for (int i = 0; i < mPipesVec.size(); i++)
	{
		pipes.addItem(mPipesVec[i]);
	}
	return true;
}

bool CivProjInpDbHelper::getTank(CivTank& tanks)
{
	TankTable tankTable;
	std::string sql = "select \"code\",\"本点号\",\"高程\",\"初始水位\",\"最低水位\",\"最高水位\",\"直径\",\
		\"最小容积\",\"容积曲线\" from \"水池\"\
		where \"code\" not in(select det.\"code\" from \"方案详情表\" as det\
		 where det.\"方案编码\" = '"+mPorjCode+"' and det.\"图层\" = '水池') \
		union select  \"code\",\"本点号\",\"高程\",\"初始水位\",\"最低水位\",\
			\"最高水位\",\"直径\", \"最小容积\",\"容积曲线\" from \"方案水池\"\
		where \"方案编码\"='"+mPorjCode+"'";

	if (!mDbConn->execSql(sql))
		return false;

	std::vector<std::map<std::string, std::string>> resultVector;
	mDbConn->queryResult(resultVector);

	// 找出节点图层修改的值
	std::vector<std::map<std::string, std::string>> tankDetailVec;
	auto iter = mDetailMap.find(PIPENODE);
	if (iter != mDetailMap.end())
		tankDetailVec = iter->second;

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

		std::string code = map.find("code")->second;

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

		// 根据code匹配
		for (int i = 0; i < tankDetailVec.size(); i++)
		{
			auto detailMap = tankDetailVec[i];

			//code 不匹配，继续下一次循环查找
			auto codeIter = detailMap.find("code");
			if (codeIter == detailMap.end())
				continue;

			// 详情表code 与原管网code不想等, 不符合条件
			if (codeIter->second == code)
				continue;

			// 根据属性修改对应的值
			std::string attri = detailMap.find("属性")->second;
			std::string newValue = detailMap.find("属性新值")->second;

			if (attri == tankTable.elev)
			{
				Elev = newValue;
			}
			else if (attri == tankTable.initLevel)
			{
				InitLevel = newValue;
			}
			else if (attri == tankTable.minLevel)
			{
				MinLevel = newValue;
			}
			else if (attri == tankTable.maxLevel)
			{
				MaxLevel = newValue;
			}
			else if (attri == tankTable.diametor)
			{
				Diameter = newValue;
			}
			else if (attri == tankTable.minVolume)
			{
				MinVol = newValue;
			}
			else if (attri == tankTable.volumeCurve)
			{
				VolCurve = newValue;
			}
		}

		tank.Elev = Elev;
		tank.InitLevel = InitLevel;
		tank.MinLevel = MinLevel;
		tank.MaxLevel = MaxLevel;
		tank.Diameter = Diameter;
		tank.MinVol = MinVol;
		tank.VolCurve = VolCurve;

		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);
	mDbConn->query(PROJ_VALVE, fields, resultVector, mCondition);

	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);
	}*/
	for (int i = 0; i < mValuvesVec.size(); i++)
	{
		valves.addItem(mValuvesVec[i]);
	}
	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;
	std::string condition1 = splicingConditions(PUMP);
	mDbConn->query(PUMP, fields, resultVector, condition1);

	std::string condition2 = " \"方案编码\" = '" + mPorjCode + "'";
	mDbConn->query(PROJ_PUMP, fields, resultVector, condition2);

	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::string sql = "select \"code\",\"本点号\",\"总水头\",\"水头模式\" from \"水库\" \
							where code not in(select \"code\" from \"方案详情表\" \
							where \"方案编码\"='"+mPorjCode+"' and \"图层\"='水库' and \"操作类型\" =0) \
							union select  \"code\",\"本点号\",\"总水头\",\"水头模式\" from \"方案水库\" where \"方案编码\" = '"+mPorjCode+"'";

	std::vector<std::map<std::string, std::string>> resultVector;

	if (!mDbConn->execSql(sql))
		return false;

	mDbConn->queryResult(resultVector);

	// 找出节点图层修改的值
	std::vector<std::map<std::string, std::string>> resourceDetailVec;
	auto iter = mDetailMap.find(PIPENODE);
	if (iter != mDetailMap.end())
		resourceDetailVec = iter->second;

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

		std::string code = map.find("code")->second;
		std::string totalHead = map.find(restemTable.totalHead)->second;
		std::string Pattern = map.find(restemTable.headPattern)->second;

		for (int i = 0; i < resourceDetailVec.size(); i++)
		{
			std::map<std::string, std::string> detailMap = resourceDetailVec[i];


			//code 不匹配，继续下一次循环查找
			auto codeIter = detailMap.find("code");
			if (codeIter == detailMap.end())
				continue;

			// 详情表code 与原管网code不想等, 不符合条件
			if (codeIter->second != code)
				continue;

			// 根据属性修改对应的值
			std::string attri = detailMap.find("属性")->second;
			std::string newValue = detailMap.find("属性新值")->second;
			if (attri == restemTable.totalHead)
			{
				totalHead = newValue;
			}
			else if (attri == restemTable.headPattern)
			{
				Pattern = newValue;
			}
		}
		resTable.ID = map.find(restemTable.sn)->second;
		resTable.Head = totalHead;
		resTable.Pattern = Pattern;

		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;


	std::string condition1 = splicingConditions(PIPENODE);
	mDbConn->query(PIPENODE, fields, resultVector, condition1);

	std::string condition2 = splicingConditions(TANK);
	mDbConn->query(TANK, fields, resultVector,condition2);

	std::string condition3 = splicingConditions(RESIVOIR);
	mDbConn->query(RESIVOIR, fields, resultVector, condition3);

	std::string condition = " \"方案编码\" = '"+mPorjCode+"'";
	mDbConn->query(PROJ_PIPENODE, fields, resultVector, condition);
	mDbConn->query(PROJ_TANK, fields, resultVector, condition);
	mDbConn->query(PROJ_RESIVOIR, fields, resultVector, condition);
	
	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);
	}

	// 虚拟点坐标
	for (int i = 0; i < mCoords.size(); i++)
	{
		coord.addItem(mCoords[i]);
	}
	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);
	mDbConn->query(RESIVOIR, fields, resultVector);
	mDbConn->query(PROJ_PIPENODE, fields, resultVector, mCondition);
	mDbConn->query(PROJ_RESIVOIR, fields, resultVector, mCondition);
	
	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;
}

bool CivProjInpDbHelper::getStatus(CivStatus& status)
{
	// 阀门状态
	for (int i = 0; i < mStatusVec.size(); i++)
	{
		status.addItem(mStatusVec[i]);
	}
	return true;
}

std::string CivProjInpDbHelper::genSql(const std::string& table)
{
	return "";
}