DataBaseService.cpp 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372
  1. #include "DataBaseService.h"
  2. #include <fstream>
  3. #include <iostream>
  4. #include "../Util/Util.h"
  5. CDataBaseService::CDataBaseService(void)
  6. {
  7. GetModuleDir(m_strModuleDir);
  8. m_strIndexDbPath.append(m_strModuleDir);
  9. m_strIndexDbPath.append(L"index.db3");
  10. }
  11. CDataBaseService::~CDataBaseService(void)
  12. {
  13. }
  14. bool CDataBaseService::OpenSQLiteDB(CppSQLite3DB& db, const wstring& strdbpath)
  15. {
  16. try
  17. {
  18. string strTest = UnicodeToUtf8(strdbpath.c_str());
  19. db.open(strTest.c_str());
  20. db.execDML("pragma journal_mode = MEMORY");
  21. }
  22. catch (...)
  23. {
  24. return false;
  25. }
  26. return true;
  27. }
  28. vector<batch_info> CDataBaseService::GetFailedBatch()
  29. {
  30. vector<batch_info> infos;
  31. CppSQLite3DB index_db;
  32. if (OpenSQLiteDB(index_db, m_strIndexDbPath))
  33. {
  34. CppSQLite3Query q = index_db.execQuery("select * from batchs where state > 0");
  35. while (!q.eof())
  36. {
  37. batch_info info;
  38. info.id = q.getIntField("id");
  39. strcpy_s(info.create_time, q.getStringField("create_time"));
  40. string strVal = q.getStringField("batchdb_path");
  41. strVal = UtfToGbk(strVal);
  42. strcpy_s(info.batchdb_path, strVal.c_str());
  43. strcpy_s(info.work_dir, q.getStringField("work_dir"));
  44. info.total_cnt = q.getIntField("total_cnt");
  45. info.success_cnt = q.getIntField("success_cnt");
  46. info.state = q.getIntField("state");
  47. strcpy_s(info.zipfile_name, q.getStringField("zipfile_name"));
  48. info.zipfile_cnt = q.getIntField("zipfile_cnt");
  49. infos.push_back(info);
  50. q.nextRow();
  51. }
  52. q.finalize();
  53. index_db.close();
  54. }
  55. return infos;
  56. }
  57. void CDataBaseService::GetBatchInfo(batch_info& info)
  58. {
  59. CppSQLite3DB index_db;
  60. if (OpenSQLiteDB(index_db, m_strIndexDbPath))
  61. {
  62. char buf[256] = { 0 };
  63. sprintf(buf, "select * from batchs where id=%d", info.id);
  64. CppSQLite3Query q = index_db.execQuery(buf);
  65. if (!q.eof())
  66. {
  67. info.state = q.getIntField("state");
  68. }
  69. q.finalize();
  70. index_db.close();
  71. }
  72. }
  73. vector<wstring> CDataBaseService::GetCashBatch()
  74. {
  75. vector<wstring> vct;
  76. CppSQLite3DB index_db;
  77. if (OpenSQLiteDB(index_db, m_strIndexDbPath))
  78. {
  79. char sql[512] = { 0 };
  80. __int64 _date = std::time(0) - 3600 * 24 * 30; // ÏÖÔÚʱ¼äµ¹ÍÆ30Ììʱ¼ä´Á
  81. sprintf_s(sql, "select work_dir from batchs where state == -1 and create_time < %lld order by id asc", _date);
  82. CppSQLite3Query q = index_db.execQuery(sql);
  83. while (!q.eof())
  84. {
  85. vct.push_back(Utf8ToUnicode(q.getStringField("work_dir")));
  86. q.nextRow();
  87. }
  88. memset(sql, 0, sizeof(sql) / sizeof(char));
  89. sprintf_s(sql, "delete from batchs where state == -1 and create_time < %lld", _date);
  90. index_db.execDML(sql);
  91. q.finalize();
  92. index_db.close();
  93. }
  94. return vct;
  95. }
  96. bool CDataBaseService::InsertPapers(const wstring& db_path, vector<string>& list_papers)
  97. {
  98. CppSQLite3DB batch_db;
  99. if (OpenSQLiteDB(batch_db, db_path))
  100. {
  101. try
  102. {
  103. batch_db.execDML("begin transaction");
  104. batch_db.execDML("delete from papers");
  105. for (size_t i = 0; i < list_papers.size() / 2; i++)
  106. {
  107. string _page1 = list_papers[i * 2];
  108. string _page2 = list_papers[i * 2 + 1];
  109. CppSQLite3Statement stmt = batch_db.compileStatement("INSERT INTO papers (page0, page1, state) VALUES (:page0, :page1, :state)");
  110. stmt.bind(":page0", _page1.c_str());
  111. stmt.bind(":page1", _page2.c_str());
  112. stmt.bind(":state", 0);
  113. stmt.execDML();
  114. stmt.finalize();
  115. }
  116. batch_db.execDML("commit transaction");
  117. batch_db.close();
  118. return true;
  119. }
  120. catch (...)
  121. {
  122. batch_db.execDML("rollback transaction");
  123. OutputDebugString(_T("IResultHandler.SavePaper.catch CppSQLite3Exception"));
  124. batch_db.close();
  125. return false;
  126. }
  127. }
  128. else
  129. {
  130. return false;
  131. }
  132. }
  133. bool CDataBaseService::DeletePaper(const wstring& db_path, int paper_id)
  134. {
  135. CppSQLite3DB batch_db;
  136. if (OpenSQLiteDB(batch_db, db_path))
  137. {
  138. try
  139. {
  140. CppSQLite3Statement stmt = batch_db.compileStatement("delete from papers where id=:id");
  141. stmt.bind(":id", paper_id);
  142. stmt.execDML();
  143. stmt.finalize();
  144. batch_db.close();
  145. return true;
  146. }
  147. catch (...)
  148. {
  149. OutputDebugString(_T("IResultHandler.SavePaper.catch CppSQLite3Exception"));
  150. batch_db.close();
  151. return false;
  152. }
  153. }
  154. else
  155. {
  156. return false;
  157. }
  158. }
  159. bool CDataBaseService::DeletePaperAll(const wstring& db_path, int state)
  160. {
  161. CppSQLite3DB batch_db;
  162. if (OpenSQLiteDB(batch_db, db_path))
  163. {
  164. try
  165. {
  166. CppSQLite3Statement stmt = batch_db.compileStatement("delete from papers where state=:state");
  167. stmt.bind(":state", state);
  168. stmt.execDML();
  169. stmt.finalize();
  170. batch_db.close();
  171. return true;
  172. }
  173. catch (...)
  174. {
  175. OutputDebugString(_T("IResultHandler.SavePaper.catch CppSQLite3Exception"));
  176. batch_db.close();
  177. return false;
  178. }
  179. }
  180. else
  181. {
  182. return false;
  183. }
  184. }
  185. bool CDataBaseService::DeleteIndex(int id)
  186. {
  187. CppSQLite3DB index_db;
  188. if (OpenSQLiteDB(index_db, m_strIndexDbPath))
  189. {
  190. try
  191. {
  192. CppSQLite3Statement stmt = index_db.compileStatement("update batchs set state=:state where id=:id");
  193. stmt.bind(":state", batch_exc_success);
  194. stmt.bind(":id", id);
  195. stmt.execDML();
  196. stmt.finalize();
  197. index_db.close();
  198. return true;
  199. }
  200. catch (...)
  201. {
  202. OutputDebugString(_T("IResultHandler.SavePaper.catch CppSQLite3Exception"));
  203. index_db.close();
  204. return false;
  205. }
  206. }
  207. else
  208. {
  209. return false;
  210. }
  211. }
  212. bool CDataBaseService::UpdateIndexPicWrong(int total_cnt, int id)
  213. {
  214. CppSQLite3DB index_db;
  215. if (OpenSQLiteDB(index_db, m_strIndexDbPath))
  216. {
  217. try
  218. {
  219. CppSQLite3Statement stmt = index_db.compileStatement("UPDATE batchs SET total_cnt=:total_cnt, state=:state WHERE id=:id");
  220. stmt.bind(":total_cnt", total_cnt);
  221. stmt.bind(":state", batch_exc_ori);
  222. stmt.bind(":id", id);
  223. stmt.execDML();
  224. stmt.finalize();
  225. index_db.close();
  226. return true;
  227. }
  228. catch (...)
  229. {
  230. OutputDebugString(_T("IResultHandler.SavePaper.catch CppSQLite3Exception"));
  231. index_db.close();
  232. return false;
  233. }
  234. }
  235. else
  236. {
  237. return false;
  238. }
  239. }
  240. bool CDataBaseService::InsertIndex(const wstring& strBatchDBPath, const wstring& strWorkPath, const wstring& strZipfileName, int total_cnt, int& last_id)
  241. {
  242. CppSQLite3DB index_db;
  243. if (!OpenSQLiteDB(index_db, m_strIndexDbPath))
  244. {
  245. return false;
  246. }
  247. CString strBatch(strBatchDBPath.c_str());
  248. strBatch.Replace(L"\\", L"/");
  249. CString strDir(strWorkPath.c_str());
  250. strDir.Replace(L"\\", L"/");
  251. CppSQLite3Statement stmt = index_db.compileStatement("INSERT INTO batchs (state, create_time, batchdb_path, total_cnt, success_cnt, work_dir, zipfile_name, zipfile_cnt) \
  252. VALUES (:state, :create_time, :batchdb_path, :total_cnt, :success_cnt, :work_dir, :zipfile_name, :zipfile_cnt)");
  253. stmt.bind(":state", 0);
  254. stmt.bind(":create_time", (sqlite_int64)std::time(0));
  255. stmt.bind(":batchdb_path", UnicodeToUtf8(strBatch).c_str());
  256. stmt.bind(":total_cnt", total_cnt);
  257. stmt.bind(":success_cnt", 0);
  258. stmt.bind(":work_dir", UnicodeToUtf8(strDir).c_str());
  259. stmt.bind(":zipfile_cnt", total_cnt * 2);
  260. stmt.bind(":zipfile_name", UnicodeToUtf8(strZipfileName.c_str()).c_str());
  261. stmt.execDML();
  262. stmt.finalize();
  263. last_id = index_db.execScalar("SELECT id FROM batchs ORDER BY ID DESC");
  264. index_db.close();
  265. return true;
  266. }
  267. void CDataBaseService::GetBatchPapers(const wstring& db_path, vector<paper_info>& vctPapers)
  268. {
  269. vctPapers.clear();
  270. CppSQLite3DB batch_db;
  271. if (!OpenSQLiteDB(batch_db, db_path))
  272. {
  273. return;
  274. }
  275. CppSQLite3Query q = batch_db.execQuery("select p.*, b.* from papers p left join batchinfo b order by p.state");
  276. while (!q.eof())
  277. {
  278. paper_info info;
  279. info.id = q.getIntField("id");
  280. strcpy_s(info.page0_path, q.getStringField("page0"));
  281. strcpy_s(info.page1_path, q.getStringField("page1"));
  282. strcpy_s(info.student_id, q.getStringField("student_id"));
  283. strcpy_s(info.qr_code, q.getStringField("qr_code"));
  284. strcpy_s(info.exam_name, q.getStringField("exam_name"));
  285. strcpy_s(info.subject_name, q.getStringField("subject_name"));
  286. strcpy_s(info.err_msg, q.getStringField("err_msg"));
  287. info.state = q.getIntField("state");
  288. info.scan_time = q.getIntField("scan_time");
  289. vctPapers.push_back(info);
  290. q.nextRow();
  291. }
  292. q.finalize();
  293. batch_db.close();
  294. }
  295. void CDataBaseService::GetPaperByID(const wstring& db_path, int id, paper_info& info)
  296. {
  297. CppSQLite3DB batch_db;
  298. if (!OpenSQLiteDB(batch_db, db_path))
  299. {
  300. return;
  301. }
  302. char sql[512] = { 0 };
  303. sprintf_s(sql, "select * from papers where id=%d", id);
  304. CppSQLite3Query q = batch_db.execQuery(sql);
  305. if (!q.eof())
  306. {
  307. info.id = q.getIntField("id");
  308. strcpy_s(info.page0_path, q.getStringField("page0"));
  309. strcpy_s(info.page1_path, q.getStringField("page1"));
  310. info.state = q.getIntField("state");
  311. }
  312. q.finalize();
  313. batch_db.close();
  314. }
  315. bool CDataBaseService::UpdatePapersPath(const wstring& db_path, paper_info& info)
  316. {
  317. CppSQLite3DB batch_db;
  318. if (!OpenSQLiteDB(batch_db, db_path))
  319. {
  320. return false;
  321. }
  322. try
  323. {
  324. CppSQLite3Statement stmt = batch_db.compileStatement("update papers set page0=:page0, page1=:page1 where id=:id");
  325. stmt.bind(":page0", info.page0_path);
  326. stmt.bind(":page1", info.page1_path);
  327. stmt.bind(":id", info.id);
  328. stmt.execDML();
  329. stmt.finalize();
  330. batch_db.close();
  331. return true;
  332. }
  333. catch (...)
  334. {
  335. OutputDebugString(_T("IResultHandler.SavePaper.catch CppSQLite3Exception"));
  336. batch_db.close();
  337. return false;
  338. }
  339. return false;
  340. }