#include "CivPumpHelper.h"
#include "CivPgDbConnection.h"
#include <set>

CivPumpHelper::CivPumpHelper(const string& uri)
{
	mConn = new CivPgDbConnection();
	mConn->connect(uri);
}

CivPumpHelper::~CivPumpHelper()
{
	mConn->disconnect();
	delete mConn;
}

void CivPumpHelper::getMap(const string& key, const string& val, map<string, string>& res)
{
	vector<map<string, string>> vecMap;
	if (!mConn->query(mTable, { key ,val }, vecMap))
		return;

	if (vecMap.size() <= 0)
		return;

	size_t total = vecMap.size();
	for (int i = 0; i < total; i++)
	{
		map<string, string> tempMap = vecMap[i];

		string keyVal = tempMap.find(key)->second;
		string valVal = tempMap.find(val)->second;

		res.insert(pair<string, string>(keyVal, valVal));
	}
}

int CivPumpHelper::getPumpNumbers()
{
	string sql = "select count(id) as cunt from " + mTable;
	if (!mConn->execSql(sql))
		return 0;

	vector<map<string,string>> pumpRes;
	if (!mConn->queryResult(pumpRes))
		return 0;

	if (pumpRes.size() <= 0)
		return 0;

	map<string, string> pumpMap = pumpRes[0];
	map<string, string>::iterator iter = pumpMap.find("cunt");
	if (iter == pumpMap.end())
		return 0;

	string pumpSize = iter->second;
	int pumps = atoi(pumpSize.c_str());
	return pumps;
}

void CivPumpHelper::getPumpSn(vector<string>& pumpNames,map<string,string>& pumpMap)
{
	const string sz = "本点号";
	vector<map<string, string>> vecMap;
	if (!mConn->query(mTable, { sz,"code" }, vecMap))
		return;

	if (vecMap.size() <= 0)
		return;

	size_t total = vecMap.size();
	for (int i = 0; i < total; i++)
	{
		map<string, string> tempMap = vecMap[i];
		string val = tempMap.find(sz)->second;
		string code = tempMap.find("code")->second;
		pumpNames.push_back(val);
		pumpMap.insert(pair<string, string>(val, code));
	}
}


void CivPumpHelper::getPumpStations(vector<PumpStation>& stations)
{
	vector<map<string, string>> vecMap;
	if (!mConn->query(mTable, { "本点号","code","价格模式","能量价格","功率","泵站","水泵状态","转速比" }, vecMap))
		return;

	if (vecMap.size() <= 0)
		return;

	size_t total = vecMap.size();
	map<string, vector<PumpStruct>> stationMap;

	for (int i = 0; i < total; i++)
	{
		map<string, string> tempMap = vecMap[i];
		
		PumpStruct pumpStruct;
		pumpStruct.sz = tempMap.find("本点号")->second;
		pumpStruct.pricePattern = tempMap.find("价格模式")->second;
		pumpStruct.power = tempMap.find("功率")->second;
		pumpStruct.status = tempMap.find("水泵状态")->second;
		pumpStruct.speedRatio = tempMap.find("转速比")->second;
		pumpStruct.code = tempMap.find("code")->second;

		string pumpStation = tempMap.find("泵站")->second;
		stationMap[pumpStation].push_back(pumpStruct);
	}

	// 构造泵站集合
	map<string, vector<PumpStruct>>::iterator iter = stationMap.begin();
	for (; iter != stationMap.end(); iter++)
	{
		PumpStation station;
		station.stationName = iter->first;
		station.pumps = iter->second;
		stations.push_back(station);
	}
}

void CivPumpHelper::getMonitors(vector<string>& monitors,map<string,string>& monitorMap)
{
	string sql = "select  node.\"本点号\",node.\"code\" from \"节点\" as node, \"压力监测点\" as ja where node.\"code\" = ja.\"节点id\"";

	if (!mConn->execSql(sql))
		return;

	vector<map<string, string>> tempMapVec;
	mConn->queryResult(tempMapVec);

	for (int i = 0; i < tempMapVec.size(); i++)
	{
		map<string, string> tempMap = tempMapVec[i];
		string szNo = tempMap.find("本点号")->second;
		string code = tempMap.find("code")->second;
		monitors.push_back(szNo);
		monitorMap.insert(pair<string, string>(szNo, code));
	}
}