Commit 2a411181 authored by 刘乐's avatar 刘乐

1,接口调优

parent 84ec23b4
......@@ -46,3 +46,20 @@ void CivConGetDataTest::test(char* uri)
bool isSucces = getDataByInterval(uri, dates, intervals);
std::cout << "测试结果:" << isSucces << std::endl;
};
void CivConGetRptTest::test(char* uri)
{
std::string condtion = "\"日期\"='2020-07-13' and \"时段\"=1";
std::string type = "管段";
char result[1024 * 128];
char newCondition[256];
strcpy(newCondition, condtion.c_str());
char newType[32];
strcpy(newType, type.c_str());
bool isSucc = getRptByCondition(uri, newType, newCondition, result);
std::cout << result << std::endl;
std::cout << "测试结果:" << isSucc << std::endl;
}
......@@ -36,3 +36,12 @@ class CivConGetDataTest :public CivHydrTest
public:
virtual void test(char* uri);
};
/**
获取水力分析报告测试
*/
class CivConGetRptTest :public CivHydrTest
{
public:
virtual void test(char* uri);
};
......@@ -24,13 +24,16 @@ int main(int argc, char* argv[])
context.contextTest(findUri);*/
// 测试追踪分析
CivTestContext context(new CivConTrackTest());
context.contextTest(findUri);
/*CivTestContext context(new CivConTrackTest());
context.contextTest(findUri);*/
// 测试根据时段获取值
/*CivTestContext context(new CivConGetDataTest());
context.contextTest(findUri);*/
CivTestContext context(new CivConGetDataTest());
context.contextTest(findUri);
// 测试根据
/* CivTestContext context(new CivConGetRptTest());
context.contextTest(findUri);*/
getchar();
return 0;
}
\ No newline at end of file
#include "CivHydrFuncInter.h"
#include "CivHydrCalc.h"
#include "CivSysLog.h"
#include "CivTableTemp.h"
#include "CivPgConn.h"
#include "CivHydrSimulation.h"
......@@ -36,12 +37,6 @@ bool DLLEXPORT simulation(char* uri, char* netName, char* flag)
return msg;
}
bool DLLEXPORT hdySimulation(char* uri, char* netName)
{
return true;
}
bool DLLEXPORT getDataByInterval(char* uri, char* date, char* interval)
{
CivDbConn* dbConn = new CivPgConn(uri);
......@@ -66,7 +61,6 @@ bool DLLEXPORT getDataByInterval(char* uri, char* date, char* interval)
return true;
}
bool DLLEXPORT hdyrSimulation(char* uri)
{
// 创建数据库连接
......@@ -102,7 +96,7 @@ bool DLLEXPORT qualitySimulation(char* uri)
return flag;
}
bool DLLEXPORT trackingSimulation(char* uri, char* sN, int hours, char* result)
bool DLLEXPORT trackingSimulation(char* uri, char* sN, int hours, char* result)
{
// 创建数据库连接
......@@ -126,4 +120,88 @@ bool DLLEXPORT trackingSimulation(char* uri, char* sN, int hours, char* resul
delete mHyDr;
return flag;
}
bool DLLEXPORT getRptByCondition(char* uri, char* type, char* condition, char* result)
{
CivDbConn* dbConn = new CivPgConn(uri);
if (!dbConn->open())
{
delete dbConn;
return false;
}
std::string table;
std::vector<std::string> fileds;
if (type == PIPENODE)
{
table = "__node_result__";
CivNodeResultTableTemp::NodeResultTable tableTemp;
fileds.push_back(tableTemp.Number);
fileds.push_back(tableTemp.dDemand);
fileds.push_back(tableTemp.dHead);
fileds.push_back(tableTemp.dPressure);
fileds.push_back(tableTemp.dElevation);
fileds.push_back(tableTemp.dQuality);
}
else if (type == PIPELINE)
{
table = "__pipe_result__";
CivPipeResultTableTemp::PipeResultTable pipeTable;
fileds.push_back(pipeTable.szNo);
fileds.push_back(pipeTable.dFlow);
fileds.push_back(pipeTable.dVelocity);
fileds.push_back(pipeTable.dHeadloss);
fileds.push_back(pipeTable.dUnitHeadloss);
fileds.push_back(pipeTable.dLocalHeadloss);
fileds.push_back(pipeTable.dFrictionHeadloss);
fileds.push_back(pipeTable.dQuality);
}
else
{
delete dbConn;
return false;
}
std::vector<std::map<std::string,std::string>> res;
if (!dbConn->getValuesFromTableByCondition(table, condition, fileds, res))
{
delete dbConn;
return false;
}
delete dbConn;
// 拼接字符串
size_t total = res.size();
std::string strRes;
strRes.append("[");
for (int i = 0; i < total; i++)
{
std::map<std::string, std::string> tempMap = res[i];
strRes.append("{");
for (auto iter = tempMap.begin(); iter != tempMap.end(); iter++)
{
strRes.append(iter->first);
strRes.append(":");
strRes.append(iter->second);
strRes.append(",");
}
strRes = strRes.substr(0, strRes.length() - 1);
strRes.append("},");
}
strRes = strRes.substr(0, strRes.length() - 1);
strRes.append("]");
std::cout << strRes << std::endl;
int leng = strRes.length();
for (int i = 0; i < leng; i++)
{
result[i] = strRes[i];
}
return true;
}
\ No newline at end of file
......@@ -6,6 +6,16 @@
extern "C" {
#endif
/**
*@brief 获取当前日期和时段的结果值
*@param uri:数据库连接地址
*@param type: 节点或者管段
*@param condition:时段
*@param result 获取的结果值
*@return 1: 成功,其他值失败
*/
bool DLLEXPORT getRptByCondition(char* uri, char* type, char* condition, char* result);
/**
*@brief 水力计算服务
*@param uri:数据库连接地址
......@@ -38,23 +48,6 @@ extern "C" {
*/
bool DLLEXPORT simulation(char* uri, char* netName, char* flag);
/**
*@brief 水力模拟计算
*@param uri: 管网数据库连接
*@param netName: 管网名
*/
bool DLLEXPORT hdySimulation(char* uri, char* netName);
/**
*@brief 水力模拟计算
*@param uri: 管网数据库连接
*@param netName: 管网名
*@param type: 水质模拟类型选择,水龄,追踪,化学成分
*/
//bool DLLEXPORT qualitySimulation(char* uri, char* netName, char* type);
/**
*@brief 获取管网计算特定时段的结果
*@param uri: 管网数据库连接uri
......
......@@ -283,12 +283,12 @@ bool CivHydrSimulation::trackingSimulation(char* snNode, int hour)
}
// 扩散分析不存储数据
////模拟数据刷新
//if (!brushingResult())
//{
// CivSysLog::getInstance()->error("追踪分析计算失败", "CivHydrCalc", __FUNCTION__);
// return false;
//}
//模拟数据刷新
if (!brushingResult())
{
CivSysLog::getInstance()->error("追踪分析计算失败", "CivHydrCalc", __FUNCTION__);
return false;
}
return true;
......@@ -344,7 +344,7 @@ bool CivHydrSimulation::qualityCompute()
errcode = ENrunH(&t); // errcode > 100 是错误
if (errcode > 100)
{
ENopenQ();
ENcloseH();
ENclose();
return false;
}
......@@ -402,6 +402,7 @@ void CivHydrSimulation::getTrackingResult(int hours, std::string& newresult)
std::string result;
result.append("[");
// 缓存节点和管段空间信息
CivTrackingResultCache trackingCache;
trackingCache.cacheInfoFromDb(mDbConn);
......
......@@ -21,3 +21,4 @@ Str CivDbConn::getLastError() const
return mLastError;
}
......@@ -105,6 +105,19 @@ public:
const std::vector<std::string>& fileds,
std::vector< std::vector<std::string>>& result) = 0;
/**
*@brief 根据条件获取某表的指定字段的值
*@param table 查询的表
*@param condition 查询条件
*@param fileds 需要查询那些字段
*@param result 查询的结果集
*/
virtual bool getValuesFromTableByCondition(const std::string& table,
const std::string& condition,
const std::vector<std::string>& fileds,
std::vector< std::map<std::string, std::string>>& result) = 0;
// 获取当前执行错误信息
Str getLastError() const;
......
......@@ -54,25 +54,26 @@ bool CivPgConn::updateNodeByInterval(StrQuote currDate, StrQuote interval)
NodeFieldName filedNames;
os << "UPDATE "
<< TransUTFCoding(PIPENODE) << " AS tb1 set ("
<< TransUTFCoding("总水头") << ","
<< TransUTFCoding("压力") << ","
<< TransUTFCoding("实际需水量")
<< PIPENODE << " AS tb1 set ("
<< "总水头"<< ","
<< "压力"<< ","
<< "实际需水量"
<< ")=( "
<< "tb2." << TransUTFCoding(filedNames.dHead) << ","
<< "tb2." << TransUTFCoding(filedNames.dPressure) << ","
<< "tb2." << TransUTFCoding(filedNames.dDemand) << ")" << " from "
<< TransUTFCoding(NODERESULTTABLE) << " AS tb2 where tb1."
<< TransUTFCoding(filedNames.Number) << "= tb2."
<< TransUTFCoding(filedNames.Number) << " AND tb2."
<< TransUTFCoding(filedNames.dDate) << "='" << currDate << "' AND tb2."
<< TransUTFCoding(filedNames.dInterval) << "=" << interval;
<< "tb2." << filedNames.dHead << ","
<< "tb2." << filedNames.dPressure << ","
<< "tb2." << filedNames.dDemand<< ")" << " from "
<< NODERESULTTABLE<< " AS tb2 where tb1."
<< filedNames.Number << "= tb2."
<<filedNames.Number << " AND tb2."
<< filedNames.dDate<< "='" << currDate << "' AND tb2."
<< filedNames.dInterval<< "=" << interval;
std::string updateSql = os.str();
os.clear();
os.str("");
if (!execSql(updateSql))
std::string utfSql = TransUTFCoding(updateSql);
if (!execSql(utfSql))
{
CivSysLog::getInstance()->error(mLastError, "CivPgConn", __FUNCTION__);
return false;
......@@ -90,35 +91,36 @@ bool CivPgConn::updateLinkByInterval(StrQuote currDate, StrQuote interval)
LinkFiledName filedNames;
os << "UPDATE "
<< TransUTFCoding(PIPELINE) << " AS tb1 set ("
<< TransUTFCoding("流量") << ","
<< TransUTFCoding("流速") << ","
<< TransUTFCoding("单位水头损") << ","
<< TransUTFCoding("水头总损失") << ","
<< TransUTFCoding("摩擦水头损") << ","
<< TransUTFCoding("上点水头") << ","
<< TransUTFCoding("本点水头") << ","
<< TransUTFCoding("局部水头损")
<< PIPELINE<< " AS tb1 set ("
<< "流量"<< ","
<< "流速"<< ","
<< "单位水头损"<< ","
<< "水头总损失"<< ","
<< "摩擦水头损"<< ","
<< "上点水头"<< ","
<< "本点水头"<< ","
<< "局部水头损"
<< ")=( "
<< "tb2." << TransUTFCoding(filedNames.dFlow) << ","
<< "tb2." << TransUTFCoding(filedNames.dVelocity) << ","
<< "tb2." << TransUTFCoding(filedNames.dUnitHeadloss) << ","
<< "tb2." << TransUTFCoding(filedNames.dHeadloss) << ","
<< "tb2." << TransUTFCoding(filedNames.dFrictionHeadloss) << ","
<< "tb2." << TransUTFCoding(filedNames.dFromNodHeadloss) << ","
<< "tb2." << TransUTFCoding(filedNames.dToNodHeadloss) << ","
<< "tb2." << TransUTFCoding(filedNames.dLocalHeadloss) << ")" << " from "
<< TransUTFCoding(PIPERESULTTABLE) << " AS tb2 where tb1."
<< TransUTFCoding(filedNames.szNo) << "= tb2."
<< TransUTFCoding(filedNames.szNo) << " AND tb2."
<< TransUTFCoding(filedNames.dDate) << "='" << currDate << "' AND tb2."
<< TransUTFCoding(filedNames.dInterval) << "=" << interval;
<< "tb2." << filedNames.dFlow<< ","
<< "tb2." << filedNames.dVelocity<< ","
<< "tb2." << filedNames.dUnitHeadloss<< ","
<< "tb2." << filedNames.dHeadloss<< ","
<< "tb2." << filedNames.dFrictionHeadloss<< ","
<< "tb2." << filedNames.dFromNodHeadloss<< ","
<< "tb2." << filedNames.dToNodHeadloss<< ","
<< "tb2." << filedNames.dLocalHeadloss<< ")" << " from "
<< PIPERESULTTABLE<< " AS tb2 where tb1."
<< filedNames.szNo<< "= tb2."
<< filedNames.szNo<< " AND tb2."
<< filedNames.dDate<< "='" << currDate << "' AND tb2."
<< filedNames.dInterval<< "=" << interval;
std::string updateSql = os.str();
os.clear();
os.str("");
if (!execSql(updateSql))
std::string utfSql = TransUTFCoding(updateSql);
if (!execSql(utfSql))
{
CivSysLog::getInstance()->error(mLastError, "CivPgConn", __FUNCTION__);
return false;
......@@ -363,7 +365,7 @@ bool CivPgConn::getNode(CivNode& node)
}
int rows = PQntuples(mResult);
int columns = PQnfields(mResult);
for (int i = 0; i < rows; i++)
{
CivNode::NodeTable nodeTable;
......@@ -591,7 +593,6 @@ bool CivPgConn::getReservoirs(CivReservoirs& reservoirs)
}
int rows = PQntuples(mResult);
int columns = PQnfields(mResult);
for (int i = 0; i < rows; i++)
{
CivReservoirs::ReservoirsTable resTable;
......@@ -651,7 +652,6 @@ bool CivPgConn::getCoordinates(CivCoordinates& coord)
}
int rows = PQntuples(mResult);
int columns = PQnfields(mResult);
for (int i = 0; i < rows; i++)
{
CivCoordinates::CoordTable coordTable;
......@@ -690,7 +690,7 @@ bool CivPgConn::getParameter(std::vector<CivParameter>& params)
}
int rows = PQntuples(mResult);
int columns = PQnfields(mResult);
std::map<std::string, std::vector<CivParameter::ParamTable>> tempMap;
CivParameter::ParamTable resTable;
for (int i = 0; i < rows; i++)
......@@ -742,8 +742,7 @@ bool CivPgConn::getPatterns(CivPatterns& patterns)
}
int rows = PQntuples(mResult);
int columns = PQnfields(mResult);
for (int i = 0; i < rows; i++)
{
char* name = PQgetvalue(mResult, i, 0);
......@@ -781,7 +780,6 @@ bool CivPgConn::getCurves(CivCurves& curves)
}
int rows = PQntuples(mResult);
int columns = PQnfields(mResult);
for (int i = 0; i < rows; i++)
{
CivCurves::CurvesTable cTable;
......@@ -817,7 +815,8 @@ bool CivPgConn::getQuality(CivQuality& quality)
strcat_s(sql, nodeTable.thePoint.c_str());
strcat_s(sql, ",");
strcat_s(sql, nodeTable.initQuality.c_str());
strcat_s(sql, " from 节点");
strcat_s(sql, " from ");
strcat_s(sql, PIPENODE.c_str());
std::string utf8Sql = TransUTFCoding(sql);
if (!execSql(utf8Sql))
......@@ -850,7 +849,8 @@ bool CivPgConn::getTags(CivTags& tags)
return true;
}
bool CivPgConn::getFieldValueFromTable(const std::string& table,
bool CivPgConn::getFieldValueFromTable(
const std::string& table,
const std::vector<std::string>& fileds,
std::vector< std::vector<std::string>>& result)
{
......@@ -895,3 +895,52 @@ bool CivPgConn::getFieldValueFromTable(const std::string& table,
return true;
}
bool CivPgConn::getValuesFromTableByCondition(
const std::string& table,
const std::string& condition,
const std::vector<std::string>& fileds,
std::vector< std::map<std::string,std::string>>& result)
{
std::string sql;
sql.append("select ");
size_t total = fileds.size();
for (int i = 0; i < total; i++)
{
sql.append(fileds[i]);
sql.append(",");
}
sql = sql.substr(0, sql.length() - 1);
sql.append(" from public.\"");
sql.append(table);
sql.append("\" where ");
sql.append(condition);
std::string utf8Sql = TransUTFCoding(sql);
if (!execSql(utf8Sql))
{
CivSysLog::getInstance()->error(mLastError, "CivPgConn", __FUNCTION__);
return false;
}
int rows = PQntuples(mResult);
int columns = PQnfields(mResult);
for (int i = 0; i < rows; i++)
{
std::map<std::string, std::string> tempMap;
for (int j = 0; j < columns; j++)
{
std::string res = PQgetvalue(mResult, i, j);
std::string filedName = UTFTransCoding(PQfname(mResult, j));
tempMap.insert(std::pair<std::string, std::string>(filedName,res));
}
result.push_back(tempMap);
}
PQclear(mResult);
return true;
}
......@@ -86,7 +86,12 @@ public:
const std::vector<std::string>& fileds,
std::vector< std::vector<std::string>>& result) override;
bool getValuesFromTableByCondition(
const std::string& table,
const std::string& condition,
const std::vector<std::string>& fileds,
std::vector< std::map<std::string, std::string>>& result) override;
private:
PGconn* mConn; // 连接对象
PGresult* mResult; // 查询结果集
......
......@@ -293,7 +293,6 @@ public:
typedef struct TableField
{
Str Number = "本点号";
Str dFlow = "流量";
Str dHead = "水头";
Str dPressure = "压力";
Str dElevation = "标高";
......
{
"节点":["本点号","流量","水头","压力","标高","实际需水量","液位高度","水质"],
"管段":["编号","流量","流向","流速","水头损失","单位水头损","上点水头","本点水头","局部水头损","摩擦水头损","水质"],
"时段":24
}
\ No newline at end of file
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment