ToEQMDataBase.cpp 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  1. #include "ToEQMDataBase.h"
  2. #include <QJsonDocument>
  3. #include <QJsonObject>
  4. #include <QVector>
  5. ToEQMDataBase::ToEQMDataBase()
  6. {
  7. m_logger = spdlog::get("ToEQMDataBase");
  8. if(m_logger == nullptr)
  9. {
  10. SPDLOG_ERROR("ToEQMDataBase logger is nullptr");
  11. return;
  12. }
  13. }
  14. ToEQMDataBase::~ToEQMDataBase()
  15. {
  16. if(m_httpApi != nullptr)
  17. {
  18. delete m_httpApi;
  19. m_httpApi = nullptr;
  20. }
  21. }
  22. /* 初始化WebApi */
  23. bool ToEQMDataBase::initWebApi(const QString& url, const QString& serverIP, const QString& serID)
  24. {
  25. if(m_httpApi == nullptr)
  26. {
  27. m_httpApi = new lhhttpapi;
  28. }
  29. int ret = 0;
  30. ret = m_httpApi->DBQInit(url);
  31. if(ret < 0)
  32. {
  33. SPDLOG_LOGGER_ERROR(m_logger,"Init WebApi failed:{}, error Info:{}",ret,m_httpApi->DoGetLastError(&ret).toStdString());
  34. return false;
  35. }
  36. // SPDLOG_LOGGER_TRACE(m_logger,"初始化WebApi成功!");
  37. QString serverList;
  38. ret = m_httpApi->DBQGetServerList(serverList);
  39. if(ret < 0)
  40. {
  41. SPDLOG_LOGGER_DEBUG(m_logger,"Get server list failed:{}, error info:{}",ret,m_httpApi->DoGetLastError(&ret).toStdString());
  42. return false;
  43. }
  44. SPDLOG_LOGGER_TRACE(m_logger,"Server list:{}",serverList.toStdString());
  45. SPDLOG_LOGGER_DEBUG(m_logger,"WebAPI Sucess!");
  46. /* 登录,第二个参数是限制的服务 */
  47. ret = m_httpApi->DBQLogin(serverIP, serID, "SPSS", m_userToken);
  48. if(ret < 0)
  49. {
  50. SPDLOG_LOGGER_ERROR(m_logger,"Login failed:{}, error info:{}",ret,m_httpApi->DoGetLastError(&ret).toStdString());
  51. return false;
  52. }
  53. SPDLOG_LOGGER_TRACE(m_logger,"Login sucess!");
  54. return true;
  55. }
  56. /**
  57. * @brief 写入算法信息,写入tAction表
  58. *
  59. * @param vecInfo 要写入的表格数据
  60. * @param vecNowInfo 现有的标配个数据,主要是为了重复利用已经删除的自增主键而传入的
  61. * @return true
  62. * @return false
  63. */
  64. bool ToEQMDataBase::writeAlgorithmInfo(std::vector<AlgorithmInfo>& vecInfo, std::vector<AlgorithmInfo>& vecNowInfo)
  65. {
  66. if(m_httpApi == nullptr)
  67. {
  68. SPDLOG_LOGGER_ERROR(m_logger,"WebApi is nullptr");
  69. return false;
  70. }
  71. /* 取出可用的自增主键,从0开始计算 */
  72. std::list<int> listPKID;
  73. for(const auto& it : vecNowInfo)
  74. {
  75. listPKID.push_back(it.PKID);
  76. }
  77. for(const auto& it : vecInfo)
  78. {
  79. QString retStr;
  80. /* 操作名称,现在一次性将设备位置和线条信息都下载下来 */
  81. nJson json0;
  82. json0["opName"] = "SPSS_InsertToAction";
  83. nJson json1;
  84. json1["actionID"] = it.ActionID; /* 算法ID */
  85. json1["actionName"] = it.ActionName; /* 算法名称 */
  86. json1["actionTaskID"] = it.ActionTaskID; /* 算法类型 */
  87. json0["paramList"] = json1;
  88. QString strCmd = QString::fromStdString(json0.dump());
  89. int ret = m_httpApi->DBQDoInterface(enDBOperatorType::EDBOT_Insert, strCmd, retStr);
  90. if(ret < 0)
  91. {
  92. SPDLOG_LOGGER_DEBUG(m_logger,"写入tAction失败:{}, 错误信息:{}",ret,m_httpApi->DoGetLastError(&ret).toStdString());
  93. }
  94. SPDLOG_LOGGER_DEBUG(m_logger,"写入一条算法 {} 到 tAction成功!", it.ActionID);
  95. }
  96. return true;
  97. }
  98. /* 删除算法信息 */
  99. bool ToEQMDataBase::deleteAlgorithmInfo(std::vector<AlgorithmInfo>& vecDeleteInfo)
  100. {
  101. if(m_httpApi == nullptr)
  102. {
  103. SPDLOG_LOGGER_ERROR(m_logger,"WebApi is nullptr");
  104. return false;
  105. }
  106. for(const auto& it : vecDeleteInfo)
  107. {
  108. nJson json0;
  109. json0["opName"] = "SPSS_DeleteFromAction";
  110. nJson json1;
  111. json1["actionID"] = it.ActionID;
  112. json0["paramList"] = json1;
  113. QString strCmd = QString::fromStdString(json0.dump());
  114. QString strRet;
  115. int ret = m_httpApi->DBQDoInterface(enDBOperatorType::EDBOT_Delete, strCmd, strRet);
  116. if(ret < 0)
  117. {
  118. SPDLOG_LOGGER_DEBUG(m_logger,"删除tAction失败:{}, 错误信息:{}",ret,m_httpApi->DoGetLastError(&ret).toStdString());
  119. }
  120. SPDLOG_LOGGER_DEBUG(m_logger,"从tAction 删除算法 {} 成功!", it.ActionID);
  121. }
  122. return true;
  123. }
  124. /* 获取tAction数据 */
  125. bool ToEQMDataBase::getAlgorithmInfo(std::vector<AlgorithmInfo>& vecInfo)
  126. {
  127. if(m_httpApi == nullptr)
  128. {
  129. SPDLOG_LOGGER_ERROR(m_logger, "WebAPI is nullptr");
  130. return false;
  131. }
  132. /* 清空内容 */
  133. vecInfo.clear();
  134. nJson json0;
  135. json0["opName"] = "SPSS_SelectFromAction";
  136. QString strCmd = QString::fromStdString(json0.dump());
  137. QString strRet;
  138. auto ret = m_httpApi->DBQDoInterface(enDBOperatorType::EDBOT_Select, strCmd, strRet);
  139. if(ret < 0)
  140. {
  141. SPDLOG_LOGGER_DEBUG(m_logger,"获取tAction失败:{}, 错误信息:{}",ret,m_httpApi->DoGetLastError(&ret).toStdString());
  142. return false;
  143. }
  144. /* 解析获取到的JSON数据 */
  145. // SPDLOG_LOGGER_DEBUG(m_logger,"\n{}",strRet.toStdString());
  146. nJson json1 = nJson::parse(strRet.toStdString());
  147. int retCode = json1["code"].get<int>();
  148. if(retCode != 0)
  149. {
  150. SPDLOG_LOGGER_ERROR(m_logger,"获取tAction失败");
  151. return false;
  152. }
  153. nJson result = json1["result"];
  154. for(auto& it : result)
  155. {
  156. AlgorithmInfo info;
  157. info.ActionID = it["actionId"].is_null() ? "" : it["actionId"].get<std::string>();
  158. info.ActionName = it["actionName"].get<std::string>();
  159. info.ActionTaskID = it["actionTaskid"].get<int>();
  160. // SPDLOG_LOGGER_DEBUG(m_logger,"ActionID:{}, ActionName:{}, ActionTaskID:{}",info.ActionID,info.ActionName,info.ActionTaskID);
  161. vecInfo.push_back(info);
  162. }
  163. return true;
  164. }