UserConfigService.cpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306
  1. #include "UserConfigService.h"
  2. #include "SqlDBHelper.h"
  3. #include <QDebug>
  4. UserConfigService::UserConfigService(QObject *parent) { }
  5. //////////////////////用户工程配置-start////////////////////
  6. int UserConfigService::AddUserConfig(const UserConfig &cfg)
  7. {
  8. int returnId = -1;
  9. try {
  10. Transaction t(SqlDBHelper::getDatabase());
  11. InsertQuery query = t.insertInto("t_user_config (user_id, engineer_id, "
  12. "weight, create_time, update_time,user_name, is_valid)");
  13. NonQueryResult result = query.values(cfg.userId, cfg.engineerId, cfg.weight, cfg.createTime, cfg.updateTime,
  14. cfg.userName, cfg.isValid)
  15. .exec();
  16. t.commit();
  17. returnId = result.lastInsertId().toInt();
  18. } catch (const DBException &ex) {
  19. qDebug() << ex.lastError.text();
  20. returnId = -1;
  21. }
  22. return returnId;
  23. }
  24. //添加配置信息
  25. bool UserConfigService::AddUserConfigList(QList<UserConfig *> cfgList)
  26. {
  27. bool ret = false;
  28. try {
  29. Transaction t(SqlDBHelper::getDatabase());
  30. for (auto cfg : cfgList) {
  31. InsertQuery query = t.insertInto("t_user_config (user_id, engineer_id, "
  32. "weight, create_time, update_time,user_name)");
  33. NonQueryResult result = query.values(cfg->userId, cfg->engineerId, cfg->weight * 100, cfg->createTime,
  34. cfg->updateTime, cfg->userName)
  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. int UserConfigService::QueryUserWeightSum(int engineerId)
  45. {
  46. int returnId = -1;
  47. try {
  48. Transaction t(SqlDBHelper::getDatabase());
  49. QString selectSql = QString("SELECT sum(weight) from t_user_config "
  50. " WHERE engineer_id = %1 and is_valid =1")
  51. .arg(engineerId);
  52. QueryResult queryResult = t.execQuery(selectSql);
  53. if (queryResult.next()) {
  54. returnId = queryResult.value(0).toInt();
  55. }
  56. } catch (const DBException &ex) {
  57. qDebug() << ex.lastError.text();
  58. returnId = -1;
  59. }
  60. return returnId;
  61. }
  62. int UserConfigService::QueryUserWeightSum(int engineerId, int userId)
  63. {
  64. int returnId = -1;
  65. try {
  66. Transaction t(SqlDBHelper::getDatabase());
  67. QString selectSql = QString("SELECT sum(weight) from t_user_config "
  68. " WHERE engineer_id = %1 and user_id != %2 and is_valid =1")
  69. .arg(engineerId)
  70. .arg(userId);
  71. QueryResult queryResult = t.execQuery(selectSql);
  72. if (queryResult.next()) {
  73. returnId = queryResult.value(0).toInt();
  74. }
  75. } catch (const DBException &ex) {
  76. qDebug() << ex.lastError.text();
  77. returnId = -1;
  78. }
  79. return returnId;
  80. }
  81. bool UserConfigService::UpdateUserConfig(const UserConfig &cfg)
  82. {
  83. bool ret = false;
  84. try {
  85. Transaction t(SqlDBHelper::getDatabase());
  86. t.update("t_user_config")
  87. .set("user_id", cfg.userId)
  88. .set("engineer_id", cfg.engineerId)
  89. .set("weight", cfg.weight)
  90. .set("user_name", cfg.userName)
  91. .set("update_time", QDateTime::currentDateTime().toString("yyyy.MM.dd hh:mm:ss.zzz"))
  92. .where("id=?", cfg.id);
  93. t.commit();
  94. ret = true;
  95. } catch (const DBException &ex) {
  96. qDebug() << ex.lastError.text();
  97. }
  98. return ret;
  99. }
  100. bool UserConfigService::UpdateUserValid(int userId, int flag)
  101. {
  102. bool ret = false;
  103. try {
  104. Transaction t(SqlDBHelper::getDatabase());
  105. t.update("t_user_config")
  106. .set("is_valid", flag)
  107. .set("update_time", QDateTime::currentDateTime().toString("yyyy.MM.dd hh:mm:ss.zzz"))
  108. .where("user_id=?", userId);
  109. t.commit();
  110. ret = true;
  111. } catch (const DBException &ex) {
  112. qDebug() << ex.lastError.text();
  113. }
  114. return ret;
  115. }
  116. bool UserConfigService::QueryUserConfigInfoById(UserConfig *cfg, int cfgId)
  117. {
  118. bool ret = false;
  119. try {
  120. Transaction t(SqlDBHelper::getDatabase());
  121. QString selectSql = QString("SELECT id, user_id, engineer_id, weight, "
  122. "create_time, update_time,user_name from t_user_config "
  123. " WHERE id = %1")
  124. .arg(cfgId);
  125. QueryResult queryResult = t.execQuery(selectSql);
  126. if (queryResult.next()) {
  127. cfg->id = queryResult.value(0).toInt();
  128. cfg->userId = queryResult.value(1).toInt();
  129. cfg->engineerId = queryResult.value(2).toInt();
  130. cfg->weight = queryResult.value(3).toDouble();
  131. cfg->createTime = queryResult.value(4).toString();
  132. cfg->updateTime = queryResult.value(5).toString();
  133. cfg->userName = queryResult.value(6).toString();
  134. ret = true;
  135. }
  136. } catch (const DBException &ex) {
  137. qDebug() << ex.lastError.text();
  138. }
  139. return ret;
  140. }
  141. bool UserConfigService::QueryUserConfigListInfoByUserIdAndEngineerId(UserConfig *cfg, int userId, int engineerId)
  142. {
  143. bool ret = false;
  144. try {
  145. Transaction t(SqlDBHelper::getDatabase());
  146. QString selectSql = QString("SELECT id, user_id, engineer_id, weight, "
  147. "create_time, update_time,user_name from t_user_config "
  148. " WHERE user_id = %1 and engineer_id = %2")
  149. .arg(userId)
  150. .arg(engineerId);
  151. QueryResult queryResult = t.execQuery(selectSql);
  152. if (queryResult.next()) {
  153. cfg->id = queryResult.value(0).toInt();
  154. cfg->userId = queryResult.value(1).toInt();
  155. cfg->engineerId = queryResult.value(2).toInt();
  156. cfg->weight = queryResult.value(3).toDouble();
  157. cfg->createTime = queryResult.value(4).toString();
  158. cfg->updateTime = queryResult.value(5).toString();
  159. cfg->userName = queryResult.value(6).toString();
  160. ret = true;
  161. }
  162. } catch (const DBException &ex) {
  163. qDebug() << ex.lastError.text();
  164. }
  165. return ret;
  166. }
  167. bool UserConfigService::QueryUserConfigListInfoByEngineerId(QList<UserConfig *> *userCfgList, int engineerId)
  168. {
  169. bool ret = false;
  170. try {
  171. Transaction t(SqlDBHelper::getDatabase());
  172. QString selectSql = QString("SELECT id, user_id, engineer_id, weight, "
  173. "create_time, update_time,user_name from t_user_config"
  174. " WHERE engineer_id = %1 and is_valid =1 ")
  175. .arg(engineerId);
  176. QueryResult queryResult = t.execQuery(selectSql);
  177. while (queryResult.next()) {
  178. UserConfig *cfg = new UserConfig();
  179. cfg->id = queryResult.value(0).toInt();
  180. cfg->userId = queryResult.value(1).toInt();
  181. cfg->engineerId = queryResult.value(2).toInt();
  182. cfg->weight = queryResult.value(3).toDouble();
  183. cfg->createTime = queryResult.value(4).toString();
  184. cfg->updateTime = queryResult.value(5).toString();
  185. cfg->userName = queryResult.value(6).toString();
  186. userCfgList->append(cfg);
  187. }
  188. ret = true;
  189. } catch (const DBException &ex) {
  190. qDebug() << ex.lastError.text();
  191. }
  192. return ret;
  193. }
  194. bool UserConfigService::QueryUserConfigListInfoByUserId(QList<UserConfig *> *userCfgList, int userId)
  195. {
  196. bool ret = false;
  197. try {
  198. Transaction t(SqlDBHelper::getDatabase());
  199. QString selectSql = QString("SELECT id, user_id, engineer_id, weight, "
  200. "create_time, update_time,user_name from t_user_config"
  201. " WHERE user_id = %1 and is_valid=1 ")
  202. .arg(userId);
  203. QueryResult queryResult = t.execQuery(selectSql);
  204. while (queryResult.next()) {
  205. UserConfig *cfg = new UserConfig();
  206. cfg->id = queryResult.value(0).toInt();
  207. cfg->userId = queryResult.value(1).toInt();
  208. cfg->engineerId = queryResult.value(2).toInt();
  209. cfg->weight = queryResult.value(3).toDouble();
  210. cfg->createTime = queryResult.value(4).toString();
  211. cfg->updateTime = queryResult.value(5).toString();
  212. cfg->userName = queryResult.value(6).toString();
  213. userCfgList->append(cfg);
  214. }
  215. ret = true;
  216. } catch (const DBException &ex) {
  217. qDebug() << ex.lastError.text();
  218. }
  219. return ret;
  220. }
  221. bool UserConfigService::QueryAllUserConfig(QList<UserConfig *> *userCfgList)
  222. {
  223. bool ret = false;
  224. try {
  225. Transaction t(SqlDBHelper::getDatabase());
  226. QString selectSql = QString("SELECT id, user_id, engineer_id, weight, "
  227. "create_time, update_time,user_name from t_user_config");
  228. QueryResult queryResult = t.execQuery(selectSql);
  229. while (queryResult.next()) {
  230. UserConfig *cfg = new UserConfig();
  231. cfg->id = queryResult.value(0).toInt();
  232. cfg->userId = queryResult.value(1).toInt();
  233. cfg->engineerId = queryResult.value(2).toInt();
  234. cfg->weight = queryResult.value(3).toDouble();
  235. cfg->createTime = queryResult.value(4).toString();
  236. cfg->updateTime = queryResult.value(5).toString();
  237. cfg->userName = queryResult.value(6).toString();
  238. userCfgList->append(cfg);
  239. }
  240. ret = true;
  241. } catch (const DBException &ex) {
  242. qDebug() << ex.lastError.text();
  243. }
  244. return ret;
  245. }
  246. bool UserConfigService::DeleteUserConfigById(int cfgId)
  247. {
  248. QSqlDatabase db = SqlDBHelper::getDatabase();
  249. QSqlQuery query(db);
  250. bool ret = false;
  251. QString deleteSql = QString("DELETE FROM t_user_config WHERE id = %1").arg(cfgId);
  252. if (query.exec(deleteSql)) {
  253. ret = true;
  254. qDebug() << "deleteSql success!";
  255. } else {
  256. qDebug() << query.lastError();
  257. }
  258. return ret;
  259. }
  260. bool UserConfigService::DeleteUserConfigByEngineerId(int enjId)
  261. {
  262. QSqlDatabase db = SqlDBHelper::getDatabase();
  263. QSqlQuery query(db);
  264. bool ret = false;
  265. QString deleteSql = QString("DELETE FROM t_user_config WHERE engineer_id = %1").arg(enjId);
  266. if (query.exec(deleteSql)) {
  267. ret = true;
  268. qDebug() << "deleteSql success!";
  269. } else {
  270. qDebug() << query.lastError();
  271. }
  272. return ret;
  273. }
  274. bool UserConfigService::DeleteUserConfigByUserId(int userId)
  275. {
  276. QSqlDatabase db = SqlDBHelper::getDatabase();
  277. QSqlQuery query(db);
  278. bool ret = false;
  279. QString deleteSql = QString("DELETE FROM t_user_config WHERE user_id = %1").arg(userId);
  280. if (query.exec(deleteSql)) {
  281. ret = true;
  282. qDebug() << "deleteSql success!";
  283. } else {
  284. qDebug() << query.lastError();
  285. }
  286. return ret;
  287. }
  288. //////////////////////用户工程配置-end////////////////////