CNodeDataService.cpp 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257
  1. #include "CNodeDataService.h"
  2. #include "SqlDBHelper.h"
  3. #include <QDebug>
  4. CNodeDataService::CNodeDataService(QObject *parent) { }
  5. //新增节点
  6. int CNodeDataService::AddCNodeData(const CNodeData &cNodeData)
  7. {
  8. int ret = -1;
  9. try {
  10. Transaction t(SqlDBHelper::getDatabase());
  11. InsertQuery query = t.insertInto("t_node_data(`project_id`, `eval_type`, `number`, `p_number`, `name`, "
  12. "`remark`, `create_time`, `dimension`, `type`, `is_effective`)");
  13. NonQueryResult result = query.values(cNodeData.projectId, cNodeData.indexType, cNodeData.number,
  14. cNodeData.pNumber, cNodeData.name, cNodeData.remark,
  15. QDateTime::currentDateTime().toString("yyyy-MM-dd hh:mm:ss"),
  16. cNodeData.dimension, cNodeData.type, cNodeData.isEffective)
  17. .exec();
  18. t.commit();
  19. ret = result.lastInsertId().toInt();
  20. } catch (const DBException &ex) {
  21. qDebug() << ex.lastError.text();
  22. }
  23. return ret;
  24. }
  25. bool CNodeDataService::SaveCNodeData(const CNodeData &cNodeData)
  26. {
  27. bool ret = false;
  28. try {
  29. Transaction t(SqlDBHelper::getDatabase());
  30. InsertQuery query = t.insertInto("t_node_data(`id`, `project_id`, `eval_type`, `number`, `p_number`, `name`, "
  31. "`remark`, `dimension`, `type`, `is_effective`)");
  32. NonQueryResult result = query.values(cNodeData.id, cNodeData.projectId, cNodeData.indexType, cNodeData.number,
  33. cNodeData.pNumber, cNodeData.name, cNodeData.remark, cNodeData.dimension,
  34. cNodeData.type, cNodeData.isEffective)
  35. .exec();
  36. // qDebug() << "SaveCNodeData" << result.lastQuery();
  37. t.commit();
  38. ret = true;
  39. } catch (const DBException &ex) {
  40. qDebug() << "SaveCNodeData" << ex.lastError.text();
  41. }
  42. return ret;
  43. }
  44. //更新节点信息
  45. bool CNodeDataService::UpdateCNodeData(const CNodeData &cNodeData)
  46. {
  47. bool ret = false;
  48. try {
  49. Transaction t(SqlDBHelper::getDatabase());
  50. t.update("t_node_data")
  51. .set("project_id", cNodeData.projectId)
  52. .set("eval_type", cNodeData.indexType)
  53. .set("number", cNodeData.number)
  54. .set("p_number", cNodeData.pNumber)
  55. .set("name", cNodeData.name)
  56. .set("remark", cNodeData.remark)
  57. .set("dimension", cNodeData.dimension)
  58. .set("type", cNodeData.type)
  59. .set("is_effective", cNodeData.isEffective)
  60. .set("update_time", QDateTime::currentDateTime().toString("yyyy-MM-dd hh:mm:ss"))
  61. .where("id = ?", cNodeData.id);
  62. t.commit();
  63. ret = true;
  64. } catch (const DBException &ex) {
  65. qDebug() << ex.lastError.text();
  66. }
  67. return ret;
  68. }
  69. /*根据项目id查询节点信息*/
  70. bool CNodeDataService::QueryCNodeDataById(CNodeData &cNodeData, int id)
  71. {
  72. QSqlDatabase db = SqlDBHelper::getDatabase();
  73. QSqlQuery query(db);
  74. bool ret = false;
  75. QString selectSql =
  76. QString("SELECT id,project_id, eval_type,number,p_number,name,remark,dimension,type,is_effective FROM "
  77. "t_node_data WHERE id = %1")
  78. .arg(id);
  79. if (query.exec(selectSql)) {
  80. if (query.next()) {
  81. if (query.isNull(0) == false) {
  82. cNodeData.id = query.value(0).toInt();
  83. cNodeData.projectId = query.value(1).toInt();
  84. cNodeData.indexType = query.value(1).toInt();
  85. cNodeData.number = query.value(2).toInt();
  86. cNodeData.pNumber = query.value(3).toInt();
  87. cNodeData.name = query.value(4).toString();
  88. cNodeData.remark = query.value(5).toString();
  89. cNodeData.dimension = query.value(6).toString();
  90. cNodeData.type = query.value(7).toInt();
  91. cNodeData.isEffective = query.value(8).toInt();
  92. }
  93. ret = true;
  94. }
  95. } else {
  96. qDebug() << query.lastError();
  97. }
  98. return ret;
  99. }
  100. //查询所有
  101. bool CNodeDataService::QueryAll(QList<CNodeData> &cNodeDataList, int projectId, int evalType)
  102. {
  103. QSqlDatabase db = SqlDBHelper::getDatabase();
  104. QSqlQuery query(db);
  105. bool ret = false;
  106. QString selectSql =
  107. QString("SELECT id,project_id, eval_type,number,p_number,name,remark,dimension,type,is_effective FROM "
  108. "t_node_data WHERE "
  109. "project_id = %1 and eval_type= %2 order by number")
  110. .arg(projectId)
  111. .arg(evalType);
  112. if (query.exec(selectSql)) {
  113. while (query.next()) {
  114. if (query.isNull(0) == false) {
  115. CNodeData cNodeData;
  116. cNodeData.id = query.value(0).toInt();
  117. cNodeData.projectId = query.value(1).toInt();
  118. cNodeData.indexType = query.value(2).toInt();
  119. cNodeData.number = query.value(3).toInt();
  120. cNodeData.pNumber = query.value(4).toInt();
  121. cNodeData.name = query.value(5).toString();
  122. cNodeData.remark = query.value(6).toString();
  123. cNodeData.dimension = query.value(7).toString();
  124. cNodeData.type = query.value(8).toInt();
  125. cNodeData.isEffective = query.value(9).toInt();
  126. cNodeDataList.append(cNodeData);
  127. }
  128. }
  129. ret = true;
  130. } else {
  131. qDebug() << "QueryAll" << query.lastError();
  132. }
  133. return ret;
  134. }
  135. bool CNodeDataService::QueryAllValid(QList<CNodeData> &cNodeDataList, int projectId, int evalType)
  136. {
  137. QSqlDatabase db = SqlDBHelper::getDatabase();
  138. QSqlQuery query(db);
  139. bool ret = false;
  140. QString selectSql =
  141. QString("SELECT id,project_id, eval_type,number,p_number,name,remark,dimension,type,is_effective FROM "
  142. "t_node_data WHERE "
  143. "project_id = %1 and eval_type= %2 order by number")
  144. .arg(projectId)
  145. .arg(evalType);
  146. QList<CNodeData> tempList;
  147. if (query.exec(selectSql)) {
  148. while (query.next()) {
  149. if (query.isNull(0) == false) {
  150. CNodeData cNodeData;
  151. cNodeData.id = query.value(0).toInt();
  152. cNodeData.projectId = query.value(1).toInt();
  153. cNodeData.indexType = query.value(2).toInt();
  154. cNodeData.number = query.value(3).toInt();
  155. cNodeData.pNumber = query.value(4).toInt();
  156. cNodeData.name = query.value(5).toString();
  157. cNodeData.remark = query.value(6).toString();
  158. cNodeData.dimension = query.value(7).toString();
  159. cNodeData.type = query.value(8).toInt();
  160. cNodeData.isEffective = query.value(9).toInt();
  161. tempList.append(cNodeData);
  162. }
  163. }
  164. ret = true;
  165. CMind mind;
  166. mind.setNodeList(tempList);
  167. for (CNodeData node : tempList) {
  168. if (mind.isInvalidated(node.number) == false) {
  169. cNodeDataList.append(node);
  170. }
  171. }
  172. } else {
  173. qDebug() << "QueryAllValid" << query.lastError();
  174. }
  175. return ret;
  176. }
  177. bool CNodeDataService::QueryAllByProjectId(QList<CNodeData> &cNodeDataList, int projectId)
  178. {
  179. QSqlDatabase db = SqlDBHelper::getDatabase();
  180. QSqlQuery query(db);
  181. bool ret = false;
  182. QString selectSql =
  183. QString("SELECT id,project_id, eval_type,number,p_number,name,remark,dimension,type,is_effective FROM "
  184. "t_node_data WHERE "
  185. "project_id = %1 ")
  186. .arg(projectId);
  187. if (query.exec(selectSql)) {
  188. while (query.next()) {
  189. if (query.isNull(0) == false) {
  190. CNodeData cNodeData;
  191. cNodeData.id = query.value(0).toInt();
  192. cNodeData.projectId = query.value(1).toInt();
  193. cNodeData.indexType = query.value(2).toInt();
  194. cNodeData.number = query.value(3).toInt();
  195. cNodeData.pNumber = query.value(4).toInt();
  196. cNodeData.name = query.value(5).toString();
  197. cNodeData.remark = query.value(6).toString();
  198. cNodeData.dimension = query.value(7).toString();
  199. cNodeData.type = query.value(8).toInt();
  200. cNodeData.isEffective = query.value(9).toInt();
  201. cNodeDataList.append(cNodeData);
  202. }
  203. }
  204. ret = true;
  205. } else {
  206. qDebug() << query.lastError();
  207. }
  208. return ret;
  209. }
  210. //根据id删除
  211. bool CNodeDataService::DeleteCNodeDataById(int id)
  212. {
  213. bool ret = false;
  214. try {
  215. qDebug() << __FUNCTION__ << __LINE__
  216. << SqlDBHelper::getDatabase().driver()->hasFeature(QSqlDriver::Transactions) << endl;
  217. Transaction t(SqlDBHelper::getDatabase());
  218. t.deleteFrom("t_node_data").where("id = ?", id);
  219. t.commit();
  220. ret = true;
  221. } catch (const DBException &ex) {
  222. qDebug() << ex.lastError.text();
  223. }
  224. return ret;
  225. }
  226. //根据id删除
  227. bool CNodeDataService::DeleteAllNodeData()
  228. {
  229. QSqlDatabase db = SqlDBHelper::getDatabase();
  230. QSqlQuery query(db);
  231. bool ret = false;
  232. QString deleteSql = QString("DELETE FROM t_node_data ");
  233. if (query.exec(deleteSql)) {
  234. ret = true;
  235. qDebug() << "t_node_data deleteSql success!";
  236. } else {
  237. qDebug() << query.lastError();
  238. }
  239. return ret;
  240. }