ProjectService.cpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293
  1. #include "ProjectService.h"
  2. #include "SqlDBHelper.h"
  3. #include <QDebug>
  4. ProjectService::ProjectService(QObject *parent) { }
  5. //新增项目
  6. int ProjectService::AddProjectInfo(const ProjectInfo &projectInfo)
  7. {
  8. int ret = -1;
  9. try {
  10. Transaction t(SqlDBHelper::getDatabase());
  11. InsertQuery query = t.insertInto(
  12. "t_project_info(`project_name`, `remark`, `task_name`, `estimate_time`, `estimate_objective`, "
  13. "`estimate_dept`, `estimate_person`,`estimate_type`, `positional_titles`, `create_time`)");
  14. NonQueryResult result =
  15. query.values(projectInfo.projectName, projectInfo.remark, projectInfo.taskName,
  16. projectInfo.estimateTime, projectInfo.estimateObjective, projectInfo.estimateDept,
  17. projectInfo.estimatePerson, projectInfo.estimateType, projectInfo.positionalTitles,
  18. QDateTime::currentDateTime().toString("yyyy-MM-dd hh:mm:ss"))
  19. .exec();
  20. t.commit();
  21. ret = result.lastInsertId().toInt();
  22. } catch (const DBException &ex) {
  23. qDebug() << ex.lastError.text();
  24. }
  25. return ret;
  26. }
  27. //保存项目
  28. bool ProjectService::SaveProjectInfo(const ProjectInfo &projectInfo)
  29. {
  30. bool ret = false;
  31. try {
  32. Transaction t(SqlDBHelper::getDatabase());
  33. InsertQuery query = t.insertInto(
  34. "t_project_info(`id`,`project_name`, `remark`, `task_name`, `estimate_time`, `estimate_objective`, "
  35. "`estimate_dept`, `estimate_person`,`estimate_type`, `positional_titles`, `create_time`)");
  36. NonQueryResult result =
  37. query.values(projectInfo.id, projectInfo.projectName, projectInfo.remark, projectInfo.taskName,
  38. projectInfo.estimateTime, projectInfo.estimateObjective, projectInfo.estimateDept,
  39. projectInfo.estimatePerson, projectInfo.estimateType, projectInfo.positionalTitles,
  40. projectInfo.createTime)
  41. .exec();
  42. // qDebug() << "------------------" << result.lastQuery();
  43. t.commit();
  44. ret = true;
  45. } catch (const DBException &ex) {
  46. qDebug() << ex.lastError.text();
  47. }
  48. return ret;
  49. }
  50. //更新项目信息
  51. bool ProjectService::UpdateProjectInfo(const ProjectInfo &proInfo)
  52. {
  53. bool ret = false;
  54. try {
  55. Transaction t(SqlDBHelper::getDatabase());
  56. t.update("t_project_info")
  57. .set("project_name", proInfo.projectName)
  58. .set("remark", proInfo.remark)
  59. .set("task_name", proInfo.taskName)
  60. .set("estimate_time", proInfo.estimateTime)
  61. .set("estimate_objective", proInfo.estimateObjective)
  62. .set("estimate_dept", proInfo.estimateDept)
  63. .set("estimate_person", proInfo.estimatePerson)
  64. .set("estimate_type", proInfo.estimateType)
  65. .set("positional_titles", proInfo.positionalTitles)
  66. .set("update_time", proInfo.updateTime)
  67. .where("id = ?", proInfo.id);
  68. t.commit();
  69. ret = true;
  70. } catch (const DBException &ex) {
  71. qDebug() << ex.lastError.text();
  72. }
  73. return ret;
  74. }
  75. //更新脑图ID
  76. bool ProjectService::UpdateDemandById(int enjId, int demandId)
  77. {
  78. bool ret = false;
  79. try {
  80. Transaction t(SqlDBHelper::getDatabase());
  81. t.update("t_project_info").set("demand_mind_id", demandId).where("id = ?", enjId);
  82. t.commit();
  83. ret = true;
  84. } catch (const DBException &ex) {
  85. qDebug() << ex.lastError.text();
  86. }
  87. return ret;
  88. }
  89. bool ProjectService::UpdateGeneralById(int enjId, int generalId)
  90. {
  91. bool ret = false;
  92. try {
  93. Transaction t(SqlDBHelper::getDatabase());
  94. t.update("t_project_info").set("general", generalId).where("id = ?", enjId);
  95. t.commit();
  96. ret = true;
  97. } catch (const DBException &ex) {
  98. qDebug() << ex.lastError.text();
  99. }
  100. return ret;
  101. }
  102. bool ProjectService::UpdateProgrammeById(int enjId, int programmeId)
  103. {
  104. bool ret = false;
  105. try {
  106. Transaction t(SqlDBHelper::getDatabase());
  107. t.update("t_project_info").set("programme_mind_id", programmeId).where("id = ?", enjId);
  108. t.commit();
  109. ret = true;
  110. } catch (const DBException &ex) {
  111. qDebug() << ex.lastError.text();
  112. }
  113. return ret;
  114. }
  115. //获取项目详情
  116. bool ProjectService::QueryProjectById(ProjectInfo *proJectInfo, int id)
  117. {
  118. QSqlDatabase db = SqlDBHelper::getDatabase();
  119. QSqlQuery query(db);
  120. bool ret = false;
  121. QString selectSql =
  122. QString("SELECT id, project_name,remark,task_name,estimate_time,estimate_objective,estimate_dept,"
  123. "estimate_person,positional_titles,create_time,update_time,estimate_type FROM "
  124. "t_project_info WHERE id = %1")
  125. .arg(id);
  126. if (query.exec(selectSql)) {
  127. if (query.next()) {
  128. if (query.isNull(0) == false) {
  129. proJectInfo->id = query.value(0).toInt();
  130. proJectInfo->projectName = query.value(1).toString();
  131. proJectInfo->remark = query.value(2).toString();
  132. proJectInfo->taskName = query.value(3).toString();
  133. proJectInfo->estimateTime = query.value(4).toString();
  134. proJectInfo->estimateObjective = query.value(5).toString();
  135. proJectInfo->estimateDept = query.value(6).toString();
  136. proJectInfo->estimatePerson = query.value(7).toString();
  137. proJectInfo->positionalTitles = query.value(8).toString();
  138. proJectInfo->createTime = query.value(9).toString();
  139. proJectInfo->updateTime = query.value(10).toString();
  140. proJectInfo->estimateType = query.value(11).toString();
  141. }
  142. ret = true;
  143. }
  144. } else {
  145. qDebug() << query.lastError();
  146. }
  147. return ret;
  148. }
  149. //判断项目名是否存在
  150. bool ProjectService::QueryProjectByName(QString name)
  151. {
  152. QSqlDatabase db = SqlDBHelper::getDatabase();
  153. QSqlQuery query(db);
  154. bool ret = false;
  155. QString selectSql =
  156. QString("SELECT id, project_name,remark,task_name,estimate_time,estimate_objective,estimate_dept,"
  157. "estimate_person,positional_titles,create_time,update_time,estimate_type FROM "
  158. "t_project_info WHERE project_name = %1")
  159. .arg(name);
  160. if (query.exec(selectSql)) {
  161. if (query.next()) {
  162. ret = true;
  163. }
  164. } else {
  165. qDebug() << query.lastError();
  166. }
  167. return ret;
  168. }
  169. bool ProjectService::QueryAll(QList<ProjectInfo *> *projectInfoList)
  170. {
  171. QSqlDatabase db = SqlDBHelper::getDatabase();
  172. QSqlQuery query(db);
  173. bool ret = false;
  174. QString selectSql =
  175. QString("SELECT id, project_name, remark,task_name,estimate_time,estimate_objective,estimate_dept,"
  176. "estimate_person,positional_titles,create_time,update_time,estimate_type FROM "
  177. "t_project_info order by create_time desc");
  178. if (query.exec(selectSql)) {
  179. while (query.next()) {
  180. if (query.isNull(0) == false) {
  181. ProjectInfo *projectInfo = new ProjectInfo();
  182. projectInfo->id = query.value(0).toInt();
  183. projectInfo->projectName = query.value(1).toString();
  184. projectInfo->remark = query.value(2).toString();
  185. projectInfo->taskName = query.value(3).toString();
  186. projectInfo->estimateTime = query.value(4).toString();
  187. projectInfo->estimateObjective = query.value(5).toString();
  188. projectInfo->estimateDept = query.value(6).toString();
  189. projectInfo->estimatePerson = query.value(7).toString();
  190. projectInfo->positionalTitles = query.value(8).toString();
  191. projectInfo->createTime = query.value(9).toString();
  192. projectInfo->updateTime = query.value(10).toString();
  193. projectInfo->estimateType = query.value(11).toString();
  194. projectInfoList->append(projectInfo);
  195. }
  196. ret = true;
  197. }
  198. } else {
  199. qDebug() << query.lastError();
  200. }
  201. return ret;
  202. }
  203. //根据id删除
  204. bool ProjectService::DeleteById(int id)
  205. {
  206. bool ret = false;
  207. try {
  208. Transaction t(SqlDBHelper::getDatabase());
  209. t.deleteFrom("t_project_info").where("id = ?", id);
  210. t.commit();
  211. ret = true;
  212. } catch (const DBException &ex) {
  213. qDebug() << ex.lastError.text();
  214. }
  215. return ret;
  216. }
  217. //根据id删除
  218. bool ProjectService::DeleteAll()
  219. {
  220. QSqlDatabase db = SqlDBHelper::getDatabase();
  221. QSqlQuery query(db);
  222. bool ret = false;
  223. QString deleteSql = QString("DELETE FROM t_project_info ");
  224. if (query.exec(deleteSql)) {
  225. ret = true;
  226. qDebug() << "t_project_info deleteSql success!";
  227. } else {
  228. qDebug() << query.lastError();
  229. }
  230. return ret;
  231. }
  232. bool ProjectService::SelectAllByPage(QList<ProjectInfo *> *projectInfoList, int &totalPages, int page, int pageSize,
  233. QString name)
  234. {
  235. int offset = (page - 1) * pageSize;
  236. name = "'%" + name + "%'";
  237. QSqlDatabase db = SqlDBHelper::getDatabase();
  238. QSqlQuery query(db);
  239. bool ret = false;
  240. QString selectSql =
  241. QString("SELECT id, project_name, remark,task_name,estimate_time,estimate_objective,estimate_dept,"
  242. "estimate_person,positional_titles,create_time,update_time,estimate_type FROM "
  243. "t_project_info where project_name like %1 order by create_time desc")
  244. .arg(name);
  245. QString selectByPage = QString(" LIMIT %1 OFFSET %2 ").arg(pageSize).arg(offset);
  246. selectSql.append(selectByPage);
  247. if (query.exec(selectSql)) {
  248. while (query.next()) {
  249. if (query.isNull(0) == false) {
  250. ProjectInfo *projectInfo = new ProjectInfo();
  251. projectInfo->id = query.value(0).toInt();
  252. projectInfo->projectName = query.value(1).toString();
  253. projectInfo->remark = query.value(2).toString();
  254. projectInfo->taskName = query.value(3).toString();
  255. projectInfo->estimateTime = query.value(4).toString();
  256. projectInfo->estimateObjective = query.value(5).toString();
  257. projectInfo->estimateDept = query.value(6).toString();
  258. projectInfo->estimatePerson = query.value(7).toString();
  259. projectInfo->positionalTitles = query.value(8).toString();
  260. projectInfo->createTime = query.value(9).toString();
  261. projectInfo->updateTime = query.value(10).toString();
  262. projectInfo->estimateType = query.value(11).toString();
  263. projectInfoList->append(projectInfo);
  264. }
  265. }
  266. QString selectSqlAll = QString("SELECT COUNT(*) FROM t_project_info where project_name like %1 ").arg(name);
  267. if (query.exec(selectSqlAll)) {
  268. query.next();
  269. totalPages = query.value(0).toInt();
  270. ret = true;
  271. }
  272. } else {
  273. qDebug() << query.lastError();
  274. }
  275. return ret;
  276. }