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
#include "CivSimulResultCache.h"
#include"CivSysLog.h"
void CivSimulResultCache::addNodeQuality(float quality, int interval, Str sNo)
{
if (mNodeItemsMap.find(interval) == mNodeItemsMap.end())
return;
auto nodeMap = mNodeItemsMap.find(interval)->second;
if (nodeMap.find(sNo) == nodeMap.end())
return;
mNodeItemsMap[interval][sNo].dQuality = quality;
}
void CivSimulResultCache::addLinkQuality(float quality, int interval, Str sNo)
{
if (mLinkItemsMap.find(interval) == mLinkItemsMap.end())
return;
auto linkMap = mLinkItemsMap.find(interval)->second;
if (linkMap.find(sNo) == linkMap.end())
return;
mLinkItemsMap[interval][sNo].dQuality = quality;
}
void CivSimulResultCache::addNodeItems(const NodeResultItem& nodeItem)
{
mNodeItemsMap[nodeItem.dInterval][nodeItem.szNo] = nodeItem;
}
void CivSimulResultCache::addLinkItems(const LinkResultItem& linkItem)
{
mLinkItemsMap[linkItem.dInterval][linkItem.szNo] = linkItem;
}
bool CivSimulResultCache::updateToDb(CivDbConn* dbConn)
{
CivSysLog::getInstance()->error("开始写入数据库", "CivSimulResultCache", __FUNCTION__);
if (dbConn == nullptr)
{
CivSysLog::getInstance()->error("dbConn == nullptr", "CivSimulResultCache", __FUNCTION__);
return false;
}
for (auto iter = mNodeItemsMap.begin(); iter != mNodeItemsMap.end(); iter++)
{
NodeResultItems nodeItems = iter->second;
if (!dbConn->updateNode(nodeItems))
{
CivSysLog::getInstance()->error("存储节点数据失败", "CivSimulResultCache", __FUNCTION__);
}
}
for (auto iter = mLinkItemsMap.begin(); iter != mLinkItemsMap.end(); iter++)
{
LinkResultItems nodeItems = iter->second;
if (!dbConn->updateLink(nodeItems))
{
CivSysLog::getInstance()->error("存储管段数据失败", "CivSimulResultCache", __FUNCTION__);
}
}
CivSysLog::getInstance()->error("写入数据库完成", "CivSimulResultCache", __FUNCTION__);
return true;
}
bool CivSimulResultCache::updateToDb(CivDbConnection* dbConn)
{
CivSysLog::getInstance()->error("开始写入数据库", "CivSimulResultCache", __FUNCTION__);
if (dbConn == nullptr)
{
CivSysLog::getInstance()->error("dbConn == nullptr", "CivSimulResultCache", __FUNCTION__);
return false;
}
for (auto iter = mNodeItemsMap.begin(); iter != mNodeItemsMap.end(); iter++)
{
NodeResultItems nodeItems = iter->second;
if (!dbConn->updateNode(nodeItems))
{
CivSysLog::getInstance()->error("存储节点数据失败", "CivSimulResultCache", __FUNCTION__);
}
}
for (auto iter = mLinkItemsMap.begin(); iter != mLinkItemsMap.end(); iter++)
{
LinkResultItems nodeItems = iter->second;
if (!dbConn->updateLink(nodeItems))
{
CivSysLog::getInstance()->error("存储管段数据失败", "CivSimulResultCache", __FUNCTION__);
}
}
CivSysLog::getInstance()->error("写入数据库完成", "CivSimulResultCache", __FUNCTION__);
clear();
return true;
}
void CivSimulResultCache::getResultByTime(int time, NodeResultItems& nodeItems, LinkResultItems& linkItems)
{
nodeItems.clear();
linkItems.clear();
auto nodeIter = mNodeItemsMap.find(time);
auto linkIter = mLinkItemsMap.find(time);
if (nodeIter == mNodeItemsMap.end() || linkIter == mLinkItemsMap.end())
{
CivSysLog::getInstance()->error("存储数据出错了,找不到该对应时段的值", "CivSimulResultCache", __FUNCTION__);
return;
}
// 避免发生异常
try
{
nodeItems = nodeIter->second;
linkItems = linkIter->second;
}
catch (const std::exception& e)
{
CivSysLog::getInstance()->error(e.what(), "CivSimulResultCache", __FUNCTION__);
}
}
void CivSimulResultCache::clear()
{
mLinkItemsMap.clear();
mNodeItemsMap.clear();
}