UserService.cpp 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515
  1. #include "UserService.h"
  2. #include "SqlDBHelper.h"
  3. #include <QDebug>
  4. #include "DBServiceSet.h"
  5. UserService::UserService(QObject *parent) { }
  6. //////////////////////用户信息表-start////////////////////
  7. int UserService::AddUserInfo(const QFUser &userInfo)
  8. {
  9. int returnId = -1;
  10. try {
  11. // returnId = DBServiceSet::getNextId("t_user_info");
  12. Transaction t(SqlDBHelper::getDatabase());
  13. InsertQuery query = t.insertInto("t_user_info (user_name, user_no, password, role, post, "
  14. "major,work_position,education_degree,phone,project_id,write_time,remark)");
  15. NonQueryResult result =
  16. query.values(userInfo.userName, userInfo.userNo, userInfo.password, userInfo.role, userInfo.post,
  17. userInfo.major, userInfo.workPosition, userInfo.educationDegree, userInfo.phone,
  18. userInfo.projectId, userInfo.writeTime, userInfo.remark)
  19. .exec();
  20. t.commit();
  21. returnId = result.lastInsertId().toInt();
  22. } catch (const DBException &ex) {
  23. qDebug() << ex.lastError.text();
  24. returnId = -1;
  25. }
  26. return returnId;
  27. }
  28. /*暂时用id修改*/
  29. bool UserService::UpdateUserInfo(const QFUser &userInfo)
  30. {
  31. bool ret = false;
  32. try {
  33. Transaction t(SqlDBHelper::getDatabase());
  34. t.update("t_user_info")
  35. .set("user_name", userInfo.userName)
  36. .set("user_no", userInfo.userNo)
  37. .set("password", userInfo.password)
  38. .set("role", userInfo.role)
  39. .set("post", userInfo.post)
  40. .set("major", userInfo.major)
  41. .set("work_position", userInfo.workPosition)
  42. .set("education_degree", userInfo.educationDegree)
  43. .set("phone", userInfo.phone)
  44. .set("remark", userInfo.remark)
  45. .set("project_id", userInfo.projectId)
  46. .where("id=?", userInfo.id);
  47. t.commit();
  48. ret = true;
  49. } catch (const DBException &ex) {
  50. qDebug() << ex.lastError.text();
  51. }
  52. return ret;
  53. }
  54. bool UserService::UpdateUserPassword(QString userNo, QString password)
  55. {
  56. bool ret = false;
  57. try {
  58. Transaction t(SqlDBHelper::getDatabase());
  59. t.update("t_user_info").set("password", password).where("user_no=?", userNo);
  60. t.commit();
  61. ret = true;
  62. } catch (const DBException &ex) {
  63. qDebug() << ex.lastError.text();
  64. }
  65. return ret;
  66. }
  67. bool UserService::UpdateUserById(QString userNo, QString password, int id)
  68. {
  69. bool ret = false;
  70. try {
  71. Transaction t(SqlDBHelper::getDatabase());
  72. t.update("t_user_info").set("password", password).set("user_no", userNo).where("id=?", id);
  73. t.commit();
  74. ret = true;
  75. } catch (const DBException &ex) {
  76. qDebug() << ex.lastError.text();
  77. }
  78. return ret;
  79. }
  80. bool UserService::QueryUserInfoById(QFUser *userInfo, int userId)
  81. {
  82. QSqlDatabase db = SqlDBHelper::getDatabase();
  83. QSqlQuery query(db);
  84. bool ret = false;
  85. QString selectSql = QString("SELECT id, user_name,user_no,password,role,post, "
  86. "major,work_position,education_degree,phone,remark,project_id,"
  87. "write_time from "
  88. "t_user_info WHERE id = %1")
  89. .arg(userId);
  90. if (query.exec(selectSql)) {
  91. if (query.next()) {
  92. if (query.isNull(0) == false) {
  93. userInfo->id = query.value(0).toInt();
  94. userInfo->userName = query.value(1).toString();
  95. userInfo->userNo = query.value(2).toString();
  96. userInfo->password = query.value(3).toString();
  97. userInfo->role = static_cast<QFUser::Role>(query.value(4).toInt());
  98. userInfo->post = query.value(5).toString();
  99. userInfo->major = query.value(6).toString();
  100. userInfo->workPosition = query.value(7).toString();
  101. userInfo->educationDegree = query.value(8).toString();
  102. userInfo->phone = query.value(9).toString();
  103. userInfo->remark = query.value(10).toString();
  104. userInfo->projectId = query.value(11).toString();
  105. userInfo->writeTime = query.value(12).toString();
  106. }
  107. ret = true;
  108. }
  109. } else {
  110. qDebug() << query.lastError();
  111. }
  112. return ret;
  113. }
  114. bool UserService::QueryUserInfoByUnserInfo(const QFUser &paramInfo, QFUser &queryInfo)
  115. {
  116. QSqlDatabase db = SqlDBHelper::getDatabase();
  117. QSqlQuery query(db);
  118. bool ret = false;
  119. QString selectSql = QString("SELECT id, user_name,user_no,password,role,post, "
  120. "major,work_position,education_degree,phone,project_id,write_time from "
  121. "t_user_info WHERE 1 = 1 ");
  122. if (!paramInfo.userName.isNull() && !paramInfo.userName.isEmpty()) {
  123. selectSql += " and user_name = '" + paramInfo.userName + "'";
  124. }
  125. if (!paramInfo.userNo.isNull() && !paramInfo.userNo.isEmpty()) {
  126. selectSql += " and user_no = '" + paramInfo.userNo + "'";
  127. }
  128. if (!paramInfo.post.isNull() && !paramInfo.post.isEmpty()) {
  129. selectSql += " and post = '" + paramInfo.post + "'";
  130. }
  131. if (!paramInfo.major.isNull() && !paramInfo.major.isEmpty()) {
  132. selectSql += " and major = '" + paramInfo.major + "'";
  133. }
  134. if (!paramInfo.workPosition.isNull() && !paramInfo.workPosition.isEmpty()) {
  135. selectSql += " and work_position = '" + paramInfo.workPosition + "'";
  136. }
  137. if (!paramInfo.educationDegree.isNull() && !paramInfo.educationDegree.isEmpty()) {
  138. selectSql += " and education_degree = '" + paramInfo.educationDegree + "'";
  139. }
  140. if (!paramInfo.phone.isNull() && !paramInfo.phone.isEmpty()) {
  141. selectSql += " and phone = '" + paramInfo.phone + "'";
  142. }
  143. if (!paramInfo.projectId.isNull() && !paramInfo.projectId.isEmpty()) {
  144. selectSql += " and project_id = '" + paramInfo.projectId + "'";
  145. }
  146. int role = paramInfo.role;
  147. if (role == 0) {
  148. selectSql += " and role != '" + QString::number(role) + "'";
  149. } else {
  150. selectSql += " and role = '" + QString::number(role) + "'";
  151. }
  152. if (query.exec(selectSql)) {
  153. if (query.next()) {
  154. queryInfo = paramInfo;
  155. queryInfo.id = query.value(0).toInt();
  156. ret = true;
  157. }
  158. } else {
  159. qDebug() << query.lastError();
  160. }
  161. return ret;
  162. }
  163. /*获取到用户列表*/
  164. bool UserService::QueryUserListByEngineerId(QList<QFUser *> *userInfoList, int engineerId)
  165. {
  166. QSqlDatabase db = SqlDBHelper::getDatabase();
  167. QSqlQuery query(db);
  168. bool ret = false;
  169. QString selectSql = QString("SELECT id, user_name,user_no,password,role,post, "
  170. "major,work_position,education_degree,phone,remark,project_id,"
  171. "write_time "
  172. "from t_user_info"
  173. " WHERE id in (select uc.user_id from t_user_config uc where "
  174. "uc.engineer_id = %1 and uc.is_valid =1 )")
  175. .arg(engineerId);
  176. // qDebug() << selectSql;
  177. if (query.exec(selectSql)) {
  178. while (query.next()) {
  179. if (query.isNull(0) == false) {
  180. QFUser *userInfo = new QFUser();
  181. userInfo->id = query.value(0).toInt();
  182. userInfo->userName = query.value(1).toString();
  183. userInfo->userNo = query.value(2).toString();
  184. userInfo->password = query.value(3).toString();
  185. userInfo->role = static_cast<QFUser::Role>(query.value(4).toInt());
  186. userInfo->post = query.value(5).toString();
  187. userInfo->major = query.value(6).toString();
  188. userInfo->workPosition = query.value(7).toString();
  189. userInfo->educationDegree = query.value(8).toString();
  190. userInfo->phone = query.value(9).toString();
  191. userInfo->remark = query.value(10).toString();
  192. userInfo->projectId = query.value(11).toString();
  193. userInfo->writeTime = query.value(12).toString();
  194. userInfoList->append(userInfo);
  195. }
  196. ret = true;
  197. }
  198. } else {
  199. qDebug() << query.lastError();
  200. }
  201. return ret;
  202. }
  203. bool UserService::QueryUserListByColumnAndColumnValue(QList<QFUser *> *userInfoList, QString columnName,
  204. QString columnValue)
  205. {
  206. QSqlDatabase db = SqlDBHelper::getDatabase();
  207. QSqlQuery query(db);
  208. bool ret = false;
  209. QString selectSql = QString("SELECT id, user_name,user_no,password,role,post, "
  210. "major,work_position,education_degree,phone,remark,project_id,"
  211. "write_time "
  212. "from t_user_info"
  213. " WHERE %1 = '%2'")
  214. .arg(columnName)
  215. .arg(columnValue);
  216. // qDebug() << selectSql;
  217. if (query.exec(selectSql)) {
  218. while (query.next()) {
  219. if (query.isNull(0) == false) {
  220. QFUser *userInfo = new QFUser();
  221. userInfo->id = query.value(0).toInt();
  222. userInfo->userName = query.value(1).toString();
  223. userInfo->userNo = query.value(2).toString();
  224. userInfo->password = query.value(3).toString();
  225. userInfo->role = static_cast<QFUser::Role>(query.value(4).toInt());
  226. userInfo->post = query.value(5).toString();
  227. userInfo->major = query.value(6).toString();
  228. userInfo->workPosition = query.value(7).toString();
  229. userInfo->educationDegree = query.value(8).toString();
  230. userInfo->phone = query.value(9).toString();
  231. userInfo->remark = query.value(10).toString();
  232. userInfo->projectId = query.value(11).toString();
  233. userInfo->writeTime = query.value(12).toString();
  234. userInfoList->append(userInfo);
  235. }
  236. ret = true;
  237. }
  238. } else {
  239. qDebug() << query.lastError();
  240. }
  241. return ret;
  242. }
  243. bool UserService::QueryUserList(QList<QFUser *> *userInfoList)
  244. {
  245. QSqlDatabase db = SqlDBHelper::getDatabase();
  246. QSqlQuery query(db);
  247. bool ret = false;
  248. QString selectSql = QString("SELECT id, user_name,user_no,password,role,post, "
  249. "major,work_position,education_degree,phone,remark,project_id,"
  250. "write_time from t_user_info");
  251. if (query.exec(selectSql)) {
  252. while (query.next()) {
  253. if (query.isNull(0) == false) {
  254. QFUser *userInfo = new QFUser();
  255. userInfo->id = query.value(0).toInt();
  256. userInfo->userName = query.value(1).toString();
  257. userInfo->userNo = query.value(2).toString();
  258. userInfo->password = query.value(3).toString();
  259. userInfo->role = static_cast<QFUser::Role>(query.value(4).toInt());
  260. userInfo->post = query.value(5).toString();
  261. userInfo->major = query.value(6).toString();
  262. userInfo->workPosition = query.value(7).toString();
  263. userInfo->educationDegree = query.value(8).toString();
  264. userInfo->phone = query.value(9).toString();
  265. userInfo->remark = query.value(10).toString();
  266. userInfo->projectId = query.value(11).toString();
  267. userInfo->writeTime = query.value(12).toString();
  268. userInfoList->append(userInfo);
  269. }
  270. ret = true;
  271. }
  272. } else {
  273. qDebug() << query.lastError();
  274. }
  275. return ret;
  276. }
  277. bool UserService::QueryUserListNotAdmin(QList<QFUser *> *userInfoList)
  278. {
  279. QSqlDatabase db = SqlDBHelper::getDatabase();
  280. QSqlQuery query(db);
  281. bool ret = false;
  282. try {
  283. QString selectSql = QString("SELECT id, user_name,user_no,password,role,post, "
  284. "major,work_position,education_degree,phone,remark,project_id,"
  285. "write_time from t_user_info where role !=0");
  286. if (query.exec(selectSql)) {
  287. while (query.next()) {
  288. if (query.isNull(0) == false) {
  289. QFUser *userInfo = new QFUser();
  290. userInfo->id = query.value(0).toInt();
  291. userInfo->userName = query.value(1).toString();
  292. userInfo->userNo = query.value(2).toString();
  293. userInfo->password = query.value(3).toString();
  294. userInfo->role = static_cast<QFUser::Role>(query.value(4).toInt());
  295. userInfo->post = query.value(5).toString();
  296. userInfo->major = query.value(6).toString();
  297. userInfo->workPosition = query.value(7).toString();
  298. userInfo->educationDegree = query.value(8).toString();
  299. userInfo->phone = query.value(9).toString();
  300. userInfo->remark = query.value(10).toString();
  301. userInfo->projectId = query.value(11).toString();
  302. userInfo->writeTime = query.value(12).toString();
  303. userInfoList->append(userInfo);
  304. }
  305. }
  306. } else {
  307. qDebug() << query.lastError();
  308. }
  309. ret = true;
  310. } catch (const DBException &ex) {
  311. qDebug() << ex.lastError.text();
  312. }
  313. return ret;
  314. }
  315. bool UserService::DeleteUserById(int Id)
  316. {
  317. QSqlDatabase db = SqlDBHelper::getDatabase();
  318. QSqlQuery query(db);
  319. bool ret = false;
  320. QString deleteSql = QString("DELETE FROM t_user_info WHERE id = %1").arg(Id);
  321. if (query.exec(deleteSql)) {
  322. ret = true;
  323. qDebug() << "deleteSql success!";
  324. } else {
  325. qDebug() << query.lastError();
  326. }
  327. return ret;
  328. }
  329. //删除除管理管外的所有用户
  330. bool UserService::DeleteUserByRole(int roleType)
  331. {
  332. QSqlDatabase db = SqlDBHelper::getDatabase();
  333. QSqlQuery query(db);
  334. bool ret = false;
  335. QString deleteSql = QString("DELETE FROM t_user_info WHERE role != %1").arg(roleType);
  336. if (query.exec(deleteSql)) {
  337. ret = true;
  338. qDebug() << "deleteSql success!";
  339. } else {
  340. qDebug() << query.lastError();
  341. }
  342. return ret;
  343. }
  344. bool UserService::QueryUserByNoAndPassword(QFUser *userInfo, QString userNo, QString password)
  345. {
  346. QSqlDatabase db = SqlDBHelper::getDatabase();
  347. QSqlQuery query(db);
  348. bool ret = false;
  349. QString selectSql = QString("SELECT id, user_name,user_no,password,role,post, "
  350. "major,work_position,education_degree,phone,remark,project_id,"
  351. "write_time from "
  352. "t_user_info WHERE user_no = %1 and password = %2")
  353. .arg("'" + userNo + "'")
  354. .arg("'" + password + "'");
  355. if (query.exec(selectSql)) {
  356. if (query.next()) {
  357. if (query.isNull(0) == false) {
  358. userInfo->id = query.value(0).toInt();
  359. userInfo->userName = query.value(1).toString();
  360. userInfo->userNo = query.value(2).toString();
  361. userInfo->password = query.value(3).toString();
  362. userInfo->role = static_cast<QFUser::Role>(query.value(4).toInt());
  363. userInfo->post = query.value(5).toString();
  364. userInfo->major = query.value(6).toString();
  365. userInfo->workPosition = query.value(7).toString();
  366. userInfo->educationDegree = query.value(8).toString();
  367. userInfo->phone = query.value(9).toString();
  368. userInfo->remark = query.value(10).toString();
  369. userInfo->projectId = query.value(11).toString();
  370. userInfo->writeTime = query.value(12).toString();
  371. }
  372. ret = true;
  373. }
  374. } else {
  375. qDebug() << query.lastError();
  376. }
  377. return ret;
  378. }
  379. bool UserService::QueryUserByNo(QFUser *userInfo, QString userNo)
  380. {
  381. QSqlDatabase db = SqlDBHelper::getDatabase();
  382. QSqlQuery query(db);
  383. bool ret = false;
  384. QString selectSql = QString("SELECT id, user_name,user_no,password,role,post, "
  385. "major,work_position,education_degree,phone,remark,project_id,"
  386. "write_time from "
  387. "t_user_info WHERE user_no = %1 ")
  388. .arg("'" + userNo + "'");
  389. if (query.exec(selectSql)) {
  390. if (query.next()) {
  391. if (query.isNull(0) == false) {
  392. userInfo->id = query.value(0).toInt();
  393. userInfo->userName = query.value(1).toString();
  394. userInfo->userNo = query.value(2).toString();
  395. userInfo->password = query.value(3).toString();
  396. userInfo->role = static_cast<QFUser::Role>(query.value(4).toInt());
  397. userInfo->post = query.value(5).toString();
  398. userInfo->major = query.value(6).toString();
  399. userInfo->workPosition = query.value(7).toString();
  400. userInfo->educationDegree = query.value(8).toString();
  401. userInfo->phone = query.value(9).toString();
  402. userInfo->remark = query.value(10).toString();
  403. userInfo->projectId = query.value(11).toString();
  404. userInfo->writeTime = query.value(12).toString();
  405. }
  406. ret = true;
  407. }
  408. } else {
  409. qDebug() << query.lastError();
  410. }
  411. return ret;
  412. }
  413. bool UserService::QueryUserInfoById2(QFUser *userInfo, int userId)
  414. {
  415. QSqlDatabase db = SqlDBHelper::getDatabase2();
  416. QSqlQuery query(db);
  417. bool ret = false;
  418. QString selectSql = QString("SELECT id, user_name,user_no,password,role,post, "
  419. "major,work_position,education_degree,phone,remark,project_id,"
  420. "write_time from "
  421. "t_user_info WHERE id = %1")
  422. .arg(userId);
  423. qDebug() << selectSql;
  424. if (query.exec(selectSql)) {
  425. if (query.next()) {
  426. if (query.isNull(0) == false) {
  427. userInfo->id = query.value(0).toInt();
  428. userInfo->userName = query.value(1).toString();
  429. userInfo->userNo = query.value(2).toString();
  430. userInfo->password = query.value(3).toString();
  431. userInfo->role = static_cast<QFUser::Role>(query.value(4).toInt());
  432. userInfo->post = query.value(5).toString();
  433. userInfo->major = query.value(6).toString();
  434. userInfo->workPosition = query.value(7).toString();
  435. userInfo->educationDegree = query.value(8).toString();
  436. userInfo->phone = query.value(9).toString();
  437. userInfo->remark = query.value(10).toString();
  438. userInfo->projectId = query.value(11).toString();
  439. userInfo->writeTime = query.value(12).toString();
  440. }
  441. ret = true;
  442. }
  443. } else {
  444. qDebug() << query.lastError();
  445. }
  446. return ret;
  447. }
  448. bool UserService::UpdateUserInfo2(const QFUser &userInfo)
  449. {
  450. bool ret = false;
  451. try {
  452. Transaction t(SqlDBHelper::getDatabase2());
  453. t.update("t_user_info")
  454. .set("user_name", userInfo.userName)
  455. .set("user_no", userInfo.userNo)
  456. .set("password", userInfo.password)
  457. .set("role", userInfo.role)
  458. .set("post", userInfo.post)
  459. .set("major", userInfo.major)
  460. .set("work_position", userInfo.workPosition)
  461. .set("education_degree", userInfo.educationDegree)
  462. .set("phone", userInfo.phone)
  463. .set("remark", userInfo.remark)
  464. .set("project_id", userInfo.projectId)
  465. .where("id=?", userInfo.id);
  466. t.commit();
  467. ret = true;
  468. } catch (const DBException &ex) {
  469. qDebug() << ex.lastError.text();
  470. }
  471. return ret;
  472. }
  473. int UserService::AddUserInfo2(const QFUser &userInfo)
  474. {
  475. int returnId = -1;
  476. try {
  477. Transaction t(SqlDBHelper::getDatabase2());
  478. InsertQuery query = t.insertInto("t_user_info (id,user_name, user_no, password, role, post, "
  479. "major,work_position,education_degree,phone,project_id,write_time)");
  480. NonQueryResult result =
  481. query.values(userInfo.id, userInfo.userName, userInfo.userNo, userInfo.password, userInfo.role,
  482. userInfo.post, userInfo.major, userInfo.workPosition, userInfo.educationDegree,
  483. userInfo.phone, userInfo.projectId, userInfo.writeTime)
  484. .exec();
  485. t.commit();
  486. returnId = result.lastInsertId().toInt();
  487. } catch (const DBException &ex) {
  488. qDebug() << ex.lastError.text();
  489. returnId = -1;
  490. }
  491. return returnId;
  492. }
  493. //////////////////////用户信息表-end////////////////////