1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
#include "CivHydrTableHelper.h"
#include "CivTableFields.h"
#include "CivCommonUtils.h"
#include "CivPgDbConnection.h"
CivHydrTableHelper::CivHydrTableHelper(const std::string& uri)
{
mConn = new CivPgDbConnection();
mConn->connect(uri);
}
CivHydrTableHelper::~CivHydrTableHelper()
{
mConn->disconnect();
delete mConn;
mConn == nullptr;
}
bool CivHydrTableHelper::mainTain()
{
//unTopoMaintain();
// 管网组件
updateAssem(TABLE_NODE, std::string("本点号"), "JD");
updateAssem(TABLE_PIPE, std::string("编号"), "GD");
updateAssem(TABLE_TANK, std::string("本点号"), "TK");
updateAssem(TABLE_VALVE, std::string("本点号"), "VA");
updateAssem(TABLE_RESERVOIR, std::string("本点号"), "RE");
updateAssem(TABLE_PUMP, std::string("本点号"), "PU");
// 方案管网组件
updateAssem(PROJ_TABLE_NODE, std::string("本点号"), "PJD");
updateAssem(PROJ_TABLE_PIPE, std::string("编号"), "PGD");
updateAssem(PROJ_TABLE_TANK, std::string("本点号"), "PTK");
updateAssem(PROJ_TABLE_VALVE, std::string("本点号"), "PVA");
updateAssem(PROJ_TABLE_RESERVOIR, std::string("本点号"), "PRE");
updateAssem(PROJ_TABLE_PUMP, std::string("本点号"), "PPU");
if (!topoMaintain())
return false;
return true;
}
void CivHydrTableHelper::updateAssem(const std::string& table, const std::string& filed,const std::string& prefix)
{
// 节点
std::vector<std::map<std::string, std::string>> resVec;
if (!mConn->query(table, { "id" }, resVec))
{
mErrInfo = mConn->getLastError();
return;
}
if (resVec.size() <= 0)
return;
std::vector<std::string> sNs;
CivCommonUtils::genSeqNo(prefix, resVec.size(), sNs);
std::string sql = "update "+ table +" set "+ filed +" = tmp."+ filed +" from(values";
for (int i = 0; i < sNs.size(); i++)
{
std::map<std::string,std::string> resMap = resVec[i];
std::string id = resMap.find("id")->second;
sql.append("(");
sql.append(id);
sql.append(",\'");
sql.append(sNs[i]);
sql.append("\'),");
}
sql = sql.substr(0, sql.length() - 1);
sql += ") as tmp(id, \""+ filed +"\") where "+ table +".id = tmp.id; ";
if (mConn && mConn->execSql(sql))
{
mErrInfo = mConn->getLastError();
return;
}
}
bool CivHydrTableHelper::topoMaintain()
{
// 管段与
topoMainTable(TABLE_PIPE, TABLE_NODE, std::string("起始节点"), std::string("本点号"), std::string("from_code"));
topoMainTable(TABLE_PIPE, TABLE_NODE, std::string("终止节点"), std::string("本点号"), std::string("to_code"));
topoMainTable(TABLE_PIPE, TABLE_RESERVOIR, std::string("起始节点"), std::string("本点号"), std::string("from_code"));
topoMainTable(TABLE_PIPE, TABLE_TANK, std::string("起始节点"), std::string("本点号"), std::string("from_code"));
topoMainTable(TABLE_PIPE, TABLE_VALVE, std::string("起始节点"), std::string("本点号"), std::string("from_code"));
topoMainTable(TABLE_PIPE, TABLE_VALVE, std::string("终止节点"), std::string("本点号"), std::string("to_code"));
topoMainTable(TABLE_PIPE, PROJ_TABLE_NODE, std::string("起始节点"), std::string("本点号"), std::string("from_code"));
topoMainTable(TABLE_PIPE, PROJ_TABLE_NODE, std::string("终止节点"), std::string("本点号"), std::string("to_code"));
topoMainTable(TABLE_PIPE, PROJ_TABLE_RESERVOIR, std::string("起始节点"), std::string("本点号"), std::string("from_code"));
topoMainTable(TABLE_PIPE, PROJ_TABLE_TANK, std::string("起始节点"), std::string("本点号"), std::string("from_code"));
topoMainTable(TABLE_PIPE, PROJ_TABLE_VALVE, std::string("起始节点"), std::string("本点号"), std::string("from_code"));
topoMainTable(TABLE_PIPE, PROJ_TABLE_VALVE, std::string("终止节点"), std::string("本点号"), std::string("to_code"));
topoMainTable(PROJ_TABLE_PIPE, TABLE_NODE, std::string("起始节点"), std::string("本点号"), std::string("from_code"));
topoMainTable(PROJ_TABLE_PIPE, TABLE_NODE, std::string("终止节点"), std::string("本点号"), std::string("to_code"));
topoMainTable(PROJ_TABLE_PIPE, TABLE_RESERVOIR, std::string("起始节点"), std::string("本点号"), std::string("from_code"));
topoMainTable(PROJ_TABLE_PIPE, TABLE_TANK, std::string("起始节点"), std::string("本点号"), std::string("from_code"));
topoMainTable(PROJ_TABLE_PIPE, TABLE_VALVE, std::string("起始节点"), std::string("本点号"), std::string("from_code"));
topoMainTable(PROJ_TABLE_PIPE, TABLE_VALVE, std::string("终止节点"), std::string("本点号"), std::string("to_code"));
topoMainTable(PROJ_TABLE_PIPE, PROJ_TABLE_NODE, std::string("起始节点"), std::string("本点号"), std::string("from_code"));
topoMainTable(PROJ_TABLE_PIPE, PROJ_TABLE_NODE, std::string("终止节点"), std::string("本点号"), std::string("to_code"));
topoMainTable(PROJ_TABLE_PIPE, PROJ_TABLE_RESERVOIR, std::string("起始节点"), std::string("本点号"), std::string("from_code"));
topoMainTable(PROJ_TABLE_PIPE, PROJ_TABLE_TANK, std::string("起始节点"), std::string("本点号"), std::string("from_code"));
topoMainTable(PROJ_TABLE_PIPE, PROJ_TABLE_VALVE, std::string("起始节点"), std::string("本点号"), std::string("from_code"));
topoMainTable(PROJ_TABLE_PIPE, PROJ_TABLE_VALVE, std::string("终止节点"), std::string("本点号"), std::string("to_code"));
// 处理方案和阀门的拓扑关系
topoLikelyPipe(TABLE_PIPE, TABLE_VALVE, std::string("起始节点"), std::string("to_code"));
topoLikelyPipe(TABLE_PIPE, TABLE_VALVE, std::string("终止节点"), std::string("from_code"));
topoLikelyPipe(PROJ_TABLE_PIPE, TABLE_VALVE, std::string("起始节点"), std::string("to_code"));
topoLikelyPipe(PROJ_TABLE_PIPE, TABLE_VALVE, std::string("终止节点"), std::string("from_code"));
topoLikelyPipe(TABLE_PIPE, PROJ_TABLE_VALVE, std::string("起始节点"), std::string("to_code"));
topoLikelyPipe(TABLE_PIPE, PROJ_TABLE_VALVE, std::string("终止节点"), std::string("from_code"));
return true;
}
bool CivHydrTableHelper::unTopoMaintain()
{
unTopoTable(TABLE_NODE, {std::string("本点号")});
unTopoTable(TABLE_PIPE, { std::string("编号"),std::string("起始节点"),std::string("终止节点") });
unTopoTable(TABLE_RESERVOIR, { std::string("本点号")});
unTopoTable(TABLE_TANK, { std::string("本点号") });
unTopoTable(TABLE_VALVE, { std::string("本点号"),std::string("起始节点"),std::string("终止节点") });
return true;
}
bool CivHydrTableHelper::topoMainTable(const std::string& sourceTable,
const std::string& targetTable,
const std::string& pipesn,
const std::string& nodeSn,
const std::string& code)
{
std::string sql = "UPDATE \"" + sourceTable + "\" \
SET \"" + pipesn + "\" = cqt.\"" + nodeSn + "\" FROM \"" + targetTable + "\" cqt WHERE \
\""+sourceTable+"\".\"" + code + "\" = cqt.\"code\"";
if (mConn && !mConn->execSql(sql))
{
mErrInfo = mConn->getLastError();
return false;
}
return true;
}
bool CivHydrTableHelper::topoLikelyPipe(const std::string& pipeTable, const std::string& nodeTable, const std::string& filed, const std::string& tocode)
{
std::string sql = "update \""+nodeTable+"\" as pu set \""+filed+"\" = \
(select pipe.\"编号\" from \""+pipeTable+"\" as pipe where pipe.\""+tocode+"\"=pu.\"code\") where exists (select 1 from \""+pipeTable+"\" as pipe1 where pipe1.\""+tocode+"\"=pu.\"code\")";
if (mConn && !mConn->execSql(sql))
{
mErrInfo = mConn->getLastError();
return false;
}
return true;
}
bool CivHydrTableHelper::topoValve(const std::string& sn,
const std::string& fromcode,
const std::string& tocode)
{
std::string sql = "update \"";
sql.append(TABLE_VALVE);
sql.append("\" set \"" + sn + "\" = dd.\"本点号\"\
from(select node.\"本点号\" \"本点号\", node.\"code\" \"nodecode\", tt.\"code\" \"vacode\" from \
\""+TABLE_NODE+"\" node, (select t2.\""+tocode+"\" \""+tocode+"\", t1.\"code\" \"code\" from \""+TABLE_PIPE+"\" t2, \""+TABLE_VALVE+"\" t1 where t2.\""+fromcode+"\" = t1.\"code\") tt\
where node.\"code\" = tt.\""+tocode+"\") dd\
where \"code\" = dd.\"vacode\"");
if (mConn && !mConn->execSql(sql))
{
mErrInfo = mConn->getLastError();
return false;
}
return false;
}
bool CivHydrTableHelper::unTopoTable(const std::string& table, const std::vector<std::string>& fields)
{
if (fields.size() < 1)
{
mErrInfo = "fileds is empty";
return false;
}
std::string sql = "update \""+ table +"\" set ";
for (int i = 0; i < fields.size(); i++)
{
sql.append(fields[i]);
sql.append("= null");
sql.append(",");
}
sql = sql.substr(0, sql.length() - 1);
if (mConn && !mConn->execSql(sql))
{
mErrInfo = mConn->getLastError();
return false;
}
return false;
}
void CivHydrTableHelper::getCodeToSn(const std::string& code, std::string& sn)
{
std::string sql = "select \"本点号\" from 节点 where code = '" + code+"'";
if (mConn && !mConn->execSql(sql))
{
mErrInfo = mConn->getLastError();
return;
}
std::vector<std::map<std::string, std::string>> res;
mConn->queryResult(res);
if (res.size() <= 0)
{
return;
}
sn = res[0].find("本点号")->second;
return;
}