#include "CIvProjSimulResHelper.h"
#include "CivPgDbConnection.h"


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

	mPreNodeSQL = "insert into \"方案模拟点表\"(\"code\",\"方案编码\",\"时间点\",\"水头\",\"压力\",\"方案水头\",\"方案压力\",\"修改时间\")";
	mPrePipeSQL = "insert into \"方案模拟线表\" (\"code\",\"方案编码\",\"时间点\",\"流量\",\"流速\",\"单位水损\",\"方案流量\",\"方案流速\",\"方案单位水损\",\"修改时间\")";
}

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

void CIvProjSimulResHelper::saveNodeToDb(const std::vector<ProjNodeResultItem>& nodeItems)
{
	size_t total = nodeItems.size();

	std::string nodeSql;
	nodeSql.append("  values");
	for (int i = 0; i < total; i++)
	{
		ProjNodeResultItem nodeItem = nodeItems[i];
		if (nodeItem.dProjHead == 0 && nodeItem.dProjPressure == 0)
			continue;

		nodeSql.append("('");
		nodeSql.append(nodeItem.code);
		nodeSql.append("','");
		nodeSql.append(nodeItem.projCode);
		nodeSql.append("','");
		nodeSql.append(nodeItem.dTime);
		nodeSql.append("',");
		nodeSql.append(std::to_string(nodeItem.dHead));
		nodeSql.append(",");
		nodeSql.append(std::to_string(nodeItem.dPressure));
		nodeSql.append(",");
		nodeSql.append(std::to_string(nodeItem.dProjHead));
		nodeSql.append(",");
		nodeSql.append(std::to_string(nodeItem.dProjPressure));
		nodeSql.append(",'");
		nodeSql.append(nodeItem.dDateTime);
		nodeSql.append("'),");
	}
	nodeSql = nodeSql.substr(0, nodeSql.length() - 1);
	std::string finalSql = mPreNodeSQL + nodeSql;

	if (!mConn->execSql(finalSql))
		return;
}

void CIvProjSimulResHelper::savePipeToDb(const std::vector<PorjPipeResultItem>& pipeItems)
{
	std::string pipeSql;
	pipeSql.append("  values");

	size_t total = pipeItems.size();
	for (int i = 0; i < total; i++)
	{
		PorjPipeResultItem pipItem = pipeItems[i];
		if (pipItem.dProjFlow == 0 && pipItem.dProjVelocity == 0)
			continue;

		pipeSql.append("('");
		pipeSql.append(pipItem.code);
		pipeSql.append("','");
		pipeSql.append(pipItem.projCode);
		pipeSql.append("','");
		pipeSql.append(pipItem.dTime);
		pipeSql.append("',");
		pipeSql.append(std::to_string(pipItem.dFlow));
		pipeSql.append(",");
		pipeSql.append(std::to_string(pipItem.dVelocity));
		pipeSql.append(",");
		pipeSql.append(std::to_string(pipItem.dHeadLoss));
		pipeSql.append(",");
		pipeSql.append(std::to_string(pipItem.dProjFlow));
		pipeSql.append(",");
		pipeSql.append(std::to_string(pipItem.dProjVelocity));
		pipeSql.append(",");
		pipeSql.append(std::to_string(pipItem.dPorjHeadLoss));
		pipeSql.append(",'");
		pipeSql.append(pipItem.dDateTime);
		pipeSql.append("'),");
	}

	pipeSql = pipeSql.substr(0,pipeSql.length()-1);
	std::string finalSql = mPrePipeSQL + pipeSql;
	
	mConn->execSql(finalSql);
}

void CIvProjSimulResHelper::deleteByTimeAndPorjcode( const std::string& time,const std::string& projCode)
{
	std::string nodeDelsql = "delete from \"方案模拟点表\" where \"时间点\"='" + time + "' and \"方案编码\" = '"+projCode+"'";
	mConn->execSql(nodeDelsql);

	std::string lineDelsql = "delete from \"方案模拟线表\" where \"时间点\"='" + time + "' and \"方案编码\" = '" + projCode + "'";
	mConn->execSql(lineDelsql);

}
