DemandWeightService.cpp 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533
  1. #include "DemandWeightService.h"
  2. #include "SqlDBHelper.h"
  3. #include <QDebug>
  4. DemandWeightService::DemandWeightService(QObject *parent) { }
  5. bool DemandWeightService::AddNodeWeightInfo(const DemandWeight &demandWeight)
  6. {
  7. bool ret = false;
  8. try {
  9. Transaction t(SqlDBHelper::getDatabase());
  10. InsertQuery q = t.insertInto("t_demand_weight (engineer_id,expert_id, node_name, node_weight, "
  11. "node_value,table_index )");
  12. q.values(demandWeight.engineerId, demandWeight.expertId, demandWeight.nodeName, demandWeight.nodeWeight,
  13. demandWeight.nodeValue, demandWeight.tableIndex)
  14. .exec();
  15. t.commit();
  16. ret = true;
  17. } catch (const DBException &ex) {
  18. qDebug() << ex.lastError.text();
  19. }
  20. return ret;
  21. }
  22. /*批量节点信息新增*/
  23. bool DemandWeightService::AddNodeWeightInfoList(const QList<DemandWeight *> &demandWeightList)
  24. {
  25. bool ret = false;
  26. try {
  27. Transaction t(SqlDBHelper::getDatabase());
  28. for (int i = 0; i < demandWeightList.length(); i++) {
  29. DemandWeight *demandWeight = demandWeightList.at(i);
  30. InsertQuery q = t.insertInto("t_demand_weight (engineer_id,expert_id, node_name, node_weight, "
  31. "node_value,table_index,table_msg,is_valid,page_index )");
  32. q.values(demandWeight->engineerId, demandWeight->expertId, demandWeight->nodeName, demandWeight->nodeWeight,
  33. demandWeight->nodeValue, demandWeight->tableIndex, demandWeight->tableMsg, demandWeight->isValid,
  34. demandWeight->pageIndex)
  35. .exec();
  36. t.commit();
  37. }
  38. ret = true;
  39. } catch (const DBException &ex) {
  40. qDebug() << ex.lastError.text();
  41. }
  42. return ret;
  43. }
  44. /*修改节点值*/
  45. bool DemandWeightService::UpdateNodeValue(const DemandWeight &demandWeight)
  46. {
  47. bool ret = false;
  48. try {
  49. Transaction t(SqlDBHelper::getDatabase());
  50. t.update("t_demand_weight")
  51. .set("NODE_VALUE", demandWeight.nodeValue)
  52. .set("NODE_WEIGHT", demandWeight.nodeWeight)
  53. .where("ENGINEER_ID = ? and expert_id = ? and node_name = ? ", demandWeight.engineerId,
  54. demandWeight.expertId, demandWeight.nodeName);
  55. t.commit();
  56. ret = true;
  57. } catch (const DBException &ex) {
  58. qDebug() << ex.lastError.text();
  59. }
  60. return ret;
  61. }
  62. bool DemandWeightService::UpdateNodeValueList(const QList<DemandWeight *> demandWeightList)
  63. {
  64. QSqlDatabase db = SqlDBHelper::getDatabase();
  65. QSqlQuery query(db);
  66. bool ret = false;
  67. for (int i = 0; i < demandWeightList.length(); i++) {
  68. DemandWeight *demandWeight = demandWeightList.at(i);
  69. QString updateSql = QString("UPDATE t_demand_weight SET NODE_VALUE ='%1' , NODE_WEIGHT = '%2' "
  70. "WHERE ENGINEER_ID =%3 AND expert_id =%4"
  71. " AND node_name = '%5' AND table_index =%6 and table_msg ='%7' and page_index=%8")
  72. .arg(demandWeight->nodeValue)
  73. .arg(demandWeight->nodeWeight)
  74. .arg(demandWeight->engineerId)
  75. .arg(demandWeight->expertId)
  76. .arg(demandWeight->nodeName)
  77. .arg(demandWeight->tableIndex)
  78. .arg(demandWeight->tableMsg)
  79. .arg(demandWeight->pageIndex);
  80. // qDebug() << updateSql;
  81. query.exec(updateSql);
  82. ret = true;
  83. }
  84. return ret;
  85. }
  86. bool DemandWeightService::QueryByTableIndexAndTableMsg(int expertId, int engineerId, int tableIndex, QString tableMsg)
  87. {
  88. QSqlDatabase db = SqlDBHelper::getDatabase();
  89. QSqlQuery query(db);
  90. bool ret = false;
  91. QString selectSql = QString("select * from t_demand_weight where expert_id "
  92. "=%1 and engineer_id =%2 and table_index=%3 and table_msg='%4'")
  93. .arg(QString::number(expertId))
  94. .arg(QString::number(engineerId))
  95. .arg(QString::number(tableIndex))
  96. .arg(tableMsg);
  97. // qDebug() << "sql===" << selectSql;
  98. if (query.exec(selectSql)) {
  99. if (query.next()) {
  100. ret = true;
  101. }
  102. } else {
  103. qDebug() << query.lastError();
  104. }
  105. return ret;
  106. }
  107. bool DemandWeightService::QueryByTableIndexAndTableMsg(QString expertId, int engineerId, int tableIndex,
  108. QString tableMsg)
  109. {
  110. QSqlDatabase db = SqlDBHelper::getDatabase();
  111. QSqlQuery query(db);
  112. bool ret = false;
  113. QString selectSql = QString("select * from t_demand_weight where expert_id "
  114. "='%1' and engineer_id =%2 and table_index=%3 and table_msg='%4'")
  115. .arg(expertId)
  116. .arg(QString::number(engineerId))
  117. .arg(QString::number(tableIndex))
  118. .arg(tableMsg);
  119. // qDebug() << "sql===" << selectSql;
  120. if (query.exec(selectSql)) {
  121. if (query.next()) {
  122. ret = true;
  123. }
  124. } else {
  125. qDebug() << query.lastError();
  126. }
  127. return ret;
  128. }
  129. bool DemandWeightService::QueryByTableIndexAndTableMsgAndPage(QString expertId, int engineerId, int tableIndex,
  130. QString tableMsg, int page)
  131. {
  132. QSqlDatabase db = SqlDBHelper::getDatabase();
  133. QSqlQuery query(db);
  134. bool ret = false;
  135. QString selectSql = QString("select * from t_demand_weight where expert_id "
  136. "='%1' and engineer_id =%2 and table_index=%3 and table_msg='%4' and page_index=%5")
  137. .arg(expertId)
  138. .arg(QString::number(engineerId))
  139. .arg(QString::number(tableIndex))
  140. .arg(tableMsg)
  141. .arg(page);
  142. // qDebug() << "sql===" << selectSql;
  143. if (query.exec(selectSql)) {
  144. if (query.next()) {
  145. ret = true;
  146. }
  147. } else {
  148. qDebug() << query.lastError();
  149. }
  150. return ret;
  151. }
  152. bool DemandWeightService::QueryByTableIndexAndTableMsg(QList<DemandWeight *> *demandWeightList, int expertId,
  153. int engineerId, int tableIndex, QString tableMsg)
  154. {
  155. QSqlDatabase db = SqlDBHelper::getDatabase();
  156. QSqlQuery query(db);
  157. bool ret = false;
  158. QString selectSql = QString("select * from t_demand_weight where expert_id "
  159. "=%1 and engineer_id =%2 and table_index=%3 and table_msg='%4'")
  160. .arg(QString::number(expertId))
  161. .arg(QString::number(engineerId))
  162. .arg(QString::number(tableIndex))
  163. .arg(tableMsg);
  164. // qDebug() << "sql=" << selectSql;
  165. if (query.exec(selectSql)) {
  166. while (query.next()) {
  167. if (query.isNull(0) == false) {
  168. DemandWeight *demandWeight = new DemandWeight();
  169. demandWeight->id = query.value(0).toInt();
  170. demandWeight->engineerId = query.value(1).toInt();
  171. demandWeight->expertId = query.value(2).toInt();
  172. demandWeight->nodeName = query.value(3).toString();
  173. demandWeight->nodeValue = query.value(4).toDouble();
  174. demandWeight->nodeWeight = query.value(5).toDouble();
  175. demandWeight->tableIndex = query.value(6).toInt();
  176. demandWeight->tableMsg = query.value(7).toString();
  177. demandWeightList->append(demandWeight);
  178. }
  179. ret = true;
  180. }
  181. } else {
  182. qDebug() << query.lastError();
  183. }
  184. return ret;
  185. }
  186. bool DemandWeightService::QueryByTableIndexAndTableMsg(QList<DemandWeight *> *demandWeightList, QString expertId,
  187. int engineerId, int tableIndex, QString tableMsg)
  188. {
  189. QSqlDatabase db = SqlDBHelper::getDatabase();
  190. QSqlQuery query(db);
  191. bool ret = false;
  192. QString selectSql = QString("select * from t_demand_weight where expert_id "
  193. "='%1' and engineer_id =%2 and table_index=%3 and table_msg='%4'")
  194. .arg(expertId)
  195. .arg(QString::number(engineerId))
  196. .arg(QString::number(tableIndex))
  197. .arg(tableMsg);
  198. // qDebug() << "sql=" << selectSql;
  199. if (query.exec(selectSql)) {
  200. while (query.next()) {
  201. if (query.isNull(0) == false) {
  202. DemandWeight *demandWeight = new DemandWeight();
  203. demandWeight->id = query.value(0).toInt();
  204. demandWeight->engineerId = query.value(1).toInt();
  205. demandWeight->expertId = query.value(2).toInt();
  206. demandWeight->nodeName = query.value(3).toString();
  207. demandWeight->nodeValue = query.value(4).toDouble();
  208. demandWeight->nodeWeight = query.value(5).toDouble();
  209. demandWeight->tableIndex = query.value(6).toInt();
  210. demandWeight->tableMsg = query.value(7).toString();
  211. demandWeightList->append(demandWeight);
  212. }
  213. ret = true;
  214. }
  215. } else {
  216. qDebug() << query.lastError();
  217. }
  218. return ret;
  219. }
  220. bool DemandWeightService::QueryByPageIndexAndTableMsg(QList<DemandWeight *> *demandWeightList, QString expertId,
  221. int engineerId, int pageIndex, QString tableMsg)
  222. {
  223. QSqlDatabase db = SqlDBHelper::getDatabase();
  224. QSqlQuery query(db);
  225. bool ret = false;
  226. QString selectSql = QString("select * from t_demand_weight where expert_id "
  227. "='%1' and engineer_id =%2 and page_index=%3 and table_msg='%4'")
  228. .arg(expertId)
  229. .arg(QString::number(engineerId))
  230. .arg(QString::number(pageIndex))
  231. .arg(tableMsg);
  232. // qDebug() << "sql=" << selectSql;
  233. if (query.exec(selectSql)) {
  234. while (query.next()) {
  235. if (query.isNull(0) == false) {
  236. DemandWeight *demandWeight = new DemandWeight();
  237. demandWeight->id = query.value(0).toInt();
  238. demandWeight->engineerId = query.value(1).toInt();
  239. demandWeight->expertId = query.value(2).toInt();
  240. demandWeight->nodeName = query.value(3).toString();
  241. demandWeight->nodeValue = query.value(4).toDouble();
  242. demandWeight->nodeWeight = query.value(5).toDouble();
  243. demandWeight->tableIndex = query.value(6).toInt();
  244. demandWeight->tableMsg = query.value(7).toString();
  245. demandWeightList->append(demandWeight);
  246. }
  247. ret = true;
  248. }
  249. } else {
  250. qDebug() << query.lastError();
  251. }
  252. return ret;
  253. }
  254. bool DemandWeightService::updateValidByExperIdAndEngineerId(int expertId, int engineerId)
  255. {
  256. QSqlDatabase db = SqlDBHelper::getDatabase();
  257. QSqlQuery query(db);
  258. bool ret = false;
  259. try {
  260. QString updateSql = QString("UPDATE t_demand_weight SET is_valid =1 "
  261. "WHERE ENGINEER_ID =%1 AND expert_id =%2")
  262. .arg(engineerId)
  263. .arg(expertId);
  264. // qDebug() << updateSql;
  265. query.exec(updateSql);
  266. ret = true;
  267. } catch (const DBException &ex) {
  268. qDebug() << ex.lastError.text();
  269. }
  270. return ret;
  271. }
  272. bool DemandWeightService::QueryFirstDemandWeightByEngineerId(QList<DemandWeight *> *demandWeightList, int expertId,
  273. int engineerId, QString tableMsg)
  274. {
  275. QSqlDatabase db = SqlDBHelper::getDatabase();
  276. QSqlQuery query(db);
  277. bool ret = false;
  278. QString selectSql = QString("select * from t_demand_weight where expert_id "
  279. "=%1 and engineer_id =%2 and table_msg = "
  280. "'%3' and table_index = 0")
  281. .arg(QString::number(expertId))
  282. .arg(QString::number(engineerId))
  283. .arg(tableMsg);
  284. // qDebug() << "sql=" << selectSql;
  285. if (query.exec(selectSql)) {
  286. while (query.next()) {
  287. if (query.isNull(0) == false) {
  288. DemandWeight *demandWeight = new DemandWeight();
  289. demandWeight->id = query.value(0).toInt();
  290. demandWeight->engineerId = query.value(1).toInt();
  291. demandWeight->expertId = query.value(2).toInt();
  292. demandWeight->nodeName = query.value(3).toString();
  293. demandWeight->nodeValue = query.value(4).toDouble();
  294. demandWeight->nodeWeight = query.value(5).toDouble();
  295. demandWeight->tableIndex = query.value(6).toInt();
  296. demandWeightList->append(demandWeight);
  297. }
  298. ret = true;
  299. }
  300. } else {
  301. qDebug() << query.lastError();
  302. }
  303. return ret;
  304. }
  305. bool DemandWeightService::QueryLastDemandWeightByEngineerId(QList<DemandWeight *> *demandWeightList, int expertId,
  306. int engineerId, QString tableMsg, int tableIndex)
  307. {
  308. QSqlDatabase db = SqlDBHelper::getDatabase();
  309. QSqlQuery query(db);
  310. bool ret = false;
  311. QString selectSql = QString("select * from t_demand_weight where expert_id "
  312. "=%1 and engineer_id =%2 and table_msg = "
  313. "'%3' and table_index = %4")
  314. .arg(QString::number(expertId))
  315. .arg(QString::number(engineerId))
  316. .arg(tableMsg)
  317. .arg(QString::number(tableIndex));
  318. qDebug() << "sql=" << selectSql;
  319. if (query.exec(selectSql)) {
  320. while (query.next()) {
  321. if (query.isNull(0) == false) {
  322. DemandWeight *demandWeight = new DemandWeight();
  323. demandWeight->id = query.value(0).toInt();
  324. demandWeight->engineerId = query.value(1).toInt();
  325. demandWeight->expertId = query.value(2).toInt();
  326. demandWeight->nodeName = query.value(3).toString();
  327. demandWeight->nodeValue = query.value(4).toDouble();
  328. demandWeight->nodeWeight = query.value(5).toDouble();
  329. demandWeight->tableIndex = query.value(6).toInt();
  330. demandWeightList->append(demandWeight);
  331. }
  332. ret = true;
  333. }
  334. } else {
  335. qDebug() << query.lastError();
  336. }
  337. return ret;
  338. }
  339. bool DemandWeightService::QueryFirstDemandWeightByEngineerId(QList<DemandWeight *> *demandWeightList, QString expertId,
  340. int engineerId, QString tableMsg)
  341. {
  342. QSqlDatabase db = SqlDBHelper::getDatabase();
  343. QSqlQuery query(db);
  344. bool ret = false;
  345. QString selectSql = QString("select * from t_demand_weight where expert_id "
  346. "='%1' and engineer_id =%2 and table_msg = "
  347. "'%3' and table_index = 0")
  348. .arg(expertId)
  349. .arg(QString::number(engineerId))
  350. .arg(tableMsg);
  351. // qDebug() << "sql=" << selectSql;
  352. if (query.exec(selectSql)) {
  353. while (query.next()) {
  354. if (query.isNull(0) == false) {
  355. DemandWeight *demandWeight = new DemandWeight();
  356. demandWeight->id = query.value(0).toInt();
  357. demandWeight->engineerId = query.value(1).toInt();
  358. demandWeight->expertId = query.value(2).toInt();
  359. demandWeight->nodeName = query.value(3).toString();
  360. demandWeight->nodeValue = query.value(4).toDouble();
  361. demandWeight->nodeWeight = query.value(5).toDouble();
  362. demandWeight->tableIndex = query.value(6).toInt();
  363. demandWeightList->append(demandWeight);
  364. }
  365. ret = true;
  366. }
  367. } else {
  368. qDebug() << query.lastError();
  369. }
  370. return ret;
  371. }
  372. bool DemandWeightService::QueryLastPageDemandWeightByEngineerId(QList<DemandWeight *> *demandWeightList,
  373. QString expertId, int engineerId, QString tableMsg,
  374. int page)
  375. {
  376. QSqlDatabase db = SqlDBHelper::getDatabase();
  377. QSqlQuery query(db);
  378. bool ret = false;
  379. QString selectSql = QString("select * from t_demand_weight where expert_id "
  380. "='%1' and engineer_id =%2 and table_msg = "
  381. "'%3' and page_index = %4")
  382. .arg(expertId)
  383. .arg(QString::number(engineerId))
  384. .arg(tableMsg)
  385. .arg(page);
  386. // qDebug() << "sql=" << selectSql;
  387. if (query.exec(selectSql)) {
  388. while (query.next()) {
  389. if (query.isNull(0) == false) {
  390. DemandWeight *demandWeight = new DemandWeight();
  391. demandWeight->id = query.value(0).toInt();
  392. demandWeight->engineerId = query.value(1).toInt();
  393. demandWeight->expertId = query.value(2).toInt();
  394. demandWeight->nodeName = query.value(3).toString();
  395. demandWeight->nodeValue = query.value(4).toDouble();
  396. demandWeight->nodeWeight = query.value(5).toDouble();
  397. demandWeight->tableIndex = query.value(6).toInt();
  398. demandWeightList->append(demandWeight);
  399. }
  400. ret = true;
  401. }
  402. } else {
  403. qDebug() << query.lastError();
  404. }
  405. return ret;
  406. }
  407. bool DemandWeightService::QuerySecondDemandWeightByEngineerId(QList<DemandWeight *> *demandWeightList, int expertId,
  408. int engineerId, QString tableMsg)
  409. {
  410. QSqlDatabase db = SqlDBHelper::getDatabase();
  411. QSqlQuery query(db);
  412. bool ret = false;
  413. QString selectSql = QString("select * from t_demand_weight where expert_id "
  414. "=%1 and engineer_id =%2 and table_msg = "
  415. "'%3' and table_index != 0")
  416. .arg(QString::number(expertId))
  417. .arg(QString::number(engineerId))
  418. .arg(tableMsg);
  419. // qDebug() << "sql=" << selectSql;
  420. if (query.exec(selectSql)) {
  421. while (query.next()) {
  422. if (query.isNull(0) == false) {
  423. DemandWeight *demandWeight = new DemandWeight();
  424. demandWeight->id = query.value(0).toInt();
  425. demandWeight->engineerId = query.value(1).toInt();
  426. demandWeight->expertId = query.value(2).toInt();
  427. demandWeight->nodeName = query.value(3).toString();
  428. demandWeight->nodeValue = query.value(4).toDouble();
  429. demandWeight->nodeWeight = query.value(5).toDouble();
  430. demandWeight->tableIndex = query.value(6).toInt();
  431. demandWeightList->append(demandWeight);
  432. }
  433. ret = true;
  434. }
  435. } else {
  436. qDebug() << query.lastError();
  437. }
  438. return ret;
  439. }
  440. bool DemandWeightService::QuerySecondDemandWeightByEngineerIdAndMaxPage(QList<DemandWeight *> *demandWeightList,
  441. int expertId, int engineerId, QString tableMsg)
  442. {
  443. QSqlDatabase db = SqlDBHelper::getDatabase();
  444. QSqlQuery query(db);
  445. bool ret = false;
  446. QString selectSql = QString("select * from t_demand_weight where expert_id "
  447. "=%1 and engineer_id =%2 and table_msg = "
  448. "'%3' and page_index = (select max(page_index) from t_demand_weight where expert_id "
  449. "=%1 and engineer_id =%2 and table_msg = '%3' ) order by table_index ")
  450. .arg(QString::number(expertId))
  451. .arg(QString::number(engineerId))
  452. .arg(tableMsg);
  453. // qDebug() << "sql=" << selectSql;
  454. if (query.exec(selectSql)) {
  455. while (query.next()) {
  456. if (query.isNull(0) == false) {
  457. DemandWeight *demandWeight = new DemandWeight();
  458. demandWeight->id = query.value(0).toInt();
  459. demandWeight->engineerId = query.value(1).toInt();
  460. demandWeight->expertId = query.value(2).toInt();
  461. demandWeight->nodeName = query.value(3).toString();
  462. demandWeight->nodeValue = query.value(4).toDouble();
  463. demandWeight->nodeWeight = query.value(5).toDouble();
  464. demandWeight->tableIndex = query.value(6).toInt();
  465. demandWeightList->append(demandWeight);
  466. }
  467. ret = true;
  468. }
  469. } else {
  470. qDebug() << query.lastError();
  471. }
  472. return ret;
  473. }
  474. bool DemandWeightService::QuerySecondDemandWeightByEngineerId(QList<DemandWeight *> *demandWeightList, QString expertId,
  475. int engineerId, QString tableMsg)
  476. {
  477. QSqlDatabase db = SqlDBHelper::getDatabase();
  478. QSqlQuery query(db);
  479. bool ret = false;
  480. QString selectSql = QString("select * from t_demand_weight where expert_id "
  481. "='%1' and engineer_id =%2 and table_msg = "
  482. "'%3' and table_index != 0")
  483. .arg(expertId)
  484. .arg(QString::number(engineerId))
  485. .arg(tableMsg);
  486. // qDebug() << "sql=" << selectSql;
  487. if (query.exec(selectSql)) {
  488. while (query.next()) {
  489. if (query.isNull(0) == false) {
  490. DemandWeight *demandWeight = new DemandWeight();
  491. demandWeight->id = query.value(0).toInt();
  492. demandWeight->engineerId = query.value(1).toInt();
  493. demandWeight->expertId = query.value(2).toInt();
  494. demandWeight->nodeName = query.value(3).toString();
  495. demandWeight->nodeValue = query.value(4).toDouble();
  496. demandWeight->nodeWeight = query.value(5).toDouble();
  497. demandWeight->tableIndex = query.value(6).toInt();
  498. demandWeightList->append(demandWeight);
  499. }
  500. ret = true;
  501. }
  502. } else {
  503. qDebug() << query.lastError();
  504. }
  505. return ret;
  506. }