UserService.cpp 22 KB

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